001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.web.config;
018
019import java.util.Collection;
020
021import javax.jms.ConnectionFactory;
022import javax.management.remote.JMXServiceURL;
023import javax.naming.InitialContext;
024import javax.naming.NameNotFoundException;
025import javax.naming.NamingException;
026
027/**
028 * Configuration based on JNDI values.
029 *
030 * 
031 */
032public class JNDIConfiguration extends AbstractConfiguration {
033
034    private static final String JNDI_JMS_CONNECTION_FACTORY = "java:comp/env/jms/connectionFactory";
035    private static final String JNDI_JMS_URL = "java:comp/env/jms/url";
036    private static final String JNDI_JMS_USER = "java:comp/env/jms/user";
037    private static final String JNDI_JMS_PASSWORD = "java:comp/env/jms/password";
038
039    private static final String JNDI_JMX_URL = "java:comp/env/jmx/url";
040    private static final String JNDI_JMX_USER = "java:comp/env/jmx/user";
041    private static final String JNDI_JMX_PASSWORD = "java:comp/env/jmx/password";
042
043    private InitialContext context;
044
045    public JNDIConfiguration() throws NamingException {
046        this.context = new InitialContext();
047    }
048
049    public JNDIConfiguration(InitialContext context) {
050        this.context = context;
051    }
052
053    public ConnectionFactory getConnectionFactory() {
054        try {
055            ConnectionFactory connectionFactory = (ConnectionFactory) this.context
056                    .lookup(JNDI_JMS_CONNECTION_FACTORY);
057            return connectionFactory;
058        } catch (NameNotFoundException e) {
059            // try to find an url
060        } catch (NamingException e) {
061            throw new RuntimeException(e);
062        }
063
064        try {
065            String jmsUrl = (String) this.context.lookup(JNDI_JMS_URL);
066            if (jmsUrl == null) {
067                throw new IllegalArgumentException(
068                        "A JMS-url must be specified (system property "
069                                + JNDI_JMS_URL);
070            }
071
072            String jmsUser = getJndiString(JNDI_JMS_USER);
073            String jmsPassword = getJndiString(JNDI_JMS_PASSWORD);
074            return makeConnectionFactory(jmsUrl, jmsUser, jmsPassword);
075        } catch (NameNotFoundException e) {
076            throw new IllegalArgumentException(
077                    "Neither a ConnectionFactory nor a JMS-url were specified");
078        } catch (NamingException e) {
079            throw new RuntimeException(e);
080        }
081    }
082
083    protected String getJndiString(String name) {
084        try {
085            return (String) this.context.lookup(name);
086        } catch (NamingException e) {
087            return null;
088        }
089    }
090
091    public Collection<JMXServiceURL> getJmxUrls() {
092        String jmxUrls = getJndiString(JNDI_JMX_URL);
093        return makeJmxUrls(jmxUrls);
094    }
095
096    public String getJmxPassword() {
097        return getJndiString(JNDI_JMX_PASSWORD);
098    }
099
100    public String getJmxUser() {
101        return getJndiString(JNDI_JMX_USER);
102    }
103
104}