Example #1
0
  /**
   * Resolve the specified bundle (the bundle file must already exist in the appropriate place on
   * the file system) and return it on the specified result listener. Note that the result listener
   * may be notified before this method returns on the caller's thread if the bundle is already
   * resolved, or it may be notified on a brand new thread if the bundle requires unpacking.
   */
  public void resolveBundle(String path, final ResultListener<FileResourceBundle> listener) {
    File bfile = getResourceFile(path);
    if (bfile == null) {
      String errmsg = "ResourceManager not configured with resource directory.";
      listener.requestFailed(new IOException(errmsg));
      return;
    }

    final FileResourceBundle bundle = new FileResourceBundle(bfile, true, _unpack);
    if (bundle.isUnpacked()) {
      if (bundle.sourceIsReady()) {
        listener.requestCompleted(bundle);
      } else {
        String errmsg = "Bundle initialization failed.";
        listener.requestFailed(new IOException(errmsg));
      }
      return;
    }

    // start a thread to unpack our bundles
    ArrayList<ResourceBundle> list = Lists.newArrayList();
    list.add(bundle);
    Unpacker unpack =
        new Unpacker(
            list,
            new InitObserver() {
              public void progress(int percent, long remaining) {
                if (percent == 100) {
                  listener.requestCompleted(bundle);
                }
              }

              public void initializationFailed(Exception e) {
                listener.requestFailed(e);
              }
            });
    unpack.start();
  }
Example #2
0
  /**
   * Initializes the bundle sets to be made available by this resource manager. Applications that
   * wish to make use of resource bundles should call this method after constructing the resource
   * manager.
   *
   * @param resourceDir the base directory to which the paths in the supplied configuration file are
   *     relative. If this is null, the system property <code>resource_dir</code> will be used, if
   *     available.
   * @param configPath the path (relative to the resource dir) of the resource definition file.
   * @param initObs a bundle initialization observer to notify of unpacking progress and success or
   *     failure, or <code>null</code> if the caller doesn't care to be informed; note that in the
   *     latter case, the calling thread will block until bundle unpacking is complete.
   * @exception IOException thrown if we are unable to read our resource manager configuration.
   */
  public void initBundles(String resourceDir, String configPath, InitObserver initObs)
      throws IOException {
    // reinitialize our resource dir if it was specified
    if (resourceDir != null) {
      initResourceDir(resourceDir);
    }

    // load up our configuration
    Properties config = loadConfig(configPath);

    // resolve the configured resource sets
    List<ResourceBundle> dlist = Lists.newArrayList();
    Enumeration<?> names = config.propertyNames();
    while (names.hasMoreElements()) {
      String key = (String) names.nextElement();
      if (!key.startsWith(RESOURCE_SET_PREFIX)) {
        continue;
      }
      String setName = key.substring(RESOURCE_SET_PREFIX.length());
      String resourceSetType =
          config.getProperty(RESOURCE_SET_TYPE_PREFIX + setName, FILE_SET_TYPE);
      resolveResourceSet(setName, config.getProperty(key), resourceSetType, dlist);
    }

    // if an observer was passed in, then we do not need to block the caller
    final boolean[] shouldWait = new boolean[] {false};
    if (initObs == null) {
      // if there's no observer, we'll need to block the caller
      shouldWait[0] = true;
      initObs =
          new InitObserver() {
            public void progress(int percent, long remaining) {
              if (percent >= 100) {
                synchronized (this) {
                  // turn off shouldWait, in case we reached 100% progress before the
                  // calling thread even gets a chance to get to the blocking code, below
                  shouldWait[0] = false;
                  notify();
                }
              }
            }

            public void initializationFailed(Exception e) {
              synchronized (this) {
                shouldWait[0] = false;
                notify();
              }
            }
          };
    }

    // start a thread to unpack our bundles
    Unpacker unpack = new Unpacker(dlist, initObs);
    unpack.start();

    if (shouldWait[0]) {
      synchronized (initObs) {
        if (shouldWait[0]) {
          try {
            initObs.wait();
          } catch (InterruptedException ie) {
            log.warning("Interrupted while waiting for bundles to unpack.");
          }
        }
      }
    }
  }
Example #3
0
 public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
   return pac.unpackFloat();
 }