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 @ 1015
History | View | Annotate | Download (24 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 | import java.io.File; |
||
25 | 465 | jjdelcerro | import java.io.IOException; |
26 | 812 | jjdelcerro | import java.io.InputStream; |
27 | 630 | jjdelcerro | import java.nio.file.FileVisitOption; |
28 | import static java.nio.file.FileVisitOption.FOLLOW_LINKS; |
||
29 | 465 | jjdelcerro | import java.nio.file.FileVisitResult; |
30 | import java.nio.file.Files; |
||
31 | import java.nio.file.Path; |
||
32 | import java.nio.file.Paths; |
||
33 | import java.nio.file.SimpleFileVisitor; |
||
34 | import java.nio.file.attribute.BasicFileAttributes; |
||
35 | 822 | jjdelcerro | import java.text.MessageFormat; |
36 | 465 | jjdelcerro | import java.util.ArrayList; |
37 | 462 | jjdelcerro | import java.util.Arrays; |
38 | 822 | jjdelcerro | import java.util.Collections; |
39 | import java.util.Comparator; |
||
40 | 630 | jjdelcerro | import java.util.EnumSet; |
41 | 462 | jjdelcerro | import java.util.List; |
42 | 989 | jjdelcerro | import java.util.function.Predicate; |
43 | import java.util.logging.Level; |
||
44 | 462 | jjdelcerro | |
45 | import javax.swing.JOptionPane; |
||
46 | 844 | jjdelcerro | import javax.swing.SwingUtilities; |
47 | 471 | jjdelcerro | import org.apache.commons.io.FileUtils; |
48 | 989 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
49 | 812 | jjdelcerro | import org.apache.commons.io.IOUtils; |
50 | 989 | jjdelcerro | import org.apache.commons.lang3.BooleanUtils; |
51 | 812 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
52 | 462 | jjdelcerro | |
53 | import org.gvsig.andami.IconThemeHelper; |
||
54 | 636 | jjdelcerro | import org.gvsig.andami.PluginServices; |
55 | 462 | jjdelcerro | import org.gvsig.andami.PluginsLocator; |
56 | import org.gvsig.andami.PluginsManager; |
||
57 | import org.gvsig.andami.plugins.Extension; |
||
58 | import org.gvsig.app.ApplicationLocator; |
||
59 | import org.gvsig.app.ApplicationManager; |
||
60 | import org.gvsig.scripting.ScriptingBaseScript; |
||
61 | 465 | jjdelcerro | import org.gvsig.scripting.ScriptingFolder; |
62 | 462 | jjdelcerro | import org.gvsig.scripting.ScriptingLocator; |
63 | import org.gvsig.scripting.ScriptingManager; |
||
64 | 844 | jjdelcerro | import org.gvsig.scripting.ScriptingScript; |
65 | import org.gvsig.scripting.ScriptingUnit; |
||
66 | 955 | jjdelcerro | import org.gvsig.scripting.app.extension.messagewait.MessageWait; |
67 | 462 | jjdelcerro | import org.gvsig.scripting.swing.api.JScriptingComposer; |
68 | import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
||
69 | import org.gvsig.scripting.swing.api.ScriptingUIManager; |
||
70 | 844 | jjdelcerro | import static org.gvsig.scripting.swing.api.ScriptingUIManager.SCRIPT_COMPOSER_AUTORUN; |
71 | 462 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
72 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
73 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
74 | import org.gvsig.tools.swing.impl.windowmanager.DefaultWindowManager; |
||
75 | import org.slf4j.Logger; |
||
76 | import org.slf4j.LoggerFactory; |
||
77 | 542 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
78 | 844 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
79 | 542 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
80 | 812 | jjdelcerro | import org.gvsig.tools.script.Script; |
81 | 844 | jjdelcerro | import org.gvsig.tools.task.SimpleTaskStatus; |
82 | 821 | jjdelcerro | import org.gvsig.tools.util.Invocable; |
83 | 844 | jjdelcerro | import org.gvsig.tools.visitor.VisitCanceledException; |
84 | import org.gvsig.tools.visitor.Visitor; |
||
85 | 462 | jjdelcerro | |
86 | public class ScriptingExtension extends Extension { |
||
87 | |||
88 | 465 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(ScriptingExtension.class); |
89 | 462 | jjdelcerro | |
90 | 636 | jjdelcerro | private PluginServices plugin = null; |
91 | 844 | jjdelcerro | |
92 | 989 | jjdelcerro | private static final String SKIP_AUTORUNS = "skipAutoruns"; |
93 | |||
94 | 636 | jjdelcerro | /*
|
95 | * la funcion log y las constantes estan pensadas para usarlas desde los scripts.
|
||
96 | 844 | jjdelcerro | */
|
97 | 636 | jjdelcerro | public static final int INFO = 0; |
98 | public static final int TRACE = 1; |
||
99 | public static final int WARN = 2; |
||
100 | public static final int ERROR = 3; |
||
101 | 844 | jjdelcerro | private static boolean composer_initialized = false; |
102 | 955 | jjdelcerro | private boolean executingAutorunScripts = true; |
103 | private MessageWait message;
|
||
104 | 844 | jjdelcerro | |
105 | 636 | jjdelcerro | public static void log(String message) { |
106 | 844 | jjdelcerro | log(INFO, message, null);
|
107 | 636 | jjdelcerro | } |
108 | 844 | jjdelcerro | |
109 | 636 | jjdelcerro | public static void log(int level, String message) { |
110 | 844 | jjdelcerro | log(level, message, null);
|
111 | 636 | jjdelcerro | } |
112 | 844 | jjdelcerro | |
113 | 636 | jjdelcerro | public static void log(int level, String message, Throwable th) { |
114 | 844 | jjdelcerro | switch( level ) {
|
115 | case TRACE:
|
||
116 | logger.trace(message, th); |
||
117 | break;
|
||
118 | case ERROR:
|
||
119 | logger.error(message, th); |
||
120 | break;
|
||
121 | case WARN:
|
||
122 | logger.warn(message, th); |
||
123 | break;
|
||
124 | default:
|
||
125 | case INFO:
|
||
126 | logger.info(message, th); |
||
127 | break;
|
||
128 | 636 | jjdelcerro | } |
129 | } |
||
130 | 844 | jjdelcerro | |
131 | 478 | jjdelcerro | @Override
|
132 | 844 | jjdelcerro | public PluginServices getPlugin() {
|
133 | 636 | jjdelcerro | if( this.plugin == null ) { |
134 | this.plugin = PluginsLocator.getManager().getPlugin(ScriptingExtension.class);
|
||
135 | } |
||
136 | return this.plugin; |
||
137 | } |
||
138 | 844 | jjdelcerro | |
139 | 636 | jjdelcerro | @Override
|
140 | 463 | jjdelcerro | public void execute(String actionCommand) { |
141 | this.execute(actionCommand, null); |
||
142 | } |
||
143 | 462 | jjdelcerro | |
144 | 465 | jjdelcerro | @Override
|
145 | 463 | jjdelcerro | public void execute(String command, Object[] args) { |
146 | ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
||
147 | WindowManager winmanager = ToolsSwingLocator.getWindowManager(); |
||
148 | 462 | jjdelcerro | |
149 | 844 | jjdelcerro | if( "tools-scripting-launcher".equalsIgnoreCase(command) ) { |
150 | 639 | jjdelcerro | winmanager.showWindow(uimanager.createLauncher().asJComponent(), uimanager.getTranslation("Scripting_Launcher"), WindowManager.MODE.TOOL);
|
151 | 462 | jjdelcerro | |
152 | 844 | jjdelcerro | } else if( "tools-scripting-composer".equalsIgnoreCase(command) ) { |
153 | 955 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
154 | message.showMessage( |
||
155 | i18n.getTranslation("_notice"),
|
||
156 | i18n.getTranslation("_Initializing_the_scripting_plugin_Xhorizontal_ellipsisX") + "\n" + |
||
157 | i18n.getTranslation("_Waiting_to_terminate"),
|
||
158 | new MessageWait.WaitingCondition() {
|
||
159 | private Thread th = null; |
||
160 | |||
161 | @Override
|
||
162 | public boolean stopWaiting() { |
||
163 | if( executingAutorunScripts ) {
|
||
164 | return false; |
||
165 | } |
||
166 | if( composer_initialized ) {
|
||
167 | return true; |
||
168 | } |
||
169 | if( th == null ) { |
||
170 | th = new Thread(new StartupScriptingComposer(), "StartupScriptingComposer"); |
||
171 | th.start(); |
||
172 | } |
||
173 | return false; |
||
174 | } |
||
175 | }, |
||
176 | new Runnable() { |
||
177 | @Override
|
||
178 | public void run() { |
||
179 | showScriptingComposer(); |
||
180 | } |
||
181 | } |
||
182 | ); |
||
183 | 463 | jjdelcerro | |
184 | 955 | jjdelcerro | |
185 | 463 | jjdelcerro | } else {
|
186 | ScriptingBaseScript script = uimanager.getManager().getScript(command); |
||
187 | 844 | jjdelcerro | if( script != null ) { |
188 | 463 | jjdelcerro | script.run(args); |
189 | } else {
|
||
190 | ApplicationManager application = ApplicationLocator.getManager(); |
||
191 | 954 | jjdelcerro | application.messageDialog( |
192 | "Can't locate script '" + command + "'.", |
||
193 | "ScriptLaunch",
|
||
194 | JOptionPane.OK_OPTION
|
||
195 | ); |
||
196 | 463 | jjdelcerro | } |
197 | } |
||
198 | } |
||
199 | |||
200 | 844 | jjdelcerro | private void showScriptingComposer() { |
201 | ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
||
202 | WindowManager winmanager = ToolsSwingLocator.getWindowManager(); |
||
203 | |||
204 | DynObject preferences = getPlugin().getPluginProperties(); |
||
205 | Boolean composerUseHisWindowManager = (Boolean) preferences.getDynValue("ComposerUseHisWindowManager"); |
||
206 | ScriptingUIManager uiManager = ScriptingSwingLocator.getUIManager(); |
||
207 | if( composerUseHisWindowManager ) {
|
||
208 | winmanager = new DefaultWindowManager();
|
||
209 | uiManager.setWindowManager(winmanager); |
||
210 | } |
||
211 | JScriptingComposer composer = uimanager.createComposer(); |
||
212 | uiManager.showWindow( |
||
213 | composer.asJComponent(), |
||
214 | uimanager.getTranslation("Scripting_Composer")
|
||
215 | ); |
||
216 | } |
||
217 | |||
218 | 955 | jjdelcerro | private class StartupScriptingComposer implements Runnable { |
219 | 844 | jjdelcerro | |
220 | @Override
|
||
221 | 954 | jjdelcerro | public void run() { |
222 | 844 | jjdelcerro | SimpleTaskStatus status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Script composer startup");
|
223 | status.setIndeterminate(); |
||
224 | status.setAutoremove(true);
|
||
225 | status.add(); |
||
226 | 856 | jjdelcerro | final ApplicationManager application = ApplicationLocator.getManager();
|
227 | final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
||
228 | 844 | jjdelcerro | try {
|
229 | 856 | jjdelcerro | logger.info("Searching autorun scripts...");
|
230 | application.message( |
||
231 | i18nManager.getTranslation("_Searching_autorun_scripts_Xhorizontal_ellipsisX"),
|
||
232 | JOptionPane.INFORMATION_MESSAGE
|
||
233 | ); |
||
234 | 844 | jjdelcerro | ScriptingManager manager = ScriptingLocator.getManager(); |
235 | final List<ScriptingScript> autoruns = new ArrayList<>(); |
||
236 | |||
237 | Visitor visitor = new Visitor() {
|
||
238 | |||
239 | @Override
|
||
240 | public void visit(Object o) throws VisitCanceledException, BaseException { |
||
241 | ScriptingUnit unit = (ScriptingUnit) o; |
||
242 | if( unit instanceof ScriptingScript && SCRIPT_COMPOSER_AUTORUN.equalsIgnoreCase(unit.getId()) ) { |
||
243 | 856 | jjdelcerro | ScriptingScript autorun = (ScriptingScript) unit; |
244 | if( autorun.isEnabled() ) {
|
||
245 | autoruns.add(autorun); |
||
246 | } else {
|
||
247 | logger.info("Skip scautorun script '" + autorun.getFile().getAbsolutePath() + "'."); |
||
248 | } |
||
249 | 844 | jjdelcerro | } |
250 | } |
||
251 | }; |
||
252 | manager.getSystemFolder().accept(visitor); |
||
253 | 989 | jjdelcerro | manager.getUserFolder().accept(visitor, new Predicate<ScriptingUnit>() { |
254 | @Override
|
||
255 | public boolean test(ScriptingUnit unit) { |
||
256 | if( unit instanceof ScriptingFolder ) { |
||
257 | return !BooleanUtils.toBoolean(unit.getProperty(SKIP_AUTORUNS));
|
||
258 | } |
||
259 | return false; |
||
260 | } |
||
261 | }); |
||
262 | 844 | jjdelcerro | if( !autoruns.isEmpty() ) {
|
263 | status.setRangeOfValues(0, autoruns.size());
|
||
264 | int n=0; |
||
265 | for (ScriptingScript autorun : autoruns) {
|
||
266 | status.setCurValue(n++); |
||
267 | try {
|
||
268 | 856 | jjdelcerro | logger.info("running autorun script '" + autorun.getFile().getAbsolutePath() + "."); |
269 | application.message( |
||
270 | i18nManager.getTranslation( |
||
271 | "_Running_autorun_script_from_XnameX",
|
||
272 | new String[]{ |
||
273 | autorun.getFile().getParentFile().getName()} |
||
274 | ), |
||
275 | JOptionPane.INFORMATION_MESSAGE
|
||
276 | ); |
||
277 | } catch (Exception ex) { |
||
278 | // Ignore it
|
||
279 | } |
||
280 | try {
|
||
281 | 844 | jjdelcerro | autorun.run(); |
282 | 856 | jjdelcerro | } catch (Exception ex) { |
283 | logger.warn("Can't execute autorun from '" + autorun.getFile().getAbsolutePath() + "'.", ex); |
||
284 | 844 | jjdelcerro | } |
285 | } |
||
286 | } |
||
287 | } catch(Throwable th) { |
||
288 | |||
289 | } finally {
|
||
290 | 856 | jjdelcerro | logger.info("Running autorun scripts terminated.");
|
291 | application.message("", JOptionPane.INFORMATION_MESSAGE); |
||
292 | 844 | jjdelcerro | status.terminate(); |
293 | composer_initialized = true;
|
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | |||
298 | 799 | jjdelcerro | @Override
|
299 | 463 | jjdelcerro | public void initialize() { |
300 | IconThemeHelper.registerIcon("action", "tools-scripting-launcher", this); |
||
301 | IconThemeHelper.registerIcon("action", "tools-scripting-composer", this); |
||
302 | IconThemeHelper.registerIcon("action", "tools-scripting-console-jython", this); |
||
303 | 955 | jjdelcerro | this.message = new MessageWait(); |
304 | 844 | jjdelcerro | |
305 | 812 | jjdelcerro | initPaths(); |
306 | Thread th = new Thread(new Runnable() { |
||
307 | @Override
|
||
308 | public void run() { |
||
309 | preloadPythonEngine(); |
||
310 | } |
||
311 | }, "ScriptEnginesInitializer");
|
||
312 | th.start(); |
||
313 | try {
|
||
314 | th.join(3000); // force change to other thread |
||
315 | } catch (InterruptedException ex) { |
||
316 | // Ignore.
|
||
317 | } |
||
318 | 463 | jjdelcerro | } |
319 | 844 | jjdelcerro | |
320 | 812 | jjdelcerro | private void preloadPythonEngine() { |
321 | ScriptingManager manager = (ScriptingManager) ScriptingLocator.getManager(); |
||
322 | 844 | jjdelcerro | synchronized (manager) {
|
323 | 812 | jjdelcerro | String respath = "/scripting/langs/python/preload.py"; |
324 | InputStream res = this.getClass().getResourceAsStream(respath); |
||
325 | 844 | jjdelcerro | if( res != null ) { |
326 | 812 | jjdelcerro | logger.info("Scan for script engines");
|
327 | List<String> lines; |
||
328 | try {
|
||
329 | lines = IOUtils.readLines(res); |
||
330 | String code = StringUtils.join(lines, "\n"); |
||
331 | logger.info("Preload python script engine");
|
||
332 | Script script = manager.createScript( |
||
333 | 844 | jjdelcerro | "preload",
|
334 | code, |
||
335 | ScriptingManager.PYTHON_LANGUAGE_NAME |
||
336 | 812 | jjdelcerro | ); |
337 | logger.info("Preload python modules");
|
||
338 | script.invokeFunction("main", null); |
||
339 | 844 | jjdelcerro | |
340 | 812 | jjdelcerro | } catch (Exception ex) { |
341 | logger.warn("Can't run preload script for python.", ex);
|
||
342 | } |
||
343 | logger.info("Preload of script engines finished");
|
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | 463 | jjdelcerro | |
348 | 989 | jjdelcerro | private File getScriptsHomeFolder() { |
349 | PluginsManager pluginManager = PluginsLocator.getManager(); |
||
350 | File pluginHomeFolder = this.getPlugin().getPluginHomeFolder(); |
||
351 | |||
352 | File scriptsHomeFolder = null; |
||
353 | File f = FileUtils.getFile(pluginHomeFolder, "scripts-folder.txt"); |
||
354 | if( f.exists() ) {
|
||
355 | 471 | jjdelcerro | try {
|
356 | 989 | jjdelcerro | List<String> lines = FileUtils.readLines(f); |
357 | for( String line : lines ) { |
||
358 | line = line.trim(); |
||
359 | if( !line.startsWith("#") ) { |
||
360 | scriptsHomeFolder = new File(line); |
||
361 | if( !scriptsHomeFolder.isAbsolute() ) {
|
||
362 | scriptsHomeFolder = new File(pluginHomeFolder, line); |
||
363 | } |
||
364 | scriptsHomeFolder = new File( |
||
365 | FilenameUtils.normalizeNoEndSeparator( |
||
366 | scriptsHomeFolder.getAbsolutePath(), |
||
367 | true
|
||
368 | ) |
||
369 | ); |
||
370 | if( scriptsHomeFolder.exists() ) {
|
||
371 | return scriptsHomeFolder;
|
||
372 | } |
||
373 | } |
||
374 | } |
||
375 | 471 | jjdelcerro | } catch (IOException ex) { |
376 | } |
||
377 | 989 | jjdelcerro | } else {
|
378 | try {
|
||
379 | FileUtils.touch(f); |
||
380 | } catch (IOException ex) { |
||
381 | } |
||
382 | 471 | jjdelcerro | } |
383 | 1015 | jjdelcerro | String gvsigversion = pluginManager.getApplicationVersion().format("%M.%m.0") ; |
384 | 989 | jjdelcerro | scriptsHomeFolder = FileUtils.getFile(pluginHomeFolder, gvsigversion) ; |
385 | return scriptsHomeFolder;
|
||
386 | 471 | jjdelcerro | } |
387 | 989 | jjdelcerro | |
388 | 812 | jjdelcerro | private void initPaths() { |
389 | 463 | jjdelcerro | PluginsManager pluginManager = PluginsLocator.getManager(); |
390 | |||
391 | 989 | jjdelcerro | ScriptingManager manager = ScriptingLocator.getManager(); |
392 | manager.setHomeFolder(getScriptsHomeFolder()); |
||
393 | 986 | jjdelcerro | |
394 | 989 | jjdelcerro | ScriptingFolder folder = manager.createLink( |
395 | "Common",
|
||
396 | manager.getUserFolder(), |
||
397 | "../../scripts"
|
||
398 | 986 | jjdelcerro | ); |
399 | 989 | jjdelcerro | folder.setProperty(SKIP_AUTORUNS, BooleanUtils.toStringTrueFalse(true));
|
400 | folder.save(); |
||
401 | // manager.createLink(
|
||
402 | // "Previous version",
|
||
403 | // manager.getUserFolder(),
|
||
404 | // "../../../org.gvsig.scripting.app.extension/scripts"
|
||
405 | // );
|
||
406 | 542 | jjdelcerro | |
407 | 468 | jjdelcerro | List<File> pluginsFolders = new ArrayList<>(); |
408 | 844 | jjdelcerro | for( File f : pluginManager.getPluginsFolders() ) { |
409 | 465 | jjdelcerro | pluginsFolders.addAll(Arrays.asList(f.listFiles()));
|
410 | 463 | jjdelcerro | } |
411 | 465 | jjdelcerro | |
412 | 844 | jjdelcerro | for( File pluginFolder : pluginsFolders ) { |
413 | 465 | jjdelcerro | File scriptsFolder = new File(pluginFolder, "scripting/scripts"); |
414 | 844 | jjdelcerro | if( scriptsFolder.exists() ) {
|
415 | 465 | jjdelcerro | manager.registerSystemFolder(pluginFolder.getName(), scriptsFolder); |
416 | 463 | jjdelcerro | } |
417 | 465 | jjdelcerro | File libFolder = new File(pluginFolder, "scripting/lib"); |
418 | 844 | jjdelcerro | if( libFolder.exists() ) {
|
419 | 465 | jjdelcerro | manager.addLibFolder(libFolder); |
420 | 463 | jjdelcerro | } |
421 | } |
||
422 | 468 | jjdelcerro | manager.setPackagesFolder(pluginManager.getInstallFolder()); |
423 | 474 | jjdelcerro | |
424 | File localAddonRepositoryFolder = new File(manager.getRootUserFolder(), "addons"); |
||
425 | 844 | jjdelcerro | if( !localAddonRepositoryFolder.exists() ) {
|
426 | 474 | jjdelcerro | try {
|
427 | FileUtils.forceMkdir(localAddonRepositoryFolder); |
||
428 | } catch (IOException ex) { |
||
429 | 542 | jjdelcerro | logger.info("Can't create addons folder in '" + localAddonRepositoryFolder.getAbsolutePath() + "'.", ex); |
430 | 474 | jjdelcerro | } |
431 | 844 | jjdelcerro | } |
432 | 797 | jjdelcerro | File initAddonFile = new File(localAddonRepositoryFolder, "__init__.py"); |
433 | 844 | jjdelcerro | if( !initAddonFile.exists() ) {
|
434 | 797 | jjdelcerro | try {
|
435 | FileUtils.touch(initAddonFile); |
||
436 | } catch (IOException ex) { |
||
437 | logger.info("Can't create addons __init__ file in '" + localAddonRepositoryFolder.getAbsolutePath() + "'.", ex); |
||
438 | } |
||
439 | 474 | jjdelcerro | } |
440 | 812 | jjdelcerro | } |
441 | 844 | jjdelcerro | |
442 | 812 | jjdelcerro | @Override
|
443 | public void postInitialize() { |
||
444 | super.postInitialize();
|
||
445 | PluginsManager pluginManager = PluginsLocator.getManager(); |
||
446 | pluginManager.addStartupTask( |
||
447 | 844 | jjdelcerro | "ExecuteAutorunScripts",
|
448 | new ExecuteAutorunScriptsOnStartup(),
|
||
449 | true,
|
||
450 | 600
|
||
451 | 812 | jjdelcerro | ); |
452 | 844 | jjdelcerro | |
453 | 821 | jjdelcerro | Invocable initializer = new ScriptsInstallerInitializer();
|
454 | initializer.call(this.getPlugin().getPluginName());
|
||
455 | 463 | jjdelcerro | } |
456 | |||
457 | 478 | jjdelcerro | @Override
|
458 | 463 | jjdelcerro | public boolean isEnabled() { |
459 | return true; |
||
460 | } |
||
461 | |||
462 | 478 | jjdelcerro | @Override
|
463 | 463 | jjdelcerro | public boolean isVisible() { |
464 | return true; |
||
465 | } |
||
466 | 468 | jjdelcerro | |
467 | 954 | jjdelcerro | private class ExecuteAutorunScriptsOnStartup implements Runnable { |
468 | 463 | jjdelcerro | |
469 | 478 | jjdelcerro | @Override
|
470 | 465 | jjdelcerro | public void run() { |
471 | Thread th = new Thread(new ExecuteAutorunScripts()); |
||
472 | th.start(); |
||
473 | } |
||
474 | } |
||
475 | 468 | jjdelcerro | |
476 | 954 | jjdelcerro | private class ExecuteAutorunScripts implements Runnable { |
477 | 465 | jjdelcerro | |
478 | 478 | jjdelcerro | @Override
|
479 | 465 | jjdelcerro | public void run() { |
480 | 542 | jjdelcerro | final ApplicationManager application = ApplicationLocator.getManager();
|
481 | 954 | jjdelcerro | executingAutorunScripts = true;
|
482 | try {
|
||
483 | final ScriptingManager manager = ScriptingLocator.getManager();
|
||
484 | manager.loadEngines(); |
||
485 | 465 | jjdelcerro | |
486 | 954 | jjdelcerro | final PluginsManager pluginManager = PluginsLocator.getManager();
|
487 | final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
||
488 | 844 | jjdelcerro | |
489 | 954 | jjdelcerro | final List<ScriptingBaseScript> autoruns = new ArrayList<>(); |
490 | |||
491 | 542 | jjdelcerro | List<File> pluginsFolders = new ArrayList<>(); |
492 | 844 | jjdelcerro | for( File f : pluginManager.getPluginsFolders() ) { |
493 | 542 | jjdelcerro | pluginsFolders.addAll(Arrays.asList(f.listFiles()));
|
494 | } |
||
495 | 468 | jjdelcerro | |
496 | 542 | jjdelcerro | application.message( |
497 | 844 | jjdelcerro | i18nManager.getTranslation("_Searching_autorun_scripts_Xhorizontal_ellipsisX"),
|
498 | JOptionPane.INFORMATION_MESSAGE
|
||
499 | 542 | jjdelcerro | ); |
500 | 844 | jjdelcerro | for( File pluginFolder : pluginsFolders ) { |
501 | 822 | jjdelcerro | File autorun_file = new File(pluginFolder, "scripting/scripts/autorun.inf"); |
502 | 844 | jjdelcerro | if( autorun_file.exists() ) {
|
503 | 822 | jjdelcerro | ScriptingBaseScript autorun = manager.getScript(autorun_file); |
504 | 844 | jjdelcerro | if( autorun.isEnabled() ) {
|
505 | 822 | jjdelcerro | autoruns.add(autorun); |
506 | 630 | jjdelcerro | } else {
|
507 | logger.info("Skip autorun script '" + autorun_file.getAbsolutePath() + "'."); |
||
508 | 542 | jjdelcerro | } |
509 | 465 | jjdelcerro | } |
510 | } |
||
511 | |||
512 | 542 | jjdelcerro | SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
|
513 | @Override
|
||
514 | public FileVisitResult visitFile(Path path, BasicFileAttributes bfa) throws IOException { |
||
515 | File file = path.toFile();
|
||
516 | 844 | jjdelcerro | if( "autorun.inf".equalsIgnoreCase(file.getName()) ) { |
517 | if( file.exists() ) {
|
||
518 | 542 | jjdelcerro | ScriptingBaseScript autorun = manager.getScript(file); |
519 | 630 | jjdelcerro | if( autorun.isEnabled() ) {
|
520 | 822 | jjdelcerro | autoruns.add(autorun); |
521 | 630 | jjdelcerro | } else {
|
522 | logger.info("Skip autorun script '" + file.getAbsolutePath() + "'."); |
||
523 | 542 | jjdelcerro | } |
524 | 465 | jjdelcerro | } |
525 | } |
||
526 | 542 | jjdelcerro | return FileVisitResult.CONTINUE;
|
527 | 465 | jjdelcerro | } |
528 | 542 | jjdelcerro | }; |
529 | try {
|
||
530 | 630 | jjdelcerro | EnumSet<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); |
531 | Files.walkFileTree(Paths.get(manager.getRootUserFolder().toURI()), opts, Integer.MAX_VALUE, visitor);
|
||
532 | 542 | jjdelcerro | List<ScriptingFolder> folders = manager.getAlternativeUserFolders();
|
533 | 844 | jjdelcerro | for( ScriptingFolder folder : folders ) {
|
534 | 630 | jjdelcerro | Files.walkFileTree(Paths.get(folder.getFile().toURI()), opts, Integer.MAX_VALUE, visitor);
|
535 | 542 | jjdelcerro | } |
536 | } catch (Exception ex) { |
||
537 | logger.warn("Can't execute autoruns in home.", ex);
|
||
538 | 465 | jjdelcerro | } |
539 | 844 | jjdelcerro | |
540 | 822 | jjdelcerro | Collections.sort(autoruns, new Comparator<ScriptingBaseScript>() { |
541 | |||
542 | @Override
|
||
543 | public int compare(ScriptingBaseScript o1, ScriptingBaseScript o2) { |
||
544 | return getScriptOrderKey(o2).compareToIgnoreCase(getScriptOrderKey(o1));
|
||
545 | } |
||
546 | }); |
||
547 | 844 | jjdelcerro | |
548 | for( ScriptingBaseScript autorun : autoruns ) {
|
||
549 | 822 | jjdelcerro | try {
|
550 | 844 | jjdelcerro | logger.info("running autorun script '" + autorun.getFile().getAbsolutePath() + "' (" + getScriptOrderKey(autorun) + ", " + autorun.getIsolationGroup() + ")."); |
551 | 822 | jjdelcerro | application.message( |
552 | 844 | jjdelcerro | i18nManager.getTranslation( |
553 | "_Running_autorun_script_from_XnameX",
|
||
554 | new String[]{ |
||
555 | autorun.getFile().getParentFile().getName()} |
||
556 | ), |
||
557 | JOptionPane.INFORMATION_MESSAGE
|
||
558 | 822 | jjdelcerro | ); |
559 | } catch (Exception ex) { |
||
560 | // Ignore it
|
||
561 | } |
||
562 | try {
|
||
563 | autorun.run(); |
||
564 | } catch (Exception ex) { |
||
565 | logger.warn("Can't execute autorun from '" + autorun.getFile().getAbsolutePath() + "'.", ex); |
||
566 | 844 | jjdelcerro | } |
567 | 822 | jjdelcerro | } |
568 | 844 | jjdelcerro | |
569 | 542 | jjdelcerro | } finally {
|
570 | logger.info("Running autorun scripts terminated.");
|
||
571 | application.message("", JOptionPane.INFORMATION_MESSAGE); |
||
572 | 954 | jjdelcerro | executingAutorunScripts = false;
|
573 | 465 | jjdelcerro | } |
574 | } |
||
575 | |||
576 | 822 | jjdelcerro | private String getScriptOrderKey(ScriptingBaseScript o) { |
577 | int groupOrder = 500; |
||
578 | String groupName = "default"; |
||
579 | int scriptOrder = 500; |
||
580 | String s = o.getProperty("autorun.group.order"); |
||
581 | if( s != null ) { |
||
582 | try {
|
||
583 | groupOrder = Integer.parseInt(s);
|
||
584 | 844 | jjdelcerro | } catch (Exception ex) { |
585 | 822 | jjdelcerro | // Do nothing.
|
586 | } |
||
587 | } |
||
588 | s = o.getProperty("autorun.group.name");
|
||
589 | 844 | jjdelcerro | if( s != null ) { |
590 | 822 | jjdelcerro | groupName = s; |
591 | } |
||
592 | s = o.getProperty("autorun.order");
|
||
593 | if( s != null ) { |
||
594 | try {
|
||
595 | scriptOrder = Integer.parseInt(s);
|
||
596 | 844 | jjdelcerro | } catch (Exception ex) { |
597 | 822 | jjdelcerro | // Do nothing.
|
598 | } |
||
599 | 844 | jjdelcerro | } |
600 | String key = MessageFormat.format( |
||
601 | "{0,number,000000}.{1}.{2,number,000000}",
|
||
602 | groupOrder, |
||
603 | groupName, |
||
604 | scriptOrder |
||
605 | 822 | jjdelcerro | ); |
606 | return key;
|
||
607 | } |
||
608 | 465 | jjdelcerro | } |
609 | 462 | jjdelcerro | } |