Java Code Examples for javax.naming.Reference#getClassName()

The following examples show how to use javax.naming.Reference#getClassName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TesterFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
        Hashtable<?,?> environment) throws Exception {

    if (obj instanceof Reference) {
        Reference ref = (Reference)obj;
        String className = ref.getClassName();

        if (className == null) {
            throw new RuntimeException();
        }

        if (className.equals("org.apache.naming.resources.TesterObject")) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            Class<?> clazz =
                cl.loadClass("org.apache.naming.resources.TesterObject");
            return clazz.getConstructor().newInstance();
        }
    }
    return null;
}
 
Example 2
Source File: GenericNamingResourcesFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if ((obj == null) || !(obj instanceof Reference)) {
        return null;
    }
    Reference ref = (Reference) obj;
    Enumeration<RefAddr> refs = ref.getAll();

    String type = ref.getClassName();
    Object o = Class.forName(type).newInstance();

    while (refs.hasMoreElements()) {
        RefAddr addr = refs.nextElement();
        String param = addr.getType();
        String value = null;
        if (addr.getContent()!=null) {
            value = addr.getContent().toString();
        }
        if (setProperty(o, param, value,false)) {

        } else {
            log.debug("Property not configured["+param+"]. No setter found on["+o+"].");
        }
    }
    return o;
}
 
Example 3
Source File: TesterFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
        Hashtable<?,?> environment) throws Exception {

    if (obj instanceof Reference) {
        Reference ref = (Reference)obj;
        String className = ref.getClassName();

        if (className == null) {
            throw new RuntimeException();
        }

        if (className.equals("org.apache.naming.resources.TesterObject")) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            Class<?> clazz =
                cl.loadClass("org.apache.naming.resources.TesterObject");
            return clazz.newInstance();
        }
    }
    return null;
}
 
Example 4
Source File: GenericNamingResourcesFactory.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if ((obj == null) || !(obj instanceof Reference)) {
        return null;
    }
    Reference ref = (Reference) obj;
    Enumeration<RefAddr> refs = ref.getAll();

    String type = ref.getClassName();
    Object o = Class.forName(type).newInstance();

    while (refs.hasMoreElements()) {
        RefAddr addr = refs.nextElement();
        String param = addr.getType();
        String value = null;
        if (addr.getContent()!=null) {
            value = addr.getContent().toString();
        }
        if (setProperty(o, param, value,false)) {

        } else {
            log.debug("Property not configured["+param+"]. No setter found on["+o+"].");
        }
    }
    return o;
}
 
Example 5
Source File: TesterFactory.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
        Hashtable<?,?> environment) throws Exception {

    if (obj instanceof Reference) {
        Reference ref = (Reference)obj;
        String className = ref.getClassName();

        if (className == null) {
            throw new RuntimeException();
        }

        if (className.equals("org.apache.naming.resources.TesterObject")) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            Class<?> clazz =
                cl.loadClass("org.apache.naming.resources.TesterObject");
            return clazz.newInstance();
        }
    }
    return null;
}
 
Example 6
Source File: GenericNamingResourcesFactory.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if ((obj == null) || !(obj instanceof Reference)) {
        return null;
    }
    Reference ref = (Reference) obj;
    Enumeration<RefAddr> refs = ref.getAll();

    String type = ref.getClassName();
    Object o =
        ClassLoaderUtil.loadClass(
            type,
            GenericNamingResourcesFactory.class.getClassLoader(),
            Thread.currentThread().getContextClassLoader()).getConstructor().newInstance();

    while (refs.hasMoreElements()) {
        RefAddr addr = refs.nextElement();
        String param = addr.getType();
        String value = null;
        if (addr.getContent()!=null) {
            value = addr.getContent().toString();
        }
        if (setProperty(o, param, value)) {

        } else {
            log.debug("Property not configured["+param+"]. No setter found on["+o+"].");
        }
    }
    return o;
}
 
Example 7
Source File: JVoiceXmlObjectFactory.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Object getObjectInstance(final Object obj, final Name name,
        final Context context, final Hashtable<?, ?> environment)
        throws Exception {
    if (obj instanceof Reference) {
        final Reference ref = (Reference) obj;
        final String className = ref.getClassName();

        return resolveReference(className, context);
    }

    return null;
}
 
