Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / java / org / gvsig / scripting / app / extension / ScriptingExtension.java @ 1092

History | View | Annotate | Download (7.85 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.scripting.app.extension;
23

    
24
import java.io.File;
25
import java.io.InputStream;
26
import java.net.MalformedURLException;
27
import java.net.URL;
28
import java.util.List;
29
import org.apache.commons.io.IOUtils;
30
import org.apache.commons.lang3.StringUtils;
31

    
32
import org.gvsig.andami.IconThemeHelper;
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.PluginsLocator;
35
import org.gvsig.andami.PluginsManager;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.andami.plugins.PluginClassLoader;
38
import org.gvsig.scripting.ScriptingLocator;
39
import org.gvsig.scripting.ScriptingManager;
40
import org.gvsig.scripting.swing.api.ScriptingSwingLocator;
41
import org.gvsig.scripting.swing.api.ScriptingUIManager;
42
import org.gvsig.tools.dynobject.DynObject;
43
import org.gvsig.tools.swing.impl.windowmanager.DefaultWindowManager;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46
import org.gvsig.tools.script.Script;
47
import org.gvsig.tools.util.Invocable;
48

    
49
@SuppressWarnings("UseSpecificCatch")
50
public class ScriptingExtension extends Extension {
51

    
52
    @SuppressWarnings("FieldNameHidesFieldInSuperclass")
53
    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingExtension.class);
54

    
55
    private ScriptingUtils utils;
56
    
57
    @Deprecated
58
    public static void log(String message) {
59
        ScriptingUtils.log(ScriptingUtils.INFO, message, null);
60
    }
61

    
62
    @Deprecated
63
    public static void log(int level, String message) {
64
        ScriptingUtils.log(level, message, null);
65
    }
66

    
67
    @Deprecated
68
    public static void log(int level, String message, Throwable th) {
69
        ScriptingUtils.log(level, message, th);
70
    }
71
    
72
    @Override
73
    public void initialize() {
74
        IconThemeHelper.registerIcon("action", "tools-scripting-launcher", this);
75
        IconThemeHelper.registerIcon("action", "tools-scripting-composer", this);
76
        IconThemeHelper.registerIcon("action", "tools-scripting-console-jython", this);
77

    
78
        PluginsManager pluginManager = PluginsLocator.getManager();
79
        this.utils = new ScriptingUtils();
80

    
81
        this.utils.initializaPaths(
82
                pluginManager.getPluginsFolders(), 
83
                pluginManager.getInstallFolder(), 
84
                this.getPlugin().getPluginHomeFolder(), 
85
                pluginManager.getApplicationVersion().format(ScriptsInstallerInitializer.VERSION_FORMAT)
86
        );
87
        
88
        Thread th = new Thread(new Runnable() {
89
            @Override
90
            public void run() {
91
                preloadPythonEngine();
92
            }
93
        }, "ScriptEnginesInitializer");
94
        th.start();
95
        try {
96
            th.join(1000); // force change to other thread
97
        } catch (InterruptedException ex) {
98
            // Ignore.
99
        }
100
    }
101

    
102
    @Override
103
    public void execute(String actionCommand) {
104
        this.execute(actionCommand, null);
105
    }
106

    
107
    @Override
108
    public void execute(String command, Object[] args) {
109

    
110

    
111
        if( "tools-scripting-launcher".equalsIgnoreCase(command) ) {
112
            utils.runLauncher();
113
            
114
        } else if( "tools-scripting-composer".equalsIgnoreCase(command) ) {
115

    
116
            DynObject preferences = getPlugin().getPluginProperties();
117
            Boolean composerUseHisWindowManager = (Boolean) preferences.getDynValue("ComposerUseHisWindowManager");
118
            if( composerUseHisWindowManager ) {
119
                DefaultWindowManager winmanager = new DefaultWindowManager();
120
                ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager();
121
                uimanager.setWindowManager(winmanager);
122
            }
123
            utils.runComposer();
124

    
125
        } else {
126
            utils.runScript(command, args);
127
        }
128
    }
129

    
130
    private void preloadPythonEngine() {
131
        ScriptingManager manager = (ScriptingManager) ScriptingLocator.getManager();
132
        synchronized (manager) {
133
            String respath = "/scripting/langs/python/preload.py";
134
            InputStream res = this.getClass().getResourceAsStream(respath);
135
            if( res != null ) {
136
                LOGGER.info("Scan for script engines");
137
                List<String> lines;
138
                try {
139
                    lines = IOUtils.readLines(res);
140
                    String code = StringUtils.join(lines, "\n");
141
                    LOGGER.info("Preload python script engine");
142
                    Script script = manager.createScript(
143
                        "preload",
144
                        code,
145
                        ScriptingManager.PYTHON_LANGUAGE_NAME
146
                    );
147
                    LOGGER.info("Preload python modules");
148
                    script.invokeFunction("main", null);
149

    
150
                } catch (Exception ex) {
151
                    LOGGER.warn("Can't run preload script for python.", ex);
152
                }
153
                LOGGER.info("Preload of script engines finished");
154
            }
155
        }
156
    }
157

    
158
    
159
    @Override
160
    public void postInitialize() {
161
        super.postInitialize();
162
                        
163
        PluginsManager pluginManager = PluginsLocator.getManager();
164

    
165
        pluginManager.addStartupTask(
166
            "ExecuteAutorunScripts",
167
            utils.getAutorunScriptsOnStartup(pluginManager.getPluginsFolders()),
168
            true,
169
            200
170
        );
171

    
172
        Invocable initializer = new ScriptsInstallerInitializer();
173
        initializer.call(this.getPlugin().getPluginName());
174
    }
175

    
176
    @Override
177
    public boolean isEnabled() {
178
        return true;
179
    }
180

    
181
    @Override
182
    public boolean isVisible() {
183
        return true;
184
    }
185

    
186
    public static void add_classpath(URL url) {
187
        if( url==null ) {
188
            LOGGER.warn("Can't add to the plugin classloader a null URL.");
189
            return;
190
        }
191
        try {
192
            PluginsManager manager = PluginsLocator.getManager();
193
            PluginServices thePlugin = manager.getPlugin(ScriptingExtension.class);
194
            PluginClassLoader loader = thePlugin.getClassLoader();
195
            loader.addURL(url);
196
        } catch(Throwable th) {
197
            LOGGER.warn("Can't add the url '"+url.toString()+"' to the plugin classloader.",th);
198
        }
199
    }
200
    
201
    public static void add_classpath(File path) {
202
        if( path==null ) {
203
            LOGGER.warn("Can't create a url from a null file.");
204
            return;
205
        }
206
        try {
207
            URL url = path.toURI().toURL();
208
            add_classpath(url);
209
        } catch (MalformedURLException ex) {
210
            LOGGER.warn("Can't create a url from the path '"+path+"'.", ex);
211
        }
212
    }
213
    
214
    public static void add_classpath(String path) {
215
        if( path==null ) {
216
            LOGGER.warn("Can't create a url from a null path.");
217
            return;
218
        }
219
        File f = new File(path);
220
        add_classpath(f);
221
    }
222

    
223
    @Override
224
    public PluginServices getPlugin() {
225
        PluginServices thePlugin = super.getPlugin();
226
        if (thePlugin == null) {
227
            thePlugin = PluginsLocator.getManager().getPlugin(ScriptingExtension.class);
228
            this.setPlugin(thePlugin);
229
        }
230
        return thePlugin;
231
    }
232
    
233
}