/**
   * Bind an object to a context ensuring all subcontexts are created if necessary
   *
   * @param ctx the context into which to bind
   * @param name the name relative to context to bind
   * @param obj the object to be bound
   * @exception NamingException if an error occurs
   */
  public static Context bind(Context ctx, String nameStr, Object obj) throws NamingException {
    Name name = ctx.getNameParser("").parse(nameStr);

    // no name, nothing to do
    if (name.size() == 0) return null;

    Context subCtx = ctx;

    // last component of the name will be the name to bind
    for (int i = 0; i < name.size() - 1; i++) {
      try {
        subCtx = (Context) subCtx.lookup(name.get(i));
        if (Log.isDebugEnabled()) Log.debug("Subcontext " + name.get(i) + " already exists");
      } catch (NameNotFoundException e) {
        subCtx = subCtx.createSubcontext(name.get(i));
        if (Log.isDebugEnabled()) Log.debug("Subcontext " + name.get(i) + " created");
      }
    }

    subCtx.rebind(name.get(name.size() - 1), obj);
    if (Log.isDebugEnabled()) Log.debug("Bound object to " + name.get(name.size() - 1));
    return subCtx;
  }
  @Override
  public void bind(InetSocketAddress addr, int backlog) throws IOException {
    if (_started) throw new BindException("Already started");

    // check if there is already a connector listening
    Connector[] connectors = _server.getConnectors();
    if (connectors != null) throw new BindException("Server already bound");

    this._addr = addr;
    if (_executor != null && _server.getThreadPool() == null) {
      if (Log.isDebugEnabled()) Log.debug("using given Executor for server thread pool");
      _server.setThreadPool(new ThreadPoolExecutorAdapter(_executor));
    }

    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setAcceptors(1);
    connector.setAcceptQueueSize(backlog);
    connector.setPort(addr.getPort());
    connector.setHost(addr.getHostName());
    _server.addConnector(connector);
  }
示例#3
0
  /* ------------------------------------------------------------ */
  public static void extract(Resource resource, File directory, boolean deleteOnExit)
      throws IOException {
    if (Log.isDebugEnabled()) Log.debug("Extract " + resource + " to " + directory);

    String urlString = resource.getURL().toExternalForm().trim();
    int endOfJarUrl = urlString.indexOf("!/");
    int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);

    if (endOfJarUrl < 0) throw new IOException("Not a valid jar url: " + urlString);

    URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
    String subEntryName =
        (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
    boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);

    if (Log.isDebugEnabled())
      Log.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);

    InputStream is = jarFileURL.openConnection().getInputStream();
    JarInputStream jin = new JarInputStream(is);
    JarEntry entry = null;
    boolean shouldExtract = true;
    while ((entry = jin.getNextJarEntry()) != null) {
      String entryName = entry.getName();

      if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
        // if there is a particular subEntry that we are looking for, only
        // extract it.
        if (subEntryIsDir) {
          // if it is a subdirectory we are looking for, then we
          // are looking to extract its contents into the target
          // directory. Remove the name of the subdirectory so
          // that we don't wind up creating it too.
          entryName = entryName.substring(subEntryName.length());
          if (!entryName.equals("")) {
            // the entry is
            shouldExtract = true;
          } else shouldExtract = false;
        } else shouldExtract = true;
      } else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
        // there is a particular entry we are looking for, and this one
        // isn't it
        shouldExtract = false;
      } else {
        // we are extracting everything
        shouldExtract = true;
      }

      if (!shouldExtract) {
        if (Log.isDebugEnabled()) Log.debug("Skipping entry: " + entryName);
        continue;
      }

      File file = new File(directory, entryName);
      if (entry.isDirectory()) {
        // Make directory
        if (!file.exists()) file.mkdirs();
      } else {
        // make directory (some jars don't list dirs)
        File dir = new File(file.getParent());
        if (!dir.exists()) dir.mkdirs();

        // Make file
        FileOutputStream fout = null;
        try {
          fout = new FileOutputStream(file);
          IO.copy(jin, fout);
        } finally {
          IO.close(fout);
        }

        // touch the file.
        if (entry.getTime() >= 0) file.setLastModified(entry.getTime());
      }
      if (deleteOnExit) file.deleteOnExit();
    }
  }