Example 8
Source File: ReferenceableDataSource.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
	Re-Create Derby datasource given a reference.

	@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 nameCtx, or null if no
	name is specified. 
	@param nameCtx The context relative to which the name parameter is
	specified, or null if name is relative to the default initial context. 
	@param environment The possibly null environment that is used in
	creating the object. 

	@return One of the Derby datasource object created; null if an
	object cannot be created. 

	@exception Exception  if this object factory encountered an exception
	while attempting to create an object, and no other object factories are
	to be tried. 
 */
public Object getObjectInstance(Object obj,
								Name name,
								Context nameCtx,
								Hashtable environment)
	 throws Exception
{
	Reference ref = (Reference)obj;
	String classname = ref.getClassName();

	Object ds = Class.forName(classname).newInstance();

	for (Enumeration e = ref.getAll(); e.hasMoreElements(); ) {
		
		RefAddr attribute = (RefAddr) e.nextElement();

		String propertyName = attribute.getType();

		String value = (String) attribute.getContent();

		String methodName = "set" + propertyName.substring(0,1).toUpperCase(java.util.Locale.ENGLISH) + propertyName.substring(1);

		Method m;
		
		Object argValue;
		try {
			m = ds.getClass().getMethod(methodName, STRING_ARG);
			argValue = value;
		} catch (NoSuchMethodException nsme) {
			try {
				m = ds.getClass().getMethod(methodName, INT_ARG);
				argValue = Integer.valueOf(value);
			} catch (NoSuchMethodException nsme2) {
				m = ds.getClass().getMethod(methodName, BOOLEAN_ARG);
				argValue = Boolean.valueOf(value);
			}
		}
		m.invoke(ds, new Object[] { argValue });
	}

	return ds;
}
 
Example 9
Source File: ResourceLinkFactory.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * 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 {

    if (!(obj instanceof ResourceLinkRef)) {
        return null;
    }

    // Can we process this request?
    Reference ref = (Reference) obj;

    // Read the global ref addr
    String globalName = null;
    RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
    if (refAddr != null) {
        globalName = refAddr.getContent().toString();
        // Confirm that the current web application is currently configured
        // to access the specified global resource
        if (!validateGlobalResourceAccess(globalName)) {
            return null;
        }
        Object result = null;
        result = globalContext.lookup(globalName);
        // Check the expected type
        String expectedClassName = ref.getClassName();
        if (expectedClassName == null) {
            throw new IllegalArgumentException(
                    sm.getString("resourceLinkFactory.nullType", name, globalName));
        }
        try {
            Class<?> expectedClazz = Class.forName(
                    expectedClassName, true, Thread.currentThread().getContextClassLoader());
            if (!expectedClazz.isAssignableFrom(result.getClass())) {
                throw new IllegalArgumentException(sm.getString("resourceLinkFactory.wrongType",
                        name, globalName, expectedClassName, result.getClass().getName()));
            }
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(sm.getString("resourceLinkFactory.unknownType",
                    name, globalName, expectedClassName), e);
        }
        return result;
    }

    return null;
}
 
Example 10
Source File: EnumFactory.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public Object getObjectInstance(final Object object, final Name name, final Context context, final Hashtable environment) throws Exception {
    if (!(object instanceof Reference)) {
        return null;
    }

    final Reference reference = ((Reference) object);

    final String typeName = reference.getClassName();

    final ClassLoader loader = Thread.currentThread().getContextClassLoader();

    final Class<?> type = loader.loadClass(typeName);

    final String value = NamingUtil.getProperty(reference, ENUM_VALUE);

    return Enum.valueOf((Class<Enum>) type, value);
}
 
Example 11
Source File: ReferenceableDataSource.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
	Re-Create Derby datasource given a reference.

	@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 nameCtx, or null if no
	name is specified. 
	@param nameCtx The context relative to which the name parameter is
	specified, or null if name is relative to the default initial context. 
	@param environment The possibly null environment that is used in
	creating the object. 

	@return One of the Derby datasource object created; null if an
	object cannot be created. 

	@exception Exception  if this object factory encountered an exception
	while attempting to create an object, and no other object factories are
	to be tried. 
 */
