Example usage for javax.naming InitialContext lookup

List of usage examples for javax.naming InitialContext lookup

Introduction

In this page you can find the example usage for javax.naming InitialContext lookup.

Prototype

public Object lookup(Name name) throws NamingException 

Source Link

Usage

From source file:com.emc.plants.utils.Util.java

/**
 * Lookup and return an EJB home.//from  w w  w.  j  a va  2  s.co  m
 *
 * @param jndiName jndi name of the EJB
 * @return EJBHome EJB home instance, or null if a naming exception on lookup.
 */
/*    static public EJBHome getEJBHome(String jndiName, Class homeClass) {
EJBHome home = (EJBHome) homeTable.get(jndiName);
if (home == null) {
    try {
        InitialContext ic = getInitialContext();
        if (ic != null) {
            Object obj = ic.lookup(jndiName);
            if (obj != null) {
                home = (EJBHome) PortableRemoteObject.narrow(obj, homeClass);
                if (home != null) {
                    homeTable.put(jndiName, home);
                }
            }
        }
    }
    // Naming Exception will cause a null return.
    catch (NamingException e) {}
}
return home;
    }
    */
static public Object getBean(String jndiName) {
    Object session = null;
    try {
        InitialContext ic = getInitialContext();
        if (ic != null) {
            session = ic.lookup(jndiName);
        }
    }
    // Naming Exception will cause a null return.
    catch (NamingException e) {
        debug("Util.getEJBLocalHome(): Exception: " + e);
    }
    return session;
}

From source file:org.firstopen.singularity.util.JMSUtil.java

public static void deliverMessageToQueue(String host, String queueName, String xml) {
    log.debug("IntegrationMod.deliverMessageToQueue queueName = " + queueName + " and doc = " + xml);
    MessageProducer m_sender = null;//www .  jav a2 s.  c o m
    Session m_session = null;
    Connection connection = null;

    char test = queueName.charAt(0);

    if (test == '/')
        queueName = queueName.substring(1);

    try {

        InitialContext context = JNDIUtil.getInitialContext(host);

        ConnectionFactory qcf = (ConnectionFactory) context.lookup("ConnectionFactory");

        Queue queue = (Queue) context.lookup("queue/" + queueName);

        connection = qcf.createConnection();

        m_session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        m_sender = m_session.createProducer(queue);

        TextMessage message = m_session.createTextMessage();

        log.debug("message value is -> " + xml);

        message.setText(xml);

        m_sender.send(message);

    } catch (Exception e) {
        log.error("IntegrationMod.deliverMessageToQueue() Exception = ", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.error("unable ot close JMS Connection", e);
            }
        }
    }
}

From source file:org.firstopen.singularity.util.JMSUtil.java

public static Queue getQueue(String queueName) throws InfrastructureException {
    InitialContext jndiContext;
    Queue queue;//from   w w w.j  a va2 s  . com
    try {
        jndiContext = JNDIUtil.getInitialContext();

        queue = (Queue) jndiContext.lookup("queue/" + queueName);
    } catch (Exception e) {

        log.error("unsable to get queue connnetion" + queueName, e);
        throw new InfrastructureException("unsable to get queue connnetion" + queueName);
    }
    return queue;
}

From source file:org.josso.jb32.agent.JBossCatalinaRealm.java

/** Lookup the authentication CachePolicy object for a security domain. This
 method first treats the cacheJndiName as a ObjectFactory location that is
 capable of returning CachePolicy instances on a per security domain basis
 by appending a '/security-domain-name' string to the cacheJndiName when
 looking up the CachePolicy for a domain. If this fails then the cacheJndiName
 location is treated as a single CachePolicy for all security domains.
 @deprecated No longer used for JBoss 3.2.6 support
 *//*from w  w  w. ja  v a 2 s  .  co  m*/
private static CachePolicy lookupCachePolicy(String securityDomain) {
    CachePolicy authCache = null;
    String domainCachePath = cacheJndiName + '/' + securityDomain;
    try {
        InitialContext iniCtx = new InitialContext();
        authCache = (CachePolicy) iniCtx.lookup(domainCachePath);
    } catch (Exception e) {
        // Failed, treat the cacheJndiName name as a global CachePolicy binding
        try {
            InitialContext iniCtx = new InitialContext();
            authCache = (CachePolicy) iniCtx.lookup(cacheJndiName);
        } catch (Exception e2) {
            logger.warn("Failed to locate auth CachePolicy at: " + cacheJndiName + " for securityDomain="
                    + securityDomain);
        }
    }
    return authCache;
}

