View Javadoc

1   /***
2    * 
3    * Copyright RAJD Consultancy Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12   * specific language governing permissions and limitations under the License.
13   * 
14   */
15  package org.logicblaze.lingo.jmx.remote.jms;
16  
17  import java.io.IOException;
18  import java.util.Iterator;
19  import java.util.List;
20  import javax.jms.Connection;
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  import javax.jms.MessageConsumer;
24  import javax.jms.MessageListener;
25  import javax.jms.ObjectMessage;
26  import javax.jms.Session;
27  import javax.jms.Topic;
28  import javax.management.InstanceNotFoundException;
29  import javax.management.ListenerNotFoundException;
30  import javax.management.Notification;
31  import javax.management.NotificationBroadcasterSupport;
32  import javax.management.NotificationFilter;
33  import javax.management.NotificationListener;
34  import javax.management.ObjectName;
35  import org.apache.activemq.util.IdGenerator;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
39  /***
40   * @version $Revision: 1.2 $
41   */
42  public class MBeanJmsServerConnectionClient extends MBeanServerConnectionDelegate implements MessageListener{
43      private static final Log log=LogFactory.getLog(MBeanJmsServerConnectionClient.class);
44      private MBeanJmsServerConnection serverConnection;
45      private Topic replyToDestination;
46      private List listeners=new CopyOnWriteArrayList();
47      private IdGenerator idGenerator=new IdGenerator();
48      private NotificationBroadcasterSupport localNotifier=new NotificationBroadcasterSupport();
49  
50      /***
51       * Construct this thing
52       * 
53       * @param connection
54       * @throws JMSException
55       */
56      public MBeanJmsServerConnectionClient(MBeanJmsServerConnection connection,Connection jmsConnection)
57                      throws JMSException{
58          super(connection);
59          this.serverConnection=connection;
60          Session jmsSession=jmsConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
61          replyToDestination=jmsSession.createTemporaryTopic();
62          MessageConsumer consumer=jmsSession.createConsumer(replyToDestination);
63          consumer.setMessageListener(this);
64      }
65  
66      /***
67       * Add a notification listener
68       * @param name 
69       * @param listener 
70       * @param filter 
71       * @param handback 
72       */
73      public void addNotificationListener(ObjectName name,NotificationListener listener,NotificationFilter filter,
74                      Object handback){
75          String id=idGenerator.generateId();
76          ListenerInfo info=new ListenerInfo(id,listener,filter,handback);
77          listeners.add(info);
78          localNotifier.addNotificationListener(listener,filter,handback);
79          serverConnection.addNotificationListener(id,name,replyToDestination);
80      }
81  
82      /***
83       * Remove a Notification Listener
84       * @param name 
85       * @param listener 
86       * @throws ListenerNotFoundException 
87       */
88      public void removeNotificationListener(ObjectName name,NotificationListener listener)
89                      throws ListenerNotFoundException{
90          for(Iterator i=listeners.iterator();i.hasNext();){
91              ListenerInfo li=(ListenerInfo) i.next();
92              if(li.getListener()==listener){
93                  listeners.remove(i);
94                  serverConnection.removeNotificationListener(li.getId());
95                  break;
96              }
97          }
98          localNotifier.removeNotificationListener(listener);
99      }
100 
101     /***
102      * Remove a Notification Listener
103      * @param name 
104      * @param listener 
105      * @param filter 
106      * @param handback 
107      * @throws ListenerNotFoundException 
108      */
109     public void removeNotificationListener(ObjectName name,NotificationListener listener,NotificationFilter filter,
110                     Object handback) throws ListenerNotFoundException{
111         for(Iterator i=listeners.iterator();i.hasNext();){
112             ListenerInfo li=(ListenerInfo) i.next();
113             if(li.getListener()==listener&&li.getFilter()==filter&&li.getHandback()==handback){
114                 listeners.remove(i);
115                 serverConnection.removeNotificationListener(li.getId());
116             }
117         }
118         localNotifier.removeNotificationListener(listener,filter,handback);
119     }
120 
121     /***
122      * MessageListener implementation
123      * @param msg 
124      */
125     public void onMessage(Message msg){
126         ObjectMessage objMsg=(ObjectMessage) msg;
127         try{
128             Notification notification=(Notification) objMsg.getObject();
129             localNotifier.sendNotification(notification);
130         }catch(JMSException jmsEx){
131             log.error("Failed to send Notification",jmsEx);
132         }
133     }
134 }