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

History | View | Annotate | Download (7.85 KB)

1 462 jjdelcerro
/* 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 1092 jjdelcerro
import java.io.File;
25 812 jjdelcerro
import java.io.InputStream;
26 1092 jjdelcerro
import java.net.MalformedURLException;
27
import java.net.URL;
28 462 jjdelcerro
import java.util.List;
29 812 jjdelcerro
import org.apache.commons.io.IOUtils;
30
import org.apache.commons.lang3.StringUtils;
31 462 jjdelcerro
32
import org.gvsig.andami.IconThemeHelper;
33 1087 omartinez
import org.gvsig.andami.PluginServices;
34 462 jjdelcerro
import org.gvsig.andami.PluginsLocator;
35
import org.gvsig.andami.PluginsManager;
36
import org.gvsig.andami.plugins.Extension;
37 1092 jjdelcerro
import org.gvsig.andami.plugins.PluginClassLoader;
38 462 jjdelcerro
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 812 jjdelcerro
import org.gvsig.tools.script.Script;
47 821 jjdelcerro
import org.gvsig.tools.util.Invocable;
48 462 jjdelcerro
49 1085 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
50 462 jjdelcerro
public class ScriptingExtension extends Extension {
51
52 1023 jjdelcerro
    @SuppressWarnings("FieldNameHidesFieldInSuperclass")
53 1085 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingExtension.class);
54 462 jjdelcerro
55 1063 jjdelcerro
    private ScriptingUtils utils;
56 989 jjdelcerro
57 1063 jjdelcerro
    @Deprecated
58 636 jjdelcerro
    public static void log(String message) {
59 1063 jjdelcerro
        ScriptingUtils.log(ScriptingUtils.INFO, message, null);
60 636 jjdelcerro
    }
61 844 jjdelcerro
62 1063 jjdelcerro
    @Deprecated
63 636 jjdelcerro
    public static void log(int level, String message) {
64 1063 jjdelcerro
        ScriptingUtils.log(level, message, null);
65 636 jjdelcerro
    }
66 844 jjdelcerro
67 1063 jjdelcerro
    @Deprecated
68 636 jjdelcerro
    public static void log(int level, String message, Throwable th) {
69 1063 jjdelcerro
        ScriptingUtils.log(level, message, th);
70 636 jjdelcerro
    }
71 844 jjdelcerro
72 799 jjdelcerro
    @Override
73 463 jjdelcerro
    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 844 jjdelcerro
78 1085 jjdelcerro
        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 812 jjdelcerro
        Thread th = new Thread(new Runnable() {
89
            @Override
90
            public void run() {
91
                preloadPythonEngine();
92
            }
93
        }, "ScriptEnginesInitializer");
94
        th.start();
95
        try {
96 1063 jjdelcerro
            th.join(1000); // force change to other thread
97 812 jjdelcerro
        } catch (InterruptedException ex) {
98
            // Ignore.
99
        }
100 463 jjdelcerro
    }
101 844 jjdelcerro
102 1063 jjdelcerro
    @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 812 jjdelcerro
    private void preloadPythonEngine() {
131
        ScriptingManager manager = (ScriptingManager) ScriptingLocator.getManager();
132 844 jjdelcerro
        synchronized (manager) {
133 812 jjdelcerro
            String respath = "/scripting/langs/python/preload.py";
134
            InputStream res = this.getClass().getResourceAsStream(respath);
135 844 jjdelcerro
            if( res != null ) {
136 1085 jjdelcerro
                LOGGER.info("Scan for script engines");
137 812 jjdelcerro
                List<String> lines;
138
                try {
139
                    lines = IOUtils.readLines(res);
140
                    String code = StringUtils.join(lines, "\n");
141 1085 jjdelcerro
                    LOGGER.info("Preload python script engine");
142 812 jjdelcerro
                    Script script = manager.createScript(
143 844 jjdelcerro
                        "preload",
144
                        code,
145
                        ScriptingManager.PYTHON_LANGUAGE_NAME
146 812 jjdelcerro
                    );
147 1085 jjdelcerro
                    LOGGER.info("Preload python modules");
148 812 jjdelcerro
                    script.invokeFunction("main", null);
149 844 jjdelcerro
150 812 jjdelcerro
                } catch (Exception ex) {
151 1085 jjdelcerro
                    LOGGER.warn("Can't run preload script for python.", ex);
152 812 jjdelcerro
                }
153 1085 jjdelcerro
                LOGGER.info("Preload of script engines finished");
154 812 jjdelcerro
            }
155
        }
156
    }
157 463 jjdelcerro
158 989 jjdelcerro
159 812 jjdelcerro
    @Override
160
    public void postInitialize() {
161
        super.postInitialize();
162 1085 jjdelcerro
163 812 jjdelcerro
        PluginsManager pluginManager = PluginsLocator.getManager();
164 1063 jjdelcerro
165 812 jjdelcerro
        pluginManager.addStartupTask(
166 844 jjdelcerro
            "ExecuteAutorunScripts",
167 1063 jjdelcerro
            utils.getAutorunScriptsOnStartup(pluginManager.getPluginsFolders()),
168 1105 jjdelcerro
            false,
169 1086 jjdelcerro
            200
170 812 jjdelcerro
        );
171 844 jjdelcerro
172 821 jjdelcerro
        Invocable initializer = new ScriptsInstallerInitializer();
173
        initializer.call(this.getPlugin().getPluginName());
174 463 jjdelcerro
    }
175
176 478 jjdelcerro
    @Override
177 463 jjdelcerro
    public boolean isEnabled() {
178
        return true;
179
    }
180
181 478 jjdelcerro
    @Override
182 463 jjdelcerro
    public boolean isVisible() {
183
        return true;
184
    }
185 468 jjdelcerro
186 1092 jjdelcerro
    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 1087 omartinez
    @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 462 jjdelcerro
}