Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/gbean/ActiveMQContainerGBean.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * 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   **/
18  package org.activemq.gbean;
19  
20  import java.util.Properties;
21  import javax.jms.JMSException;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.geronimo.gbean.GBeanInfo;
25  import org.apache.geronimo.gbean.GBeanInfoBuilder;
26  import org.apache.geronimo.gbean.GBeanLifecycle;
27  import org.activemq.broker.BrokerAdmin;
28  import org.activemq.broker.BrokerContainer;
29  import org.activemq.broker.BrokerContext;
30  import org.activemq.broker.impl.BrokerContainerImpl;
31  import org.activemq.security.jassjacc.JassJaccSecurityAdapter;
32  import org.activemq.security.jassjacc.PropertiesConfigLoader;
33  import org.activemq.store.PersistenceAdapter;
34  
35  /**
36   * Default implementation of the ActiveMQ Message Server
37   *
38   * @version $Revision: 1.1.1.1 $
39   */
40  public class ActiveMQContainerGBean implements GBeanLifecycle, ActiveMQContainer {
41  
42      private Log log = LogFactory.getLog(getClass().getName());
43  
44      private final String brokerName;
45  
46      private BrokerContext context = BrokerContext.getInstance();
47      private BrokerContainer container;
48  
49      private final PersistenceAdapter persistenceAdapter;
50    private final String jaasConfiguration;
51    private final Properties securityRoles;
52  
53      //default constructor for use as gbean endpoint.
54      public ActiveMQContainerGBean() {
55          brokerName = null;
56          jaasConfiguration = null;
57          securityRoles = null;
58          persistenceAdapter=null;
59      }
60    
61      public ActiveMQContainerGBean(String brokerName, PersistenceAdapter persistenceAdapter,  String jaasConfiguration, Properties securityRoles) {
62      
63          assert brokerName != null;
64      assert persistenceAdapter != null;
65      
66          this.brokerName = brokerName;
67          this.jaasConfiguration=jaasConfiguration;
68      this.persistenceAdapter = persistenceAdapter;
69          this.securityRoles = securityRoles;
70      }
71  
72      public synchronized BrokerContainer getBrokerContainer() {
73          return container;
74      }
75  
76      /**
77       * @see org.activemq.gbean.ActiveMQContainer#getBrokerAdmin()
78       */
79      public BrokerAdmin getBrokerAdmin() {
80          return container.getBroker().getBrokerAdmin();
81      }
82  
83      public synchronized void doStart() throws Exception {
84        ClassLoader old = Thread.currentThread().getContextClassLoader();
85        Thread.currentThread().setContextClassLoader(ActiveMQContainerGBean.class.getClassLoader());
86        try {
87            if (container == null) {
88                container = createContainer();
89                container.start();
90            }
91        } finally {
92            Thread.currentThread().setContextClassLoader(old);
93        }
94      }
95  
96      public synchronized void doStop() throws Exception {
97          if (container != null) {
98              BrokerContainer temp = container;
99              container = null;
100             temp.stop();
101         }
102     }
103 
104     public synchronized void doFail() {
105         if (container != null) {
106             BrokerContainer temp = container;
107             container = null;
108             try {
109                 temp.stop();
110             }
111             catch (JMSException e) {
112                 log.info("Caught while closing due to failure: " + e, e);
113             }
114         }
115     }
116 
117     protected BrokerContainer createContainer() throws Exception {
118       BrokerContainerImpl answer = new BrokerContainerImpl(brokerName, context);
119       answer.setPersistenceAdapter( persistenceAdapter );
120       if( jaasConfiguration != null ) {
121         answer.setSecurityAdapter(new JassJaccSecurityAdapter(jaasConfiguration));
122       }
123       if( securityRoles != null ) {
124         // Install JACC configuration.
125         PropertiesConfigLoader loader = new PropertiesConfigLoader(brokerName, securityRoles);
126         loader.installSecurity();
127       }
128       return answer;
129     }
130 
131     public static final GBeanInfo GBEAN_INFO;
132 
133     static {
134         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ActiveMQ Message Broker", ActiveMQContainerGBean.class, "JMSServer");
135         infoFactory.addAttribute("brokerName", String.class, true);
136         infoFactory.addReference("persistenceAdapter", PersistenceAdapter.class);
137         infoFactory.addAttribute("jaasConfiguration", String.class, true);
138         infoFactory.addAttribute("securityRoles", Properties.class, true);
139         infoFactory.addInterface(ActiveMQContainer.class);
140         infoFactory.setConstructor(new String[]{"brokerName", "persistenceAdapter", "jaasConfiguration", "securityRoles"});
141         GBEAN_INFO = infoFactory.getBeanInfo();
142     }
143 
144     public static GBeanInfo getGBeanInfo() {
145         return GBEAN_INFO;
146     }
147 
148   /**
149    * @return Returns the brokerName.
150    */
151   public String getBrokerName() {
152     return brokerName;
153   }
154   
155   /**
156    * @return Returns the jassConfiguration.
157    */
158   public String getJaasConfiguration() {
159     return jaasConfiguration;
160   }
161   
162   /**
163    * @return Returns the securityRoles.
164    */
165   public Properties getSecurityRoles() {
166     return securityRoles;
167   }
168   
169 }