public Object getObjectInstance(Object obj,
								Name name,
								Context nameCtx,
								Hashtable environment)
	 throws Exception
{
	Reference ref = (Reference)obj;
	String classname = ref.getClassName();

	Object ds = Class.forName(classname).newInstance();

	for (Enumeration e = ref.getAll(); e.hasMoreElements(); ) {
		
		RefAddr attribute = (RefAddr) e.nextElement();

		String propertyName = attribute.getType();

		String value = (String) attribute.getContent();

		String methodName = "set" + propertyName.substring(0,1).toUpperCase(java.util.Locale.ENGLISH) + propertyName.substring(1);

		Method m;
		
		Object argValue;
		try {
			m = ds.getClass().getMethod(methodName, STRING_ARG);
			argValue = value;
		} catch (NoSuchMethodException nsme) {
			try {
				m = ds.getClass().getMethod(methodName, INT_ARG);
				argValue = Integer.valueOf(value);
			} catch (NoSuchMethodException nsme2) {
				m = ds.getClass().getMethod(methodName, BOOLEAN_ARG);
				argValue = Boolean.valueOf(value);
			}
		}
		m.invoke(ds, new Object[] { argValue });
	}

	return ds;
}
 
Example 12
Source File: MysqlDataSourceFactory.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Object getObjectInstance(Object refObj, Name nm, Context ctx, Hashtable<?, ?> env) throws Exception {
    Reference ref = (Reference) refObj;
    String className = ref.getClassName();

    if ((className != null)
            && (className.equals(DATA_SOURCE_CLASS_NAME) || className.equals(POOL_DATA_SOURCE_CLASS_NAME) || className.equals(XA_DATA_SOURCE_CLASS_NAME))) {
        MysqlDataSource dataSource = null;

        try {
            dataSource = (MysqlDataSource) Class.forName(className).newInstance();
        } catch (Exception ex) {
            throw new RuntimeException(Messages.getString("MysqlDataSourceFactory.0", new Object[] { className, ex.toString() }));
        }

        int portNumber = 3306;

        String portNumberAsString = nullSafeRefAddrStringGet("port", ref);

        if (portNumberAsString != null) {
            portNumber = Integer.parseInt(portNumberAsString);
        }

        dataSource.setPort(portNumber);

        String user = nullSafeRefAddrStringGet(PropertyKey.USER.getKeyName(), ref);

        if (user != null) {
            dataSource.setUser(user);
        }

        String password = nullSafeRefAddrStringGet(PropertyKey.PASSWORD.getKeyName(), ref);

        if (password != null) {
            dataSource.setPassword(password);
        }

        String serverName = nullSafeRefAddrStringGet("serverName", ref);

        if (serverName != null) {
            dataSource.setServerName(serverName);
        }

        String databaseName = nullSafeRefAddrStringGet("databaseName", ref);

        if (databaseName != null) {
            dataSource.setDatabaseName(databaseName);
        }

        String explicitUrlAsString = nullSafeRefAddrStringGet("explicitUrl", ref);

        if (explicitUrlAsString != null) {
            if (Boolean.valueOf(explicitUrlAsString).booleanValue()) {
                dataSource.setUrl(nullSafeRefAddrStringGet("url", ref));
            }
        }

        dataSource.setPropertiesViaRef(ref);

        return dataSource;
    }

    // We can't create an instance of the reference
    return null;
}
 
Example 13
Source File: ResourceLinkFactory.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * 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 {

    if (!(obj instanceof ResourceLinkRef)) {
        return null;
    }

    // Can we process this request?
    Reference ref = (Reference) obj;

    // Read the global ref addr
    String globalName = null;
    RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
    if (refAddr != null) {
        globalName = refAddr.getContent().toString();
        // Confirm that the current web application is currently configured
        // to access the specified global resource
        if (!validateGlobalResourceAccess(globalName)) {
            return null;
        }
        Object result = null;
        result = globalContext.lookup(globalName);
        // Check the expected type
        String expectedClassName = ref.getClassName();
        if (expectedClassName == null) {
            throw new IllegalArgumentException(
                    sm.getString("resourceLinkFactory.nullType", name, globalName));
        }
        try {
            Class<?> expectedClazz = Class.forName(
                    expectedClassName, true, Thread.currentThread().getContextClassLoader());
            if (!expectedClazz.isAssignableFrom(result.getClass())) {
                throw new IllegalArgumentException(sm.getString("resourceLinkFactory.wrongType",
                        name, globalName, expectedClassName, result.getClass().getName()));
            }
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(sm.getString("resourceLinkFactory.unknownType",
                    name, globalName, expectedClassName), e);
        }
        return result;
    }

    return null;
}
 
