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
/* 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
public class ScriptingExtension extends Extension {
45

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

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

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

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

    
72
        Thread th = new Thread(new Runnable() {
73
            @Override
74
            public void run() {
75
                preloadPythonEngine();
76
            }
77
        }, "ScriptEnginesInitializer");
78
        th.start();
79
        try {
80
            th.join(1000); // force change to other thread
81
        } catch (InterruptedException ex) {
82
            // Ignore.
83
        }
84
    }
85

    
86
    @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
    private void preloadPythonEngine() {
115
        ScriptingManager manager = (ScriptingManager) ScriptingLocator.getManager();
116
        synchronized (manager) {
117
            String respath = "/scripting/langs/python/preload.py";
118
            InputStream res = this.getClass().getResourceAsStream(respath);
119
            if( res != null ) {
120
                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
                        "preload",
128
                        code,
129
                        ScriptingManager.PYTHON_LANGUAGE_NAME
130
                    );
131
                    logger.info("Preload python modules");
132
                    script.invokeFunction("main", null);
133

    
134
                } 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

    
142
    
143
    @Override
144
    public void postInitialize() {
145
        super.postInitialize();
146
        
147
        PluginsManager pluginManager = PluginsLocator.getManager();
148
        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
        pluginManager.addStartupTask(
158
            "ExecuteAutorunScripts",
159
            utils.getAutorunScriptsOnStartup(pluginManager.getPluginsFolders()),
160
            true,
161
            600
162
        );
163

    
164
        Invocable initializer = new ScriptsInstallerInitializer();
165
        initializer.call(this.getPlugin().getPluginName());
166
    }
167

    
168
    @Override
169
    public boolean isEnabled() {
170
        return true;
171
    }
172

    
173
    @Override
174
    public boolean isVisible() {
175
        return true;
176
    }
177

    
178
}