Search in sources :

Example 81 with RefAddr

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

the class DataSourceUserDatabaseFactory method getObjectInstance.

// --------------------------------------------------------- Public Methods
/**
 * <p>Create and return a new <code>DataSourceUserDatabase</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;
    }
    DataSource dataSource = null;
    String dataSourceName = null;
    RefAddr ra = null;
    ra = ref.get("dataSourceName");
    if (ra != null) {
        dataSourceName = ra.getContent().toString();
        dataSource = (DataSource) nameCtx.lookup(dataSourceName);
    }
    // Create and configure a DataSourceUserDatabase instance based on the
    // RefAddr values associated with this Reference
    DataSourceUserDatabase database = new DataSourceUserDatabase(dataSource, name.toString());
    database.setDataSourceName(dataSourceName);
    ra = ref.get("readonly");
    if (ra != null) {
        database.setReadonly(Boolean.parseBoolean(ra.getContent().toString()));
    }
    ra = ref.get("userTable");
    if (ra != null) {
        database.setUserTable(ra.getContent().toString());
    }
    ra = ref.get("groupTable");
    if (ra != null) {
        database.setGroupTable(ra.getContent().toString());
    }
    ra = ref.get("roleTable");
    if (ra != null) {
        database.setRoleTable(ra.getContent().toString());
    }
    ra = ref.get("userRoleTable");
    if (ra != null) {
        database.setUserRoleTable(ra.getContent().toString());
    }
    ra = ref.get("userGroupTable");
    if (ra != null) {
        database.setUserGroupTable(ra.getContent().toString());
    }
    ra = ref.get("groupRoleTable");
    if (ra != null) {
        database.setGroupRoleTable(ra.getContent().toString());
    }
    ra = ref.get("roleNameCol");
    if (ra != null) {
        database.setRoleNameCol(ra.getContent().toString());
    }
    ra = ref.get("roleAndGroupDescriptionCol");
    if (ra != null) {
        database.setRoleAndGroupDescriptionCol(ra.getContent().toString());
    }
    ra = ref.get("groupNameCol");
    if (ra != null) {
        database.setGroupNameCol(ra.getContent().toString());
    }
    ra = ref.get("userCredCol");
    if (ra != null) {
        database.setUserCredCol(ra.getContent().toString());
    }
    ra = ref.get("userFullNameCol");
    if (ra != null) {
        database.setUserFullNameCol(ra.getContent().toString());
    }
    ra = ref.get("userNameCol");
    if (ra != null) {
        database.setUserNameCol(ra.getContent().toString());
    }
    // Return the configured database instance
    database.open();
    return database;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) DataSource(javax.sql.DataSource)

Example 82 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()));
    }
    ra = ref.get("watchSource");
    if (ra != null) {
        database.setWatchSource(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 83 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 84 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(sm.getString("factoryBase.factoryClassError"));
                ex.initCause(e);
                throw ex;
            }
            try {
                factory = (ObjectFactory) factoryClass.getConstructor().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(sm.getString("factoryBase.factoryCreationError"));
                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(sm.getString("factoryBase.instanceCreationError"));
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException)

Example 85 with RefAddr

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

the class LookupFactory method getObjectInstance.

/**
 * Create a new Resource env instance.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    String lookupName = null;
    Object result = null;
    if (obj instanceof LookupRef) {
        Reference ref = (Reference) obj;
        ObjectFactory factory = null;
        RefAddr lookupNameRefAddr = ref.get(LookupRef.LOOKUP_NAME);
        if (lookupNameRefAddr != null) {
            lookupName = lookupNameRefAddr.getContent().toString();
        }
        try {
            if (lookupName != null) {
                if (!names.get().add(lookupName)) {
                    String msg = sm.getString("lookupFactory.circularReference", lookupName);
                    NamingException ne = new NamingException(msg);
                    log.warn(msg, ne);
                    throw ne;
                }
            }
            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(sm.getString("lookupFactory.loadFailed"));
                        ex.initCause(e);
                        throw ex;
                    }
                } else {
                    try {
                        factoryClass = Class.forName(factoryClassName);
                    } catch (ClassNotFoundException e) {
                        NamingException ex = new NamingException(sm.getString("lookupFactory.loadFailed"));
                        ex.initCause(e);
                        throw ex;
                    }
                }
                if (factoryClass != null) {
                    try {
                        factory = (ObjectFactory) factoryClass.getConstructor().newInstance();
                    } catch (Throwable t) {
                        if (t instanceof NamingException) {
                            throw (NamingException) t;
                        }
                        NamingException ex = new NamingException(sm.getString("lookupFactory.createFailed"));
                        ex.initCause(t);
                        throw ex;
                    }
                }
            }
            // Note: No defaults here
            if (factory != null) {
                result = factory.getObjectInstance(obj, name, nameCtx, environment);
            } else {
                if (lookupName == null) {
                    throw new NamingException(sm.getString("lookupFactory.createFailed"));
                } else {
                    result = new InitialContext().lookup(lookupName);
                }
            }
            Class<?> clazz = Class.forName(ref.getClassName());
            if (result != null && !clazz.isAssignableFrom(result.getClass())) {
                String msg = sm.getString("lookupFactory.typeMismatch", name, ref.getClassName(), lookupName, result.getClass().getName());
                NamingException ne = new NamingException(msg);
                log.warn(msg, ne);
                // Close the resource we no longer need if we know how to do so
                if (result instanceof AutoCloseable) {
                    try {
                        ((AutoCloseable) result).close();
                    } catch (Exception e) {
                    // Ignore
                    }
                }
                throw ne;
            }
        } finally {
            names.get().remove(lookupName);
        }
    }
    return result;
}
Also used : Reference(javax.naming.Reference) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) NamingException(javax.naming.NamingException) LookupRef(org.apache.naming.LookupRef)

Aggregations

RefAddr (javax.naming.RefAddr)94 Reference (javax.naming.Reference)60 NamingException (javax.naming.NamingException)23 StringRefAddr (javax.naming.StringRefAddr)18 Properties (java.util.Properties)16 ObjectFactory (javax.naming.spi.ObjectFactory)12 InitialContext (javax.naming.InitialContext)9 Method (java.lang.reflect.Method)7 NameNotFoundException (javax.naming.NameNotFoundException)6 EjbRef (org.apache.naming.EjbRef)5 ResourceRef (org.apache.naming.ResourceRef)5 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)5 SerializableObjectRefAddr (org.glassfish.resources.naming.SerializableObjectRefAddr)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 Enumeration (java.util.Enumeration)4 DataSource (javax.sql.DataSource)4 BeanInfo (java.beans.BeanInfo)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 ResourceLinkRef (org.apache.naming.ResourceLinkRef)3