KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > jmx > ManagedTransportConnection


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.broker.jmx;
19
20 import org.apache.activemq.broker.Broker;
21 import org.apache.activemq.broker.TransportConnection;
22 import org.apache.activemq.broker.TransportConnector;
23 import org.apache.activemq.command.ConnectionInfo;
24 import org.apache.activemq.command.Response;
25 import org.apache.activemq.thread.TaskRunnerFactory;
26 import org.apache.activemq.transport.Transport;
27 import org.apache.activemq.util.IOExceptionSupport;
28 import org.apache.activemq.util.JMXSupport;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 import java.io.IOException JavaDoc;
36 import java.util.Hashtable JavaDoc;
37
38 /**
39  * A managed transport connection
40  *
41  * @version $Revision: 1.1 $
42  */

43 public class ManagedTransportConnection extends TransportConnection {
44     private static final Log log = LogFactory.getLog(ManagedTransportConnection.class);
45
46     private final MBeanServer JavaDoc server;
47     private final ObjectName JavaDoc connectorName;
48     private ConnectionViewMBean mbean;
49
50     private ObjectName JavaDoc byClientIdName;
51     private ObjectName JavaDoc byAddressName;
52
53     public ManagedTransportConnection(TransportConnector connector, Transport transport, Broker broker, TaskRunnerFactory factory, MBeanServer JavaDoc server,
54             ObjectName JavaDoc connectorName) throws IOException JavaDoc {
55         super(connector, transport, broker, factory);
56         this.server = server;
57         this.connectorName = connectorName;
58         this.mbean = new ConnectionView(this);
59         byAddressName = createByAddressObjectName("address", transport.getRemoteAddress());
60         registerMBean(byAddressName);
61     }
62
63     public synchronized void stop() throws Exception JavaDoc {
64         if (isStarting()) {
65             setPendingStop(true);
66             return;
67         }
68         synchronized(this) {
69             unregisterMBean(byClientIdName);
70             unregisterMBean(byAddressName);
71             byClientIdName=null;
72             byAddressName=null;
73         }
74         super.stop();
75     }
76
77     /**
78      * Sets the connection ID of this connection. On startup this connection ID
79      * is set to an incrementing counter; once the client registers it is set to
80      * the clientID of the JMS client.
81      */

82     public void setConnectionId(String JavaDoc connectionId) throws IOException JavaDoc {
83     }
84
85     public Response processAddConnection(ConnectionInfo info) throws Exception JavaDoc {
86         Response answer = super.processAddConnection(info);
87         String JavaDoc clientId = info.getClientId();
88         if (clientId != null) {
89             if(byClientIdName==null) {
90                 byClientIdName = createByClientIdObjectName(clientId);
91                 registerMBean(byClientIdName);
92             }
93         }
94         return answer;
95     }
96
97     // Implementation methods
98
// -------------------------------------------------------------------------
99
protected void registerMBean(ObjectName JavaDoc name) {
100         if( name!=null ) {
101             try {
102                 server.registerMBean(mbean, name);
103             } catch (Throwable JavaDoc e) {
104                 log.warn("Failed to register MBean: "+name);
105                 log.debug("Failure reason: "+e,e);
106             }
107         }
108     }
109
110     protected void unregisterMBean(ObjectName JavaDoc name) {
111         if (name != null) {
112             try {
113                 server.unregisterMBean(name);
114             }
115             catch (Throwable JavaDoc e) {
116                 log.warn("Failed to unregister mbean: " + name);
117                 log.debug("Failure reason: "+e,e);
118             }
119         }
120     }
121
122     protected ObjectName JavaDoc createByAddressObjectName(String JavaDoc type, String JavaDoc value) throws IOException JavaDoc {
123         // Build the object name for the destination
124
Hashtable JavaDoc map = connectorName.getKeyPropertyList();
125         try {
126             return new ObjectName JavaDoc(
127                     connectorName.getDomain()+":"+
128                     "BrokerName="+JMXSupport.encodeObjectNamePart((String JavaDoc) map.get("BrokerName"))+","+
129                     "Type=Connection,"+
130                     "ConnectorName="+JMXSupport.encodeObjectNamePart((String JavaDoc) map.get("ConnectorName"))+","+
131                     "ViewType="+JMXSupport.encodeObjectNamePart(type)+","+
132                     "Name="+JMXSupport.encodeObjectNamePart(value)
133                     );
134         }
135         catch (Throwable JavaDoc e) {
136             throw IOExceptionSupport.create(e);
137         }
138     }
139     
140     protected ObjectName JavaDoc createByClientIdObjectName(String JavaDoc value) throws IOException JavaDoc {
141         // Build the object name for the destination
142
Hashtable JavaDoc map = connectorName.getKeyPropertyList();
143         try {
144             return new ObjectName JavaDoc(
145                     connectorName.getDomain()+":"+
146                     "BrokerName="+JMXSupport.encodeObjectNamePart((String JavaDoc) map.get("BrokerName"))+","+
147                     "Type=Connection,"+
148                     "ConnectorName="+JMXSupport.encodeObjectNamePart((String JavaDoc) map.get("ConnectorName"))+","+
149                     "Connection="+JMXSupport.encodeObjectNamePart(value)
150                     );
151         }
152         catch (Throwable JavaDoc e) {
153             throw IOExceptionSupport.create(e);
154         }
155     }
156
157
158 }
159
Popular Tags