List of usage examples for javax.naming InitialContext lookup
public Object lookup(Name name) throws NamingException
From source file:org.vulpe.commons.util.VulpeEmailUtil.java
/** * Send mail to recipients by Web Service. * * @param recipients/*w w w . j av a2 s . c o m*/ * * @param subject * * @param body * * @param mailerService * * @throws VulpeSystemException * exception */ public static void sendMailByService(final String[] recipients, final String subject, final String body, final String mailerService) { try { final ResourceBundle bundle = ResourceBundle.getBundle("mail"); String mailFrom = ""; if (bundle.containsKey("mail.from")) { mailFrom = bundle.getString("mail.from"); } final InitialContext initialContext = new InitialContext(); final Session session = (Session) initialContext.lookup(mailerService); final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(mailFrom)); for (String recipient : recipients) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); } // msg.setRecipient(Message.RecipientType.TO, new // InternetAddress(to)); message.setSubject(subject); message.setText(body); Transport.send(message); } catch (Exception e) { LOG.error(e.getMessage()); } }
From source file:com.glaf.core.jdbc.DBConnectionFactory.java
public static boolean checkConnection(String systemName) { if (systemName == null) { throw new RuntimeException("systemName is required."); }/*from ww w . jav a 2s.co m*/ logger.debug("systemName:" + systemName); Connection connection = null; try { Properties props = DBConfiguration.getDataSourcePropertiesByName(systemName); logger.debug("props:" + props); if (props != null) { if (StringUtils.isNotEmpty(props.getProperty(DBConfiguration.JDBC_DATASOURCE))) { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup(props.getProperty(DBConfiguration.JDBC_DATASOURCE)); connection = ds.getConnection(); } else { ConnectionProvider provider = ConnectionProviderFactory.createProvider(systemName); if (provider != null) { connection = provider.getConnection(); } } } else { // DataSource ds = ContextFactory.getBean("dataSource"); // connection = ds.getConnection(); } if (connection != null) { ConnectionThreadHolder.addConnection(connection); } if (connection != null) { return true; } } catch (Exception ex) { logger.error(ex); ex.printStackTrace(); } finally { JdbcUtils.close(connection); } return false; }
From source file:org.apache.stratos.status.monitor.agent.clients.service.CEPServerClient.java
private static void executeService() throws SQLException { int serviceID = MySQLConnectionInitializer.getServiceID(StatusMonitorConstants.CEP); AuthConfigBean authConfigBean = StatusMonitorConfigurationBuilder.getAuthConfigBean(); String userName = authConfigBean.getUserName(); tcpUserName = userName.replace('@', '!'); //check whether login success if (ServiceLoginClient.loginChecker(StatusMonitorConstants.CEP_HOST, serviceID)) { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, StatusMonitorAgentConstants.QPID_ICF); properties.put(StatusMonitorAgentConstants.CF_NAME_PREFIX + StatusMonitorAgentConstants.CF_NAME, getTCPConnectionURL(tcpUserName, authConfigBean.getPassword())); InitialContext ctx; try {// www .jav a 2s .c o m ctx = new InitialContext(properties); // Lookup connection factory QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx .lookup(StatusMonitorAgentConstants.CF_NAME); QueueConnection queueConnection = connFactory.createQueueConnection(); queueConnection.start(); QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); // Send message Queue queue = queueSession.createQueue( StatusMonitorAgentConstants.QUEUE_NAME_CEP + ";{create:always, node:{durable: True}}"); // create the message to send TextMessage textMessage = queueSession.createTextMessage("Test Message Hello"); javax.jms.QueueSender queueSender = queueSession.createSender(queue); queueSender.setTimeToLive(100000000); QueueReceiver queueReceiver = queueSession.createReceiver(queue); queueSender.send(textMessage); TextMessage message = (TextMessage) queueReceiver.receiveNoWait(); if (log.isDebugEnabled()) { log.debug("Message in the execute() of CEPServer Client: " + message.getText()); } if (message.getText().equals("Test Message Hello")) { MySQLConnector.insertStats(serviceID, true); MySQLConnector.insertState(serviceID, true, ""); } else { MySQLConnector.insertStats(serviceID, false); MySQLConnector.insertState(serviceID, false, "Send or retrieve messages failed"); } queueSender.close(); queueSession.close(); queueConnection.close(); } catch (JMSException e) { MySQLConnector.insertStats(serviceID, false); MySQLConnector.insertState(serviceID, false, e.getMessage()); String msg = "JMS Exception in inserting stats into the DB for the CEPServerClient"; log.warn(msg, e); } catch (NamingException e) { MySQLConnector.insertStats(serviceID, false); MySQLConnector.insertState(serviceID, false, e.getMessage()); String msg = "Naming Exception in inserting stats into the DB for the CEPServerClient"; log.warn(msg, e); } } }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static Queue queueExists(String queueName) throws InfrastructureException { Queue queue = null;/*from ww w.j a va 2s.co m*/ try { InitialContext jndiContext = JNDIUtil.getInitialContext(); queue = (Queue) jndiContext.lookup("queue/" + queueName); } catch (Exception x) { throw new InfrastructureException(x); } return queue; }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static Queue topicExists(String topicName) throws InfrastructureException { Queue queue = null;/*from ww w. j a v a 2s . c om*/ try { InitialContext jndiContext = JNDIUtil.getInitialContext(); queue = (Queue) jndiContext.lookup("queue/" + topicName); } catch (Exception x) { throw new InfrastructureException(x); } return queue; }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static Connection registerListenerOnQueue(MessageListener listener, String queueName) throws Exception { InitialContext jndiContext = JNDIUtil.getInitialContext(); Queue queue = (Queue) jndiContext.lookup("queue/" + queueName); ConnectionFactory qcf = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); Connection connection = qcf.createConnection(); Session m_session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer m_receiver = m_session.createConsumer(queue); m_receiver.setMessageListener(listener); return connection; }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static Queue createTopic(String topicName) throws InfrastructureException { Queue queue = null;//w w w . ja va 2 s.c o m InitialContext jndiContext = null; try { queue = (Queue) jndiContext.lookup("topic/" + topicName); } catch (Exception e) { /* * try to create the queue */ try { jndiContext = JNDIUtil.getInitialContext(); ManagementHome mejbHome = (ManagementHome) jndiContext.lookup("ejb/mgmt/MEJB"); Management mejb = mejbHome.create(); ObjectName objectName = new ObjectName("jboss.mq:service=DestinationManager"); mejb.invoke(objectName, "createTopic", new Object[] { topicName, "topic/" + topicName }, new String[] { String.class.getName(), String.class.getName() }); queue = queueExists(topicName); } catch (Exception e1) { log.error("unable to create queue/" + topicName, e1); throw new InfrastructureException(); } } return queue; }
From source file:org.eclipse.ecr.runtime.transaction.TransactionHelper.java
/** * Looks up the User Transaction in JNDI. * * @return the User Transaction/*from w w w. j av a2s .c o m*/ * @throws NamingException if not found */ public static UserTransaction lookupUserTransaction() throws NamingException { InitialContext context = new InitialContext(); int i = 0; for (String name : UT_NAMES) { try { UserTransaction userTransaction = (UserTransaction) context.lookup(name); if (userTransaction != null) { if (i != 0) { // put successful name first for next time UT_NAMES[i] = UT_NAMES[0]; UT_NAMES[0] = name; } return userTransaction; } } catch (NamingException e) { // try next one } i++; } throw new NamingException("UserTransaction not found in JNDI"); }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static String listMessageCounter(String queueName) throws Exception { InitialContext 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); return (String) mejb.invoke(objectName, "listMessageCounter", new Object[] {}, new String[] {}); }
From source file:org.eclipse.ecr.runtime.transaction.TransactionHelper.java
/** * Looks up the TransactionManager in JNDI. * * @return the TransactionManager/*w w w. ja v a2s . c om*/ * @throws NamingException if not found */ public static TransactionManager lookupTransactionManager() throws NamingException { InitialContext context = new InitialContext(); int i = 0; for (String name : TM_NAMES) { try { TransactionManager transactionManager = (TransactionManager) context.lookup(name); if (transactionManager != null) { if (i != 0) { // put successful name first for next time TM_NAMES[i] = TM_NAMES[0]; TM_NAMES[0] = name; } return transactionManager; } } catch (NamingException e) { // try next one } i++; } throw new NamingException("TransactionManager not found in JNDI"); }