Search in sources :

Example 21 with Context

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

the class UserDatabaseRealm method startInternal.

// ------------------------------------------------------ Lifecycle Methods
/**
     * Prepare for the beginning of active use of the public methods of this
     * component and implement the requirements of
     * {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
@Override
protected void startInternal() throws LifecycleException {
    try {
        Context context = getServer().getGlobalNamingContext();
        database = (UserDatabase) context.lookup(resourceName);
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        containerLog.error(sm.getString("userDatabaseRealm.lookup", resourceName), e);
        database = null;
    }
    if (database == null) {
        throw new LifecycleException(sm.getString("userDatabaseRealm.noDatabase", resourceName));
    }
    super.startInternal();
}
Also used : Context(javax.naming.Context) LifecycleException(org.apache.catalina.LifecycleException)

Example 22 with Context

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

the class DataSourceRealm method open.

/**
     * Open the specified database connection.
     *
     * @return Connection to the database
     */
protected Connection open() {
    try {
        Context context = null;
        if (localDataSource) {
            context = ContextBindings.getClassLoader();
            context = (Context) context.lookup("comp/env");
        } else {
            context = getServer().getGlobalNamingContext();
        }
        DataSource dataSource = (DataSource) context.lookup(dataSourceName);
        Connection connection = dataSource.getConnection();
        connectionSuccess = true;
        return connection;
    } catch (Exception e) {
        connectionSuccess = false;
        // Log the problem for posterity
        containerLog.error(sm.getString("dataSourceRealm.exception"), e);
    }
    return null;
}
Also used : Context(javax.naming.Context) Connection(java.sql.Connection) SQLException(java.sql.SQLException) LifecycleException(org.apache.catalina.LifecycleException) DataSource(javax.sql.DataSource)

Example 23 with Context

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

the class InstanceKeyDataSource method testCPDS.

protected ConnectionPoolDataSource testCPDS(final String username, final String password) throws javax.naming.NamingException, SQLException {
    // The source of physical db connections
    ConnectionPoolDataSource cpds = this.dataSource;
    if (cpds == null) {
        Context ctx = null;
        if (jndiEnvironment == null) {
            ctx = new InitialContext();
        } else {
            ctx = new InitialContext(jndiEnvironment);
        }
        final Object ds = ctx.lookup(dataSourceName);
        if (ds instanceof ConnectionPoolDataSource) {
            cpds = (ConnectionPoolDataSource) ds;
        } else {
            throw new SQLException("Illegal configuration: " + "DataSource " + dataSourceName + " (" + ds.getClass().getName() + ")" + " doesn't implement javax.sql.ConnectionPoolDataSource");
        }
    }
    // try to get a connection with the supplied username/password
    PooledConnection conn = null;
    try {
        if (username != null) {
            conn = cpds.getPooledConnection(username, password);
        } else {
            conn = cpds.getPooledConnection();
        }
        if (conn == null) {
            throw new SQLException("Cannot connect using the supplied username/password");
        }
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (final SQLException e) {
            // at least we could connect
            }
        }
    }
    return cpds;
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) InitialContext(javax.naming.InitialContext)

Example 24 with Context

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

the class ContextBindings method bindThread.

/**
     * Binds a naming context to a thread.
     *
     * @param obj   Object bound to the required naming context
     * @param token Security token
     *
     * @throws NamingException If no naming context is bound to the provided
     *         object
     */
public static void bindThread(Object obj, Object token) throws NamingException {
    if (ContextAccessController.checkSecurityToken(obj, token)) {
        Context context = objectBindings.get(obj);
        if (context == null) {
            throw new NamingException(sm.getString("contextBindings.unknownContext", obj));
        }
        threadBindings.put(Thread.currentThread(), context);
        threadObjectBindings.put(Thread.currentThread(), obj);
    }
}
Also used : Context(javax.naming.Context) NamingException(javax.naming.NamingException)

Example 25 with Context

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

the class NamingContext method bind.

/**
     * Binds a name to an object. All intermediate contexts and the target
     * context (that named by all but terminal atomic component of the name)
     * must already exist.
     *
     * @param name the name to bind; may not be empty
     * @param obj the object to bind; possibly null
     * @param rebind if true, then perform a rebind (ie, overwrite)
     * @exception NameAlreadyBoundException if name is already bound
     * @exception javax.naming.directory.InvalidAttributesException if object
     * did not supply all mandatory attributes
     * @exception NamingException if a naming exception is encountered
     */
protected void bind(Name name, Object obj, boolean rebind) throws NamingException {
    if (!checkWritable()) {
        return;
    }
    while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException(sm.getString("namingContext.invalidName"));
    NamingEntry entry = bindings.get(name.get(0));
    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0)));
        }
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
            }
        } else {
            throw new NamingException(sm.getString("namingContext.contextExpected"));
        }
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException(sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind = NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
            }
            bindings.put(name.get(0), entry);
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) Referenceable(javax.naming.Referenceable) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Aggregations

Context (javax.naming.Context)507 InitialContext (javax.naming.InitialContext)250 Test (org.junit.Test)173 NamingException (javax.naming.NamingException)156 DataSource (javax.sql.DataSource)72 Properties (java.util.Properties)67 Connection (java.sql.Connection)62 NameNotFoundException (javax.naming.NameNotFoundException)52 UserTransaction (javax.transaction.UserTransaction)48 SQLException (java.sql.SQLException)46 IOException (java.io.IOException)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)44 Statement (java.sql.Statement)42 Name (javax.naming.Name)38 Hashtable (java.util.Hashtable)35 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)27 BeanContext (org.apache.openejb.BeanContext)27 Binding (javax.naming.Binding)25 Reference (javax.naming.Reference)25 NotContextException (javax.naming.NotContextException)23