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

History | View | Annotate | Download (6.52 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.InputStream;
25
import java.util.List;
26
import org.apache.commons.io.IOUtils;
27
import org.apache.commons.lang3.StringUtils;
28

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

    
45
@SuppressWarnings("UseSpecificCatch")
46
public class ScriptingExtension extends Extension {
47

    
48
    @SuppressWarnings("FieldNameHidesFieldInSuperclass")
49
    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingExtension.class);
50

    
51
    private ScriptingUtils utils;
52
    
53
    @Deprecated
54
    public static void log(String message) {
55
        ScriptingUtils.log(ScriptingUtils.INFO, message, null);
56
    }
57

    
58
    @Deprecated
59
    public static void log(int level, String message) {
60
        ScriptingUtils.log(level, message, null);
61
    }
62

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

    
74
        PluginsManager pluginManager = PluginsLocator.getManager();
75
        this.utils = new ScriptingUtils();
76

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

    
98
    @Override
99
    public void execute(String actionCommand) {
100
        this.execute(actionCommand, null);
101
    }
102

    
103
    @Override
104
    public void execute(String command, Object[] args) {
105

    
106

    
107
        if( "tools-scripting-launcher".equalsIgnoreCase(command) ) {
108
            utils.runLauncher();
109
            
110
        } else if( "tools-scripting-composer".equalsIgnoreCase(command) ) {
111

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

    
121
        } else {
122
            utils.runScript(command, args);
123
        }
124
    }
125

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

    
146
                } catch (Exception ex) {
147
                    LOGGER.warn("Can't run preload script for python.", ex);
148
                }
149
                LOGGER.info("Preload of script engines finished");
150
            }
151
        }
152
    }
153

    
154
    
155
    @Override
156
    public void postInitialize() {
157
        super.postInitialize();
158
                        
159
        PluginsManager pluginManager = PluginsLocator.getManager();
160

    
161
        pluginManager.addStartupTask(
162
            "ExecuteAutorunScripts",
163
            utils.getAutorunScriptsOnStartup(pluginManager.getPluginsFolders()),
164
            true,
165
            200
166
        );
167

    
168
        Invocable initializer = new ScriptsInstallerInitializer();
169
        initializer.call(this.getPlugin().getPluginName());
170
    }
171

    
172
    @Override
173
    public boolean isEnabled() {
174
        return true;
175
    }
176

    
177
    @Override
178
    public boolean isVisible() {
179
        return true;
180
    }
181

    
182
    @Override
183
    public PluginServices getPlugin() {
184
        PluginServices thePlugin = super.getPlugin();
185
        if (thePlugin == null) {
186
            thePlugin = PluginsLocator.getManager().getPlugin(ScriptingExtension.class);
187
            this.setPlugin(thePlugin);
188
        }
189
        return thePlugin;
190
    }
191
    
192

    
193
}