svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / impl / DefaultPluginsManager.java @ 42959
History | View | Annotate | Download (14.2 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.net.URI; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Arrays; |
32 |
import java.util.Collections; |
33 |
import java.util.Date; |
34 |
import java.util.Enumeration; |
35 |
import java.util.Iterator; |
36 |
import java.util.List; |
37 |
|
38 |
import javax.swing.SwingUtilities; |
39 |
|
40 |
import org.apache.commons.io.FileUtils; |
41 |
import org.jfree.util.Log; |
42 |
|
43 |
import org.gvsig.andami.Launcher; |
44 |
import org.gvsig.andami.PluginServices; |
45 |
import org.gvsig.andami.PluginsManager; |
46 |
import org.gvsig.andami.Utilities; |
47 |
import org.gvsig.andami.config.generate.AndamiConfig; |
48 |
import org.gvsig.andami.config.generate.Plugin; |
49 |
import org.gvsig.andami.firewall.DefaultFirewallConfiguration; |
50 |
import org.gvsig.andami.firewall.FirewallConfiguration; |
51 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
52 |
import org.gvsig.andami.plugins.Extension; |
53 |
import org.gvsig.andami.plugins.IExtension; |
54 |
import org.gvsig.andami.plugins.PluginClassLoader; |
55 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
56 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
57 |
import org.gvsig.installer.lib.api.PackageInfo; |
58 |
import org.gvsig.installer.lib.api.Version; |
59 |
import org.gvsig.tools.ToolsLocator; |
60 |
import org.gvsig.tools.packageutils.PackageManager; |
61 |
|
62 |
import org.slf4j.Logger; |
63 |
import org.slf4j.LoggerFactory; |
64 |
|
65 |
public class DefaultPluginsManager implements PluginsManager { |
66 |
|
67 |
private class Task implements Comparable, Runnable { |
68 |
|
69 |
private String type = ""; |
70 |
private Runnable task = null; |
71 |
private boolean in_event_thread = false; |
72 |
private int priority = 0; |
73 |
private String name = null; |
74 |
|
75 |
public Task(String type, String name, Runnable task, boolean in_event_thread, int priority) { |
76 |
this.type = type;
|
77 |
this.in_event_thread = in_event_thread;
|
78 |
this.task = task;
|
79 |
this.priority = priority;
|
80 |
this.name = name;
|
81 |
} |
82 |
|
83 |
public int compareTo(Object t) { |
84 |
return this.priority - ((Task) t).priority; |
85 |
} |
86 |
|
87 |
public boolean equals(Object t) { |
88 |
return this.compareTo(t) == 0; |
89 |
} |
90 |
|
91 |
public void run() { |
92 |
if ( this.in_event_thread ) { |
93 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
94 |
try {
|
95 |
SwingUtilities.invokeAndWait(new Runnable() { |
96 |
public void run() { |
97 |
Task.this.run(); |
98 |
} |
99 |
}); |
100 |
} catch (InterruptedException ex) { |
101 |
// Do nothing
|
102 |
} catch (InvocationTargetException ex) { |
103 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
104 |
|
105 |
} |
106 |
return;
|
107 |
} |
108 |
} |
109 |
logger.info("Running " + type + " task '" + name + "' (priority " + priority + ")."); |
110 |
try {
|
111 |
task.run(); |
112 |
logger.info("Terminated " + type + " task '" + name + "'."); |
113 |
} catch (Throwable ex) { |
114 |
// Catch Exceptions and Errors (class not found)
|
115 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
116 |
} |
117 |
} |
118 |
|
119 |
} |
120 |
|
121 |
private static Logger logger |
122 |
= LoggerFactory.getLogger(DefaultPluginsManager.class); |
123 |
|
124 |
private List<File> pluginsFolders = null; |
125 |
private List<Task> startupTasks = new ArrayList<Task>(); |
126 |
private List<Task> shutdownTasks = new ArrayList<Task>(); |
127 |
|
128 |
public ExclusiveUIExtension getExclusiveUIExtension() {
|
129 |
return PluginServices.getExclusiveUIExtension();
|
130 |
} |
131 |
|
132 |
public IExtension getExtension(Class<? extends IExtension> extension) { |
133 |
return PluginServices.getExtension(extension);
|
134 |
} |
135 |
|
136 |
@SuppressWarnings("unchecked") |
137 |
public Iterator<IExtension> getExtensions() { |
138 |
return PluginServices.getExtensions();
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* Return the associated pluginServices to the extension class passed as
|
143 |
* parameter.
|
144 |
*
|
145 |
*/
|
146 |
public PluginServices getPlugin(Class<? extends IExtension> extension) { |
147 |
String pluginName = ((PluginClassLoader) extension.getClassLoader()).getPluginName();
|
148 |
return this.getPlugin(pluginName); |
149 |
} |
150 |
|
151 |
public PluginServices getPlugin(Object obj) { |
152 |
if ( obj instanceof Extension ) { |
153 |
return ((Extension)obj).getPlugin();
|
154 |
} |
155 |
if ( obj instanceof IExtension ) { |
156 |
Class<? extends IExtension> klass = (Class<? extends IExtension>) obj.getClass(); |
157 |
return this.getPlugin(klass); |
158 |
} |
159 |
PluginClassLoader loader = (PluginClassLoader) obj.getClass().getClassLoader(); |
160 |
String pluginName = loader.getPluginName();
|
161 |
return this.getPlugin(pluginName); |
162 |
} |
163 |
|
164 |
public PluginServices getPlugin(String pluginName) { |
165 |
return Launcher.getPluginServices(pluginName);
|
166 |
} |
167 |
|
168 |
public PackageInfo getPackageInfo(Class<? extends IExtension> extension) { |
169 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
170 |
File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info"); |
171 |
|
172 |
PackageInfo packageInfo = null;
|
173 |
try {
|
174 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
175 |
} catch (Exception e) { |
176 |
logger.info("Error while reading package info file from "
|
177 |
+ pinfo_file.toString(), e); |
178 |
} |
179 |
return packageInfo;
|
180 |
} |
181 |
|
182 |
public PackageInfo getPackageInfo(String pluginName) { |
183 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
184 |
File pinfo_file = new File(this.getPlugin(pluginName) |
185 |
.getPluginDirectory(), "package.info");
|
186 |
|
187 |
PackageInfo packageInfo = null;
|
188 |
try {
|
189 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
190 |
} catch (Exception e) { |
191 |
logger.info("Error while reading package info file from "
|
192 |
+ pinfo_file.toString(), e); |
193 |
} |
194 |
return packageInfo;
|
195 |
} |
196 |
|
197 |
public PackageInfo getPackageInfo() {
|
198 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
199 |
File pinfo_file = new File( |
200 |
this.getApplicationFolder(), "package.info"); |
201 |
PackageInfo packageInfo = null;
|
202 |
try {
|
203 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
204 |
} catch (Exception e) { |
205 |
logger.info("Error while reading package info file from "
|
206 |
+ pinfo_file.toString(), e); |
207 |
} |
208 |
return packageInfo;
|
209 |
} |
210 |
|
211 |
@SuppressWarnings("unchecked") |
212 |
public List<PluginServices> getPlugins() { |
213 |
List<PluginServices> pluginServices = new ArrayList<PluginServices>(); |
214 |
|
215 |
AndamiConfig config = Launcher.getAndamiConfig(); |
216 |
Enumeration<Plugin> plugins = config.enumeratePlugin();
|
217 |
while ( plugins.hasMoreElements() ) {
|
218 |
Plugin plugin = plugins.nextElement(); |
219 |
pluginServices.add(PluginServices.getPluginServices(plugin.getName())); |
220 |
} |
221 |
return pluginServices;
|
222 |
} |
223 |
|
224 |
public void setExclusiveUIExtension(ExclusiveUIExtension extension) { |
225 |
PluginServices.setExclusiveUIExtension(extension); |
226 |
} |
227 |
|
228 |
public String getText(Object obj, String msg) { |
229 |
return PluginServices.getText(obj, msg);
|
230 |
} |
231 |
|
232 |
public String translate(String msg) { |
233 |
return org.gvsig.i18n.Messages.translate(msg);
|
234 |
} |
235 |
|
236 |
public File getApplicationFolder() { |
237 |
return Launcher.getApplicationFolder();
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* @deprecated use {@link #getPluginsFolders()}
|
242 |
*/
|
243 |
public File getPluginsDirectory() { |
244 |
return getPluginsFolder();
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* @deprecated use {@link #getPluginsFolders()}
|
249 |
*/
|
250 |
public File getPluginsFolder() { |
251 |
List<File> l = this.getPluginsFolders(); |
252 |
if ( l == null || l.size() < 1 ) { |
253 |
return null; |
254 |
} |
255 |
return l.get(0); |
256 |
} |
257 |
|
258 |
public List<File> getPluginsFolders() { |
259 |
if ( this.pluginsFolders != null ) { |
260 |
return this.pluginsFolders; |
261 |
} |
262 |
File folder;
|
263 |
String folderPath = "gvSIG/extensiones"; |
264 |
if ( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory() == null) ) { |
265 |
folderPath = Launcher.getAndamiConfig().getPluginsDirectory(); |
266 |
} |
267 |
|
268 |
this.pluginsFolders = new ArrayList<File>(); |
269 |
|
270 |
folder = new File(this.getApplicationFolder(), folderPath); |
271 |
if( !folder.exists() ) {
|
272 |
try {
|
273 |
FileUtils.forceMkdir(folder); |
274 |
} catch (IOException ex) { |
275 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
276 |
} |
277 |
} |
278 |
this.pluginsFolders.add(folder);
|
279 |
|
280 |
folder = new File(this.getApplicationHomeFolder(), "installation"); |
281 |
folder = new File(folder, folderPath); |
282 |
if( !folder.exists() ) {
|
283 |
try {
|
284 |
FileUtils.forceMkdir(folder); |
285 |
} catch (IOException ex) { |
286 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
287 |
} |
288 |
} |
289 |
this.pluginsFolders.add(folder);
|
290 |
|
291 |
return this.pluginsFolders; |
292 |
} |
293 |
|
294 |
public File getInstallFolder() { |
295 |
return new File(getApplicationFolder(), "install"); |
296 |
} |
297 |
|
298 |
public File getApplicationHomeFolder() { |
299 |
return Launcher.getApplicationHomeFolder();
|
300 |
} |
301 |
|
302 |
public void addStartupTask(String name, Runnable task, boolean in_event_thread, int priority) { |
303 |
this.startupTasks.add(new Task("startup", name, task, in_event_thread, priority)); |
304 |
} |
305 |
|
306 |
public void addShutdownTask(String name, Runnable task, boolean in_event_thread, int priority) { |
307 |
this.shutdownTasks.add(new Task("shutdown", name, task, in_event_thread, priority)); |
308 |
} |
309 |
|
310 |
public void executeStartupTasks() { |
311 |
logger.info("Executing startup tasks.");
|
312 |
Thread th = new Thread(new Runnable() { |
313 |
public void run() { |
314 |
try {
|
315 |
Thread.sleep(10); |
316 |
} catch (Exception exc) { |
317 |
// Ignore error
|
318 |
} |
319 |
Collections.sort(startupTasks);
|
320 |
for ( int i = startupTasks.size() - 1; i >= 0; i-- ) { |
321 |
Task task = startupTasks.get(i); |
322 |
task.run(); |
323 |
} |
324 |
} |
325 |
}); |
326 |
th.start(); |
327 |
} |
328 |
|
329 |
public void executeShutdownTasks() { |
330 |
logger.info("Executing shutdown tasks.");
|
331 |
Collections.sort(shutdownTasks);
|
332 |
for ( int i = shutdownTasks.size() - 1; i >= 0; i-- ) { |
333 |
Task task = shutdownTasks.get(i); |
334 |
task.run(); |
335 |
} |
336 |
} |
337 |
|
338 |
public File getApplicationI18nFolder() { |
339 |
return new File(this.getApplicationFolder(), "i18n"); |
340 |
} |
341 |
|
342 |
public FirewallConfiguration getFirewallConfiguration() {
|
343 |
return (FirewallConfiguration) ToolsLocator.getFirewallManager();
|
344 |
} |
345 |
|
346 |
public Version getApplicationVersion() {
|
347 |
PackageInfo pinfo = this.getPackageInfo();
|
348 |
Version version = pinfo.getVersion(); |
349 |
return version;
|
350 |
} |
351 |
|
352 |
public List<IUnsavedData> getUnsavedData() { |
353 |
List<IUnsavedData> unsavedDatas = new ArrayList<IUnsavedData>(); |
354 |
Iterator<IExtension> extensions = getExtensions();
|
355 |
while(extensions.hasNext()){
|
356 |
IExtension extension = extensions.next(); |
357 |
IExtensionStatus status = extension.getStatus(); |
358 |
if(status != null && status.hasUnsavedData()){ |
359 |
IUnsavedData[] unsavedData = status.getUnsavedData();
|
360 |
if (unsavedData != null){ |
361 |
unsavedDatas.addAll(Arrays.asList(unsavedData));
|
362 |
} |
363 |
} |
364 |
} |
365 |
return unsavedDatas;
|
366 |
} |
367 |
|
368 |
public void saveUnsavedData(List<IUnsavedData> unsavedData) throws UnsavedDataException { |
369 |
List<IUnsavedData> errors = new ArrayList<IUnsavedData>(); |
370 |
|
371 |
for (Iterator iterator = unsavedData.iterator(); iterator.hasNext();) { |
372 |
IUnsavedData itemUnsavedData = (IUnsavedData) iterator.next(); |
373 |
try {
|
374 |
itemUnsavedData.saveData(); |
375 |
} catch (Exception e) { |
376 |
errors.add(itemUnsavedData); |
377 |
logger.warn("Can't save "+itemUnsavedData.getResourceName());
|
378 |
} |
379 |
} |
380 |
if(!errors.isEmpty()){
|
381 |
throw new UnsavedDataException(errors); |
382 |
} |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public String getApplicationName() { |
387 |
return Launcher.getApplicationName();
|
388 |
} |
389 |
|
390 |
@Override
|
391 |
public File getTempFolder() { |
392 |
return new File(Utilities.TEMPDIRECTORYPATH); |
393 |
} |
394 |
|
395 |
@Override
|
396 |
public File getTempFile(String name) { |
397 |
return new File(Utilities.TEMPDIRECTORYPATH, name); |
398 |
} |
399 |
|
400 |
@Override
|
401 |
public File getTempFile(String name, String sufix) { |
402 |
File tempFolder = new File(Utilities.TEMPDIRECTORYPATH); |
403 |
if( !tempFolder.exists() ) {
|
404 |
try {
|
405 |
FileUtils.forceMkdir(tempFolder); |
406 |
} catch(Throwable th) { |
407 |
throw new RuntimeException(th); |
408 |
} |
409 |
} |
410 |
long t = new Date().getTime(); |
411 |
String fname = String.format("%s-%x%x%s", name,t,sufix); |
412 |
return new File(tempFolder,fname); |
413 |
} |
414 |
|
415 |
@Override
|
416 |
public boolean desktopBrowse(URI uri){ |
417 |
return DesktopApi.browse(uri);
|
418 |
} |
419 |
|
420 |
@Override
|
421 |
public boolean desktopOpen(File file){ |
422 |
return desktopOpen(file);
|
423 |
} |
424 |
} |