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     */
017    package org.apache.activemq.web.config;
018    
019    import org.osgi.framework.BundleContext;
020    import org.osgi.framework.Constants;
021    import org.osgi.framework.FrameworkUtil;
022    import org.osgi.framework.ServiceRegistration;
023    import org.osgi.service.cm.ConfigurationException;
024    import org.osgi.service.cm.ManagedService;
025    import sun.util.LocaleServiceProviderPool;
026    
027    import javax.jms.ConnectionFactory;
028    import javax.management.remote.JMXServiceURL;
029    import java.util.Collection;
030    import java.util.Dictionary;
031    import java.util.Hashtable;
032    
033    public class OsgiConfiguration extends AbstractConfiguration implements ManagedService {
034    
035        private ServiceRegistration service;
036    
037        private String jmxUrl = "service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root";
038        private String jmxUser = "karaf";
039        private String jmxPassword = "karaf";
040    
041        private String jmsUrl = "tcp://localhost:61616";
042        private String jmsUser = "karaf";
043        private String jmsPassword = "karaf";
044    
045        public OsgiConfiguration() {
046    
047            BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
048            Dictionary properties = new Hashtable();
049            properties.put(Constants.SERVICE_PID, "org.apache.activemq.webconsole");
050            service = context.registerService(ManagedService.class.getName(),
051                this, properties);
052    
053        }
054    
055        @Override
056        public String getJmxPassword() {
057            return jmxPassword;
058        }
059    
060        @Override
061        public Collection<JMXServiceURL> getJmxUrls() {
062            return makeJmxUrls(jmxUrl);
063        }
064    
065        @Override
066        public String getJmxUser() {
067            return jmxUser;
068        }
069    
070        @Override
071        public ConnectionFactory getConnectionFactory() {
072            return makeConnectionFactory(jmsUrl, jmsUser, jmsPassword);
073        }
074    
075        @Override
076        public void updated(Dictionary dictionary) throws ConfigurationException {
077            if (dictionary != null) {
078                jmxUrl = (String) dictionary.get(SystemPropertiesConfiguration.PROPERTY_JMX_URL);
079                if (jmxUrl == null) {
080                    throw new IllegalArgumentException("A JMS-url must be specified (system property " + SystemPropertiesConfiguration.PROPERTY_JMX_URL);
081                }
082                jmxUser = (String) dictionary.get(SystemPropertiesConfiguration.PROPERTY_JMX_USER);
083                jmxPassword = (String) dictionary.get(SystemPropertiesConfiguration.PROPERTY_JMX_PASSWORD);
084                jmsUrl = (String) dictionary.get(SystemPropertiesConfiguration.PROPERTY_JMS_URL);
085                jmsUser = (String) dictionary.get(SystemPropertiesConfiguration.PROPERTY_JMS_USER);
086                jmsPassword = (String) dictionary.get(SystemPropertiesConfiguration.PROPERTY_JMS_PASSWORD);
087            }
088        }
089    }