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.net.MalformedURLException;
020import java.util.ArrayList;
021import java.util.Collection;
022
023import javax.jms.ConnectionFactory;
024import javax.management.remote.JMXServiceURL;
025
026import org.apache.activemq.ActiveMQConnectionFactory;
027
028/**
029 * Base class for configurations.
030 * 
031 * 
032 */
033public abstract class AbstractConfiguration implements WebConsoleConfiguration {
034
035        public ConnectionFactory getConnectionFactory() {
036                return null;
037        }
038
039        public String getJmxPassword() {
040                return null;
041        }
042
043        public Collection<JMXServiceURL> getJmxUrls() {
044                return null;
045        }
046
047        public String getJmxUser() {
048                return null;
049        }
050
051        /**
052         * Creates the ActiveMQ-ConnectionFactory.
053         * 
054         * @param jmsUrl
055         *            not <code>null</code>
056         * @param jmsUser
057         *            <code>null</code> if no authentication
058         * @param jmsPassword
059         *            <code>null</code> is ok
060         * @return not <code>null</code>
061         */
062        protected ConnectionFactory makeConnectionFactory(String jmsUrl, String jmsUser,
063                        String jmsPassword) {
064                                if (jmsUser != null && jmsUser.length() > 0)
065                                        return new ActiveMQConnectionFactory(jmsUser, jmsPassword, jmsUrl);
066                                else
067                                        return new ActiveMQConnectionFactory(jmsUrl);
068                        }
069
070        /**
071         * Splits the JMX-Url string into a series of JMSServiceURLs.
072         * 
073         * @param jmxUrls
074         *            the JMX-url, multiple URLs are separated by commas.
075         * @return not <code>null</code>, contains at least one element.
076         */
077        protected Collection<JMXServiceURL> makeJmxUrls(String jmxUrls) {
078                String[] urls = jmxUrls.split(",");
079                if (urls == null || urls.length == 0) {
080                        urls = new String[] { jmxUrls };
081                }
082        
083                try {
084                        Collection<JMXServiceURL> result = new ArrayList<JMXServiceURL>(
085                                        jmxUrls.length());
086                        for (String url : urls) {
087                                result.add(new JMXServiceURL(url));
088                        }
089                        return result;
090                } catch (MalformedURLException e) {
091                        throw new IllegalArgumentException("Invalid JMX-url", e);
092                }
093        }
094
095}