svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / impl / DefaultPluginsManager.java @ 41736
History | View | Annotate | Download (11.6 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.Collections; |
31 |
import java.util.Enumeration; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
|
35 |
import javax.swing.SwingUtilities; |
36 |
import org.apache.commons.io.FileUtils; |
37 |
|
38 |
import org.gvsig.andami.Launcher; |
39 |
import org.gvsig.andami.PluginServices; |
40 |
import org.gvsig.andami.PluginsManager; |
41 |
import org.gvsig.andami.config.generate.AndamiConfig; |
42 |
import org.gvsig.andami.config.generate.Plugin; |
43 |
import org.gvsig.andami.firewall.DefaultFirewallConfiguration; |
44 |
import org.gvsig.andami.firewall.FirewallConfiguration; |
45 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
46 |
import org.gvsig.andami.plugins.IExtension; |
47 |
import org.gvsig.andami.plugins.PluginClassLoader; |
48 |
import org.gvsig.installer.lib.api.PackageInfo; |
49 |
import org.gvsig.installer.lib.api.Version; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.packageutils.PackageManager; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
public class DefaultPluginsManager implements PluginsManager { |
56 |
|
57 |
private class Task implements Comparable, Runnable { |
58 |
|
59 |
private String type = ""; |
60 |
private Runnable task = null; |
61 |
private boolean in_event_thread = false; |
62 |
private int priority = 0; |
63 |
private String name = null; |
64 |
|
65 |
public Task(String type, String name, Runnable task, boolean in_event_thread, int priority) { |
66 |
this.type = type;
|
67 |
this.in_event_thread = in_event_thread;
|
68 |
this.task = task;
|
69 |
this.priority = priority;
|
70 |
this.name = name;
|
71 |
} |
72 |
|
73 |
public int compareTo(Object t) { |
74 |
return this.priority - ((Task) t).priority; |
75 |
} |
76 |
|
77 |
public boolean equals(Object t) { |
78 |
return this.compareTo(t) == 0; |
79 |
} |
80 |
|
81 |
public void run() { |
82 |
if ( this.in_event_thread ) { |
83 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
84 |
try {
|
85 |
SwingUtilities.invokeAndWait(new Runnable() { |
86 |
public void run() { |
87 |
Task.this.run(); |
88 |
} |
89 |
}); |
90 |
} catch (InterruptedException ex) { |
91 |
// Do nothing
|
92 |
} catch (InvocationTargetException ex) { |
93 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
94 |
|
95 |
} |
96 |
return;
|
97 |
} |
98 |
} |
99 |
logger.info("Running " + type + " task '" + name + "' (priority " + priority + ")."); |
100 |
try {
|
101 |
task.run(); |
102 |
logger.info("Terminated " + type + " task '" + name + "'."); |
103 |
} catch (Throwable ex) { |
104 |
// Catch Exceptions and Errors (class not found)
|
105 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
106 |
} |
107 |
} |
108 |
|
109 |
} |
110 |
|
111 |
private static Logger logger |
112 |
= LoggerFactory.getLogger(DefaultPluginsManager.class); |
113 |
|
114 |
private List<File> pluginsFolders = null; |
115 |
private List<Task> startupTasks = new ArrayList<Task>(); |
116 |
private List<Task> shutdownTasks = new ArrayList<Task>(); |
117 |
|
118 |
public ExclusiveUIExtension getExclusiveUIExtension() {
|
119 |
return PluginServices.getExclusiveUIExtension();
|
120 |
} |
121 |
|
122 |
public IExtension getExtension(Class<? extends IExtension> extension) { |
123 |
return PluginServices.getExtension(extension);
|
124 |
} |
125 |
|
126 |
@SuppressWarnings("unchecked") |
127 |
public Iterator<IExtension> getExtensions() { |
128 |
return PluginServices.getExtensions();
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* Return the associated pluginServices to the extension class passed as
|
133 |
* parameter.
|
134 |
*
|
135 |
*/
|
136 |
public PluginServices getPlugin(Class<? extends IExtension> extension) { |
137 |
String pluginName = ((PluginClassLoader) extension.getClassLoader()).getPluginName();
|
138 |
return this.getPlugin(pluginName); |
139 |
} |
140 |
|
141 |
public PluginServices getPlugin(Object obj) { |
142 |
if ( obj instanceof IExtension ) { |
143 |
Class<? extends IExtension> klass = (Class<? extends IExtension>) obj.getClass(); |
144 |
return this.getPlugin(klass); |
145 |
} |
146 |
PluginClassLoader loader = (PluginClassLoader) obj.getClass().getClassLoader(); |
147 |
String pluginName = loader.getPluginName();
|
148 |
return this.getPlugin(pluginName); |
149 |
} |
150 |
|
151 |
public PluginServices getPlugin(String pluginName) { |
152 |
return Launcher.getPluginServices(pluginName);
|
153 |
} |
154 |
|
155 |
public PackageInfo getPackageInfo(Class<? extends IExtension> extension) { |
156 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
157 |
File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info"); |
158 |
|
159 |
PackageInfo packageInfo = null;
|
160 |
try {
|
161 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
162 |
} catch (Exception e) { |
163 |
logger.info("Error while reading package info file from "
|
164 |
+ pinfo_file.toString(), e); |
165 |
} |
166 |
return packageInfo;
|
167 |
} |
168 |
|
169 |
public PackageInfo getPackageInfo(String pluginName) { |
170 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
171 |
File pinfo_file = new File(this.getPlugin(pluginName) |
172 |
.getPluginDirectory(), "package.info");
|
173 |
|
174 |
PackageInfo packageInfo = null;
|
175 |
try {
|
176 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
177 |
} catch (Exception e) { |
178 |
logger.info("Error while reading package info file from "
|
179 |
+ pinfo_file.toString(), e); |
180 |
} |
181 |
return packageInfo;
|
182 |
} |
183 |
|
184 |
public PackageInfo getPackageInfo() {
|
185 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
186 |
File pinfo_file = new File( |
187 |
this.getApplicationFolder(), "package.info"); |
188 |
PackageInfo packageInfo = null;
|
189 |
try {
|
190 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
191 |
} catch (Exception e) { |
192 |
logger.info("Error while reading package info file from "
|
193 |
+ pinfo_file.toString(), e); |
194 |
} |
195 |
return packageInfo;
|
196 |
} |
197 |
|
198 |
@SuppressWarnings("unchecked") |
199 |
public List<PluginServices> getPlugins() { |
200 |
List<PluginServices> pluginServices = new ArrayList<PluginServices>(); |
201 |
|
202 |
AndamiConfig config = Launcher.getAndamiConfig(); |
203 |
Enumeration<Plugin> plugins = config.enumeratePlugin();
|
204 |
while ( plugins.hasMoreElements() ) {
|
205 |
Plugin plugin = plugins.nextElement(); |
206 |
pluginServices.add(PluginServices.getPluginServices(plugin.getName())); |
207 |
} |
208 |
return pluginServices;
|
209 |
} |
210 |
|
211 |
public void setExclusiveUIExtension(ExclusiveUIExtension extension) { |
212 |
PluginServices.setExclusiveUIExtension(extension); |
213 |
} |
214 |
|
215 |
public String getText(Object obj, String msg) { |
216 |
return PluginServices.getText(obj, msg);
|
217 |
} |
218 |
|
219 |
public String translate(String msg) { |
220 |
return org.gvsig.i18n.Messages.translate(msg);
|
221 |
} |
222 |
|
223 |
public File getApplicationFolder() { |
224 |
return Launcher.getApplicationFolder();
|
225 |
} |
226 |
|
227 |
/**
|
228 |
* @deprecated use {@link #getPluginsFolders()}
|
229 |
*/
|
230 |
public File getPluginsDirectory() { |
231 |
return getPluginsFolder();
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* @deprecated use {@link #getPluginsFolders()}
|
236 |
*/
|
237 |
public File getPluginsFolder() { |
238 |
List<File> l = this.getPluginsFolders(); |
239 |
if ( l == null || l.size() < 1 ) { |
240 |
return null; |
241 |
} |
242 |
return l.get(0); |
243 |
} |
244 |
|
245 |
public List<File> getPluginsFolders() { |
246 |
if ( this.pluginsFolders != null ) { |
247 |
return this.pluginsFolders; |
248 |
} |
249 |
File folder;
|
250 |
String folderPath = "gvSIG/extensiones"; |
251 |
if ( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory() == null) ) { |
252 |
folderPath = Launcher.getAndamiConfig().getPluginsDirectory(); |
253 |
} |
254 |
|
255 |
this.pluginsFolders = new ArrayList<File>(); |
256 |
|
257 |
folder = new File(this.getApplicationFolder(), folderPath); |
258 |
if( !folder.exists() ) {
|
259 |
try {
|
260 |
FileUtils.forceMkdir(folder); |
261 |
} catch (IOException ex) { |
262 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
263 |
} |
264 |
} |
265 |
this.pluginsFolders.add(folder);
|
266 |
|
267 |
folder = new File(this.getApplicationHomeFolder(), "installation"); |
268 |
folder = new File(folder, folderPath); |
269 |
if( !folder.exists() ) {
|
270 |
try {
|
271 |
FileUtils.forceMkdir(folder); |
272 |
} catch (IOException ex) { |
273 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
274 |
} |
275 |
} |
276 |
this.pluginsFolders.add(folder);
|
277 |
|
278 |
return this.pluginsFolders; |
279 |
} |
280 |
|
281 |
public File getInstallFolder() { |
282 |
return new File(getApplicationFolder(), "install"); |
283 |
} |
284 |
|
285 |
public File getApplicationHomeFolder() { |
286 |
return Launcher.getApplicationHomeFolder();
|
287 |
} |
288 |
|
289 |
public void addStartupTask(String name, Runnable task, boolean in_event_thread, int priority) { |
290 |
this.startupTasks.add(new Task("startup", name, task, in_event_thread, priority)); |
291 |
} |
292 |
|
293 |
public void addShutdownTask(String name, Runnable task, boolean in_event_thread, int priority) { |
294 |
this.shutdownTasks.add(new Task("shutdown", name, task, in_event_thread, priority)); |
295 |
} |
296 |
|
297 |
public void executeStartupTasks() { |
298 |
logger.info("Executing startup tasks.");
|
299 |
Thread th = new Thread(new Runnable() { |
300 |
public void run() { |
301 |
try {
|
302 |
Thread.sleep(10); |
303 |
} catch (Exception exc) { |
304 |
// Ignore error
|
305 |
} |
306 |
Collections.sort(startupTasks);
|
307 |
for ( int i = startupTasks.size() - 1; i >= 0; i-- ) { |
308 |
Task task = startupTasks.get(i); |
309 |
task.run(); |
310 |
} |
311 |
} |
312 |
}); |
313 |
th.start(); |
314 |
} |
315 |
|
316 |
public void executeShutdownTasks() { |
317 |
logger.info("Executing shutdown tasks.");
|
318 |
Collections.sort(shutdownTasks);
|
319 |
for ( int i = shutdownTasks.size() - 1; i >= 0; i-- ) { |
320 |
Task task = shutdownTasks.get(i); |
321 |
task.run(); |
322 |
} |
323 |
} |
324 |
|
325 |
public File getApplicationI18nFolder() { |
326 |
return new File(this.getApplicationFolder(), "i18n"); |
327 |
} |
328 |
|
329 |
public FirewallConfiguration getFirewallConfiguration() {
|
330 |
return new DefaultFirewallConfiguration(); |
331 |
} |
332 |
|
333 |
public Version getApplicationVersion() {
|
334 |
PackageInfo pinfo = this.getPackageInfo();
|
335 |
Version version = pinfo.getVersion(); |
336 |
return version;
|
337 |
} |
338 |
|
339 |
} |