001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.activemq.gbean;
019    
020    import java.util.Properties;
021    import javax.jms.JMSException;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.geronimo.gbean.GBeanInfo;
025    import org.apache.geronimo.gbean.GBeanInfoBuilder;
026    import org.apache.geronimo.gbean.GBeanLifecycle;
027    import org.apache.geronimo.management.geronimo.JMSManager;
028    import org.apache.geronimo.management.geronimo.NetworkConnector;
029    import org.activemq.broker.BrokerAdmin;
030    import org.activemq.broker.BrokerContainer;
031    import org.activemq.broker.BrokerContext;
032    import org.activemq.broker.impl.BrokerContainerImpl;
033    import org.activemq.security.jassjacc.JassJaccSecurityAdapter;
034    import org.activemq.security.jassjacc.PropertiesConfigLoader;
035    import org.activemq.store.PersistenceAdapter;
036    
037    /**
038     * Default implementation of the ActiveMQ Message Server
039     *
040     * @version $Revision: 1.1.1.1 $
041     */
042    public class ActiveMQContainerGBean implements GBeanLifecycle, ActiveMQContainer {
043    
044        private Log log = LogFactory.getLog(getClass().getName());
045    
046        private final String brokerName;
047        private final String objectName;
048    
049        private BrokerContext context = BrokerContext.getInstance();
050        private BrokerContainer container;
051    
052        private final PersistenceAdapter persistenceAdapter;
053            private final String jaasConfiguration;
054            private final Properties securityRoles;
055        private final JMSManager manager;
056    
057        //default constructor for use as gbean endpoint.
058        public ActiveMQContainerGBean() {
059            brokerName = null;
060            jaasConfiguration = null;
061            securityRoles = null;
062            persistenceAdapter=null;
063            objectName = null;
064            manager = null;
065        }
066            
067        public ActiveMQContainerGBean(String brokerName, PersistenceAdapter persistenceAdapter,  String jaasConfiguration, Properties securityRoles, String objectName, JMSManager manager) {
068                    
069            assert brokerName != null;
070                    assert persistenceAdapter != null;
071                    
072            this.brokerName = brokerName;
073            this.jaasConfiguration=jaasConfiguration;
074                    this.persistenceAdapter = persistenceAdapter;
075            this.securityRoles = securityRoles;
076            this.objectName = objectName;
077            this.manager = manager;
078        }
079    
080        public synchronized BrokerContainer getBrokerContainer() {
081            return container;
082        }
083    
084        public String getObjectName() {
085            return objectName;
086        }
087    
088        public boolean isStateManageable() {
089            return true;
090        }
091    
092        public boolean isStatisticsProvider() {
093            return false; // todo: return true once stats are integrated
094        }
095    
096        public boolean isEventProvider() {
097            return true;
098        }
099    
100        public NetworkConnector[] getConnectors() {
101            return manager.getConnectorsForContainer(this);
102        }
103    
104        public NetworkConnector[] getConnectors(String protocol) {
105            return manager.getConnectorsForContainer(this, protocol);
106        }
107    
108        /**
109         * @see org.activemq.gbean.ActiveMQContainer#getBrokerAdmin()
110         */
111        public BrokerAdmin getBrokerAdmin() {
112            return container.getBroker().getBrokerAdmin();
113        }
114    
115        public synchronized void doStart() throws Exception {
116            ClassLoader old = Thread.currentThread().getContextClassLoader();
117            Thread.currentThread().setContextClassLoader(ActiveMQContainerGBean.class.getClassLoader());
118            try {
119                    if (container == null) {
120                        container = createContainer();
121                        container.start();
122                    }
123            } finally {
124                    Thread.currentThread().setContextClassLoader(old);
125            }
126        }
127    
128        public synchronized void doStop() throws Exception {
129            if (container != null) {
130                BrokerContainer temp = container;
131                container = null;
132                temp.stop();
133            }
134        }
135    
136        public synchronized void doFail() {
137            if (container != null) {
138                BrokerContainer temp = container;
139                container = null;
140                try {
141                    temp.stop();
142                }
143                catch (JMSException e) {
144                    log.info("Caught while closing due to failure: " + e, e);
145                }
146            }
147        }
148    
149        protected BrokerContainer createContainer() throws Exception {
150            BrokerContainerImpl answer = new BrokerContainerImpl(brokerName, context);
151            answer.setPersistenceAdapter( persistenceAdapter );
152            if( jaasConfiguration != null ) {
153                    answer.setSecurityAdapter(new JassJaccSecurityAdapter(jaasConfiguration));
154            }
155            if( securityRoles != null ) {
156                    // Install JACC configuration.
157                    PropertiesConfigLoader loader = new PropertiesConfigLoader(brokerName, securityRoles);
158                    loader.installSecurity();
159            }
160            return answer;
161        }
162    
163        public static final GBeanInfo GBEAN_INFO;
164    
165        static {
166            GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ActiveMQ Message Broker", ActiveMQContainerGBean.class, "JMSServer");
167            infoFactory.addAttribute("brokerName", String.class, true);
168            infoFactory.addReference("persistenceAdapter", PersistenceAdapter.class);
169            infoFactory.addReference("JMSManager", JMSManager.class);
170            infoFactory.addAttribute("jaasConfiguration", String.class, true);
171            infoFactory.addAttribute("securityRoles", Properties.class, true);
172            infoFactory.addAttribute("objectName", String.class, false);
173            infoFactory.addInterface(ActiveMQContainer.class);
174            infoFactory.setConstructor(new String[]{"brokerName", "persistenceAdapter", "jaasConfiguration", "securityRoles", "objectName", "JMSManager"});
175            GBEAN_INFO = infoFactory.getBeanInfo();
176        }
177    
178        public static GBeanInfo getGBeanInfo() {
179            return GBEAN_INFO;
180        }
181    
182            /**
183             * @return Returns the brokerName.
184             */
185            public String getBrokerName() {
186                    return brokerName;
187            }
188            
189            /**
190             * @return Returns the jassConfiguration.
191             */
192            public String getJaasConfiguration() {
193                    return jaasConfiguration;
194            }
195            
196            /**
197             * @return Returns the securityRoles.
198             */
199            public Properties getSecurityRoles() {
200                    return securityRoles;
201            }
202            
203    }