Search in sources :

Example 21 with RefAddr

use of javax.naming.RefAddr in project tomcat by apache.

the class DriverAdapterCPDS method getObjectInstance.

// ----------------------------------------------------------------------
// ObjectFactory implementation
/**
     * implements ObjectFactory to create an instance of this class
     */
@Override
public Object getObjectInstance(final Object refObj, final Name name, final Context context, final Hashtable<?, ?> env) throws Exception {
    // The spec says to return null if we can't create an instance
    // of the reference
    DriverAdapterCPDS cpds = null;
    if (refObj instanceof Reference) {
        final Reference ref = (Reference) refObj;
        if (ref.getClassName().equals(getClass().getName())) {
            RefAddr ra = ref.get("description");
            if (ra != null && ra.getContent() != null) {
                setDescription(ra.getContent().toString());
            }
            ra = ref.get("driver");
            if (ra != null && ra.getContent() != null) {
                setDriver(ra.getContent().toString());
            }
            ra = ref.get("url");
            if (ra != null && ra.getContent() != null) {
                setUrl(ra.getContent().toString());
            }
            ra = ref.get("user");
            if (ra != null && ra.getContent() != null) {
                setUser(ra.getContent().toString());
            }
            ra = ref.get("password");
            if (ra != null && ra.getContent() != null) {
                setPassword(ra.getContent().toString());
            }
            ra = ref.get("poolPreparedStatements");
            if (ra != null && ra.getContent() != null) {
                setPoolPreparedStatements(Boolean.valueOf(ra.getContent().toString()).booleanValue());
            }
            ra = ref.get("maxIdle");
            if (ra != null && ra.getContent() != null) {
                setMaxIdle(Integer.parseInt(ra.getContent().toString()));
            }
            ra = ref.get("timeBetweenEvictionRunsMillis");
            if (ra != null && ra.getContent() != null) {
                setTimeBetweenEvictionRunsMillis(Integer.parseInt(ra.getContent().toString()));
            }
            ra = ref.get("numTestsPerEvictionRun");
            if (ra != null && ra.getContent() != null) {
                setNumTestsPerEvictionRun(Integer.parseInt(ra.getContent().toString()));
            }
            ra = ref.get("minEvictableIdleTimeMillis");
            if (ra != null && ra.getContent() != null) {
                setMinEvictableIdleTimeMillis(Integer.parseInt(ra.getContent().toString()));
            }
            ra = ref.get("maxPreparedStatements");
            if (ra != null && ra.getContent() != null) {
                setMaxPreparedStatements(Integer.parseInt(ra.getContent().toString()));
            }
            ra = ref.get("accessToUnderlyingConnectionAllowed");
            if (ra != null && ra.getContent() != null) {
                setAccessToUnderlyingConnectionAllowed(Boolean.valueOf(ra.getContent().toString()).booleanValue());
            }
            cpds = this;
        }
    }
    return cpds;
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference)

Example 22 with RefAddr

use of javax.naming.RefAddr in project tomcat by apache.

the class DataSourceLinkFactory method getObjectInstance.

// ------------------------------------------------- ObjectFactory Methods
/**
     * Create a new DataSource instance.
     *
     * @param obj The reference object describing the DataSource
     */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException {
    Object result = super.getObjectInstance(obj, name, nameCtx, environment);
    // Can we process this request?
    if (result != null) {
        Reference ref = (Reference) obj;
        RefAddr userAttr = ref.get("username");
        RefAddr passAttr = ref.get("password");
        if (userAttr.getContent() != null && passAttr.getContent() != null) {
            result = wrapDataSource(result, userAttr.getContent().toString(), passAttr.getContent().toString());
        }
    }
    return result;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference)

Example 23 with RefAddr

use of javax.naming.RefAddr in project tomcat by apache.

the class FactoryBase method getObjectInstance.

/**
     * Creates a new object instance.
     *
     * @param obj The reference object describing the object to create
     */
