Revision 41314 trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DefaultPluginsManager.java

View differences:

DefaultPluginsManager.java
10 10
 *
11 11
 * This program is distributed in the hope that it will be useful,
12 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14
 * GNU General Public License for more details.
15 15
 *
16 16
 * You should have received a copy of the GNU General Public License
17 17
 * along with this program; if not, write to the Free Software
18 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
19
 * MA 02110-1301, USA.
20 20
 *
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
......
24 24
package org.gvsig.andami.impl;
25 25

  
26 26
import java.io.File;
27
import java.lang.reflect.InvocationTargetException;
27 28
import java.util.ArrayList;
28 29
import java.util.Collections;
29 30
import java.util.Enumeration;
30 31
import java.util.Iterator;
31 32
import java.util.List;
33
import java.util.Locale;
34
import java.util.logging.Level;
35
import javax.swing.JComponent;
32 36

  
33 37
import javax.swing.SwingUtilities;
34 38

  
......
43 47
import org.gvsig.installer.lib.api.PackageInfo;
44 48
import org.gvsig.tools.ToolsLocator;
45 49
import org.gvsig.tools.packageutils.PackageManager;
50
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
46 51
import org.slf4j.Logger;
47 52
import org.slf4j.LoggerFactory;
48 53

  
49
public class DefaultPluginsManager implements PluginsManager{
54
public class DefaultPluginsManager implements PluginsManager {
50 55

  
51 56
    private class Task implements Comparable, Runnable {
52 57

  
......
84 89
                    } catch (InterruptedException ex) {
85 90
                        // Do nothing
86 91
                    } catch (Exception ex) {
87
                       logger.warn("Errors in execution of "+type+" task '"+name+"'.",ex);
92
                        logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex);
88 93

  
89 94
                    }
90 95
                    return;
91 96
                }
92 97
            }
93
            logger.info("Running "+type+" task '"+name+"' (priority "+priority+").");
98
            logger.info("Running " + type + " task '" + name + "' (priority " + priority + ").");
94 99
            try {
95 100
                task.run();
96
            } catch(Exception ex) {
97
                logger.warn("Errors in execution of "+type+" task '"+name+"'.",ex);
101
                logger.info("Terminated " + type + " task '" + name + "'.");
102
            } catch (Exception ex) {
103
                logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex);
98 104
            }
99 105
        }
100 106

  
101 107
    }
102 108

  
103
    private static Logger logger =
104
        LoggerFactory.getLogger(DefaultPluginsManager.class);
105
    
106
	private List<File> pluginsFolders = null;
107
	private List<Task> onStartupTasks = new ArrayList<Task>();
108
        private List<Task> onShutdownTasks = new ArrayList<Task>();
109
	
110
	public ExclusiveUIExtension getExclusiveUIExtension() {
111
		return PluginServices.getExclusiveUIExtension();
112
	}
109
    private static Logger logger
110
            = LoggerFactory.getLogger(DefaultPluginsManager.class);
113 111

  
114
	public IExtension getExtension(Class<? extends IExtension> extension) {
115
		return PluginServices.getExtension(extension);
116
	}
112
    private List<File> pluginsFolders = null;
113
    private List<Task> startupTasks = new ArrayList<Task>();
114
    private List<Task> shutdownTasks = new ArrayList<Task>();
117 115

  
118
	@SuppressWarnings("unchecked")
119
	public Iterator<IExtension> getExtensions() {
120
		return PluginServices.getExtensions();
121
	}
116
    public ExclusiveUIExtension getExclusiveUIExtension() {
117
        return PluginServices.getExclusiveUIExtension();
118
    }
122 119

  
123
	/**
124
	 * Return the associated pluginServices to the extension class passed as parameter.
125
	 *  
126
	 */
127
	public PluginServices getPlugin(Class<? extends IExtension> extension) {
128
            String pluginName = ((PluginClassLoader)extension.getClassLoader()).getPluginName();
129
            return  this.getPlugin(pluginName);
130
	}
120
    public IExtension getExtension(Class<? extends IExtension> extension) {
121
        return PluginServices.getExtension(extension);
122
    }
131 123

  
132
	public PluginServices getPlugin(Object obj) {
133
            if( obj instanceof IExtension ) {
134
                Class<? extends IExtension> klass = (Class<? extends IExtension>) obj.getClass();
135
                return this.getPlugin(klass);
136
            }
137
            PluginClassLoader loader = (PluginClassLoader) obj.getClass().getClassLoader();
138
            String pluginName = loader.getPluginName();
139
            return this.getPlugin(pluginName);
140
	}
124
    @SuppressWarnings("unchecked")
125
    public Iterator<IExtension> getExtensions() {
126
        return PluginServices.getExtensions();
127
    }
141 128

  
142
        public PluginServices getPlugin(String pluginName) {
143
		return Launcher.getPluginServices(pluginName);
144
	}
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
    }
