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
/* 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.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
import org.gvsig.tools.script.Script;
42
import org.gvsig.tools.util.Invocable;
43

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

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

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

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

    
62
    @Deprecated
63
    public static void log(int level, String message, Throwable th) {
64
        ScriptingUtils.log(level, message, th);
65
    }
66
    
67
    @Override
68
    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

    
73
        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
        Thread th = new Thread(new Runnable() {
84
            @Override
85
            public void run() {
86
                preloadPythonEngine();
87
            }
88
        }, "ScriptEnginesInitializer");
89
        th.start();
90
        try {
91
            th.join(1000); // force change to other thread
92
        } catch (InterruptedException ex) {
93
            // Ignore.
94
        }
95
    }
96

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

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

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

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

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

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

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

    
181
}