Exemplo n.º 1
0
 /**
  * Search for a resource in a local path, or the main repository path.
  *
  * @param path the resource name
  * @param loaders optional list of module loaders
  * @param localRoot a repository to look first
  * @return the resource
  * @throws IOException if an I/O error occurred
  */
 public Resource findResource(String path, ModuleLoader[] loaders, Repository localRoot)
     throws IOException {
   // Note: as an extension to the CommonJS modules API
   // we allow absolute module paths for resources
   File file = new File(path);
   if (file.isAbsolute()) {
     Resource res;
     outer:
     if (loaders != null) {
       // loaders must contain at least one loader
       assert loaders.length > 0 && loaders[0] != null;
       for (ModuleLoader loader : loaders) {
         res = new FileResource(path + loader.getExtension());
         if (res.exists()) {
           break outer;
         }
       }
       res = new FileResource(path + loaders[0].getExtension());
     } else {
       res = new FileResource(file);
     }
     res.setAbsolute(true);
     return res;
   } else if (localRoot != null && (path.startsWith("./") || path.startsWith("../"))) {
     String newpath = localRoot.getRelativePath() + path;
     return findResource(newpath, loaders, null);
   } else {
     return config.getResource(normalizePath(path), loaders);
   }
 }
Exemplo n.º 2
0
 /**
  * Get the script arguments as object array suitable for use with Context.newArray().
  *
  * @return the script arguments
  */
 public Object[] getArguments() {
   String[] args = config.getArguments();
   if (args == null) {
     return ScriptRuntime.emptyArgs;
   } else {
     Object[] array = new Object[args.length];
     System.arraycopy(args, 0, array, 0, args.length);
     return array;
   }
 }
Exemplo n.º 3
0
 /**
  * Search for a repository in the local path, or the main repository path.
  *
  * @param path the repository name
  * @param localPath a repository to look first
  * @return the repository
  * @throws IOException if an I/O error occurred
  */
 public Repository findRepository(String path, Repository localPath) throws IOException {
   // To be consistent, always return absolute repository if path is absolute
   // if we make this dependent on whether files exist we introduce a lot of
   // vague and undetermined behaviour.
   File file = new File(path);
   if (file.isAbsolute()) {
     return new FileRepository(file);
   }
   if (localPath != null) {
     Repository repository = localPath.getChildRepository(path);
     if (repository != null && repository.exists()) {
       return repository;
     }
   }
   return config.getRepository(normalizePath(path));
 }
Exemplo n.º 4
0
 private void evaluateBootstrapScripts(Context cx) throws IOException {
   List<String> bootstrapScripts = config.getBootstrapScripts();
   if (bootstrapScripts != null) {
     for (String script : bootstrapScripts) {
       Resource resource = new FileResource(script);
       // not found, attempt to resolve the file relative to ringo home
       if (!resource.exists()) {
         resource = getRingoHome().getResource(script);
       }
       if (resource == null || !resource.exists()) {
         throw new FileNotFoundException("Bootstrap script " + script + " not found");
       }
       mainWorker.evaluateScript(cx, new ReloadableScript(resource, this), globalScope);
     }
   }
 }
Exemplo n.º 5
0
  /**
   * Create a RhinoEngine with the given configuration. If <code>globals</code> is not null, its
   * contents are added as properties on the global object.
   *
   * @param config the configuration used to initialize the engine.
   * @param globals an optional map of global properties
   * @throws Exception if the engine can't be created
   */
  public RhinoEngine(RingoConfig config, Map<String, Object> globals) throws Exception {
    this.config = config;
    workers = new LinkedBlockingDeque<RingoWorker>();
    currentWorker = new ThreadLocal<RingoWorker>();
    mainWorker = new RingoWorker(this);
    compiledScripts = new ConcurrentHashMap<Trackable, ReloadableScript>();
    interpretedScripts = new ConcurrentHashMap<Trackable, ReloadableScript>();
    singletons = new HashMap<Singleton, Singleton>();
    contextFactory = new RingoContextFactory(this, config);
    repositories = config.getRepositories();
    wrapFactory = config.getWrapFactory();

    loaders =
        new ModuleLoader[] {new JsModuleLoader(), new JsonModuleLoader(), new ClassModuleLoader()};

    RingoDebugger debugger = null;
    if (config.getDebug()) {
      debugger = new RingoDebugger(config);
      debugger.setScopeProvider(this);
      debugger.attachTo(contextFactory);
      debugger.setBreakOnExceptions(true);
    }

    // create and initialize global scope
    Context cx = contextFactory.enterContext();
    try {
      boolean sealed = config.isSealed();
      globalScope = new RingoGlobal(cx, this, sealed);
      Class<Scriptable>[] classes = config.getHostClasses();
      if (classes != null) {
        for (Class<Scriptable> clazz : classes) {
          defineHostClass(clazz);
        }
      }
      ScriptableList.init(globalScope);
      ScriptableMap.init(globalScope);
      ScriptableObject.defineClass(globalScope, ScriptableWrapper.class);
      ScriptableObject.defineClass(globalScope, ModuleObject.class);
      if (globals != null) {
        for (Map.Entry<String, Object> entry : globals.entrySet()) {
          ScriptableObject.defineProperty(
              globalScope, entry.getKey(), entry.getValue(), ScriptableObject.DONTENUM);
        }
      }
      mainWorker.evaluateScript(cx, getScript("globals"), globalScope);
      evaluateBootstrapScripts(cx);
      if (sealed) {
        globalScope.sealObject();
      }
      if (debugger != null) {
        debugger.setBreak();
      }
      try {
        Runtime.getRuntime()
            .addShutdownHook(
                new Thread() {
                  public void run() {
                    shutdown();
                  }
                });
      } catch (java.security.AccessControlException e) {
        log.log(Level.WARNING, "Could not register shutdown hook due to security exception", e);
      }
    } finally {
      Context.exit();
    }
  }
Exemplo n.º 6
0
 /**
  * Get a list of all child resources for the given path relative to our script repository.
  *
  * @param path the repository path
  * @param recursive whether to include nested resources
  * @return a list of all contained child resources
  */
 public List<Resource> findResources(String path, boolean recursive) throws IOException {
   return config.getResources(path, recursive);
 }
Exemplo n.º 7
0
 /**
  * Get the our installation directory.
  *
  * @return the RingoJS installation directory
  */
 public Repository getRingoHome() {
   return config.getRingoHome();
 }
Exemplo n.º 8
0
 protected boolean isPolicyEnabled() {
   // only use security when ringo runs standalone with default security manager,
   // not with google app engine
   return config.isPolicyEnabled();
 }
Exemplo n.º 9
0
 /**
  * Create a sandboxed scripting engine with the same install directory as this and the given
  * module paths, global properties, class shutter and sealing
  *
  * @param config the sandbox configuration
  * @param globals a map of predefined global properties, may be null
  * @return a sandboxed RhinoEngine instance
  * @throws FileNotFoundException if any part of the module paths does not exist
  */
 public RhinoEngine createSandbox(RingoConfig config, Map<String, Object> globals)
     throws Exception {
   config.setPolicyEnabled(this.config.isPolicyEnabled());
   return new RhinoEngine(config, globals);
 }
Exemplo n.º 10
0
 public String getCharset() {
   return config.getCharset();
 }
Exemplo n.º 11
0
 /**
  * Get the name of the main script as module name, if any
  *
  * @return the main module name, or null
  */
 public String getMainModule() {
   return config.getMainModule();
 }