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