Statistics
| Revision:

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
/**
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.util.ArrayList;
28
import java.util.Collections;
29
import java.util.Enumeration;
30
import java.util.Iterator;
31
import java.util.List;
32

    
33
import javax.swing.SwingUtilities;
34

    
35
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
    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
                    try {
79
                        SwingUtilities.invokeAndWait(new Runnable() {
80
                            public void run() {
81
                                Task.this.run();
82
                            }
83
                        });
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
                    return;
91
                }
92
            }
93
            logger.info("Running "+type+" task '"+name+"' (priority "+priority+").");
94
            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
    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
        }
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
            String pluginName = ((PluginClassLoader)extension.getClassLoader()).getPluginName();
129
            return  this.getPlugin(pluginName);
130
        }
131

    
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
        }
141

    
142
        public PluginServices getPlugin(String pluginName) {
143
                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
        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
        }
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
        public String translate(String msg) {
211
            return org.gvsig.i18n.Messages.translate(msg);
212
        }
213
        
214
    public File getApplicationFolder() {
215
            return Launcher.getApplicationFolder();
216
    }
217

    
218
        /**
219
         * @deprecated use {@link #getPluginsFolders()}
220
         */
221
    public File getPluginsDirectory() {
222
        return getPluginsFolder();
223
    }
224

    
225
        /**
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
    }
235

    
236
    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
        this.pluginsFolders = new ArrayList<File>();
245
        this.pluginsFolders.add(new File(getApplicationFolder(), folder));
246
        return this.pluginsFolders;
247
    }
248

    
249
    public File getInstallFolder() {
250
        return new File(getApplicationFolder(), "install");
251
    }
252

    
253
    public File getApplicationHomeFolder() {
254
        return Launcher.getApplicationHomeFolder();
255
    }
256

    
257
    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));
259
    }
260

    
261
    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));
263
    }
264

    
265
    public void executeStartupTasks() {
266
        logger.info("Executing startup tasks.");
267
        Thread th = new Thread(new Runnable() {
268
            public void run() {
269
                try {
270
                    Thread.sleep(1000);
271
                } catch (Exception exc) { 
272
                    // Ignore error
273
                }
274
                Collections.sort(onStartupTasks);
275
                for( int i=onStartupTasks.size()-1; i>=0; i--) {
276
                    Task task = onStartupTasks.get(i);
277
                    task.run();
278
                }
279
            }
280
        });
281
        th.start();
282
    }
283
    
284
    public void executeShutdownTasks() {
285
        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);
289
            task.run();
290
        }
291
    }
292
    
293
    public File getApplicationI18nFolder() {
294
        return new File(this.getApplicationFolder(),"i18n");
295
    }
296

    
297
}