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.broker.jmx;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.lang.reflect.InvocationTargetException;
022    import java.lang.reflect.Method;
023    import java.net.URI;
024    import java.net.URL;
025    import java.util.concurrent.atomic.AtomicInteger;
026    
027    import javax.management.ObjectName;
028    
029    import org.apache.activemq.broker.Broker;
030    import org.apache.activemq.broker.BrokerService;
031    import org.apache.activemq.broker.ConnectionContext;
032    import org.apache.activemq.broker.TransportConnector;
033    import org.apache.activemq.broker.region.Subscription;
034    import org.apache.activemq.command.ActiveMQQueue;
035    import org.apache.activemq.command.ActiveMQTopic;
036    import org.apache.activemq.command.ConsumerId;
037    import org.apache.activemq.command.ConsumerInfo;
038    import org.apache.activemq.command.RemoveSubscriptionInfo;
039    import org.apache.activemq.network.NetworkConnector;
040    
041    /**
042     * @version $Revision$
043     */
044    public class BrokerView implements BrokerViewMBean {
045    
046        ManagedRegionBroker broker;
047        private final BrokerService brokerService;
048        private final AtomicInteger sessionIdCounter = new AtomicInteger(0);
049    
050        public BrokerView(BrokerService brokerService, ManagedRegionBroker managedBroker) throws Exception {
051            this.brokerService = brokerService;
052            this.broker = managedBroker;
053        }
054    
055        public ManagedRegionBroker getBroker() {
056            return broker;
057        }
058    
059        public void setBroker(ManagedRegionBroker broker) {
060            this.broker = broker;
061        }
062        
063        public String getBrokerId() {
064            return broker.getBrokerId().toString();
065        }
066        
067        public String getBrokerName() {
068            return broker.getBrokerName();
069        }    
070    
071        public void gc() throws Exception {
072            brokerService.getBroker().gc();
073        }
074    
075        public void start() throws Exception {
076            brokerService.start();
077        }
078    
079        public void stop() throws Exception {
080            brokerService.stop();
081        }
082        
083        public void stopGracefully(String connectorName, String queueName, long timeout, long pollInterval)
084                throws Exception {
085            brokerService.stopGracefully(connectorName, queueName, timeout, pollInterval);
086        }
087    
088        
089        public long getTotalEnqueueCount() {
090            return broker.getDestinationStatistics().getEnqueues().getCount();
091        }
092    
093        public long getTotalDequeueCount() {
094            return broker.getDestinationStatistics().getDequeues().getCount();
095        }
096    
097        public long getTotalConsumerCount() {
098            return broker.getDestinationStatistics().getConsumers().getCount();
099        }
100    
101        public long getTotalMessageCount() {
102            return broker.getDestinationStatistics().getMessages().getCount();
103        }
104    
105        public long getTotalMessagesCached() {
106            return broker.getDestinationStatistics().getMessagesCached().getCount();
107        }
108    
109        public int getMemoryPercentUsage() {
110            return brokerService.getSystemUsage().getMemoryUsage().getPercentUsage();
111        }
112    
113        public long getMemoryLimit() {
114            return brokerService.getSystemUsage().getMemoryUsage().getLimit();
115        }
116    
117        public void setMemoryLimit(long limit) {
118            brokerService.getSystemUsage().getMemoryUsage().setLimit(limit);
119        }
120        
121        public long getStoreLimit() {
122            return brokerService.getSystemUsage().getStoreUsage().getLimit();
123        }
124    
125        public int getStorePercentUsage() {
126            return brokerService.getSystemUsage().getStoreUsage().getPercentUsage();
127        }
128    
129     
130        public long getTempLimit() {
131           return brokerService.getSystemUsage().getTempUsage().getLimit();
132        }
133    
134        public int getTempPercentUsage() {
135           return brokerService.getSystemUsage().getTempUsage().getPercentUsage();
136        }
137    
138        public void setStoreLimit(long limit) {
139            brokerService.getSystemUsage().getStoreUsage().setLimit(limit);
140        }
141    
142        public void setTempLimit(long limit) {
143            brokerService.getSystemUsage().getTempUsage().setLimit(limit);
144        }
145        
146    
147        public void resetStatistics() {
148            broker.getDestinationStatistics().reset();
149        }
150    
151        public void enableStatistics() {
152            broker.getDestinationStatistics().setEnabled(true);
153        }
154    
155        public void disableStatistics() {
156            broker.getDestinationStatistics().setEnabled(false);
157        }
158    
159        public boolean isStatisticsEnabled() {
160            return broker.getDestinationStatistics().isEnabled();
161        }
162        
163        public boolean isPersistent() {
164            return brokerService.isPersistent();
165        }
166        
167        public boolean isSlave() {
168            return brokerService.isSlave();
169        }
170    
171        public void terminateJVM(int exitCode) {
172            System.exit(exitCode);
173        }
174    
175        public ObjectName[] getTopics() {
176            return broker.getTopics();
177        }
178    
179        public ObjectName[] getQueues() {
180            return broker.getQueues();
181        }
182    
183        public ObjectName[] getTemporaryTopics() {
184            return broker.getTemporaryTopics();
185        }
186    
187        public ObjectName[] getTemporaryQueues() {
188            return broker.getTemporaryQueues();
189        }
190    
191        public ObjectName[] getTopicSubscribers() {
192            return broker.getTopicSubscribers();
193        }
194    
195        public ObjectName[] getDurableTopicSubscribers() {
196            return broker.getDurableTopicSubscribers();
197        }
198    
199        public ObjectName[] getQueueSubscribers() {
200            return broker.getQueueSubscribers();
201        }
202    
203        public ObjectName[] getTemporaryTopicSubscribers() {
204            return broker.getTemporaryTopicSubscribers();
205        }
206    
207        public ObjectName[] getTemporaryQueueSubscribers() {
208            return broker.getTemporaryQueueSubscribers();
209        }
210    
211        public ObjectName[] getInactiveDurableTopicSubscribers() {
212            return broker.getInactiveDurableTopicSubscribers();
213        }
214    
215        public String addConnector(String discoveryAddress) throws Exception {
216            TransportConnector connector = brokerService.addConnector(discoveryAddress);
217            connector.start();
218            return connector.getName();
219        }
220    
221        public String addNetworkConnector(String discoveryAddress) throws Exception {
222            NetworkConnector connector = brokerService.addNetworkConnector(discoveryAddress);
223            connector.start();
224            return connector.getName();
225        }
226    
227        public boolean removeConnector(String connectorName) throws Exception {
228            TransportConnector connector = brokerService.getConnectorByName(connectorName);
229            connector.stop();
230            return brokerService.removeConnector(connector);
231        }
232    
233        public boolean removeNetworkConnector(String connectorName) throws Exception {
234            NetworkConnector connector = brokerService.getNetworkConnectorByName(connectorName);
235            connector.stop();
236            return brokerService.removeNetworkConnector(connector);
237        }
238    
239        public void addTopic(String name) throws Exception {
240            broker.addDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQTopic(name));
241        }
242    
243        public void addQueue(String name) throws Exception {
244            broker.addDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQQueue(name));
245        }
246    
247        public void removeTopic(String name) throws Exception {
248            broker.removeDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQTopic(name),
249                                     1000);
250        }
251    
252        public void removeQueue(String name) throws Exception {
253            broker.removeDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQQueue(name),
254                                     1000);
255        }
256    
257        public ObjectName createDurableSubscriber(String clientId, String subscriberName, String topicName,
258                                                  String selector) throws Exception {
259            ConnectionContext context = new ConnectionContext();
260            context.setBroker(broker);
261            context.setClientId(clientId);
262            ConsumerInfo info = new ConsumerInfo();
263            ConsumerId consumerId = new ConsumerId();
264            consumerId.setConnectionId(clientId);
265            consumerId.setSessionId(sessionIdCounter.incrementAndGet());
266            consumerId.setValue(0);
267            info.setConsumerId(consumerId);
268            info.setDestination(new ActiveMQTopic(topicName));
269            info.setSubscriptionName(subscriberName);
270            info.setSelector(selector);
271            Subscription subscription = broker.addConsumer(context, info);
272            broker.removeConsumer(context, info);
273            if (subscription != null) {
274                return subscription.getObjectName();
275            }
276            return null;
277        }
278    
279        public void destroyDurableSubscriber(String clientId, String subscriberName) throws Exception {
280            RemoveSubscriptionInfo info = new RemoveSubscriptionInfo();
281            info.setClientId(clientId);
282            info.setSubscriptionName(subscriberName);
283            ConnectionContext context = new ConnectionContext();
284            context.setBroker(broker);
285            context.setClientId(clientId);
286            broker.removeSubscription(context, info);
287        }
288    
289        /**
290         * Returns the broker's administration connection context used for
291         * configuring the broker at startup
292         */
293        public static ConnectionContext getConnectionContext(Broker broker) {
294            ConnectionContext adminConnectionContext = broker.getAdminConnectionContext();
295            if (adminConnectionContext == null) {
296                adminConnectionContext = createAdminConnectionContext(broker);
297                broker.setAdminConnectionContext(adminConnectionContext);
298            }
299            return adminConnectionContext;
300        }
301    
302        /**
303         * Factory method to create the new administration connection context
304         * object. Note this method is here rather than inside a default broker
305         * implementation to ensure that the broker reference inside it is the outer
306         * most interceptor
307         */
308        protected static ConnectionContext createAdminConnectionContext(Broker broker) {
309            ConnectionContext context = new ConnectionContext();
310            context.setBroker(broker);
311            return context;
312        }
313        
314        //  doc comment inherited from BrokerViewMBean
315        public void reloadLog4jProperties() throws Throwable {
316    
317            // Avoid a direct dependency on log4j.. use reflection.
318            try {
319                ClassLoader cl = getClass().getClassLoader();
320                Class logManagerClass = cl.loadClass("org.apache.log4j.LogManager");
321                
322                Method resetConfiguration = logManagerClass.getMethod("resetConfiguration", new Class[]{});
323                resetConfiguration.invoke(null, new Object[]{});
324                
325                URL log4jprops = cl.getResource("log4j.properties");
326                if (log4jprops != null) {
327                    Class propertyConfiguratorClass = cl.loadClass("org.apache.log4j.PropertyConfigurator");
328                    Method configure = propertyConfiguratorClass.getMethod("configure", new Class[]{URL.class});
329                    configure.invoke(null, new Object[]{log4jprops});
330                }
331            } catch (InvocationTargetException e) {
332                throw e.getTargetException();
333            }
334        }
335        
336    
337        public String getOpenWireURL() {
338            String answer = brokerService.getTransportConnectorURIsAsMap().get("tcp");
339            return answer != null ? answer : "";
340        }
341    
342        public String getStompURL() {
343            String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp");
344            return answer != null ? answer : "";
345        }
346    
347        public String getSslURL() {
348            String answer = brokerService.getTransportConnectorURIsAsMap().get("ssl");
349            return answer != null ? answer : "";
350        }
351    
352        public String getStompSslURL() {
353            String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp+ssl");
354            return answer != null ? answer : "";
355        }
356    
357        public String getVMURL() {
358            URI answer = brokerService.getVmConnectorURI();
359            return answer != null ? answer.toString() : "";
360        }
361        
362        public String getDataDirectory() {
363            File file = brokerService.getDataDirectoryFile();
364            try {
365                return file != null ? file.getCanonicalPath():"";
366            } catch (IOException e) {
367                return "";
368            }
369        }
370    }