Search in sources :

Example 1 with CyAppAdapter

use of org.cytoscape.app.CyAppAdapter in project cytoscape-impl by cytoscape.

the class ExecuteScriptTask method run.

@Override
public void run(TaskMonitor taskMonitor) throws Exception {
    final String selected = engineNames.getSelectedValue();
    // Special case: CyCommand
    if (selected == CYCOMMAND_TITLE) {
        executeCyCommandFile();
        return;
    }
    final ScriptEngineFactory engineFactory = name2engineMap.get(engineNames.getSelectedValue());
    final ScriptEngine engine = engineFactory.getScriptEngine();
    // This object should be injected to all scripts to access manager objects from scripts.
    engine.put("cyAppAdapter", serviceRegistrar.getService(CyAppAdapter.class));
    // Execute
    FileReader reader = null;
    try {
        reader = new FileReader(file);
        engine.eval(reader);
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }
}
Also used : CyAppAdapter(org.cytoscape.app.CyAppAdapter) ScriptEngineFactory(javax.script.ScriptEngineFactory) FileReader(java.io.FileReader) ScriptEngine(javax.script.ScriptEngine)

Example 2 with CyAppAdapter

use of org.cytoscape.app.CyAppAdapter in project cytoscape-impl by cytoscape.

the class ExecuteScriptCommandTask method run.

@Override
public void run(TaskMonitor taskMonitor) throws Exception {
    final ScriptEngine engine = manager.getEngineByName(engineName);
    // This object should be injected to all scripts to access manager objects from scripts.
    engine.put("cyAppAdapter", serviceRegistrar.getService(CyAppAdapter.class));
    final String[] argArray = new String[args.size()];
    for (int i = 0; i < args.size(); i++) {
        System.out.println("* ARG = " + args.get(i));
        argArray[i] = args.get(i);
    }
    engine.put("args", argArray);
    // Execute
    FileReader reader = null;
    try {
        reader = new FileReader(new File(filename));
        engine.eval(reader);
    } catch (FileNotFoundException e) {
        throw new IOException("Could not open the file.", e);
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }
}
Also used : CyAppAdapter(org.cytoscape.app.CyAppAdapter) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) ScriptEngine(javax.script.ScriptEngine)

Example 3 with CyAppAdapter

use of org.cytoscape.app.CyAppAdapter in project cytoscape-impl by cytoscape.

the class SimpleApp method load.

@Override
public void load(AppManager appManager) throws AppLoadingException {
    if (appConstructor != null)
        return;
    // Make a copy used to create app instance
    LinkedList<String> uniqueNameDirectory = new LinkedList<String>();
    uniqueNameDirectory.add(appManager.getTemporaryInstallPath());
    try {
        if (appTemporaryInstallFile == null) {
            File targetFile = new File(appManager.getTemporaryInstallPath() + File.separator + suggestFileName(uniqueNameDirectory, this.getAppFile().getName()));
            FileUtils.copyFile(this.getAppFile(), targetFile);
            appTemporaryInstallFile = targetFile;
        }
    } catch (IOException e) {
        throw new AppLoadingException("Unable to make copy of app jar for instancing", e);
    }
    File installFile = appTemporaryInstallFile;
    if (installFile == null) {
        throw new AppLoadingException("No copy of app jar for instancing was found");
    }
    URL appURL = null;
    try {
        appURL = installFile.toURI().toURL();
    } catch (MalformedURLException e) {
        throw new AppLoadingException("Unable to obtain URL for file: " + installFile, e);
    }
    // TODO: Currently uses the CyAppAdapter's loader to load apps' classes. Is there reason to use a different one?
    ClassLoader appClassLoader = new URLClassLoader(new URL[] { appURL }, appManager.getSwingAppAdapter().getClass().getClassLoader());
    // Attempt to load the class
    try {
        appEntryClass = appClassLoader.loadClass(this.getEntryClassName());
    } catch (ClassNotFoundException e) {
        throw new AppLoadingException("Class " + this.getEntryClassName() + " not found in URL: " + appURL, e);
    }
    // Attempt to obtain the constructor
    try {
        try {
            appConstructor = appEntryClass.getConstructor(CyAppAdapter.class);
        } catch (SecurityException e) {
            throw new AppLoadingException("Access to the constructor for " + appEntryClass + " denied.", e);
        } catch (NoSuchMethodException e) {
            throw new AppLoadingException("Unable to find a constructor for " + appEntryClass + " that takes a CyAppAdapter as its argument.", e);
        }
    } catch (AppLoadingException e) {
        try {
            appConstructor = appEntryClass.getConstructor(CySwingAppAdapter.class);
        } catch (SecurityException e2) {
            throw new AppLoadingException("Access to the constructor for " + appEntryClass + " taking a CySwingAppAdapter as its argument denied.", e2);
        } catch (NoSuchMethodException e2) {
            throw new AppLoadingException("Unable to find an accessible constructor that takes either" + " a CyAppAdapter or a CySwingAppAdapter as its argument.", e2);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) AppLoadingException(org.cytoscape.app.internal.exception.AppLoadingException) LinkedList(java.util.LinkedList) URL(java.net.URL) CyAppAdapter(org.cytoscape.app.CyAppAdapter) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Aggregations

CyAppAdapter (org.cytoscape.app.CyAppAdapter)3 File (java.io.File)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 ScriptEngine (javax.script.ScriptEngine)2 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 LinkedList (java.util.LinkedList)1 ScriptEngineFactory (javax.script.ScriptEngineFactory)1 AppLoadingException (org.cytoscape.app.internal.exception.AppLoadingException)1