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