View Javadoc

1   /*
2    * Copyright 2007-2009 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * 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
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.client.event;
17  
18  import javax.jms.JMSException;
19  import javax.jms.Message;
20  import javax.jms.MessageListener;
21  import javax.jms.ObjectMessage;
22  import javax.jms.TopicSubscriber;
23  
24  import nl.hippo.client.api.ClientException;
25  import nl.hippo.client.api.event.EventAwareManager;
26  import nl.hippo.client.api.event.NamedEvent;
27  import nl.hippo.client.event.service.UpdateNotificationConfig;
28  import nl.hippo.slide.event.SlideEvent;
29  
30  public class RepositoryChangeListener extends Thread implements MessageListener {
31  
32      private EventAwareManager eventAwareManager;
33      private JmsTopicManager jmsTopicManager;
34      private boolean running = true;
35  
36      public RepositoryChangeListener(UpdateNotificationConfig config, EventAwareManager eventManager)
37              throws ClientException {
38          this.eventAwareManager = eventManager;
39          this.jmsTopicManager = new JmsTopicManager(config);
40      }
41  
42      public EventAwareManager getManager() {
43          return eventAwareManager;
44      }
45  
46      public void run() {
47          running = true;
48          jmsTopicManager.start();
49          while (running) {
50              boolean received = false;
51              try {
52                  TopicSubscriber subscriber = jmsTopicManager.getSubscriber();
53                  if (subscriber != null) {
54                      Message message = subscriber.receive();
55                      if (message != null) {
56                          onMessage(message);
57                          received = true;
58                      }
59                  }
60              } catch (Exception e) {
61                  EventLogger.log.error(jmsTopicManager.getLoglabel() + "Exception while receiving JMS message", e);
62              } finally {
63                  // prevent thread from taking up all cpu
64                  if (!received) {
65                      try {
66                          Thread.sleep(100);
67                      } catch (InterruptedException e) {
68                          // nop
69                      }
70                  }
71              }
72          }
73      }
74  
75  
76      public void shutdown() {
77          running = false;
78          jmsTopicManager.stop();
79      }
80  
81      public synchronized void onMessage(Message message) {
82          try {
83              if (message instanceof ObjectMessage) {
84                  Object obj = ((ObjectMessage) message).getObject();
85                  if (obj instanceof SlideEvent) {
86                      SlideEvent se = (SlideEvent) obj;
87                      if (EventLogger.log.isInfoEnabled()) {
88                          EventLogger.log.info(jmsTopicManager.getLoglabel() + "Incoming slide event: " + se.toString());
89                      }
90                      NamedEvent event = new NamedEvent(se.getType(), se.getNamespace(), se.getUri());
91                      eventAwareManager.notifyListeners(event);
92  
93                  } else {
94                      EventLogger.log.error(jmsTopicManager.getLoglabel() + "Unknown object type: " + obj.getClass().getName() + ", object: "
95                              + obj.toString());
96                  }
97              } else {
98                  EventLogger.log.error(jmsTopicManager.getLoglabel() + "Unknown message type: " + message.getClass().getName() + ", message: "
99                          + message.toString());
100             }
101         } catch (JMSException e) {
102             StringBuffer msg = new StringBuffer(jmsTopicManager.getLoglabel());
103             msg.append("Exception thrown while handling JMS message:\n");
104             msg.append(e.getClass().getName());
105             msg.append(": ");
106             msg.append(e.getMessage());
107             msg.append("\nCaused by:  ");
108             msg.append(e.getLinkedException().getClass().getName());
109             msg.append(": ");
110             msg.append(e.getLinkedException().getMessage());
111             EventLogger.log.error(msg.toString());
112         }
113     }
114 }