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 @ 738
History | View | Annotate | Download (15.7 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.File; |
25 |
import java.io.IOException; |
26 |
import java.nio.file.FileVisitOption; |
27 |
import static java.nio.file.FileVisitOption.FOLLOW_LINKS; |
28 |
import java.nio.file.FileVisitResult; |
29 |
import java.nio.file.Files; |
30 |
import java.nio.file.Path; |
31 |
import java.nio.file.Paths; |
32 |
import java.nio.file.SimpleFileVisitor; |
33 |
import java.nio.file.attribute.BasicFileAttributes; |
34 |
import java.util.ArrayList; |
35 |
import java.util.Arrays; |
36 |
import java.util.EnumSet; |
37 |
import java.util.List; |
38 |
|
39 |
import javax.swing.JOptionPane; |
40 |
import org.apache.commons.io.FileUtils; |
41 |
|
42 |
import org.gvsig.andami.IconThemeHelper; |
43 |
import org.gvsig.andami.PluginServices; |
44 |
import org.gvsig.andami.PluginsLocator; |
45 |
import org.gvsig.andami.PluginsManager; |
46 |
import org.gvsig.andami.plugins.Extension; |
47 |
import org.gvsig.app.ApplicationLocator; |
48 |
import org.gvsig.app.ApplicationManager; |
49 |
import org.gvsig.installer.lib.api.InstallerLocator; |
50 |
import org.gvsig.installer.lib.api.InstallerManager; |
51 |
import org.gvsig.scripting.ScriptingBaseScript; |
52 |
import org.gvsig.scripting.ScriptingFolder; |
53 |
import org.gvsig.scripting.ScriptingLocator; |
54 |
import org.gvsig.scripting.ScriptingManager; |
55 |
import org.gvsig.scripting.swing.api.JScriptingComposer; |
56 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
57 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
58 |
import org.gvsig.tools.dynobject.DynObject; |
59 |
import org.gvsig.tools.service.spi.ProviderFactory; |
60 |
import org.gvsig.tools.service.spi.ProviderManager; |
61 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
62 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
63 |
import org.gvsig.tools.swing.impl.windowmanager.DefaultWindowManager; |
64 |
import org.slf4j.Logger; |
65 |
import org.slf4j.LoggerFactory; |
66 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
67 |
import org.gvsig.tools.ToolsLocator; |
68 |
import org.gvsig.tools.i18n.I18nManager; |
69 |
|
70 |
public class ScriptingExtension extends Extension { |
71 |
|
72 |
private static final Logger logger = LoggerFactory.getLogger(ScriptingExtension.class); |
73 |
|
74 |
private PluginServices plugin = null; |
75 |
|
76 |
/*
|
77 |
* la funcion log y las constantes estan pensadas para usarlas desde los scripts.
|
78 |
*/
|
79 |
public static final int INFO = 0; |
80 |
public static final int TRACE = 1; |
81 |
public static final int WARN = 2; |
82 |
public static final int ERROR = 3; |
83 |
|
84 |
public static void log(String message) { |
85 |
log(INFO,message,null);
|
86 |
} |
87 |
|
88 |
public static void log(int level, String message) { |
89 |
log(level,message,null);
|
90 |
} |
91 |
|
92 |
public static void log(int level, String message, Throwable th) { |
93 |
switch(level) {
|
94 |
case TRACE:
|
95 |
logger.trace(message, th); |
96 |
break;
|
97 |
case ERROR:
|
98 |
logger.error(message, th); |
99 |
break;
|
100 |
case WARN:
|
101 |
logger.warn(message, th); |
102 |
break;
|
103 |
default:
|
104 |
case INFO:
|
105 |
logger.info(message, th); |
106 |
break;
|
107 |
} |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public PluginServices getPlugin() {
|
112 |
if( this.plugin == null ) { |
113 |
this.plugin = PluginsLocator.getManager().getPlugin(ScriptingExtension.class);
|
114 |
} |
115 |
return this.plugin; |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public void execute(String actionCommand) { |
120 |
this.execute(actionCommand, null); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void execute(String command, Object[] args) { |
125 |
ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
126 |
WindowManager winmanager = ToolsSwingLocator.getWindowManager(); |
127 |
|
128 |
if ("tools-scripting-launcher".equalsIgnoreCase(command)) { |
129 |
winmanager.showWindow(uimanager.createLauncher().asJComponent(), uimanager.getTranslation("Scripting_Launcher"), WindowManager.MODE.TOOL);
|
130 |
|
131 |
} else if ("tools-scripting-composer".equalsIgnoreCase(command)) { |
132 |
DynObject preferences = this.getPlugin().getPluginProperties();
|
133 |
Boolean composerUseHisWindowManager = (Boolean) preferences.getDynValue("ComposerUseHisWindowManager"); |
134 |
ScriptingUIManager uiManager = ScriptingSwingLocator.getUIManager(); |
135 |
if (composerUseHisWindowManager) {
|
136 |
winmanager = new DefaultWindowManager();
|
137 |
uiManager.setWindowManager(winmanager); |
138 |
} |
139 |
JScriptingComposer composer = uimanager.createComposer(); |
140 |
uiManager.showWindow(composer.asJComponent(), uimanager.getTranslation("Scripting_Composer"));
|
141 |
|
142 |
} else {
|
143 |
ScriptingBaseScript script = uimanager.getManager().getScript(command); |
144 |
if (script != null) { |
145 |
script.run(args); |
146 |
} else {
|
147 |
ApplicationManager application = ApplicationLocator.getManager(); |
148 |
application.messageDialog("Can't locate script '" + command + "'.", "ScriptLaunch", JOptionPane.OK_OPTION); |
149 |
} |
150 |
} |
151 |
} |
152 |
|
153 |
public void initialize() { |
154 |
IconThemeHelper.registerIcon("action", "tools-scripting-launcher", this); |
155 |
IconThemeHelper.registerIcon("action", "tools-scripting-composer", this); |
156 |
IconThemeHelper.registerIcon("action", "tools-scripting-console-jython", this); |
157 |
} |
158 |
|
159 |
private void addLinkToPreviousVersion(ScriptingManager manager) { |
160 |
|
161 |
String contents = "[Unit]\n" |
162 |
+ "type = Folder\n"
|
163 |
+ "name = Previous version\n"
|
164 |
+ "description =\n"
|
165 |
+ "createdBy =\n"
|
166 |
+ "version =\n"
|
167 |
+ "\n"
|
168 |
+ "[Folder]\n"
|
169 |
+ "path = ../../org.gvsig.scripting.app.extension/scripts\n\n\n";
|
170 |
File previousVersion = new File(manager.getUserFolder().getFile(), "previous_version.inf"); |
171 |
if (!previousVersion.exists()) {
|
172 |
try {
|
173 |
FileUtils.writeStringToFile(previousVersion, contents); |
174 |
} catch (IOException ex) { |
175 |
logger.warn("Can't create ScriptingFolder file in '" + previousVersion.getAbsolutePath() + "'.", ex); |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public void postInitialize() { |
182 |
super.postInitialize();
|
183 |
ScriptingManager manager = ScriptingLocator.getManager(); |
184 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
185 |
|
186 |
File home = this.getPlugin().getPluginHomeFolder(); |
187 |
manager.setHomeFolder(home); |
188 |
|
189 |
this.addLinkToPreviousVersion(manager);
|
190 |
|
191 |
List<File> pluginsFolders = new ArrayList<>(); |
192 |
for (File f : pluginManager.getPluginsFolders()) { |
193 |
pluginsFolders.addAll(Arrays.asList(f.listFiles()));
|
194 |
} |
195 |
|
196 |
for (File pluginFolder : pluginsFolders) { |
197 |
File scriptsFolder = new File(pluginFolder, "scripting/scripts"); |
198 |
if (scriptsFolder.exists()) {
|
199 |
manager.registerSystemFolder(pluginFolder.getName(), scriptsFolder); |
200 |
} |
201 |
File libFolder = new File(pluginFolder, "scripting/lib"); |
202 |
if (libFolder.exists()) {
|
203 |
manager.addLibFolder(libFolder); |
204 |
} |
205 |
} |
206 |
pluginManager.addStartupTask( |
207 |
"ExecuteAutorunScripts",
|
208 |
new ExecuteAutorunScriptsOnStartup(),
|
209 |
true,
|
210 |
600
|
211 |
); |
212 |
|
213 |
manager.setPackagesFolder(pluginManager.getInstallFolder()); |
214 |
|
215 |
File localAddonRepositoryFolder = new File(manager.getRootUserFolder(), "addons"); |
216 |
if (!localAddonRepositoryFolder.exists()) {
|
217 |
try {
|
218 |
FileUtils.forceMkdir(localAddonRepositoryFolder); |
219 |
} catch (IOException ex) { |
220 |
logger.info("Can't create addons folder in '" + localAddonRepositoryFolder.getAbsolutePath() + "'.", ex); |
221 |
} |
222 |
} |
223 |
ProviderManager providerManager = InstallerProviderLocator.getProviderManager(); |
224 |
InstallerManager installerManager = InstallerLocator.getInstallerManager(); |
225 |
|
226 |
ProviderFactory factory = manager.getInstallerFactory(); |
227 |
providerManager.addProviderFactory(factory); |
228 |
installerManager.setDefaultLocalAddonRepository( |
229 |
localAddonRepositoryFolder, |
230 |
factory.getName() |
231 |
); |
232 |
factory = manager.getHelpInstallerFactory(); |
233 |
providerManager.addProviderFactory(factory); |
234 |
installerManager.setDefaultLocalAddonRepository( |
235 |
manager.getHelpManager().getHelpRoots().get(0),
|
236 |
factory.getName() |
237 |
); |
238 |
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public boolean isEnabled() { |
243 |
return true; |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public boolean isVisible() { |
248 |
return true; |
249 |
} |
250 |
|
251 |
private static class ExecuteAutorunScriptsOnStartup implements Runnable { |
252 |
|
253 |
@Override
|
254 |
public void run() { |
255 |
Thread th = new Thread(new ExecuteAutorunScripts()); |
256 |
th.start(); |
257 |
} |
258 |
} |
259 |
|
260 |
private static class ExecuteAutorunScripts implements Runnable { |
261 |
|
262 |
@Override
|
263 |
public void run() { |
264 |
final ScriptingManager manager = ScriptingLocator.getManager();
|
265 |
manager.loadEngines(); |
266 |
|
267 |
final PluginsManager pluginManager = PluginsLocator.getManager();
|
268 |
final ApplicationManager application = ApplicationLocator.getManager();
|
269 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
270 |
|
271 |
// try {
|
272 |
// ScriptingHelpManager helpmanager = manager.getHelpManager();
|
273 |
// PluginServices plugin = pluginManager.getPlugin(this);
|
274 |
// File scriptingfolder = new File(plugin.getPluginDirectory(), "scripting");
|
275 |
// File helpfolder = new File(scriptingfolder, "help");
|
276 |
// for (File folder : helpfolder.listFiles()) {
|
277 |
// File helpindex = new File(folder, "help-index.xml");
|
278 |
// try {
|
279 |
// if( helpindex.exists() ) {
|
280 |
// helpmanager.addMethods(helpindex.toURI().toURL());
|
281 |
// }
|
282 |
// } catch (Exception e) {
|
283 |
// logger.info("Can't add metothods from '" + helpindex.getAbsolutePath() + "'.", e);
|
284 |
// }
|
285 |
// }
|
286 |
// } catch (Exception e) {
|
287 |
// logger.info("Can't add metothods.", e);
|
288 |
// }
|
289 |
try {
|
290 |
List<File> pluginsFolders = new ArrayList<>(); |
291 |
for (File f : pluginManager.getPluginsFolders()) { |
292 |
pluginsFolders.addAll(Arrays.asList(f.listFiles()));
|
293 |
} |
294 |
|
295 |
File autorun_file = null; |
296 |
ScriptingBaseScript autorun; |
297 |
application.message( |
298 |
i18nManager.getTranslation("_Searching_autorun_scripts_Xhorizontal_ellipsisX"),
|
299 |
JOptionPane.INFORMATION_MESSAGE
|
300 |
); |
301 |
for (File pluginFolder : pluginsFolders) { |
302 |
autorun_file = new File(pluginFolder, "scripting/scripts/autorun.inf"); |
303 |
if (autorun_file.exists()) {
|
304 |
autorun = manager.getScript(autorun_file); |
305 |
if (autorun.isEnabled()) {
|
306 |
logger.info("running autorun script '" + autorun_file.getAbsolutePath() + "'."); |
307 |
try {
|
308 |
application.message( |
309 |
i18nManager.getTranslation( |
310 |
"_Running_autorun_script_from_XnameX",
|
311 |
new String[]{autorun.getFile().getParentFile().getName()} |
312 |
), |
313 |
JOptionPane.INFORMATION_MESSAGE
|
314 |
); |
315 |
} catch (Exception ex) { |
316 |
// Ignore it
|
317 |
} |
318 |
try {
|
319 |
autorun.run(); |
320 |
} catch (Exception ex) { |
321 |
logger.warn("Can't execute autorun from '" + autorun_file.getAbsolutePath() + "'.", ex); |
322 |
} |
323 |
|
324 |
} else {
|
325 |
logger.info("Skip autorun script '" + autorun_file.getAbsolutePath() + "'."); |
326 |
} |
327 |
} |
328 |
} |
329 |
|
330 |
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
|
331 |
@Override
|
332 |
public FileVisitResult visitFile(Path path, BasicFileAttributes bfa) throws IOException { |
333 |
File file = path.toFile();
|
334 |
if ("autorun.inf".equalsIgnoreCase(file.getName())) { |
335 |
if (file.exists()) {
|
336 |
ScriptingBaseScript autorun = manager.getScript(file); |
337 |
if( autorun.isEnabled() ) {
|
338 |
logger.info("running autorun script '" + file.getAbsolutePath() + "'."); |
339 |
try {
|
340 |
application.message( |
341 |
i18nManager.getTranslation( |
342 |
"_Running_autorun_script_from_XnameX",
|
343 |
new String[]{autorun.getFile().getParentFile().getName()} |
344 |
), |
345 |
JOptionPane.INFORMATION_MESSAGE
|
346 |
); |
347 |
} catch (Exception ex) { |
348 |
// Ignore
|
349 |
} |
350 |
try {
|
351 |
autorun.run(); |
352 |
} catch (Exception ex) { |
353 |
logger.warn("Can't execute autorun from '" + file.getAbsolutePath() + "'.", ex); |
354 |
} |
355 |
} else {
|
356 |
logger.info("Skip autorun script '" + file.getAbsolutePath() + "'."); |
357 |
} |
358 |
} |
359 |
} |
360 |
return FileVisitResult.CONTINUE;
|
361 |
} |
362 |
}; |
363 |
try {
|
364 |
EnumSet<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); |
365 |
Files.walkFileTree(Paths.get(manager.getRootUserFolder().toURI()), opts, Integer.MAX_VALUE, visitor);
|
366 |
List<ScriptingFolder> folders = manager.getAlternativeUserFolders();
|
367 |
for (ScriptingFolder folder : folders) {
|
368 |
Files.walkFileTree(Paths.get(folder.getFile().toURI()), opts, Integer.MAX_VALUE, visitor);
|
369 |
} |
370 |
} catch (Exception ex) { |
371 |
logger.warn("Can't execute autoruns in home.", ex);
|
372 |
} |
373 |
} finally {
|
374 |
logger.info("Running autorun scripts terminated.");
|
375 |
application.message("", JOptionPane.INFORMATION_MESSAGE); |
376 |
} |
377 |
} |
378 |
|
379 |
} |
380 |
} |