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

The following examples show how to use javax.naming.Reference#get() . 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: CachedObjectFactory.java    From javamelody with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
// CHECKSTYLE:OFF
public synchronized Object getObjectInstance(Object obj, Name name, Context nameCtx, // NOPMD
		Hashtable<?, ?> environment) throws NamingException { // NOPMD
	// CHECKSTYLE:ON
	final Reference reference = (Reference) obj;
	final RefAddr jndiRefAddr = reference.get("jndi-ref");
	if (jndiRefAddr == null) {
		throw new NamingException("You must specify a 'jndi-ref' in the <Resource> tag");
	}
	final String jndiRef = (String) jndiRefAddr.getContent();
	Object cachedObject = CACHED_OBJECTS.get(jndiRef);
	if (cachedObject == null) {
		final InitialContext context = new InitialContext();
		cachedObject = context.lookup(jndiRef);
		if (cachedObject == null) {
			throw new NamingException("No jndi object found for the 'jndi-ref': " + jndiRef);
		}
		CACHED_OBJECTS.put(jndiRef, cachedObject);
	}
	return cachedObject;
}
 
Example 2
Source File: InVMNamingContext.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public Object lookup(String name) throws NamingException {
   name = trimSlashes(name);
   int i = name.indexOf("/");
   String tok = i == -1 ? name : name.substring(0, i);
   Object value = map.get(tok);
   if (value == null) {
      throw new NameNotFoundException("Name not found: " + tok);
   }
   if (value instanceof InVMNamingContext && i != -1) {
      return ((InVMNamingContext) value).lookup(name.substring(i));
   }
   if (value instanceof Reference) {
      Reference ref = (Reference) value;
      RefAddr refAddr = ref.get("nns");

      // we only deal with references create by NonSerializableFactory
      String key = (String) refAddr.getContent();
      return NonSerializableFactory.lookup(key);
   } else {
      return value;
   }
}
 
Example 3
Source File: DataSourceLinkFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 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 {
    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;
}
 
Example 4
Source File: DataSourceLinkFactory.java    From tomcatsrc with Apache License 2.0 6 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 {
    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;
}
 
Example 5
Source File: ConnectionPropertiesImpl.java    From Komondor with GNU General Public License v3.0 5 votes vote down vote up
void initializeFrom(Reference ref, ExceptionInterceptor exceptionInterceptor) throws SQLException {
    RefAddr refAddr = ref.get(getPropertyName());

    if (refAddr != null) {
        String refContentAsString = (String) refAddr.getContent();

        initializeFrom(refContentAsString, exceptionInterceptor);
    }
}
 
Example 6
Source File: NonSerializableFactory.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public Object getObjectInstance(final Object obj,
                                final Name name,
                                final Context nameCtx,
                                final Hashtable<?, ?> env) throws Exception {
   Reference ref = (Reference) obj;
   RefAddr addr = ref.get("nns");
   String key = (String) addr.getContent();
   return NonSerializableFactory.getWrapperMap().get(key);
}
 
Example 7
Source File: AbstractRuntimeProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void initializeFrom(Reference ref, ExceptionInterceptor exceptionInterceptor) {
    RefAddr refAddr = ref.get(getPropertyDefinition().getName());

    if (refAddr != null) {
        String refContentAsString = (String) refAddr.getContent();

        initializeFrom(refContentAsString, exceptionInterceptor);
    }
}
 
Example 8
Source File: NonSerializableFactory.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public Object getObjectInstance(final Object obj,
                                final Name name,
                                final Context nameCtx,
                                final Hashtable<?, ?> env) throws Exception {
   Reference ref = (Reference) obj;
   RefAddr addr = ref.get("nns");
   String key = (String) addr.getContent();
   return NonSerializableFactory.getWrapperMap().get(key);
}
 
Example 9
Source File: OpenEjbFactory.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new EJB instance using OpenEJB.
 * 
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
                                Hashtable<?,?> environment)
    throws Exception {

    Object beanObj = null;

    if (obj instanceof EjbRef) {

        Reference ref = (Reference) obj;

        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }

        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);

        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }

    }

    return beanObj;

}
 
Example 10
Source File: NonSerializableFactory.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public Object getObjectInstance(final Object obj,
                                final Name name,
                                final Context nameCtx,
                                final Hashtable<?, ?> env) throws Exception {
   Reference ref = (Reference) obj;
   RefAddr addr = ref.get("nns");
   String key = (String) addr.getContent();
   return NonSerializableFactory.getWrapperMap().get(key);
}
 
Example 11
Source File: BasicDataSourceFactory.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Create and return a new <code>BasicDataSource</code> instance. If no 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
 *
 * @throws Exception
 *             if an exception occurs creating the instance
 */
