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 java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import nl.hippo.client.api.event.NamedEvent;
27  import nl.hippo.client.api.event.EventAware;
28  import nl.hippo.client.api.event.EventAwareManager;
29  import nl.hippo.client.event.service.UpdateNotificationConfig;
30  
31  public class EventAwareManagerImpl implements EventAwareManager {
32  
33      private Map listeners = new HashMap();
34      private String loglabel;
35      
36      public EventAwareManagerImpl() {
37          super();
38          this.loglabel = "";
39      }
40      
41      public EventAwareManagerImpl(UpdateNotificationConfig config) {
42          super();
43          this.loglabel = config.getLoglabel();
44      }
45  
46      public void addListener(EventAware listener, String namespace) {
47          Set namespaceListeners = (Set) listeners.get(namespace);
48          if (namespaceListeners == null) {
49              namespaceListeners = new HashSet();
50              listeners.put(namespace, namespaceListeners);
51          }
52          namespaceListeners.add(listener);
53      }
54  
55      /**
56       * Remove a listener
57       * 
58       * @param listener
59       *          The listener to remove
60       * @param connectionName
61       *          The name of the Connection the Eventlistener listens to.
62       */
63      public void removeListener(EventAware listener, String namespace) {
64          Set namespaceListeners = (Set) listeners.get(namespace);
65          if (namespaceListeners != null) {
66              namespaceListeners.remove(listener);
67          }
68      }
69  
70      public void notifyListeners(NamedEvent repositoryChangeEvent) {
71          String namespace = repositoryChangeEvent.getNamespace();
72          Set namespaceListeners = (Set) listeners.get(namespace);
73          if (namespaceListeners != null) {
74  
75              List namedEvents = split(repositoryChangeEvent);
76              Iterator eventIt = namedEvents.iterator();
77              while (eventIt.hasNext()) {
78                  Object obj = eventIt.next();
79                  NamedEvent namedEvent = (NamedEvent) obj;
80                  Iterator it = namespaceListeners.iterator();
81  
82                  while (it.hasNext()) {
83                      EventAware listener = (EventAware) it.next();
84                      if(EventLogger.log.isInfoEnabled()) {
85                          EventLogger.log.info(loglabel + "processing " + namedEvent);
86                      }
87                      listener.processEvent(namedEvent);
88                  }
89              }
90          }
91      }
92  
93      public List split(NamedEvent event) {
94          ArrayList result = new ArrayList();
95          result.add(event);
96  
97          // FIXME
98          if (!event.getType().equals("delete")) {
99              String copyOfName = event.getName();
100 
101             int index = copyOfName.lastIndexOf('/');
102             while (index > -1) {
103                 copyOfName = copyOfName.substring(0, index);
104                 result.add(new NamedEvent(copyOfName));
105                 index = copyOfName.lastIndexOf('/');
106             }
107         }
108         return result;
109     }
110 
111 }