Search in sources :

Example 16 with RefAddr

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

the class BasicDataSourceFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
     * <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 {
    // 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);
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Example 17 with RefAddr

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

the class SharedPoolDataSourceFactory method getNewInstance.

@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;
}
Also used : RefAddr(javax.naming.RefAddr)

Example 18 with RefAddr

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

the class InstanceKeyDataSourceFactory method setCommonProperties.

private void setCommonProperties(final Reference ref, final InstanceKeyDataSource ikds) throws IOException, ClassNotFoundException {
    RefAddr ra = ref.get("dataSourceName");
    if (ra != null && ra.getContent() != null) {
        ikds.setDataSourceName(ra.getContent().toString());
    }
    ra = ref.get("description");
    if (ra != null && ra.getContent() != null) {
        ikds.setDescription(ra.getContent().toString());
    }
    ra = ref.get("jndiEnvironment");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        ikds.setJndiEnvironment((Properties) deserialize(serialized));
    }
    ra = ref.get("loginTimeout");
    if (ra != null && ra.getContent() != null) {
        ikds.setLoginTimeout(Integer.parseInt(ra.getContent().toString()));
    }
    // Pool properties
    ra = ref.get("blockWhenExhausted");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultBlockWhenExhausted(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("evictionPolicyClassName");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultEvictionPolicyClassName(ra.getContent().toString());
    }
    // Pool properties
    ra = ref.get("lifo");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultLifo(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("maxIdlePerKey");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMaxIdle(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("maxTotalPerKey");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMaxTotal(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("maxWaitMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMaxWaitMillis(Long.parseLong(ra.getContent().toString()));
    }
    ra = ref.get("minEvictableIdleTimeMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMinEvictableIdleTimeMillis(Long.parseLong(ra.getContent().toString()));
    }
    ra = ref.get("minIdlePerKey");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMinIdle(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("numTestsPerEvictionRun");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultNumTestsPerEvictionRun(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("softMinEvictableIdleTimeMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultSoftMinEvictableIdleTimeMillis(Long.parseLong(ra.getContent().toString()));
    }
    ra = ref.get("testOnCreate");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestOnCreate(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("testOnBorrow");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestOnBorrow(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("testOnReturn");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestOnReturn(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("testWhileIdle");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestWhileIdle(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("timeBetweenEvictionRunsMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTimeBetweenEvictionRunsMillis(Long.parseLong(ra.getContent().toString()));
    }
    // Connection factory properties
    ra = ref.get("validationQuery");
    if (ra != null && ra.getContent() != null) {
        ikds.setValidationQuery(ra.getContent().toString());
    }
    ra = ref.get("validationQueryTimeout");
    if (ra != null && ra.getContent() != null) {
        ikds.setValidationQueryTimeout(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("rollbackAfterValidation");
    if (ra != null && ra.getContent() != null) {
        ikds.setRollbackAfterValidation(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("maxConnLifetimeMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setMaxConnLifetimeMillis(Long.parseLong(ra.getContent().toString()));
    }
    // Connection properties
    ra = ref.get("defaultAutoCommit");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultAutoCommit(Boolean.valueOf(ra.getContent().toString()));
    }
    ra = ref.get("defaultTransactionIsolation");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTransactionIsolation(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("defaultReadOnly");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultReadOnly(Boolean.valueOf(ra.getContent().toString()));
    }
}
Also used : RefAddr(javax.naming.RefAddr)

Example 19 with RefAddr

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

the class InstanceKeyDataSourceFactory method getObjectInstance.

/**
     * 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 ra = ref.get("instanceKey");
            if (ra != null && ra.getContent() != null) {
                // object was bound to jndi via Referenceable api.
                obj = instanceMap.get(ra.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;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference)

Example 20 with RefAddr

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

the class PerUserPoolDataSourceFactory method getNewInstance.

// Avoid warnings on deserialization
@SuppressWarnings("unchecked")
@Override
protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
    final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
    RefAddr ra = ref.get("defaultMaxTotal");
    if (ra != null && ra.getContent() != null) {
        pupds.setDefaultMaxTotal(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("defaultMaxIdle");
    if (ra != null && ra.getContent() != null) {
        pupds.setDefaultMaxIdle(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("defaultMaxWaitMillis");
    if (ra != null && ra.getContent() != null) {
        pupds.setDefaultMaxWaitMillis(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("perUserDefaultAutoCommit");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
    }
    ra = ref.get("perUserDefaultTransactionIsolation");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
    }
    ra = ref.get("perUserMaxTotal");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
    }
    ra = ref.get("perUserMaxIdle");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
    }
    ra = ref.get("perUserMaxWaitMillis");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
    }
    ra = ref.get("perUserDefaultReadOnly");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
    }
    return pupds;
}
Also used : RefAddr(javax.naming.RefAddr)

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