Example 14
Source File: ReferenceableDataSource.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
	Re-Create Derby datasource given a reference.

	@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 nameCtx, or null if no
	name is specified. 
	@param nameCtx The context relative to which the name parameter is
	specified, or null if name is relative to the default initial context. 
	@param environment The possibly null environment that is used in
	creating the object. 

	@return One of the Derby datasource object created; null if an
	object cannot be created. 

	@exception Exception  if this object factory encountered an exception
	while attempting to create an object, and no other object factories are
	to be tried. 
 */
public Object getObjectInstance(Object obj,
								Name name,
								Context nameCtx,
								Hashtable environment)
	 throws Exception
{
	Reference ref = (Reference)obj;
	String classname = ref.getClassName();

	Object ds = Class.forName(classname).newInstance();

	for (Enumeration e = ref.getAll(); e.hasMoreElements(); ) {
		
		RefAddr attribute = (RefAddr) e.nextElement();

		String propertyName = attribute.getType();

		String value = (String) attribute.getContent();

		String methodName = "set" + propertyName.substring(0,1).toUpperCase(java.util.Locale.ENGLISH) + propertyName.substring(1);

		Method m;
		
		Object argValue;
		try {
			m = ds.getClass().getMethod(methodName, STRING_ARG);
			argValue = value;
		} catch (NoSuchMethodException nsme) {
			try {
				m = ds.getClass().getMethod(methodName, INT_ARG);
				argValue = Integer.valueOf(value);
			} catch (NoSuchMethodException nsme2) {
				m = ds.getClass().getMethod(methodName, BOOLEAN_ARG);
				argValue = Boolean.valueOf(value);
			}
		}
		m.invoke(ds, new Object[] { argValue });
	}

	return ds;
}
 
Example 15
Source File: MysqlDataSourceFactory.java    From Komondor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param refObj
 * @param nm
 * @param ctx
 * @param env
 * @throws Exception
 */
public Object getObjectInstance(Object refObj, Name nm, Context ctx, Hashtable<?, ?> env) throws Exception {
    Reference ref = (Reference) refObj;
    String className = ref.getClassName();

    if ((className != null)
            && (className.equals(DATA_SOURCE_CLASS_NAME) || className.equals(POOL_DATA_SOURCE_CLASS_NAME) || className.equals(XA_DATA_SOURCE_CLASS_NAME))) {
        MysqlDataSource dataSource = null;

        try {
            dataSource = (MysqlDataSource) Class.forName(className).newInstance();
        } catch (Exception ex) {
            throw new RuntimeException("Unable to create DataSource of class '" + className + "', reason: " + ex.toString());
        }

        int portNumber = 3306;

        String portNumberAsString = nullSafeRefAddrStringGet("port", ref);

        if (portNumberAsString != null) {
            portNumber = Integer.parseInt(portNumberAsString);
        }

        dataSource.setPort(portNumber);

        String user = nullSafeRefAddrStringGet(NonRegisteringDriver.USER_PROPERTY_KEY, ref);

        if (user != null) {
            dataSource.setUser(user);
        }

        String password = nullSafeRefAddrStringGet(NonRegisteringDriver.PASSWORD_PROPERTY_KEY, ref);

        if (password != null) {
            dataSource.setPassword(password);
        }

        String serverName = nullSafeRefAddrStringGet("serverName", ref);

        if (serverName != null) {
            dataSource.setServerName(serverName);
        }

        String databaseName = nullSafeRefAddrStringGet("databaseName", ref);

        if (databaseName != null) {
            dataSource.setDatabaseName(databaseName);
        }

        String explicitUrlAsString = nullSafeRefAddrStringGet("explicitUrl", ref);

        if (explicitUrlAsString != null) {
            if (Boolean.valueOf(explicitUrlAsString).booleanValue()) {
                dataSource.setUrl(nullSafeRefAddrStringGet("url", ref));
            }
        }

        dataSource.setPropertiesViaRef(ref);

        return dataSource;
    }

    // We can't create an instance of the reference
    return null;
}
 
Example 16
Source File: MysqlDataSourceFactory.java    From r-course with MIT License 4 votes vote down vote up
/**
 * @param refObj
 * @param nm
 * @param ctx
 * @param env
 * @throws Exception
 */
