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

History | View | Annotate | Download (6.17 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 812 jjdelcerro
import java.io.InputStream;
25 462 jjdelcerro
import java.util.List;
26 812 jjdelcerro
import org.apache.commons.io.IOUtils;
27
import org.apache.commons.lang3.StringUtils;
28 462 jjdelcerro
29
import org.gvsig.andami.IconThemeHelper;
30
import org.gvsig.andami.PluginsLocator;
31
import org.gvsig.andami.PluginsManager;
32
import org.gvsig.andami.plugins.Extension;
33
import org.gvsig.scripting.ScriptingLocator;
34
import org.gvsig.scripting.ScriptingManager;
35
import org.gvsig.scripting.swing.api.ScriptingSwingLocator;
36
import org.gvsig.scripting.swing.api.ScriptingUIManager;
37
import org.gvsig.tools.dynobject.DynObject;
38
import org.gvsig.tools.swing.impl.windowmanager.DefaultWindowManager;
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41 812 jjdelcerro
import org.gvsig.tools.script.Script;
42 821 jjdelcerro
import org.gvsig.tools.util.Invocable;
43 462 jjdelcerro
44 1085 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
45 462 jjdelcerro
public class ScriptingExtension extends Extension {
46
47 1023 jjdelcerro
    @SuppressWarnings("FieldNameHidesFieldInSuperclass")
48 1085 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingExtension.class);
49 462 jjdelcerro
50 1063 jjdelcerro
    private ScriptingUtils utils;
51 989 jjdelcerro
52 1063 jjdelcerro
    @Deprecated
53 636 jjdelcerro
    public static void log(String message) {
54 1063 jjdelcerro
        ScriptingUtils.log(ScriptingUtils.INFO, message, null);
55 636 jjdelcerro
    }
56 844 jjdelcerro
57 1063 jjdelcerro
    @Deprecated
58 636 jjdelcerro
    public static void log(int level, String message) {
59 1063 jjdelcerro
        ScriptingUtils.log(level, message, null);
60 636 jjdelcerro
    }
61 844 jjdelcerro
62 1063 jjdelcerro
    @Deprecated
63 636 jjdelcerro
    public static void log(int level, String message, Throwable th) {
64 1063 jjdelcerro
        ScriptingUtils.log(level, message, th);
65 636 jjdelcerro
    }
66 844 jjdelcerro
67 799 jjdelcerro
    @Override
68 463 jjdelcerro
    public void initialize() {
69
        IconThemeHelper.registerIcon("action", "tools-scripting-launcher", this);
70
        IconThemeHelper.registerIcon("action", "tools-scripting-composer", this);
71
        IconThemeHelper.registerIcon("action", "tools-scripting-console-jython", this);
72 844 jjdelcerro
73 1085 jjdelcerro
        PluginsManager pluginManager = PluginsLocator.getManager();
74
        this.utils = new ScriptingUtils();
75
76
        this.utils.initializaPaths(
77
                pluginManager.getPluginsFolders(),
78
                pluginManager.getInstallFolder(),
79
                this.getPlugin().getPluginHomeFolder(),
80
                pluginManager.getApplicationVersion().format(ScriptsInstallerInitializer.VERSION_FORMAT)
81
        );
82
83 812 jjdelcerro
        Thread th = new Thread(new Runnable() {
84
            @Override
85
            public void run() {
86
                preloadPythonEngine();
87
            }
88
        }, "ScriptEnginesInitializer");
89
        th.start();
90
        try {
91 1063 jjdelcerro
            th.join(1000); // force change to other thread
92 812 jjdelcerro
        } catch (InterruptedException ex) {
93
            // Ignore.
94
        }
95 463 jjdelcerro
    }
96 844 jjdelcerro
97 1063 jjdelcerro
    @Override
98
    public void execute(String actionCommand) {
99
        this.execute(actionCommand, null);
100
    }
101
102
    @Override
103
    public void execute(String command, Object[] args) {
104
105
106
        if( "tools-scripting-launcher".equalsIgnoreCase(command) ) {
107
            utils.runLauncher();
108
109
        } else if( "tools-scripting-composer".equalsIgnoreCase(command) ) {
110
111
            DynObject preferences = getPlugin().getPluginProperties();
112
            Boolean composerUseHisWindowManager = (Boolean) preferences.getDynValue("ComposerUseHisWindowManager");
113
            if( composerUseHisWindowManager ) {
114
                DefaultWindowManager winmanager = new DefaultWindowManager();
115
                ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager();
116
                uimanager.setWindowManager(winmanager);
117
            }
118
            utils.runComposer();
119
120
        } else {
121
            utils.runScript(command, args);
122
        }
123
    }
124
125 812 jjdelcerro
    private void preloadPythonEngine() {
126
        ScriptingManager manager = (ScriptingManager) ScriptingLocator.getManager();
127 844 jjdelcerro
        synchronized (manager) {
128 812 jjdelcerro
            String respath = "/scripting/langs/python/preload.py";
129
            InputStream res = this.getClass().getResourceAsStream(respath);
130 844 jjdelcerro
            if( res != null ) {
131 1085 jjdelcerro
                LOGGER.info("Scan for script engines");
132 812 jjdelcerro
                List<String> lines;
133
                try {
134
                    lines = IOUtils.readLines(res);
135
                    String code = StringUtils.join(lines, "\n");
136 1085 jjdelcerro
                    LOGGER.info("Preload python script engine");
137 812 jjdelcerro
                    Script script = manager.createScript(
138 844 jjdelcerro
                        "preload",
139
                        code,
140
                        ScriptingManager.PYTHON_LANGUAGE_NAME
141 812 jjdelcerro
                    );
142 1085 jjdelcerro
                    LOGGER.info("Preload python modules");
143 812 jjdelcerro
                    script.invokeFunction("main", null);
144 844 jjdelcerro
145 812 jjdelcerro
                } catch (Exception ex) {
146 1085 jjdelcerro
                    LOGGER.warn("Can't run preload script for python.", ex);
147 812 jjdelcerro
                }
148 1085 jjdelcerro
                LOGGER.info("Preload of script engines finished");
149 812 jjdelcerro
            }
150
        }
151
    }
152 463 jjdelcerro
153 989 jjdelcerro
154 812 jjdelcerro
    @Override
155
    public void postInitialize() {
156
        super.postInitialize();
157 1085 jjdelcerro
158 812 jjdelcerro
        PluginsManager pluginManager = PluginsLocator.getManager();
159 1063 jjdelcerro
160 812 jjdelcerro
        pluginManager.addStartupTask(
161 844 jjdelcerro
            "ExecuteAutorunScripts",
162 1063 jjdelcerro
            utils.getAutorunScriptsOnStartup(pluginManager.getPluginsFolders()),
163 844 jjdelcerro
            true,
164 1086 jjdelcerro
            200
165 812 jjdelcerro
        );
166 844 jjdelcerro
167 821 jjdelcerro
        Invocable initializer = new ScriptsInstallerInitializer();
168
        initializer.call(this.getPlugin().getPluginName());
169 463 jjdelcerro
    }
170
171 478 jjdelcerro
    @Override
172 463 jjdelcerro
    public boolean isEnabled() {
173
        return true;
174
    }
175
176 478 jjdelcerro
    @Override
177 463 jjdelcerro
    public boolean isVisible() {
178
        return true;
179
    }
180 468 jjdelcerro
181 462 jjdelcerro
}