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