Ejemplo n.º 1
0
  /**
   * Tests that we can get a connection from the DataSource bound in JNDI during test setup
   *
   * @throws Exception if an error occurs
   */
  public void testDataSource() throws Exception {
    NameParser nameParser = this.ctx.getNameParser("");
    Name datasourceName = nameParser.parse("_test");
    Object obj = this.ctx.lookup(datasourceName);
    DataSource boundDs = null;

    if (obj instanceof DataSource) {
      boundDs = (DataSource) obj;
    } else if (obj instanceof Reference) {
      //
      // For some reason, this comes back as a Reference instance under CruiseControl !?
      //
      Reference objAsRef = (Reference) obj;
      ObjectFactory factory =
          (ObjectFactory) Class.forName(objAsRef.getFactoryClassName()).newInstance();
      boundDs =
          (DataSource)
              factory.getObjectInstance(
                  objAsRef, datasourceName, this.ctx, new Hashtable<Object, Object>());
    }

    assertTrue("Datasource not bound", boundDs != null);

    Connection con = boundDs.getConnection();
    con.close();
    assertTrue("Connection can not be obtained from data source", con != null);
  }
  /**
   * Crete a new Resource env instance.
   *
   * @param obj The reference object describing the DataSource
   */
  public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
      throws Exception {

    if (obj instanceof ResourceEnvRef) {
      Reference ref = (Reference) obj;
      ObjectFactory factory = null;
      RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
      if (factoryRefAddr != null) {
        // Using the specified factory
        String factoryClassName = factoryRefAddr.getContent().toString();
        // Loading factory
        ClassLoader tcl = Thread.currentThread().getContextClassLoader();
        Class factoryClass = null;
        if (tcl != null) {
          try {
            factoryClass = tcl.loadClass(factoryClassName);
          } catch (ClassNotFoundException e) {
            NamingException ex = new NamingException("Could not load resource factory class");
            ex.initCause(e);
            throw ex;
          }
        } else {
          try {
            factoryClass = Class.forName(factoryClassName);
          } catch (ClassNotFoundException e) {
            NamingException ex = new NamingException("Could not load resource factory class");
            ex.initCause(e);
            throw ex;
          }
        }
        if (factoryClass != null) {
          try {
            factory = (ObjectFactory) factoryClass.newInstance();
          } catch (Throwable t) {
            if (t instanceof NamingException) throw (NamingException) t;
            NamingException ex = new NamingException("Could not create resource factory instance");
            ex.initCause(t);
            throw ex;
          }
        }
      }
      // Note: No defaults here
      if (factory != null) {
        return factory.getObjectInstance(obj, name, nameCtx, environment);
      } else {
        throw new NamingException("Cannot create resource instance");
      }
    }

    return null;
  }
 private Object getObjectInstance(
     final Reference reference, final Name name, final Hashtable<?, ?> environment)
     throws NamingException {
   try {
     final Class<?> factoryClass =
         Thread.currentThread().getContextClassLoader().loadClass(reference.getFactoryClassName());
     ObjectFactory factory = ObjectFactory.class.cast(factoryClass.newInstance());
     return factory.getObjectInstance(reference, name, this, environment);
   } catch (NamingException e) {
     throw e;
   } catch (Throwable t) {
     throw namingException("failed to get object instance from reference", t);
   }
 }
Ejemplo n.º 4
0
  @Override
  public Object getObjectInstance(
      Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    // What kind of resource should we create?
    JndiResourceDescriptor resource = (JndiResourceDescriptor) obj;

    // Get class loader, we'll need it to load the factory class
    Class<?> factoryClass = null;
    ObjectFactory factory = null;

    try {
      factoryClass = clResourceClasses.loadClass(resource.getFactoryClassName());
    } catch (ClassNotFoundException e) {
      NamingException ex =
          new NamingException("Could not find resource or resource factory class in the classpath");
      ex.initCause(e);
      throw ex;
    } catch (Exception e) {
      NamingException ex =
          new NamingException(
              "Could not load resource or resource factory class for an unknown reason");
      ex.initCause(e);
      throw ex;
    }

    try {
      factory = (ObjectFactory) factoryClass.newInstance();
    } catch (Exception e) {
      NamingException ex = new NamingException("Could not create resource factory instance");
      ex.initCause(e);
      throw ex;
    }

    Object result = null;
    try {
      result = factory.getObjectInstance(obj, name, nameCtx, environment);
    } catch (Exception e) {
      NamingException ex =
          new NamingException(
              "Could not create object resource from resource factory. JNDI definition & parameters may be incorrect.");
      ex.initCause(e);
      throw ex;
    }
    return result;
  }
Ejemplo n.º 5
0
 /**
  * Create an object instance.
  *
  * @param ref Object containing reference information
  * @param name The name relative to nameCtx
  * @param nameCtx The naming context
  * @param environment The environment information
  * @return The object
  * @throws Exception If any error occur
  */
 public Object getObjectInstance(
     final Object ref, final Name name, final Context nameCtx, final Hashtable<?, ?> environment)
     throws Exception {
   final ClassLoader classLoader = SecurityActions.getContextClassLoader();
   if (classLoader == null) {
     return ref;
   }
   final String factoriesProp = (String) environment.get(Context.OBJECT_FACTORIES);
   if (factoriesProp != null) {
     final String[] classes = factoriesProp.split(":");
     for (String className : classes) {
       try {
         final Class<?> factoryClass = classLoader.loadClass(className);
         final ObjectFactory objectFactory = ObjectFactory.class.cast(factoryClass.newInstance());
         final Object result = objectFactory.getObjectInstance(ref, name, nameCtx, environment);
         if (result != null) {
           return result;
         }
       } catch (Throwable ignored) {
       }
     }
   }
   return ref;
 }