public Object getObjectInstance(Object refObj, Name nm, Context ctx, Hashtable<?, ?> env) throws Exception {
    Reference ref = (Reference) refObj;
    String className = ref.getClassName();

    if ((className != null)
            && (className.equals(DATA_SOURCE_CLASS_NAME) || className.equals(POOL_DATA_SOURCE_CLASS_NAME) || className.equals(XA_DATA_SOURCE_CLASS_NAME))) {
        MysqlDataSource dataSource = null;

        try {
            dataSource = (MysqlDataSource) Class.forName(className).newInstance();
        } catch (Exception ex) {
            throw new RuntimeException("Unable to create DataSource of class '" + className + "', reason: " + ex.toString());
        }

        int portNumber = 3306;

        String portNumberAsString = nullSafeRefAddrStringGet("port", ref);

        if (portNumberAsString != null) {
            portNumber = Integer.parseInt(portNumberAsString);
        }

        dataSource.setPort(portNumber);

        String user = nullSafeRefAddrStringGet(NonRegisteringDriver.USER_PROPERTY_KEY, ref);

        if (user != null) {
            dataSource.setUser(user);
        }

        String password = nullSafeRefAddrStringGet(NonRegisteringDriver.PASSWORD_PROPERTY_KEY, ref);

        if (password != null) {
            dataSource.setPassword(password);
        }

        String serverName = nullSafeRefAddrStringGet("serverName", ref);

        if (serverName != null) {
            dataSource.setServerName(serverName);
        }

        String databaseName = nullSafeRefAddrStringGet("databaseName", ref);

        if (databaseName != null) {
            dataSource.setDatabaseName(databaseName);
        }

        String explicitUrlAsString = nullSafeRefAddrStringGet("explicitUrl", ref);

        if (explicitUrlAsString != null) {
            if (Boolean.valueOf(explicitUrlAsString).booleanValue()) {
                dataSource.setUrl(nullSafeRefAddrStringGet("url", ref));
            }
        }

        dataSource.setPropertiesViaRef(ref);

        return dataSource;
    }

    // We can't create an instance of the reference
    return null;
}
 
Example 17
Source File: MysqlDataSourceFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param refObj
 * @param nm
 * @param ctx
 * @param env
 * @throws Exception
 */
public Object getObjectInstance(Object refObj, Name nm, Context ctx, Hashtable<?, ?> env) throws Exception {
    Reference ref = (Reference) refObj;
    String className = ref.getClassName();

    if ((className != null)
            && (className.equals(DATA_SOURCE_CLASS_NAME) || className.equals(POOL_DATA_SOURCE_CLASS_NAME) || className.equals(XA_DATA_SOURCE_CLASS_NAME))) {
        MysqlDataSource dataSource = null;

        try {
            dataSource = (MysqlDataSource) Class.forName(className).newInstance();
        } catch (Exception ex) {
            throw new RuntimeException(Messages.getString("MysqlDataSourceFactory.0", new Object[] { className, ex.toString() }));
        }

        int portNumber = 3306;

        String portNumberAsString = nullSafeRefAddrStringGet("port", ref);

        if (portNumberAsString != null) {
            portNumber = Integer.parseInt(portNumberAsString);
        }

        dataSource.setPort(portNumber);

        String user = nullSafeRefAddrStringGet(PropertyDefinitions.PNAME_user, ref);

        if (user != null) {
            dataSource.setUser(user);
        }

        String password = nullSafeRefAddrStringGet(PropertyDefinitions.PNAME_password, ref);

        if (password != null) {
            dataSource.setPassword(password);
        }

        String serverName = nullSafeRefAddrStringGet("serverName", ref);

        if (serverName != null) {
            dataSource.setServerName(serverName);
        }

        String databaseName = nullSafeRefAddrStringGet("databaseName", ref);

        if (databaseName != null) {
            dataSource.setDatabaseName(databaseName);
        }

        String explicitUrlAsString = nullSafeRefAddrStringGet("explicitUrl", ref);

        if (explicitUrlAsString != null) {
            if (Boolean.valueOf(explicitUrlAsString).booleanValue()) {
                dataSource.setUrl(nullSafeRefAddrStringGet("url", ref));
            }
        }

        dataSource.setPropertiesViaRef(ref);

        return dataSource;
    }

    // We can't create an instance of the reference
    return null;
}