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