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 |
/**
|
---|---|
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.lang.reflect.InvocationTargetException; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Collections; |
30 |
import java.util.Enumeration; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Locale; |
34 |
import java.util.logging.Level; |
35 |
import javax.swing.JComponent; |
36 |
|
37 |
import javax.swing.SwingUtilities; |
38 |
|
39 |
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 |
import org.gvsig.andami.ui.mdiFrame.MDIFrame; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
public class DefaultPluginsManager implements PluginsManager { |
55 |
|
56 |
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 |
try {
|
84 |
SwingUtilities.invokeAndWait(new Runnable() { |
85 |
public void run() { |
86 |
Task.this.run(); |
87 |
} |
88 |
}); |
89 |
} catch (InterruptedException ex) { |
90 |
// Do nothing
|
91 |
} catch (Exception ex) { |
92 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
93 |
|
94 |
} |
95 |
return;
|
96 |
} |
97 |
} |
98 |
logger.info("Running " + type + " task '" + name + "' (priority " + priority + ")."); |
99 |
try {
|
100 |
task.run(); |
101 |
logger.info("Terminated " + type + " task '" + name + "'."); |
102 |
} catch (Exception ex) { |
103 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
104 |
} |
105 |
} |
106 |
|
107 |
} |
108 |
|
109 |
private static Logger logger |
110 |
= LoggerFactory.getLogger(DefaultPluginsManager.class); |
111 |
|
112 |
private List<File> pluginsFolders = null; |
113 |
private List<Task> startupTasks = new ArrayList<Task>(); |
114 |
private List<Task> shutdownTasks = new ArrayList<Task>(); |
115 |
|
116 |
public ExclusiveUIExtension getExclusiveUIExtension() {
|
117 |
return PluginServices.getExclusiveUIExtension();
|
118 |
} |
119 |
|
120 |
public IExtension getExtension(Class<? extends IExtension> extension) { |
121 |
return PluginServices.getExtension(extension);
|
122 |
} |
123 |
|
124 |
@SuppressWarnings("unchecked") |
125 |
public Iterator<IExtension> getExtensions() { |
126 |
return PluginServices.getExtensions();
|
127 |
} |
128 |
|
129 |
/**
|
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 |
|
139 |
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 |
File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info"); |
156 |
|
157 |
PackageInfo packageInfo = null;
|
158 |
try {
|
159 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
160 |
} catch (Exception e) { |
161 |
logger.info("Error while reading package info file from "
|
162 |
+ pinfo_file.toString(), e); |
163 |
} |
164 |
return packageInfo;
|
165 |
} |
166 |
|
167 |
public PackageInfo getPackageInfo(String pluginName) { |
168 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
169 |
File pinfo_file = new File(this.getPlugin(pluginName) |
170 |
.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() {
|
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 |
public File getApplicationFolder() { |
222 |
return Launcher.getApplicationFolder();
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* @deprecated use {@link #getPluginsFolders()}
|
227 |
*/
|
228 |
public File getPluginsDirectory() { |
229 |
return getPluginsFolder();
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* @deprecated use {@link #getPluginsFolders()}
|
234 |
*/
|
235 |
public File getPluginsFolder() { |
236 |
List<File> l = this.getPluginsFolders(); |
237 |
if ( l == null || l.size() < 1 ) { |
238 |
return null; |
239 |
} |
240 |
return l.get(0); |
241 |
} |
242 |
|
243 |
public List<File> getPluginsFolders() { |
244 |
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 |
this.pluginsFolders = new ArrayList<File>(); |
252 |
this.pluginsFolders.add(new File(getApplicationFolder(), folder)); |
253 |
return this.pluginsFolders; |
254 |
} |
255 |
|
256 |
public File getInstallFolder() { |
257 |
return new File(getApplicationFolder(), "install"); |
258 |
} |
259 |
|
260 |
public File getApplicationHomeFolder() { |
261 |
return Launcher.getApplicationHomeFolder();
|
262 |
} |
263 |
|
264 |
public void addStartupTask(String name, Runnable task, boolean in_event_thread, int priority) { |
265 |
this.startupTasks.add(new Task("startup", name, task, in_event_thread, priority)); |
266 |
} |
267 |
|
268 |
public void addShutdownTask(String name, Runnable task, boolean in_event_thread, int priority) { |
269 |
this.shutdownTasks.add(new Task("shutdown", name, task, in_event_thread, priority)); |
270 |
} |
271 |
|
272 |
public void executeStartupTasks() { |
273 |
logger.info("Executing startup tasks.");
|
274 |
Thread th = new Thread(new Runnable() { |
275 |
public void run() { |
276 |
try {
|
277 |
Thread.sleep(1000); |
278 |
} catch (Exception exc) { |
279 |
// Ignore error
|
280 |
} |
281 |
Collections.sort(startupTasks);
|
282 |
for ( int i = startupTasks.size() - 1; i >= 0; i-- ) { |
283 |
Task task = startupTasks.get(i); |
284 |
task.run(); |
285 |
} |
286 |
} |
287 |
}); |
288 |
th.start(); |
289 |
} |
290 |
|
291 |
public void executeShutdownTasks() { |
292 |
logger.info("Executing shutdown tasks.");
|
293 |
Collections.sort(shutdownTasks);
|
294 |
for ( int i = shutdownTasks.size() - 1; i >= 0; i-- ) { |
295 |
Task task = shutdownTasks.get(i); |
296 |
task.run(); |
297 |
} |
298 |
} |
299 |
|
300 |
public File getApplicationI18nFolder() { |
301 |
return new File(this.getApplicationFolder(), "i18n"); |
302 |
} |
303 |
|
304 |
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 |
} |