/** JNDI object factory so the proxy can be used as a resource. */
  public Object getObjectInstance(
      Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Reference ref = (Reference) obj;

    String api = null;
    String url = null;
    String user = null;
    String password = null;

    for (int i = 0; i < ref.size(); i++) {
      RefAddr addr = ref.get(i);

      String type = addr.getType();
      String value = (String) addr.getContent();

      if (type.equals("type")) api = value;
      else if (type.equals("url")) url = value;
      else if (type.equals("user")) setUser(value);
      else if (type.equals("password")) setPassword(value);
    }

    if (url == null) throw new NamingException("`url' must be configured for HessianProxyFactory.");
    // XXX: could use meta protocol to grab this
    if (api == null)
      throw new NamingException("`type' must be configured for HessianProxyFactory.");

    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Class apiClass = Class.forName(api, false, loader);

    return create(apiClass, url);
  }
  /** Returns the URLs contained within an RMI registry reference. */
  private static String[] getURLs(Reference ref) throws NamingException {

    int size = 0; // number of URLs
    String[] urls = new String[ref.size()];

    Enumeration<RefAddr> addrs = ref.getAll();
    while (addrs.hasMoreElements()) {
      RefAddr addr = addrs.nextElement();

      if ((addr instanceof StringRefAddr) && addr.getType().equals(ADDRESS_TYPE)) {

        urls[size++] = (String) addr.getContent();
      }
    }
    if (size == 0) {
      throw (new ConfigurationException("Reference contains no valid addresses"));
    }

    // Trim URL array down to size.
    if (size == ref.size()) {
      return urls;
    }
    String[] urls2 = new String[size];
    System.arraycopy(urls, 0, urls2, 0, size);
    return urls2;
  }
Example #3
0
  /**
   * Gets an address where the address type matches the specified string. There may be more than one
   * entry in the list with the same address type but this method returns the first one found in the
   * list.
   *
   * @param type the address type to look for
   * @return the first address whose type matches the string
   */
  public RefAddr get(String type) {
    Enumeration<RefAddr> elements = addrs.elements();
    RefAddr refAddr = null;

    while (elements.hasMoreElements()) {
      refAddr = elements.nextElement();
      if (type.equals(refAddr.getType())) {
        return refAddr;
      }
    }
    return null;
  }
Example #4
0
  /*
   * Ref has no factory.  For each address of type "URL", try its URL
   * context factory.  Returns null if unsuccessful in creating and
   * invoking a factory.
   */
  static Object processURLAddrs(
      Reference ref, Name name, Context nameCtx, Hashtable<?, ?> environment)
      throws NamingException {

    for (int i = 0; i < ref.size(); i++) {
      RefAddr addr = ref.get(i);
      if (addr instanceof StringRefAddr && addr.getType().equalsIgnoreCase("URL")) {

        String url = (String) addr.getContent();
        Object answer = processURL(url, name, nameCtx, environment);
        if (answer != null) {
          return answer;
        }
      }
    }
    return null;
  }
  /**
   * Create and return a new <code>BasicDataSource</code> instance. If no instance can be created,
   * return <code>null</code> instead.
   *
   * @param obj The possibly null object containing location or reference information that can be
   *     used in creating an object
   * @param name The name of this object relative to <code>nameCtx</code>
   * @param nameCtx The context relative to which the <code>name</code> parameter is specified, or
   *     <code>null</code> if <code>name</code> is relative to the default initial context
   * @param environment The possibly null environment that is used in creating this object
   * @exception Exception if an exception occurs creating the instance
   */
  @Override
  public Object getObjectInstance(
      Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {

    // We only know how to deal with <code>javax.naming.Reference</code>s
    // that specify a class name of "javax.sql.DataSource"
    if ((obj == null) || !(obj instanceof Reference)) {
      return null;
    }
    Reference ref = (Reference) obj;
    boolean XA = false;
    boolean ok = false;
    if ("javax.sql.DataSource".equals(ref.getClassName())) {
      ok = true;
    }
    if ("javax.sql.XADataSource".equals(ref.getClassName())) {
      ok = true;
      XA = true;
    }
    if (org.apache.tomcat.jdbc.pool.DataSource.class.getName().equals(ref.getClassName())) {
      ok = true;
    }

    if (!ok) {
      log.warn(ref.getClassName() + " is not a valid class name/type for this JNDI factory.");
      return null;
    }

    Properties properties = new Properties();
    for (int i = 0; i < ALL_PROPERTIES.length; i++) {
      String propertyName = ALL_PROPERTIES[i];
      RefAddr ra = ref.get(propertyName);
      if (ra != null) {
        String propertyValue = ra.getContent().toString();
        properties.setProperty(propertyName, propertyValue);
      }
    }

    return createDataSource(properties, nameCtx, XA);
  }