/** * Loads the JavaScript from file. * * @param sourceFile the source file * @return the JavaScript object loaded from file * @throws RuntimeException when failed to load a script */ public static JavaScript fromFile(File sourceFile) { String sourceCode; try { sourceCode = inputStreamToString(new FileInputStream(sourceFile)); } catch (FileNotFoundException e) { throw new IllegalStateException("Unable to find JavaScript source file '" + sourceFile + "'", e); } catch (IOException e) { throw new RuntimeException("Unable to load JavaScript from file '" + sourceFile + "'", e); } return fromString(sourceCode); }
/** * Loads the JavaScript from file. * * @param sourceFile the source file * @return the JavaScript object loaded from file * @throws RuntimeException when failed to load a script */ public static JavaScript fromFile(File sourceFile) { String sourceCode; try { sourceCode = inputStreamToString(new FileInputStream(sourceFile)); } catch (FileNotFoundException e) { throw new IllegalStateException("Unable to find JavaScript source file '" + sourceFile + "'", e); } catch (IOException e) { throw new RuntimeException("Unable to load JavaScript from file '" + sourceFile + "'", e); } return fromString(sourceCode); }
/** * Loads the JavaScript from classpath resource. * * @param resourceName the resource name, e.g. "org/jboss/test/..." * @return the JavaScript object loaded from classpath resource * @throws RuntimeException when failed to load a script */ public static JavaScript fromResource(String resourceName) { InputStream inputStream = JavaScript.class.getResourceAsStream("/" + resourceName); if (inputStream == null) { throw new IllegalStateException("Can't open the '" + resourceName + "' resource."); } String sourceCode; try { sourceCode = inputStreamToString(inputStream); } catch (IOException e) { throw new RuntimeException("Unable to load JavaScript from resource with name '" + resourceName + "'", e); } return fromString(sourceCode); }
/** * Loads the JavaScript from classpath resource. * * @param resourceName the resource name, e.g. "org/jboss/test/..." * @return the JavaScript object loaded from classpath resource * @throws RuntimeException when failed to load a script */ public static JavaScript fromResource(String resourceName) { InputStream inputStream = JavaScript.class.getResourceAsStream("/" + resourceName); if (inputStream == null) { throw new IllegalStateException("Can't open the '" + resourceName + "' resource."); } String sourceCode; try { sourceCode = inputStreamToString(inputStream); } catch (IOException e) { throw new RuntimeException("Unable to load JavaScript from resource with name '" + resourceName + "'", e); } return fromString(sourceCode); }