@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx,
        final 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;
    }
    final Reference ref = (Reference) obj;
    if (!"javax.sql.DataSource".equals(ref.getClassName())) {
        return null;
    }

    // Check property names and log warnings about obsolete and / or unknown properties
    final List<String> warnings = new ArrayList<>();
    final List<String> infoMessages = new ArrayList<>();
    validatePropertyNames(ref, name, warnings, infoMessages);
    for (final String warning : warnings) {
        log.warn(warning);
    }
    for (final String infoMessage : infoMessages) {
        log.info(infoMessage);
    }

    final Properties properties = new Properties();
    for (final String propertyName : ALL_PROPERTIES) {
        final RefAddr ra = ref.get(propertyName);
        if (ra != null) {
            final String propertyValue = ra.getContent().toString();
            properties.setProperty(propertyName, propertyValue);
        }
    }

    return createDataSource(properties);
}
 
Example 12
Source File: BasicDataSourceFactory.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * <p>
 * Create and return a new <code>BasicDataSource</code> instance. If no 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
 *
 * @throws Exception
 *             if an exception occurs creating the instance
 */
@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx,
        final 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;
    }
    final Reference ref = (Reference) obj;
    if (!"javax.sql.DataSource".equals(ref.getClassName())) {
        return null;
    }

    // Check property names and log warnings about obsolete and / or unknown properties
    final List<String> warnings = new ArrayList<>();
    final List<String> infoMessages = new ArrayList<>();
    validatePropertyNames(ref, name, warnings, infoMessages);
    for (final String warning : warnings) {
        log.warn(warning);
    }
    for (final String infoMessage : infoMessages) {
        log.info(infoMessage);
    }

    final Properties properties = new Properties();
    for (final String propertyName : ALL_PROPERTIES) {
        final RefAddr ra = ref.get(propertyName);
        if (ra != null) {
            final String propertyValue = ra.getContent().toString();
            properties.setProperty(propertyName, propertyValue);
        }
    }

    return createDataSource(properties);
}
 
Example 13
Source File: InstanceKeyDataSourceFactory.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Implements ObjectFactory to create an instance of SharedPoolDataSource or PerUserPoolDataSource
 */
@Override
public Object getObjectInstance(final Object refObj, final Name name, final Context context,
        final Hashtable<?, ?> env) throws IOException, ClassNotFoundException {
    // The spec says to return null if we can't create an instance
    // of the reference
    Object obj = null;
    if (refObj instanceof Reference) {
        final Reference ref = (Reference) refObj;
        if (isCorrectClass(ref.getClassName())) {
            final RefAddr refAddr = ref.get("instanceKey");
            if (refAddr != null && refAddr.getContent() != null) {
                // object was bound to JNDI via Referenceable API.
                obj = instanceMap.get(refAddr.getContent());
            } else {
                // Tomcat JNDI creates a Reference out of server.xml
                // <ResourceParam> configuration and passes it to an
                // instance of the factory given in server.xml.
                String key = null;
                if (name != null) {
                    key = name.toString();
                    obj = instanceMap.get(key);
                }
                if (obj == null) {
                    final InstanceKeyDataSource ds = getNewInstance(ref);
                    setCommonProperties(ref, ds);
                    obj = ds;
                    if (key != null) {
                        instanceMap.put(key, ds);
                    }
                }
            }
        }
    }
    return obj;
}
 
Example 14
Source File: SharedPoolDataSourceFactory.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected InstanceKeyDataSource getNewInstance(final Reference ref) {
    final SharedPoolDataSource spds = new SharedPoolDataSource();
    final RefAddr ra = ref.get("maxTotal");
    if (ra != null && ra.getContent() != null) {
        spds.setMaxTotal(Integer.parseInt(ra.getContent().toString()));
    }
    return spds;
}
 
Example 15
Source File: DataSourceRegressionTest.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
private void removeFromRef(Reference ref, String key) {
    int size = ref.size();

    for (int i = 0; i < size; i++) {
        RefAddr refAddr = ref.get(i);
        if (refAddr.getType().equals(key)) {
            ref.remove(i);
            break;
        }
    }
}
 
Example 16
Source File: OpenEjbFactory.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new EJB instance using OpenEJB.
 * 
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
                                Hashtable<?,?> environment)
    throws Exception {

    Object beanObj = null;

    if (obj instanceof EjbRef) {

        Reference ref = (Reference) obj;

        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }

        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);

        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }

    }

    return beanObj;

}
 
Example 17
Source File: OpenEjbFactory.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Create a new EJB instance using OpenEJB.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
                                Hashtable<?,?> environment)
    throws Exception {

    Object beanObj = null;

    if (obj instanceof EjbRef) {

        Reference ref = (Reference) obj;

        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }

        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);

        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }

    }

    return beanObj;

}
 
Example 18
Source File: DataSourceFactory.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * <p>Create and return a new <code>BasicDataSource</code> instance.  If no
 * 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
 *
 * @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);
}
 
Example 19
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 20
Source File: MysqlDataSourceFactory.java    From FoxTelem with GNU General Public License v3.0 3 votes vote down vote up
private String nullSafeRefAddrStringGet(String referenceName, Reference ref) {
    RefAddr refAddr = ref.get(referenceName);

    String asString = refAddr != null ? (String) refAddr.getContent() : null;

    return asString;
}