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 @ 41706

History | View | Annotate | Download (11.5 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.io.IOException;
28
import java.lang.reflect.InvocationTargetException;
29
import java.util.ArrayList;
30
import java.util.Collections;
31
import java.util.Enumeration;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Locale;
35
import java.util.logging.Level;
36
import javax.swing.JComponent;
37

    
38
import javax.swing.SwingUtilities;
39
import org.apache.commons.io.FileUtils;
40

    
41
import org.gvsig.andami.Launcher;
42
import org.gvsig.andami.PluginServices;
43
import org.gvsig.andami.PluginsManager;
44
import org.gvsig.andami.config.generate.AndamiConfig;
45
import org.gvsig.andami.config.generate.Plugin;
46
import org.gvsig.andami.firewall.DefaultFirewallConfiguration;
47
import org.gvsig.andami.firewall.FirewallConfiguration;
48
import org.gvsig.andami.plugins.ExclusiveUIExtension;
49
import org.gvsig.andami.plugins.IExtension;
50
import org.gvsig.andami.plugins.PluginClassLoader;
51
import org.gvsig.installer.lib.api.PackageInfo;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.packageutils.PackageManager;
54
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58
public class DefaultPluginsManager implements PluginsManager {
59

    
60
    private class Task implements Comparable, Runnable {
61

    
62
        private String type = "";
63
        private Runnable task = null;
64
        private boolean in_event_thread = false;
65
        private int priority = 0;
66
        private String name = null;
67

    
68
        public Task(String type, String name, Runnable task, boolean in_event_thread, int priority) {
69
            this.type = type;
70
            this.in_event_thread = in_event_thread;
71
            this.task = task;
72
            this.priority = priority;
73
            this.name = name;
74
        }
75

    
76
        public int compareTo(Object t) {
77
            return this.priority - ((Task) t).priority;
78
        }
79

    
80
        public boolean equals(Object t) {
81
            return this.compareTo(t) == 0;
82
        }
83

    
84
        public void run() {
85
            if ( this.in_event_thread ) {
86
                if ( !SwingUtilities.isEventDispatchThread() ) {
87
                    try {
88
                        SwingUtilities.invokeAndWait(new Runnable() {
89
                            public void run() {
90
                                Task.this.run();
91
                            }
92
                        });
93
                    } catch (InterruptedException ex) {
94
                        // Do nothing
95
                    } catch (InvocationTargetException ex) {
96
                        logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex);
97

    
98
                    }
99
                    return;
100
                }
101
            }
102
            logger.info("Running " + type + " task '" + name + "' (priority " + priority + ").");
103
            try {
104
                task.run();
105
                logger.info("Terminated " + type + " task '" + name + "'.");
106
            } catch (Throwable ex) {
107
                // Catch Exceptions and Errors (class not found)
108
                logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex);
109
            }
110
        }
111

    
112
    }
113

    
114
    private static Logger logger
115
            = LoggerFactory.getLogger(DefaultPluginsManager.class);
116

    
117
    private List<File> pluginsFolders = null;
118
    private List<Task> startupTasks = new ArrayList<Task>();
119
    private List<Task> shutdownTasks = new ArrayList<Task>();
120

    
121
    public ExclusiveUIExtension getExclusiveUIExtension() {
122
        return PluginServices.getExclusiveUIExtension();
123
    }
124

    
125
    public IExtension getExtension(Class<? extends IExtension> extension) {
126
        return PluginServices.getExtension(extension);
127
    }
128

    
129
    @SuppressWarnings("unchecked")
130
    public Iterator<IExtension> getExtensions() {
131
        return PluginServices.getExtensions();
132
    }
133

    
134
    /**
135
     * Return the associated pluginServices to the extension class passed as
136
     * parameter.
137
     *
138
     */
139
    public PluginServices getPlugin(Class<? extends IExtension> extension) {
140
        String pluginName = ((PluginClassLoader) extension.getClassLoader()).getPluginName();
141
        return this.getPlugin(pluginName);
142
    }
143

    
144
    public PluginServices getPlugin(Object obj) {
145
        if ( obj instanceof IExtension ) {
146
            Class<? extends IExtension> klass = (Class<? extends IExtension>) obj.getClass();
147
            return this.getPlugin(klass);
148
        }
149
        PluginClassLoader loader = (PluginClassLoader) obj.getClass().getClassLoader();
150
        String pluginName = loader.getPluginName();
151
        return this.getPlugin(pluginName);
152
    }
153

    
154
    public PluginServices getPlugin(String pluginName) {
155
        return Launcher.getPluginServices(pluginName);
156
    }
157

    
158
    public PackageInfo getPackageInfo(Class<? extends IExtension> extension) {
159
        PackageManager pkgmgr = ToolsLocator.getPackageManager();
160
        File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info");
161

    
162
        PackageInfo packageInfo = null;
163
        try {
164
            packageInfo = pkgmgr.createPackageInfo(pinfo_file);
165
        } catch (Exception e) {
166
            logger.info("Error while reading package info file from "
167
                    + pinfo_file.toString(), e);
168
        }
169
        return packageInfo;
170
    }
171

    
172
    public PackageInfo getPackageInfo(String pluginName) {
173
        PackageManager pkgmgr = ToolsLocator.getPackageManager();
174
        File pinfo_file = new File(this.getPlugin(pluginName)
175
                .getPluginDirectory(), "package.info");
176

    
177
        PackageInfo packageInfo = null;
178
        try {
179
            packageInfo = pkgmgr.createPackageInfo(pinfo_file);
180
        } catch (Exception e) {
181
            logger.info("Error while reading package info file from "
182
                    + pinfo_file.toString(), e);
183
        }
184
        return packageInfo;
185
    }
