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

History | View | Annotate | Download (11.7 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

    
35
import javax.swing.SwingUtilities;
36
import org.apache.commons.io.FileUtils;
37

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

    
56
public class DefaultPluginsManager implements PluginsManager {
57

    
58
    private class Task implements Comparable, Runnable {
59

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

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

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

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

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

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

    
110
    }
111

    
112
    private static Logger logger
113
            = LoggerFactory.getLogger(DefaultPluginsManager.class);
114

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

    
119
    public ExclusiveUIExtension getExclusiveUIExtension() {
120
        return PluginServices.getExclusiveUIExtension();
121
    }
122

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
249
    public List<File> getPluginsFolders() {
250
        if ( this.pluginsFolders != null ) {
251
            return this.pluginsFolders;
252
        }
253
        File folder;
254
        String folderPath = "gvSIG/extensiones";
255
        if ( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory() == null) ) {
256
            folderPath = Launcher.getAndamiConfig().getPluginsDirectory();
257
        }
258

    
259
        this.pluginsFolders = new ArrayList<File>();
260

    
261
        folder = new File(this.getApplicationFolder(), folderPath);
262
        if( !folder.exists() ) {
263
            try {
264
                FileUtils.forceMkdir(folder);
265
            } catch (IOException ex) {
266
                logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex);
267
            }
268
        }
269
        this.pluginsFolders.add(folder);
270

    
271
        folder = new File(this.getApplicationHomeFolder(), "installation");
272
        folder = new File(folder, folderPath);
273
        if( !folder.exists() ) {
274
            try {
275
                FileUtils.forceMkdir(folder);
276
            } catch (IOException ex) {
277
                logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex);
278
            }
279
        }
280
        this.pluginsFolders.add(folder);
281

    
282
        return this.pluginsFolders;
283
    }
284

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

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

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

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

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

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

    
329
    public File getApplicationI18nFolder() {
330
        return new File(this.getApplicationFolder(), "i18n");
331
    }
332

    
333
    public FirewallConfiguration getFirewallConfiguration() {
334
            return (FirewallConfiguration) ToolsLocator.getFirewallManager();
335
    }
336

    
337
    public Version getApplicationVersion() {
338
        PackageInfo pinfo = this.getPackageInfo();
339
        Version version = pinfo.getVersion();
340
        return version;
341
    }
342

    
343
}