From source file:org.firstopen.singularity.util.JMSUtil.java

public static void terminateQueue(String queueName) throws InfrastructureException {
    InitialContext jndiContext;
    try {/*from  w  w  w  . j ava 2 s  .  c o  m*/
        jndiContext = JNDIUtil.getInitialContext();
        ManagementHome mejbHome = (ManagementHome) jndiContext.lookup("ejb/mgmt/MEJB");
        Management mejb = mejbHome.create();
        ObjectName objectName = new ObjectName("jboss.mq.destination:service=Queue,id=" + queueName);

        objectName = new ObjectName("jboss.mq:service=DestinationManager");
        mejb.invoke(objectName, "destroyQueue", new Object[] { queueName },
                new String[] { String.class.getName() });
    } catch (Exception e) {

        log.error("Unable Terminate Queue: " + queueName, e);
        throw new InfrastructureException("Unable Terminate Queue: " + queueName);
    }

}

From source file:org.firstopen.singularity.util.JMSUtil.java

public static void terminateTopic(String topicName) throws InfrastructureException {
    InitialContext jndiContext;
    try {/*from www  .ja  v a  2 s  .  co m*/
        jndiContext = JNDIUtil.getInitialContext();
        ManagementHome mejbHome = (ManagementHome) jndiContext.lookup("ejb/mgmt/MEJB");
        Management mejb = mejbHome.create();
        ObjectName objectName = new ObjectName("jboss.mq.destination:service=Queue,id=" + topicName);
        // mejb.invoke(objectName, "stop", new Object[]{}, new String[]{});
        objectName = new ObjectName("jboss.mq:service=DestinationManager");
        mejb.invoke(objectName, "destroyTopic", new Object[] { topicName },
                new String[] { String.class.getName() });
    } catch (Exception e) {

        log.error("Unable Terminate Topic: " + topicName, e);
        throw new InfrastructureException("Unable Terminate Topic: " + topicName);
    }

}

From source file:com.cws.esolutions.core.utils.MQUtils.java

/**
 * Gets an MQ message off a specified queue and returns it as an
 * <code>Object</code> to the requestor for further processing.
 *
 * @param connName - The connection name to utilize
 * @param authData - The authentication data to utilize, if required
 * @param responseQueue - The request queue name to put the message on
 * @param timeout - How long to wait for a connection or response
 * @param messageId - The JMS correlation ID of the message the response is associated with
 * @return <code>Object</code> - The serializable data returned by the MQ request
 * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing
 *///from  w ww .  ja  v  a 2s  .  c o  m
