1 /***
2 *
3 * Copyright RAJD Consultancy 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
19 package org.logicblaze.lingo.jmx.remote.jms;
20
21 import org.apache.activemq.ActiveMQConnectionFactory;
22 import org.apache.activemq.command.ActiveMQTopic;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.logicblaze.lingo.jms.JmsProducerConfig;
26 import org.logicblaze.lingo.jms.JmsServiceExporter;
27 import org.logicblaze.lingo.jms.impl.MultiplexingRequestor;
28
29 import javax.jms.ConnectionFactory;
30 import javax.management.MBeanServer;
31 import javax.management.remote.JMXConnectorFactory;
32 import javax.management.remote.JMXConnectorServer;
33 import javax.management.remote.JMXServiceURL;
34
35 import java.io.IOException;
36 import java.net.URI;
37 import java.util.Collections;
38 import java.util.Map;
39
40 /***
41 * <p>The client end of a JMX API connector. An object of this type can
42 * be used to establish a connection to a connector server.</p>
43 *
44 * <p>A newly-created object of this type is unconnected. Its {@link#connect connect}
45 * method must be called before it can be used.
46 * However, objects created by {@link
47 * JMXConnectorFactory#connect(JMXServiceURL, Map)
48 * JMXConnectorFactory.connect} are already connected.</p>
49 *
50 * @since 1.5
51 * @since.unbundled 1.0
52 */
53 public class JmsJmxConnectorServer extends JMXConnectorServer {
54
55 private static final Log log = LogFactory.getLog(JmsJmxConnectorServer.class);
56 private JMXServiceURL url;
57 private final Map env;
58 private String destinationName;
59 private String destinationGroupName = JmsJmxConnectorSupport.MBEAN_GROUP_NAME;
60 private String destinationServerName = JmsJmxConnectorSupport.MBEAN_SERVER_NAME;
61 private volatile boolean stopped = true;
62 private JmsServiceExporter service;
63 private MultiplexingRequestor requestor;
64 private MBeanJmsServerConnectionImpl jmsServerConnection;
65 private URI jmsURL;
66
67
68 /***
69 * Create the JmsJmxConnectorServer
70 * @param url
71 * @param environment
72 * @param server
73 * @throws IOException
74 */
75 public JmsJmxConnectorServer(JMXServiceURL url,Map environment,MBeanServer server) throws IOException{
76 super(server);
77 this.url = url;
78 this.env = environment;
79 this.jmsURL = JmsJmxConnectorSupport.getProviderURL(url);
80
81 JmsJmxConnectorSupport.populateProperties(this, jmsURL);
82 }
83
84 /***
85 * start the connector
86 * @throws IOException
87 */
88 public void start() throws IOException{
89 try{
90 service = new JmsServiceExporter();
91 ConnectionFactory fac = new ActiveMQConnectionFactory(jmsURL);
92 if (destinationName == null){
93 destinationName = JmsJmxConnectorSupport.DEFAULT_DESTINATION_PREFIX + destinationGroupName + "." + destinationServerName;
94 }
95 service.setDestination(new ActiveMQTopic(destinationName));
96 service.setConnectionFactory(fac);
97 service.setServiceInterface(MBeanJmsServerConnection.class);
98 this.requestor = (MultiplexingRequestor) MultiplexingRequestor.newInstance(fac,new JmsProducerConfig(),null);
99 service.setResponseRequestor(requestor);
100 this.jmsServerConnection = new MBeanJmsServerConnectionImpl(getMBeanServer(),requestor.getConnection());
101 service.setService(jmsServerConnection);
102 service.afterPropertiesSet();
103 stopped = false;
104 }catch(Exception e){
105 log.error("Failed to start ",e);
106 throw new IOException(e.toString());
107 }
108
109 }
110
111 /***
112 * stop the connector
113 * @throws IOException
114 */
115 public void stop() throws IOException{
116 try{
117 if(!stopped){
118 stopped = true;
119 service.destroy();
120 }
121 }catch(Exception e){
122 log.error("Failed to stop ",e);
123 throw new IOException(e.toString());
124 }
125
126 }
127
128 public boolean isActive(){
129 return !stopped;
130 }
131
132 public JMXServiceURL getAddress(){
133 return url;
134 }
135
136 public Map getAttributes(){
137 return Collections.unmodifiableMap(env);
138 }
139
140
141
142 }