1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.activemq.web.config; 18 19 import java.util.Collection; 20 21 import javax.jms.ConnectionFactory; 22 import javax.management.remote.JMXServiceURL; 23 import javax.naming.InitialContext; 24 import javax.naming.NameNotFoundException; 25 import javax.naming.NamingException; 26 27 /** 28 * Configuration based on JNDI values. 29 * 30 * @version $Revision: $ 31 */ 32 public class JNDIConfiguration extends AbstractConfiguration { 33 34 private static final String JNDI_JMS_CONNECTION_FACTORY = "java:comp/env/jms/connectionFactory"; 35 private static final String JNDI_JMS_URL = "java:comp/env/jms/url"; 36 private static final String JNDI_JMS_USER = "java:comp/env/jms/user"; 37 private static final String JNDI_JMS_PASSWORD = "java:comp/env/jms/password"; 38 39 private static final String JNDI_JMX_URL = "java:comp/env/jmx/url"; 40 private static final String JNDI_JMX_USER = "java:comp/env/jmx/user"; 41 private static final String JNDI_JMX_PASSWORD = "java:comp/env/jmx/password"; 42 43 private InitialContext context; 44 45 public JNDIConfiguration() throws NamingException { 46 this.context = new InitialContext(); 47 } 48 49 public JNDIConfiguration(InitialContext context) { 50 this.context = context; 51 } 52 53 public ConnectionFactory getConnectionFactory() { 54 try { 55 ConnectionFactory connectionFactory = (ConnectionFactory) this.context 56 .lookup(JNDI_JMS_CONNECTION_FACTORY); 57 return connectionFactory; 58 } catch (NameNotFoundException e) { 59 // try to find an url 60 } catch (NamingException e) { 61 throw new RuntimeException(e); 62 } 63 64 try { 65 String jmsUrl = (String) this.context.lookup(JNDI_JMS_URL); 66 if (jmsUrl == null) { 67 throw new IllegalArgumentException( 68 "A JMS-url must be specified (system property " 69 + JNDI_JMS_URL); 70 } 71 72 String jmsUser = getJndiString(JNDI_JMS_USER); 73 String jmsPassword = getJndiString(JNDI_JMS_PASSWORD); 74 return makeConnectionFactory(jmsUrl, jmsUser, jmsPassword); 75 } catch (NameNotFoundException e) { 76 throw new IllegalArgumentException( 77 "Neither a ConnectionFactory nor a JMS-url were specified"); 78 } catch (NamingException e) { 79 throw new RuntimeException(e); 80 } 81 } 82 83 protected String getJndiString(String name) { 84 try { 85 return (String) this.context.lookup(name); 86 } catch (NamingException e) { 87 return null; 88 } 89 } 90 91 public Collection<JMXServiceURL> getJmxUrls() { 92 String jmxUrls = getJndiString(JNDI_JMX_URL); 93 return makeJmxUrls(jmxUrls); 94 } 95 96 public String getJmxPassword() { 97 return getJndiString(JNDI_JMX_PASSWORD); 98 } 99 100 public String getJmxUser() { 101 return getJndiString(JNDI_JMX_USER); 102 } 103 104 }