svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / impl / DefaultPluginsManager.java @ 42200
History | View | Annotate | Download (13.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.andami.impl; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
import java.lang.reflect.InvocationTargetException; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Arrays; |
31 |
import java.util.Collections; |
32 |
import java.util.Enumeration; |
33 |
import java.util.Iterator; |
34 |
import java.util.List; |
35 |
|
36 |
import javax.swing.SwingUtilities; |
37 |
|
38 |
import org.apache.commons.io.FileUtils; |
39 |
import org.jfree.util.Log; |
40 |
|
41 |
import org.gvsig.andami.Launcher; |
42 |
import org.gvsig.andami.PluginServices; |
43 |
import org.gvsig.andami.PluginsManager; |
44 |
import org.gvsig.andami.config.generate.AndamiConfig; |
45 |
import org.gvsig.andami.config.generate.Plugin; |
46 |
import org.gvsig.andami.firewall.DefaultFirewallConfiguration; |
47 |
import org.gvsig.andami.firewall.FirewallConfiguration; |
48 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
49 |
import org.gvsig.andami.plugins.Extension; |
50 |
import org.gvsig.andami.plugins.IExtension; |
51 |
import org.gvsig.andami.plugins.PluginClassLoader; |
52 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
53 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
54 |
import org.gvsig.installer.lib.api.PackageInfo; |
55 |
import org.gvsig.installer.lib.api.Version; |
56 |
import org.gvsig.tools.ToolsLocator; |
57 |
import org.gvsig.tools.packageutils.PackageManager; |
58 |
|
59 |
import org.slf4j.Logger; |
60 |
import org.slf4j.LoggerFactory; |
61 |
|
62 |
public class DefaultPluginsManager implements PluginsManager { |
63 |
|
64 |
private class Task implements Comparable, Runnable { |
65 |
|
66 |
private String type = ""; |
67 |
private Runnable task = null; |
68 |
private boolean in_event_thread = false; |
69 |
private int priority = 0; |
70 |
private String name = null; |
71 |
|
72 |
public Task(String type, String name, Runnable task, boolean in_event_thread, int priority) { |
73 |
this.type = type;
|
74 |
this.in_event_thread = in_event_thread;
|
75 |
this.task = task;
|
76 |
this.priority = priority;
|
77 |
this.name = name;
|
78 |
} |
79 |
|
80 |
public int compareTo(Object t) { |
81 |
return this.priority - ((Task) t).priority; |
82 |
} |
83 |
|
84 |
public boolean equals(Object t) { |
85 |
return this.compareTo(t) == 0; |
86 |
} |
87 |
|
88 |
public void run() { |
89 |
if ( this.in_event_thread ) { |
90 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
91 |
try {
|
92 |
SwingUtilities.invokeAndWait(new Runnable() { |
93 |
public void run() { |
94 |
Task.this.run(); |
95 |
} |
96 |
}); |
97 |
} catch (InterruptedException ex) { |
98 |
// Do nothing
|
99 |
} catch (InvocationTargetException ex) { |
100 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
101 |
|
102 |
} |
103 |
return;
|
104 |
} |
105 |
} |
106 |
logger.info("Running " + type + " task '" + name + "' (priority " + priority + ")."); |
107 |
try {
|
108 |
task.run(); |
109 |
logger.info("Terminated " + type + " task '" + name + "'."); |
110 |
} catch (Throwable ex) { |
111 |
// Catch Exceptions and Errors (class not found)
|
112 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
113 |
} |
114 |
} |
115 |
|
116 |
} |
117 |
|
118 |
private static Logger logger |
119 |
= LoggerFactory.getLogger(DefaultPluginsManager.class); |
120 |
|
121 |
private List<File> pluginsFolders = null; |
122 |
private List<Task> startupTasks = new ArrayList<Task>(); |
123 |
private List<Task> shutdownTasks = new ArrayList<Task>(); |
124 |
|
125 |
public ExclusiveUIExtension getExclusiveUIExtension() {
|
126 |
return PluginServices.getExclusiveUIExtension();
|
127 |
} |
128 |
|
129 |
public IExtension getExtension(Class<? extends IExtension> extension) { |
130 |
return PluginServices.getExtension(extension);
|
131 |
} |
132 |
|
133 |
@SuppressWarnings("unchecked") |
134 |
public Iterator<IExtension> getExtensions() { |
135 |
return PluginServices.getExtensions();
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Return the associated pluginServices to the extension class passed as
|
140 |
* parameter.
|
141 |
*
|
142 |
*/
|
143 |
public PluginServices getPlugin(Class<? extends IExtension> extension) { |
144 |
String pluginName = ((PluginClassLoader) extension.getClassLoader()).getPluginName();
|
145 |
return this.getPlugin(pluginName); |
146 |
} |
147 |
|
148 |
public PluginServices getPlugin(Object obj) { |
149 |
if ( obj instanceof Extension ) { |
150 |
return ((Extension)obj).getPlugin();
|
151 |
} |
152 |
if ( obj instanceof IExtension ) { |
153 |
Class<? extends IExtension> klass = (Class<? extends IExtension>) obj.getClass(); |
154 |
return this.getPlugin(klass); |
155 |
} |
156 |
PluginClassLoader loader = (PluginClassLoader) obj.getClass().getClassLoader(); |
157 |
String pluginName = loader.getPluginName();
|
158 |
return this.getPlugin(pluginName); |
159 |
} |
160 |
|
161 |
public PluginServices getPlugin(String pluginName) { |
162 |
return Launcher.getPluginServices(pluginName);
|
163 |
} |
164 |
|
165 |
public PackageInfo getPackageInfo(Class<? extends IExtension> extension) { |
166 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
167 |
File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info"); |
168 |
|
169 |
PackageInfo packageInfo = null;
|
170 |
try {
|
171 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
172 |
} catch (Exception e) { |
173 |
logger.info("Error while reading package info file from "
|
174 |
+ pinfo_file.toString(), e); |
175 |
} |
176 |
return packageInfo;
|
177 |
} |
178 |
|
179 |
public PackageInfo getPackageInfo(String pluginName) { |
180 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
181 |
File pinfo_file = new File(this.getPlugin(pluginName) |
182 |
.getPluginDirectory(), "package.info");
|
183 |
|
184 |
PackageInfo packageInfo = null;
|
185 |
try {
|
186 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
187 |
} catch (Exception e) { |
188 |
logger.info("Error while reading package info file from "
|
189 |
+ pinfo_file.toString(), e); |
190 |
} |
191 |
return packageInfo;
|
192 |
} |
193 |
|
194 |
public PackageInfo getPackageInfo() {
|
195 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
196 |
File pinfo_file = new File( |
197 |
this.getApplicationFolder(), "package.info"); |
198 |
PackageInfo packageInfo = null;
|
199 |
try {
|
200 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
201 |
} catch (Exception e) { |
202 |
logger.info("Error while reading package info file from "
|
203 |
+ pinfo_file.toString(), e); |
204 |
} |
205 |
return packageInfo;
|
206 |
} |
207 |
|
208 |
@SuppressWarnings("unchecked") |
209 |
public List<PluginServices> getPlugins() { |
210 |
List<PluginServices> pluginServices = new ArrayList<PluginServices>(); |
211 |
|
212 |
AndamiConfig config = Launcher.getAndamiConfig(); |
213 |
Enumeration<Plugin> plugins = config.enumeratePlugin();
|
214 |
while ( plugins.hasMoreElements() ) {
|
215 |
Plugin plugin = plugins.nextElement(); |
216 |
pluginServices.add(PluginServices.getPluginServices(plugin.getName())); |
217 |
} |
218 |
return pluginServices;
|
219 |
} |
220 |
|
221 |
public void setExclusiveUIExtension(ExclusiveUIExtension extension) { |
222 |
PluginServices.setExclusiveUIExtension(extension); |
223 |
} |
224 |
|
225 |
public String getText(Object obj, String msg) { |
226 |
return PluginServices.getText(obj, msg);
|
227 |
} |
228 |
|
229 |
public String translate(String msg) { |
230 |
return org.gvsig.i18n.Messages.translate(msg);
|
231 |
} |
232 |
|
233 |
public File getApplicationFolder() { |
234 |
return Launcher.getApplicationFolder();
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* @deprecated use {@link #getPluginsFolders()}
|
239 |
*/
|
240 |
public File getPluginsDirectory() { |
241 |
return getPluginsFolder();
|
242 |
} |
243 |
|
244 |
/**
|
245 |
* @deprecated use {@link #getPluginsFolders()}
|
246 |
*/
|
247 |
public File getPluginsFolder() { |
248 |
List<File> l = this.getPluginsFolders(); |
249 |
if ( l == null || l.size() < 1 ) { |
250 |
return null; |
251 |
} |
252 |
return l.get(0); |
253 |
} |
254 |
|
255 |
public List<File> getPluginsFolders() { |
256 |
if ( this.pluginsFolders != null ) { |
257 |
return this.pluginsFolders; |
258 |
} |
259 |
File folder;
|
260 |
String folderPath = "gvSIG/extensiones"; |
261 |
if ( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory() == null) ) { |
262 |
folderPath = Launcher.getAndamiConfig().getPluginsDirectory(); |
263 |
} |
264 |
|
265 |
this.pluginsFolders = new ArrayList<File>(); |
266 |
|
267 |
folder = new File(this.getApplicationFolder(), folderPath); |
268 |
if( !folder.exists() ) {
|
269 |
try {
|
270 |
FileUtils.forceMkdir(folder); |
271 |
} catch (IOException ex) { |
272 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
273 |
} |
274 |
} |
275 |
this.pluginsFolders.add(folder);
|
276 |
|
277 |
folder = new File(this.getApplicationHomeFolder(), "installation"); |
278 |
folder = new File(folder, folderPath); |
279 |
if( !folder.exists() ) {
|
280 |
try {
|
281 |
FileUtils.forceMkdir(folder); |
282 |
} catch (IOException ex) { |
283 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
284 |
} |
285 |
} |
286 |
this.pluginsFolders.add(folder);
|
287 |
|
288 |
return this.pluginsFolders; |
289 |
} |
290 |
|
291 |
public File getInstallFolder() { |
292 |
return new File(getApplicationFolder(), "install"); |
293 |
} |
294 |
|
295 |
public File getApplicationHomeFolder() { |
296 |
return Launcher.getApplicationHomeFolder();
|
297 |
} |
298 |
|
299 |
public void addStartupTask(String name, Runnable task, boolean in_event_thread, int priority) { |
300 |
this.startupTasks.add(new Task("startup", name, task, in_event_thread, priority)); |
301 |
} |
302 |
|
303 |
public void addShutdownTask(String name, Runnable task, boolean in_event_thread, int priority) { |
304 |
this.shutdownTasks.add(new Task("shutdown", name, task, in_event_thread, priority)); |
305 |
} |
306 |
|
307 |
public void executeStartupTasks() { |
308 |
logger.info("Executing startup tasks.");
|
309 |
Thread th = new Thread(new Runnable() { |
310 |
public void run() { |
311 |
try {
|
312 |
Thread.sleep(10); |
313 |
} catch (Exception exc) { |
314 |
// Ignore error
|
315 |
} |
316 |
Collections.sort(startupTasks);
|
317 |
for ( int i = startupTasks.size() - 1; i >= 0; i-- ) { |
318 |
Task task = startupTasks.get(i); |
319 |
task.run(); |
320 |
} |
321 |
} |
322 |
}); |
323 |
th.start(); |
324 |
} |
325 |
|
326 |
public void executeShutdownTasks() { |
327 |
logger.info("Executing shutdown tasks.");
|
328 |
Collections.sort(shutdownTasks);
|
329 |
for ( int i = shutdownTasks.size() - 1; i >= 0; i-- ) { |
330 |
Task task = shutdownTasks.get(i); |
331 |
task.run(); |
332 |
} |
333 |
} |
334 |
|
335 |
public File getApplicationI18nFolder() { |
336 |
return new File(this.getApplicationFolder(), "i18n"); |
337 |
} |
338 |
|
339 |
public FirewallConfiguration getFirewallConfiguration() {
|
340 |
return (FirewallConfiguration) ToolsLocator.getFirewallManager();
|
341 |
} |
342 |
|
343 |
public Version getApplicationVersion() {
|
344 |
PackageInfo pinfo = this.getPackageInfo();
|
345 |
Version version = pinfo.getVersion(); |
346 |
return version;
|
347 |
} |
348 |
|
349 |
public List<IUnsavedData> getUnsavedData() { |
350 |
List<IUnsavedData> unsavedDatas = new ArrayList<IUnsavedData>(); |
351 |
Iterator<IExtension> extensions = getExtensions();
|
352 |
while(extensions.hasNext()){
|
353 |
IExtension extension = extensions.next(); |
354 |
IExtensionStatus status = extension.getStatus(); |
355 |
if(status != null && status.hasUnsavedData()){ |
356 |
IUnsavedData[] unsavedData = status.getUnsavedData();
|
357 |
if (unsavedData != null){ |
358 |
unsavedDatas.addAll(Arrays.asList(unsavedData));
|
359 |
} |
360 |
} |
361 |
} |
362 |
return unsavedDatas;
|
363 |
} |
364 |
|
365 |
public void saveUnsavedData(List<IUnsavedData> unsavedData) throws UnsavedDataException { |
366 |
List<IUnsavedData> errors = new ArrayList<IUnsavedData>(); |
367 |
|
368 |
for (Iterator iterator = unsavedData.iterator(); iterator.hasNext();) { |
369 |
IUnsavedData itemUnsavedData = (IUnsavedData) iterator.next(); |
370 |
try {
|
371 |
itemUnsavedData.saveData(); |
372 |
} catch (Exception e) { |
373 |
errors.add(itemUnsavedData); |
374 |
logger.warn("Can't save "+itemUnsavedData.getResourceName());
|
375 |
} |
376 |
} |
377 |
if(!errors.isEmpty()){
|
378 |
throw new UnsavedDataException(errors); |
379 |
} |
380 |
} |
381 |
} |