use of javax.naming.InitialContext in project OpenAM by OpenRock.
the class JdbcSimpleUserDao method initialize.
/**
* This class must be called before using the methods since the datasource
* must be set up.
* This version of initialize is used when connections are retreived using
* a Java EE datasource resource which is configured through the application
* server. For example if you use your server's ability to configure and
* pool connections. This also requires a java:comp/env resource-ref
* in web.xml
*
* @throws java.lang.InstantiationException when not able to instantiate
* this class, for example if parameters are bad or null or can not
* make a connection to datasource.
*/
public void initialize(String jndiName, String userDataBaseTableName, String membershipDataBaseTableName, Debug idRepoDebugLog) throws java.lang.InstantiationException {
useJNDI = true;
//validate input parameters
if (jndiName == null || jndiName.trim().length() == 0 || userDataBaseTableName == null || userDataBaseTableName.trim().length() == 0 || idRepoDebugLog == null || membershipDataBaseTableName == null) {
String msg = "JdbcSimpleUserDao.initialize" + " validation failed to make and make a new instance" + " with paramaters: jndiName=" + jndiName + " userDataBaseTableName=" + userDataBaseTableName + " membershipDataBaseTableName=" + membershipDataBaseTableName + " debug=" + idRepoDebugLog == null ? null : idRepoDebugLog.getName();
if (idRepoDebugLog != null && idRepoDebugLog.messageEnabled()) {
idRepoDebugLog.message(msg);
}
throw new java.lang.InstantiationException(msg);
}
//set class fields to input paramater
debug = idRepoDebugLog;
datasourceName = jndiName.trim();
userTableName = userDataBaseTableName.trim();
//input value for membership table can be empty, but null is not allowed
if (membershipDataBaseTableName != null) {
membershipTableName = membershipDataBaseTableName.trim();
}
//set the datasource class field
try {
Context ctx = new InitialContext();
//java:comp/env requires a resource-ref in web.xml
datasource = (DataSource) ctx.lookup(datasourceName);
} catch (Exception ex) {
String msg = "JdbcSimpleUserDao.getInstance:" + " Not able to initialize the datasource through JNDI" + " for datasourceName=" + datasourceName;
if (debug.errorEnabled()) {
debug.error(msg + " exception =" + ex);
}
//reset to un-initialized state
datasourceName = null;
datasource = null;
userTableName = null;
debug = null;
throw new java.lang.InstantiationException(msg + ex.getMessage());
}
Connection con = null;
try {
//test out and log database info to debug log
con = getConnection();
DatabaseMetaData dbmd = con.getMetaData();
if (debug.messageEnabled()) {
debug.message("JdbcSimpleUserDao.initialize: DB Meta Data:" + " name=" + (dbmd == null ? "Not Available" : dbmd.getUserName()) + " url=" + (dbmd == null ? "Not Available" : dbmd.getURL()));
}
databaseURL = (dbmd == null ? null : dbmd.getURL());
isMySQL = isMySQL(databaseURL);
} catch (Exception ex) {
String msg = "JdbcSimpleUserDao.getInstance:" + " Not able to connect the datasource and get the meta" + " data such as DB url";
if (debug.errorEnabled()) {
debug.error(msg + " exception =" + ex);
}
//reset to un-initialized state
datasourceName = null;
datasource = null;
userTableName = null;
membershipTableName = null;
throw new java.lang.InstantiationException(msg + ex.getMessage());
} finally {
closeConnection(con);
//reset to un-initialized state
if (datasourceName == null) {
debug = null;
}
}
}
use of javax.naming.InitialContext in project ACS by ACS-Community.
the class NamingServiceRemoteDirectory method internalInitialize.
/**
* Obtains root context of the remote directory (CORBA Naming Service).
* @param orb CORBA ORB.
* @param logger logger.
*/
private void internalInitialize(ORB orb, Logger logger) {
logger.info("Connecting to CORBA Naming Service with reference '" + reference + "'...");
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
// set CosNamingFactory
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
// set NS address
env.put(Context.PROVIDER_URL, reference);
// set orb
env.put("java.naming.corba.orb", orb);
try {
context = new InitialContext(env);
logger.info("Connected to CORBA Naming Service with reference '" + reference + "'.");
} catch (Throwable ex) {
//logger.log(Level.INFO, "Failed to connect to CORBA Naming Service with reference '"+reference+"'...", ex);
logger.info("Failed to connect to CORBA Naming Service with reference '" + reference + "'...");
return;
}
}
use of javax.naming.InitialContext in project ACS by ACS-Community.
the class TestJDAL method testListContext.
/**
* Listing different contexts - simple and XML contexts
* We shouldn't see any difference in listing just 'directory' context
* or listing inside a XML data
*/
public void testListContext() throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cosylab.cdb.jdal.JNDIContextFactory");
env.put(Context.PROVIDER_URL, strIOR);
Context context = null;
context = new InitialContext(env);
assertNotNull(context);
// try simple listing
String list = "";
NamingEnumeration ne = context.list("MACI");
while (ne.hasMore()) {
NameClassPair pair = (NameClassPair) ne.nextElement();
list = list + pair.getName() + " ";
}
// same as we did listing
assertTrue(list.indexOf("Containers") != -1);
assertTrue(list.indexOf("Managers") != -1);
// try with a XML
ne = ((Context) (((Context) context.lookup("MACI")).lookup("Managers"))).list("Manager");
list = "";
while (ne.hasMore()) {
NameClassPair pair = (NameClassPair) ne.nextElement();
list = list + pair.getName() + " ";
//System.out.println(pair.getName() + " " + pair.getClassName());
}
// this should be in Manager data
assertTrue(list.indexOf("ServiceComponents") != -1);
}
use of javax.naming.InitialContext in project ACS by ACS-Community.
the class TestJDAL method testLookupContext.
/**
* Test lookup functions. We can just obtain an InitialContext
* and then use it to get any information in the CDB
*/
public void testLookupContext() throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cosylab.cdb.jdal.JNDIContextFactory");
env.put(Context.PROVIDER_URL, strIOR);
Context context = null;
context = new InitialContext(env);
assertNotNull(context);
// try simple lookup
String list = "";
Object ob = context.lookup("MACI");
assertTrue(ob instanceof Context);
// try with a value inside XML
ob = ((Context) (((Context) (((Context) context.lookup("MACI")).lookup("Managers"))).lookup("Manager"))).lookup("ContainerPingInterval");
assertTrue(ob instanceof String);
double contPingInt = Double.parseDouble((String) ob);
assertEquals(2.0, contPingInt);
}
use of javax.naming.InitialContext in project ACS by ACS-Community.
the class ManagerImpl method initializeFederation.
/*****************************************************************************/
/************************* [ Federation methods ] ****************************/
/*****************************************************************************/
/**
* Initialize manager federation.
*/
public void initializeFederation(Hashtable federationDirectoryProperties) throws CoreException {
assert (federationDirectoryProperties != null);
//
// read domain list
//
HashSet givenDomainList = new HashSet();
try {
String domainList = System.getProperty(NAME_DOMAIN_LIST);
if (domainList != null) {
// parse space/comma separated list
StringTokenizer tokenizer = new StringTokenizer(domainList, ", ");
while (tokenizer.hasMoreTokens()) {
String domainName = tokenizer.nextToken();
// do not allow '/' in domain names
if (domainName.indexOf('/') == -1)
givenDomainList.add(domainName);
}
}
if (givenDomainList.size() > 0)
logger.log(Level.INFO, "Using domain list: " + givenDomainList + ".");
} catch (Throwable t) {
logger.log(Level.WARNING, "Failed to parse domain list '" + NAME_DOMAIN_LIST + "' variable, " + t.getMessage(), t);
}
// recovery data consistency check
if (domains.size() != 0 && !domains.equals(givenDomainList)) {
CoreException ie = new CoreException("Given domain list is not consistent with recovery data: " + givenDomainList + " != " + domains + ".");
throw ie;
} else if (domains.size() == 0 && givenDomainList.size() != 0)
domains = givenDomainList;
// no domain to control, no federation
if (domains.size() == 0) {
logger.log(Level.CONFIG, "No domain list given, manager federation disabled.");
return;
}
if (remoteDirectoryComponentReference == null) {
logger.log(Level.WARNING, "No valid local domain naming service reference found, manager federation disabled.");
return;
}
//
// read federation naming service
//
String domainNS = System.getProperty(NAME_DOMAIN_DIRECTORY);
if (domainNS == null) {
logger.log(Level.WARNING, "No federation directory reference given, manager federation disabled.");
return;
}
// set NS address
federationDirectoryProperties.put(Context.PROVIDER_URL, domainNS);
logger.log(Level.INFO, "Connecting to the federation directory with reference '" + domainNS + "'...");
try {
federationDirectory = new InitialContext(federationDirectoryProperties);
} catch (Throwable th) {
logger.log(Level.INFO, "Failed to connect to the federation directory with reference '" + domainNS + "'...", th);
return;
}
logger.log(Level.INFO, "Connected to the federation directory with reference '" + domainNS + "'.");
// register domains
Iterator iter = domains.iterator();
while (iter.hasNext()) {
bind(federationDirectory, dottedToHierarchical(iter.next().toString()), null, remoteDirectoryComponentReference);
}
federationEnabled = true;
logger.log(Level.INFO, "Manager federation enabled.");
// set domain name string (one name or set of names)
if (domains.size() == 1)
domain = domains.iterator().next().toString();
else
domain = domains.toString();
}
Aggregations