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

History | View | Annotate | Download (7.68 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.Enumeration;
29
import java.util.Iterator;
30
import java.util.List;
31

    
32
import javax.swing.SwingUtilities;
33

    
34
import org.gvsig.andami.Launcher;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.PluginsManager;
37
import org.gvsig.andami.config.generate.AndamiConfig;
38
import org.gvsig.andami.config.generate.Plugin;
39
import org.gvsig.andami.plugins.ExclusiveUIExtension;
40
import org.gvsig.andami.plugins.IExtension;
41
import org.gvsig.andami.plugins.PluginClassLoader;
42
import org.gvsig.installer.lib.api.PackageInfo;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.packageutils.PackageManager;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
public class DefaultPluginsManager implements PluginsManager{
49

    
50
    private static Logger logger =
51
        LoggerFactory.getLogger(DefaultPluginsManager.class);
52
    
53
        private List<File> pluginsFolders = null;
54
        private List<Runnable> tasksAfterPlugins = new ArrayList<Runnable>();
55
        
56
        public ExclusiveUIExtension getExclusiveUIExtension() {
57
                return PluginServices.getExclusiveUIExtension();
58
        }
59

    
60
        public IExtension getExtension(Class<? extends IExtension> extension) {
61
                return PluginServices.getExtension(extension);
62
        }
63

    
64
        @SuppressWarnings("unchecked")
65
        public Iterator<IExtension> getExtensions() {
66
                return PluginServices.getExtensions();
67
        }
68

    
69
        /**
70
         * Return the associated pluginServices to the extension class passed as parameter.
71
         *  
72
         */
73
        public PluginServices getPlugin(Class<? extends IExtension> extension) {
74
            String pluginName = ((PluginClassLoader)extension.getClassLoader()).getPluginName();
75
                return  this.getPlugin(pluginName);
76
        }
77

    
78
        public PluginServices getPlugin(Object obj) {
79
            if( obj instanceof IExtension ) {
80
                return this.getPlugin(obj.getClass());
81
            }
82
            PluginClassLoader loader = (PluginClassLoader) obj.getClass().getClassLoader();
83
            String pluginName = loader.getPluginName();
84
            return this.getPlugin(pluginName);
85
        }
86

    
87
        public PluginServices getPlugin(String pluginName) {
88
                return Launcher.getPluginServices(pluginName);
89
        }
90

    
91
        public PackageInfo getPackageInfo(Class<? extends IExtension> extension) {
92
            PackageManager pkgmgr = ToolsLocator.getPackageManager();
93
        File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info");
94
        
95
        PackageInfo packageInfo = null;
96
        try {
97
            packageInfo = pkgmgr.createPackageInfo(pinfo_file);
98
        } catch (Exception e) {
99
                        logger.info("Error while reading package info file from "
100
                                        + pinfo_file.toString(), e);
101
        }
102
        return packageInfo;
103
        }
104
        
105
        public PackageInfo getPackageInfo(String pluginName) {
106
                PackageManager pkgmgr = ToolsLocator.getPackageManager();
107
                File pinfo_file = new File(this.getPlugin(pluginName)
108
                                .getPluginDirectory(), "package.info");
109

    
110
                PackageInfo packageInfo = null;
111
                try {
112
                        packageInfo = pkgmgr.createPackageInfo(pinfo_file);
113
                } catch (Exception e) {
114
                        logger.info("Error while reading package info file from "
115
                                        + pinfo_file.toString(), e);
116
                }
117
                return packageInfo;
118
        }
119
        
120
        public PackageInfo getPackageInfo() {
121
                PackageManager pkgmgr = ToolsLocator.getPackageManager();
122
                File pinfo_file = new File(
123
                            this.getApplicationFolder(), "package.info");
124
                PackageInfo packageInfo = null;
125
                try {
126
                        packageInfo = pkgmgr.createPackageInfo(pinfo_file);
127
                } catch (Exception e) {
128
                        logger.info("Error while reading package info file from "
129
                                        + pinfo_file.toString(), e);
130
                }
131
                return packageInfo;
132
        }
133
        
134
        @SuppressWarnings("unchecked")
135
        public List<PluginServices> getPlugins() {
136
                List<PluginServices> pluginServices = new ArrayList<PluginServices>();
137
                
138
                AndamiConfig config = Launcher.getAndamiConfig();
139
                Enumeration<Plugin> plugins = config.enumeratePlugin();
140
                while( plugins.hasMoreElements()) {
141
                        Plugin plugin =   plugins.nextElement();
142
                        pluginServices.add(PluginServices.getPluginServices(plugin.getName()));
143
                }
144
                return pluginServices;
145
        }
146

    
147
        public void setExclusiveUIExtension(ExclusiveUIExtension extension) {
148
                PluginServices.setExclusiveUIExtension(extension);
149
        }
150

    
151
        public String getText(Object obj, String msg) {
152
            return PluginServices.getText(obj, msg);
153
        }
154
        
155
        public String translate(String msg) {
156
            return org.gvsig.i18n.Messages.translate(msg);
157
        }
158
        
159
    public File getApplicationFolder() {
160
            return Launcher.getApplicationFolder();
161
    }
162

    
163
        /**
164
         * @deprecated use {@link #getPluginsFolders()}
165
         */
166
    public File getPluginsDirectory() {
167
        return getPluginsFolder();
168
    }
169

    
170
        /**
171
         * @deprecated use {@link #getPluginsFolders()}
172
         */
173
    public File getPluginsFolder() {
174
            List<File> l = this.getPluginsFolders();
175
            if( l==null || l.size()<1 ) {
176
                    return null;
177
            }
178
            return l.get(0);
179
    }
180

    
181
    public List<File> getPluginsFolders() {
182
            if( this.pluginsFolders!=null ) {
183
                    return this.pluginsFolders;
184
            }
185
            String folder = "gvSIG/extensiones";
186
            if( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory()==null) ) {
187
                    folder = Launcher.getAndamiConfig().getPluginsDirectory();
188
            }
189
        this.pluginsFolders = new ArrayList<File>();
190
        this.pluginsFolders.add(new File(getApplicationFolder(), folder));
191
        return this.pluginsFolders;
192
    }
193

    
194
    public File getInstallFolder() {
195
        return new File(getApplicationFolder(), "install");
196
    }
197

    
198
    public File getApplicationHomeFolder() {
199
        return Launcher.getApplicationHomeFolder();
200
    }
201

    
202
    public void addAdditionalTask(Runnable task, boolean in_event_thread, int priority) {
203
        tasksAfterPlugins.add(task);
204
    }
205

    
206
    public void executeAdditionalTasks() {
207
        Thread th = new Thread(new Runnable() {
208
            public void run() {
209
                try {
210
                    Thread.sleep(1000);
211
                } catch (Exception exc) { }
212
                executeTasksInEventThread();
213
            }
214
        });
215
        th.start();
216

    
217
    }
218
    
219
    private void executeTasksInEventThread() {
220
        
221
        if (!SwingUtilities.isEventDispatchThread()) {
222
            try {
223
                SwingUtilities.invokeLater(new Runnable() {
224
                    public void run() {
225
                        doExecuteTasks();
226
                    }
227
                });
228
            } catch (Throwable th) {
229
                logger.error("While running tasks", th);
230
            }
231
        } else {
232
            doExecuteTasks();
233
        }
234
        
235
    }
236
    
237
    private void doExecuteTasks() {
238
        
239
        Runnable item = null;
240
        for (int i=0; i<tasksAfterPlugins.size(); i++) {
241
            item = tasksAfterPlugins.get(i);
242
            item.run();
243
        }
244
        
245
    }
246

    
247
    public File getApplicationI18nFolder() {
248
        return new File(this.getApplicationFolder(),"i18n");
249
    }
250

    
251
    
252
    
253
}