145 138

  
146
	public PackageInfo getPackageInfo(Class<? extends IExtension> extension) {
147
	    PackageManager pkgmgr = ToolsLocator.getPackageManager();
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();
148 155
        File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info");
149
        
156

  
150 157
        PackageInfo packageInfo = null;
151 158
        try {
152 159
            packageInfo = pkgmgr.createPackageInfo(pinfo_file);
153 160
        } catch (Exception e) {
154
			logger.info("Error while reading package info file from "
155
					+ pinfo_file.toString(), e);
161
            logger.info("Error while reading package info file from "
162
                    + pinfo_file.toString(), e);
156 163
        }
157 164
        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");
165
    }
164 166

  
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
	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
	@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
	}
167
    public PackageInfo getPackageInfo(String pluginName) {
168
        PackageManager pkgmgr = ToolsLocator.getPackageManager();
169
        File pinfo_file = new File(this.getPlugin(pluginName)
170
                .getPluginDirectory(), "package.info");
201 171

  
202
	public void setExclusiveUIExtension(ExclusiveUIExtension extension) {
203
		PluginServices.setExclusiveUIExtension(extension);
204
	}
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
    }
205 181

  
206
	public String getText(Object obj, String msg) {
207
	    return PluginServices.getText(obj, msg);
208
	}
209
	
210
	public String translate(String msg) {
211
	    return org.gvsig.i18n.Messages.translate(msg);
212
	}
213
	
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

  
214 221
    public File getApplicationFolder() {
215
    	return Launcher.getApplicationFolder();
222
        return Launcher.getApplicationFolder();
216 223
    }
217 224

  
218
	/**
219
	 * @deprecated use {@link #getPluginsFolders()}
220
	 */
225
    /**
226
     * @deprecated use {@link #getPluginsFolders()}
227
     */
221 228
    public File getPluginsDirectory() {
222 229
        return getPluginsFolder();
223 230
    }
224 231

  
225
	/**
226
	 * @deprecated use {@link #getPluginsFolders()}
227
	 */
232
    /**
233
     * @deprecated use {@link #getPluginsFolders()}
234
     */
228 235
    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);
236
        List<File> l = this.getPluginsFolders();
237
        if ( l == null || l.size() < 1 ) {
238
            return null;
239
        }
240
        return l.get(0);
234 241
    }
235 242

  
236 243
    public List<File> getPluginsFolders() {
237
    	if( this.pluginsFolders!=null ) {
238
    		return this.pluginsFolders;
239
    	}
240
    	String folder = "gvSIG/extensiones";
241
    	if( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory()==null) ) {
242
    		folder = Launcher.getAndamiConfig().getPluginsDirectory();
243
    	}
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
        }
244 251
        this.pluginsFolders = new ArrayList<File>();
245 252
        this.pluginsFolders.add(new File(getApplicationFolder(), folder));
246 253
        return this.pluginsFolders;
......
255 262
    }
256 263

  
257 264
    public void addStartupTask(String name, Runnable task, boolean in_event_thread, int priority) {
258
        this.onStartupTasks.add( new Task("startup", name, task, in_event_thread, priority));
265
        this.startupTasks.add(new Task("startup", name, task, in_event_thread, priority));
259 266
    }
260 267

  
261 268
    public void addShutdownTask(String name, Runnable task, boolean in_event_thread, int priority) {
262
        this.onShutdownTasks.add( new Task("shutdown",name, task, in_event_thread, priority));
269
        this.shutdownTasks.add(new Task("shutdown", name, task, in_event_thread, priority));
263 270
    }
264 271

  
265 272
    public void executeStartupTasks() {
......
268 275
            public void run() {
269 276
                try {
270 277
                    Thread.sleep(1000);
271
                } catch (Exception exc) { 
278
                } catch (Exception exc) {
272 279
                    // Ignore error
273 280
                }
274
                Collections.sort(onStartupTasks);
275
                for( int i=onStartupTasks.size()-1; i>=0; i--) {
276
                    Task task = onStartupTasks.get(i);
281
                Collections.sort(startupTasks);
282
                for ( int i = startupTasks.size() - 1; i >= 0; i-- ) {
283
                    Task task = startupTasks.get(i);
277 284
                    task.run();
278 285
                }
279 286
            }
280 287
        });
281 288
        th.start();
282 289
    }
283
    
290

  
284 291
    public void executeShutdownTasks() {
285 292
        logger.info("Executing shutdown tasks.");
286
        Collections.sort(onShutdownTasks);
287
        for( int i=onShutdownTasks.size()-1; i>=0; i--) {
288
            Task task = onShutdownTasks.get(i);
293
        Collections.sort(shutdownTasks);
294
        for ( int i = shutdownTasks.size() - 1; i >= 0; i-- ) {
295
            Task task = shutdownTasks.get(i);
289 296
            task.run();
290 297
        }
291 298
    }
292
    
299

  
293 300
    public File getApplicationI18nFolder() {
294
        return new File(this.getApplicationFolder(),"i18n");
301
        return new File(this.getApplicationFolder(), "i18n");
295 302
    }
296 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

  
297 334
}

Also available in: Unified diff