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

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