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();
}
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;
}
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;
}
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);
}
}
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);
}
}
}
Aggregations