@Override
public final Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if (isReferenceTypeSupported(obj)) {
        Reference ref = (Reference) obj;
        Object linked = getLinked(ref);
        if (linked != null) {
            return linked;
        }
        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;
            try {
                if (tcl != null) {
                    factoryClass = tcl.loadClass(factoryClassName);
                } else {
                    factoryClass = Class.forName(factoryClassName);
                }
            } catch (ClassNotFoundException e) {
                NamingException ex = new NamingException("Could not load resource factory class");
                ex.initCause(e);
                throw ex;
            }
            try {
                factory = (ObjectFactory) factoryClass.newInstance();
            } catch (Throwable t) {
                if (t instanceof NamingException) {
                    throw (NamingException) t;
                }
                if (t instanceof ThreadDeath) {
                    throw (ThreadDeath) t;
                }
                if (t instanceof VirtualMachineError) {
                    throw (VirtualMachineError) t;
                }
                NamingException ex = new NamingException("Could not create resource factory instance");
                ex.initCause(t);
                throw ex;
            }
        } else {
            // Check for a default factory
            factory = getDefaultFactory(ref);
        }
        if (factory != null) {
            return factory.getObjectInstance(obj, name, nameCtx, environment);
        } else {
            throw new NamingException("Cannot create resource instance");
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException)

Example 24 with RefAddr

use of javax.naming.RefAddr in project tomcat by apache.

the class MemoryUserDatabaseFactory method getObjectInstance.

// --------------------------------------------------------- Public Methods
/**
     * <p>Create and return a new <code>MemoryUserDatabase</code> instance
     * that has been configured according to the properties of the
     * specified <code>Reference</code>.  If you instance can be created,
     * return <code>null</code> instead.</p>
     *
     * @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
     */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    // that specify a class name of "org.apache.catalina.UserDatabase"
    if ((obj == null) || !(obj instanceof Reference)) {
        return (null);
    }
    Reference ref = (Reference) obj;
    if (!"org.apache.catalina.UserDatabase".equals(ref.getClassName())) {
        return (null);
    }
    // Create and configure a MemoryUserDatabase instance based on the
    // RefAddr values associated with this Reference
    MemoryUserDatabase database = new MemoryUserDatabase(name.toString());
    RefAddr ra = null;
    ra = ref.get("pathname");
    if (ra != null) {
        database.setPathname(ra.getContent().toString());
    }
    ra = ref.get("readonly");
    if (ra != null) {
        database.setReadonly(Boolean.parseBoolean(ra.getContent().toString()));
    }
    // Return the configured database instance
    database.open();
    // Don't try something we know won't work
    if (!database.getReadonly())
        database.save();
    return (database);
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference)

Example 25 with RefAddr

use of javax.naming.RefAddr in project jetty.project by eclipse.

the class TestNamingEntries method testResourceReference.

@Test
public void testResourceReference() throws Exception {
    RefAddr refAddr = new StringRefAddr("val", "10");
    Reference ref = new Reference(SomeObject.class.getName(), refAddr, SomeObjectFactory.class.getName(), null);
    InitialContext icontext = new InitialContext();
    Resource resource = new Resource(null, "resourceByRef", ref);
    NamingEntry ne = NamingEntryUtil.lookupNamingEntry(null, "resourceByRef");
    assertNotNull(ne);
    assertTrue(ne instanceof Resource);
    Object o = icontext.lookup("resourceByRef");
    assertNotNull(o);
    assertTrue(o instanceof SomeObject);
    assertEquals(((SomeObject) o).getValue(), 10);
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Aggregations

RefAddr (javax.naming.RefAddr)33 Reference (javax.naming.Reference)22 Properties (java.util.Properties)8 NamingException (javax.naming.NamingException)6 StringRefAddr (javax.naming.StringRefAddr)6 InitialContext (javax.naming.InitialContext)4 ObjectFactory (javax.naming.spi.ObjectFactory)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 NameNotFoundException (javax.naming.NameNotFoundException)2 DirObjectFactory (javax.naming.spi.DirObjectFactory)2 DruidDataSourceFactory (com.alibaba.druid.pool.DruidDataSourceFactory)1 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1 MongoException (com.mongodb.MongoException)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1