186

    
187
    public PackageInfo getPackageInfo() {
188
        PackageManager pkgmgr = ToolsLocator.getPackageManager();
189
        File pinfo_file = new File(
190
                this.getApplicationFolder(), "package.info");
191
        PackageInfo packageInfo = null;
192
        try {
193
            packageInfo = pkgmgr.createPackageInfo(pinfo_file);
194
        } catch (Exception e) {
195
            logger.info("Error while reading package info file from "
196
                    + pinfo_file.toString(), e);
197
        }
198
        return packageInfo;
199
    }
200

    
201
    @SuppressWarnings("unchecked")
202
    public List<PluginServices> getPlugins() {
203
        List<PluginServices> pluginServices = new ArrayList<PluginServices>();
204

    
205
        AndamiConfig config = Launcher.getAndamiConfig();
206
        Enumeration<Plugin> plugins = config.enumeratePlugin();
207
        while ( plugins.hasMoreElements() ) {
208
            Plugin plugin = plugins.nextElement();
209
            pluginServices.add(PluginServices.getPluginServices(plugin.getName()));
210
        }
211
        return pluginServices;
212
    }
213

    
214
    public void setExclusiveUIExtension(ExclusiveUIExtension extension) {
215
        PluginServices.setExclusiveUIExtension(extension);
216
    }
217

    
218
    public String getText(Object obj, String msg) {
219
        return PluginServices.getText(obj, msg);
220
    }
221

    
222
    public String translate(String msg) {
223
        return org.gvsig.i18n.Messages.translate(msg);
224
    }
225

    
226
    public File getApplicationFolder() {
227
        return Launcher.getApplicationFolder();
228
    }
229

    
230
    /**
231
     * @deprecated use {@link #getPluginsFolders()}
232
     */
233
    public File getPluginsDirectory() {
234
        return getPluginsFolder();
235
    }
236

    
237
    /**
238
     * @deprecated use {@link #getPluginsFolders()}
239
     */
240
    public File getPluginsFolder() {
241
        List<File> l = this.getPluginsFolders();
242
        if ( l == null || l.size() < 1 ) {
243
            return null;
244
        }
245
        return l.get(0);
246
    }
247

    
248
    public List<File> getPluginsFolders() {
249
        if ( this.pluginsFolders != null ) {
250
            return this.pluginsFolders;
251
        }
252
        File folder;
253
        String folderPath = "gvSIG/extensiones";
254
        if ( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory() == null) ) {
255
            folderPath = Launcher.getAndamiConfig().getPluginsDirectory();
256
        }
257
        
258
        this.pluginsFolders = new ArrayList<File>();
259
        
260
        folder = new File(this.getApplicationFolder(), folderPath);
261
        if( !folder.exists() ) {
262
            try {
263
                FileUtils.forceMkdir(folder);
264
            } catch (IOException ex) {
265
                logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex);
266
            }
267
        }
268
        this.pluginsFolders.add(folder);
269
        
270
        folder = new File(this.getApplicationHomeFolder(), "installation");
271
        folder = new File(folder, folderPath);
272
        if( !folder.exists() ) {
273
            try {
274
                FileUtils.forceMkdir(folder);
275
            } catch (IOException ex) {
276
                logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex);
277
            }
278
        }
279
        this.pluginsFolders.add(folder);
280
        
281
        return this.pluginsFolders;
282
    }
283

    
284
    public File getInstallFolder() {
285
        return new File(getApplicationFolder(), "install");
286
    }
287

    
288
    public File getApplicationHomeFolder() {
289
        return Launcher.getApplicationHomeFolder();
290
    }
291

    
292
    public void addStartupTask(String name, Runnable task, boolean in_event_thread, int priority) {
293
        this.startupTasks.add(new Task("startup", name, task, in_event_thread, priority));
294
    }
295

    
296
    public void addShutdownTask(String name, Runnable task, boolean in_event_thread, int priority) {
297
        this.shutdownTasks.add(new Task("shutdown", name, task, in_event_thread, priority));
298
    }
299

    
300
    public void executeStartupTasks() {
301
        logger.info("Executing startup tasks.");
302
        Thread th = new Thread(new Runnable() {
303
            public void run() {
304
                try {
305
                    Thread.sleep(10);
306
                } catch (Exception exc) {
307
                    // Ignore error
308
                }
309
                Collections.sort(startupTasks);
310
                for ( int i = startupTasks.size() - 1; i >= 0; i-- ) {
311
                    Task task = startupTasks.get(i);
312
                    task.run();
313
                }
314
            }
315
        });
316
        th.start();
317
    }
318

    
319
    public void executeShutdownTasks() {
320
        logger.info("Executing shutdown tasks.");
321
        Collections.sort(shutdownTasks);
322
        for ( int i = shutdownTasks.size() - 1; i >= 0; i-- ) {
323
            Task task = shutdownTasks.get(i);
324
            task.run();
325
        }
326
    }
327

    
328
    public File getApplicationI18nFolder() {
329
        return new File(this.getApplicationFolder(), "i18n");
330
    }
331
    public FirewallConfiguration getFirewallConfiguration() {
332
        return new DefaultFirewallConfiguration();
333
    }
334

    
335

    
336
}