Revision 822 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
ScriptingExtension.java | ||
---|---|---|
32 | 32 |
import java.nio.file.Paths; |
33 | 33 |
import java.nio.file.SimpleFileVisitor; |
34 | 34 |
import java.nio.file.attribute.BasicFileAttributes; |
35 |
import java.text.MessageFormat; |
|
35 | 36 |
import java.util.ArrayList; |
36 | 37 |
import java.util.Arrays; |
38 |
import java.util.Collections; |
|
39 |
import java.util.Comparator; |
|
37 | 40 |
import java.util.EnumSet; |
38 | 41 |
import java.util.List; |
39 | 42 |
|
... | ... | |
309 | 312 |
final ApplicationManager application = ApplicationLocator.getManager(); |
310 | 313 |
final I18nManager i18nManager = ToolsLocator.getI18nManager(); |
311 | 314 |
|
315 |
final List<ScriptingBaseScript> autoruns = new ArrayList<>(); |
|
316 |
|
|
312 | 317 |
try { |
313 | 318 |
List<File> pluginsFolders = new ArrayList<>(); |
314 | 319 |
for (File f : pluginManager.getPluginsFolders()) { |
315 | 320 |
pluginsFolders.addAll(Arrays.asList(f.listFiles())); |
316 | 321 |
} |
317 | 322 |
|
318 |
File autorun_file = null; |
|
319 |
ScriptingBaseScript autorun; |
|
320 | 323 |
application.message( |
321 | 324 |
i18nManager.getTranslation("_Searching_autorun_scripts_Xhorizontal_ellipsisX"), |
322 | 325 |
JOptionPane.INFORMATION_MESSAGE |
323 | 326 |
); |
324 | 327 |
for (File pluginFolder : pluginsFolders) { |
325 |
autorun_file = new File(pluginFolder, "scripting/scripts/autorun.inf"); |
|
328 |
File autorun_file = new File(pluginFolder, "scripting/scripts/autorun.inf");
|
|
326 | 329 |
if (autorun_file.exists()) { |
327 |
autorun = manager.getScript(autorun_file); |
|
330 |
ScriptingBaseScript autorun = manager.getScript(autorun_file);
|
|
328 | 331 |
if (autorun.isEnabled()) { |
329 |
logger.info("running autorun script '" + autorun_file.getAbsolutePath() + "'."); |
|
330 |
try { |
|
331 |
application.message( |
|
332 |
i18nManager.getTranslation( |
|
333 |
"_Running_autorun_script_from_XnameX", |
|
334 |
new String[]{autorun.getFile().getParentFile().getName()} |
|
335 |
), |
|
336 |
JOptionPane.INFORMATION_MESSAGE |
|
337 |
); |
|
338 |
} catch (Exception ex) { |
|
339 |
// Ignore it |
|
340 |
} |
|
341 |
try { |
|
342 |
autorun.run(); |
|
343 |
} catch (Exception ex) { |
|
344 |
logger.warn("Can't execute autorun from '" + autorun_file.getAbsolutePath() + "'.", ex); |
|
345 |
} |
|
346 |
|
|
332 |
autoruns.add(autorun); |
|
347 | 333 |
} else { |
348 | 334 |
logger.info("Skip autorun script '" + autorun_file.getAbsolutePath() + "'."); |
349 | 335 |
} |
... | ... | |
358 | 344 |
if (file.exists()) { |
359 | 345 |
ScriptingBaseScript autorun = manager.getScript(file); |
360 | 346 |
if( autorun.isEnabled() ) { |
361 |
logger.info("running autorun script '" + file.getAbsolutePath() + "'."); |
|
362 |
try { |
|
363 |
application.message( |
|
364 |
i18nManager.getTranslation( |
|
365 |
"_Running_autorun_script_from_XnameX", |
|
366 |
new String[]{autorun.getFile().getParentFile().getName()} |
|
367 |
), |
|
368 |
JOptionPane.INFORMATION_MESSAGE |
|
369 |
); |
|
370 |
} catch (Exception ex) { |
|
371 |
// Ignore |
|
372 |
} |
|
373 |
try { |
|
374 |
autorun.run(); |
|
375 |
} catch (Exception ex) { |
|
376 |
logger.warn("Can't execute autorun from '" + file.getAbsolutePath() + "'.", ex); |
|
377 |
} |
|
347 |
autoruns.add(autorun); |
|
378 | 348 |
} else { |
379 | 349 |
logger.info("Skip autorun script '" + file.getAbsolutePath() + "'."); |
380 | 350 |
} |
... | ... | |
393 | 363 |
} catch (Exception ex) { |
394 | 364 |
logger.warn("Can't execute autoruns in home.", ex); |
395 | 365 |
} |
366 |
|
|
367 |
Collections.sort(autoruns, new Comparator<ScriptingBaseScript>() { |
|
368 |
|
|
369 |
@Override |
|
370 |
public int compare(ScriptingBaseScript o1, ScriptingBaseScript o2) { |
|
371 |
return getScriptOrderKey(o2).compareToIgnoreCase(getScriptOrderKey(o1)); |
|
372 |
} |
|
373 |
}); |
|
374 |
|
|
375 |
for (ScriptingBaseScript autorun : autoruns) { |
|
376 |
try { |
|
377 |
logger.info("running autorun script '" + autorun.getFile().getAbsolutePath() + "' ("+getScriptOrderKey(autorun)+", "+autorun.getIsolationGroup()+")."); |
|
378 |
application.message( |
|
379 |
i18nManager.getTranslation( |
|
380 |
"_Running_autorun_script_from_XnameX", |
|
381 |
new String[]{autorun.getFile().getParentFile().getName()} |
|
382 |
), |
|
383 |
JOptionPane.INFORMATION_MESSAGE |
|
384 |
); |
|
385 |
} catch (Exception ex) { |
|
386 |
// Ignore it |
|
387 |
} |
|
388 |
try { |
|
389 |
autorun.run(); |
|
390 |
} catch (Exception ex) { |
|
391 |
logger.warn("Can't execute autorun from '" + autorun.getFile().getAbsolutePath() + "'.", ex); |
|
392 |
} |
|
393 |
} |
|
394 |
|
|
396 | 395 |
} finally { |
397 | 396 |
logger.info("Running autorun scripts terminated."); |
398 | 397 |
application.message("", JOptionPane.INFORMATION_MESSAGE); |
399 | 398 |
} |
400 | 399 |
} |
401 | 400 |
|
401 |
private String getScriptOrderKey(ScriptingBaseScript o) { |
|
402 |
int groupOrder = 500; |
|
403 |
String groupName = "default"; |
|
404 |
int scriptOrder = 500; |
|
405 |
String s = o.getProperty("autorun.group.order"); |
|
406 |
if( s != null ) { |
|
407 |
try { |
|
408 |
groupOrder = Integer.parseInt(s); |
|
409 |
} catch(Exception ex) { |
|
410 |
// Do nothing. |
|
411 |
} |
|
412 |
} |
|
413 |
s = o.getProperty("autorun.group.name"); |
|
414 |
if( s!=null ) { |
|
415 |
groupName = s; |
|
416 |
} |
|
417 |
s = o.getProperty("autorun.order"); |
|
418 |
if( s != null ) { |
|
419 |
try { |
|
420 |
scriptOrder = Integer.parseInt(s); |
|
421 |
} catch(Exception ex) { |
|
422 |
// Do nothing. |
|
423 |
} |
|
424 |
} |
|
425 |
String key=MessageFormat.format( |
|
426 |
"{0,number,000000}.{1}.{2,number,000000}", |
|
427 |
groupOrder, |
|
428 |
groupName, |
|
429 |
scriptOrder |
|
430 |
); |
|
431 |
return key; |
|
432 |
} |
|
402 | 433 |
} |
403 | 434 |
} |
Also available in: Unified diff