public static final synchronized Object getMqMessage(final String connName, final List<String> authData,
        final String responseQueue, final long timeout, final String messageId) throws UtilityException {
    final String methodName = MQUtils.CNAME
            + "getMqMessage(final String connName, final List<String> authData, final String responseQueue, final long timeout, final String messageId) throws UtilityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", connName);
        DEBUGGER.debug("Value: {}", responseQueue);
        DEBUGGER.debug("Value: {}", timeout);
        DEBUGGER.debug("Value: {}", messageId);
    }

    Connection conn = null;
    Session session = null;
    Object response = null;
    Context envContext = null;
    MessageConsumer consumer = null;
    ConnectionFactory connFactory = null;

    try {
        try {
            InitialContext initCtx = new InitialContext();
            envContext = (Context) initCtx.lookup(MQUtils.INIT_CONTEXT);

            connFactory = (ConnectionFactory) envContext.lookup(connName);
        } catch (NamingException nx) {
            // we're probably not in a container
            connFactory = new ActiveMQConnectionFactory(connName);
        }

        if (DEBUG) {
            DEBUGGER.debug("ConnectionFactory: {}", connFactory);
        }

        if (connFactory == null) {
            throw new UtilityException("Unable to create connection factory for provided name");
        }

        // Create a Connection
        conn = connFactory.createConnection(authData.get(0),
                PasswordUtils.decryptText(authData.get(1), authData.get(2), authData.get(3),
                        Integer.parseInt(authData.get(4)), Integer.parseInt(authData.get(5)), authData.get(6),
                        authData.get(7), authData.get(8)));
        conn.start();

        if (DEBUG) {
            DEBUGGER.debug("Connection: {}", conn);
        }

        // Create a Session
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        if (DEBUG) {
            DEBUGGER.debug("Session: {}", session);
        }

        if (envContext != null) {
            try {
                consumer = session.createConsumer((Destination) envContext.lookup(responseQueue),
                        "JMSCorrelationID='" + messageId + "'");
            } catch (NamingException nx) {
                throw new UtilityException(nx.getMessage(), nx);
            }
        } else {
            Destination destination = session.createQueue(responseQueue);

            if (DEBUG) {
                DEBUGGER.debug("Destination: {}", destination);
            }

            consumer = session.createConsumer(destination, "JMSCorrelationID='" + messageId + "'");
        }

        if (DEBUG) {
            DEBUGGER.debug("MessageConsumer: {}", consumer);
        }

        ObjectMessage message = (ObjectMessage) consumer.receive(timeout);

        if (DEBUG) {
            DEBUGGER.debug("ObjectMessage: {}", message);
        }

        if (message == null) {
            throw new UtilityException("Failed to retrieve message within the timeout specified.");
        }

        response = message.getObject();
        message.acknowledge();

        if (DEBUG) {
            DEBUGGER.debug("Object: {}", response);
        }
    } catch (JMSException jx) {
        throw new UtilityException(jx.getMessage(), jx);
    } finally {
        try {
            // Clean up
            if (!(session == null)) {
                session.close();
            }

            if (!(conn == null)) {
                conn.close();
                conn.stop();
            }
        } catch (JMSException jx) {
            ERROR_RECORDER.error(jx.getMessage(), jx);
        }
    }

    return response;
}

From source file:org.rhq.plugins.jbossas5.factory.ProfileServiceFactory.java

/**
 * Returns the profile service from the JBoss server through JNDI
 *
 * @return ProfileService//www .j a  v a2s. c  o m
 */
@NotNull
public static ProfileService getProfileService() {
    if (profileService == null) {
        InitialContext initialContext;
        try {
            initialContext = new InitialContext();
        } catch (NamingException e) {
            throw new RuntimeException("Failed to create JNDI InitialContext.", e);
        }
        try {
            profileService = (ProfileService) initialContext.lookup(PROFILE_SERVICE_JNDI_NAME);
        } catch (NamingException e) {
            throw new RuntimeException(
                    "Failed to lookup JNDI name '" + PROFILE_SERVICE_JNDI_NAME + "' from InitialContext.", e);
        }
    }
    return profileService;
}

From source file:org.firstopen.singularity.util.JMSUtil.java

public static Connection getQueueConnection() throws InfrastructureException {
    InitialContext jndiContext;
    Connection connection;//from   w  w  w.  j a va2s.c  om
    try {
        jndiContext = JNDIUtil.getInitialContext();
        ConnectionFactory qcf = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
        connection = qcf.createConnection();
    } catch (Exception e) {

        log.error("unsable to get queue connnetion", e);
        throw new InfrastructureException("unsable to get queue connnetion");
    }

    return connection;
}

From source file:org.hyperic.hq.plugin.jboss.JBossUtil.java

public static MBeanServerConnection getMBeanServerConnection(Properties config)
        throws NamingException, RemoteException {
    MBeanServerConnection adaptor;

    Properties props = new Properties();

    for (int i = 0; i < NAMING_PROPS.length; i++) {
        props.setProperty(NAMING_PROPS[i][0], NAMING_PROPS[i][1]);
    }//from  w  w w  .  j  a v  a2 s  . com

    props.putAll(config);

    if (props.getProperty(Context.SECURITY_PRINCIPAL) != null) {
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    }

    InitialContext ctx = new InitialContext(props);

    try {
        Object o = ctx.lookup(props.getProperty(PROP_NAMING_CONNECTOR));
        log.debug("=> " + Arrays.asList(o.getClass().getInterfaces()));
        adaptor = (MBeanServerConnection) o;
    } finally {
        ctx.close();
    }

    return adaptor;
}