Search in sources :

Example 66 with InitialContext

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;
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) InitialContext(javax.naming.InitialContext) IdRepoUnsupportedOpException(com.sun.identity.idm.IdRepoUnsupportedOpException) SQLException(java.sql.SQLException)

Example 67 with InitialContext

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;
    }
}
Also used : Hashtable(java.util.Hashtable) InitialContext(javax.naming.InitialContext)

Example 68 with InitialContext

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);
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) NamingEnumeration(javax.naming.NamingEnumeration) InitialContext(javax.naming.InitialContext)

Example 69 with InitialContext

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);
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Hashtable(java.util.Hashtable) InitialContext(javax.naming.InitialContext)

Example 70 with InitialContext

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();
}
Also used : StringTokenizer(java.util.StringTokenizer) CoreException(com.cosylab.acs.maci.CoreException) Iterator(java.util.Iterator) InitialContext(javax.naming.InitialContext) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Aggregations

InitialContext (javax.naming.InitialContext)1068 Test (org.junit.Test)325 EJBException (javax.ejb.EJBException)228 Properties (java.util.Properties)213 Context (javax.naming.Context)194 RemoteException (java.rmi.RemoteException)173 TestFailureException (org.apache.openejb.test.TestFailureException)172 NamingException (javax.naming.NamingException)168 AssertionFailedError (junit.framework.AssertionFailedError)168 JMSException (javax.jms.JMSException)167 RemoveException (javax.ejb.RemoveException)66 CreateException (javax.ejb.CreateException)57 DataSource (javax.sql.DataSource)55 Hashtable (java.util.Hashtable)54 Assembler (org.apache.openejb.assembler.classic.Assembler)47 EjbJar (org.apache.openejb.jee.EjbJar)41 NameNotFoundException (javax.naming.NameNotFoundException)40 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)38 Connection (java.sql.Connection)37 StatelessBean (org.apache.openejb.jee.StatelessBean)30