View Javadoc

1   /*
2    Copyright 2008 Ramon Servadei
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" BASIS,
12   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 fulmine.model.container.subscription.remote;
17  
18  import static fulmine.util.Utils.nullCheck;
19  
20  import java.util.Collection;
21  
22  import fulmine.IAddressable;
23  import fulmine.context.IFrameworkContext;
24  import fulmine.event.listener.AbstractEventHandler;
25  import fulmine.event.listener.MultiSystemEventListener;
26  import fulmine.event.subscription.ISubscriptionFactory;
27  import fulmine.event.subscription.ISubscriptionParameters;
28  import fulmine.event.system.ISystemEventListener;
29  import fulmine.model.container.IContainer;
30  import fulmine.model.container.events.RemoteContainerCreatedEvent;
31  import fulmine.model.container.events.RemoteContainerDestroyedEvent;
32  import fulmine.model.container.subscription.ContainerSubscriptionManager;
33  import fulmine.util.log.AsyncLog;
34  
35  /**
36   * Adds support for handling subscriptions for remote containers.
37   * 
38   * @author Ramon Servadei
39   */
40  public class RemoteContainerSubscriptionManager extends
41      ContainerSubscriptionManager
42  {
43      private final static AsyncLog LOG =
44          new AsyncLog(RemoteContainerSubscriptionManager.class);
45  
46      /**
47       * The identity of the remote context for the subscriptions this manager
48       * handles
49       */
50      private final String remoteContextIdentity;
51  
52      /**
53       * Handles {@link RemoteContainerCreatedEvent} events
54       * 
55       * @author Ramon Servadei
56       */
57      private class RemoteContainerCreatedEventHandler extends
58          AbstractEventHandler<RemoteContainerCreatedEvent> implements
59          ISystemEventListener
60      {
61          @Override
62          public AsyncLog getLog()
63          {
64              return LOG;
65          }
66  
67          @Override
68          public void handle(RemoteContainerCreatedEvent event)
69          {
70              // only handle events for this manager's remote context
71              if (RemoteContainerSubscriptionManager.this.remoteContextIdentity.equals(event.getRemoteContextIdentity()))
72              {
73                  eventSourceCreated(event);
74              }
75              else
76              {
77                  if (getLog().isDebugEnabled())
78                  {
79                      getLog().debug(
80                          "Not handling "
81                              + event
82                              + " because the remote context identity does not match "
83                              + RemoteContainerSubscriptionManager.this.remoteContextIdentity);
84                  }
85              }
86          }
87      }
88  
89      /**
90       * Handles {@link RemoteContainerDestroyedEvent} events
91       * 
92       * @author Ramon Servadei
93       */
94      private class RemoteContainerDestroyedEventHandler extends
95          AbstractEventHandler<RemoteContainerDestroyedEvent> implements
96          ISystemEventListener
97      {
98          @Override
99          public AsyncLog getLog()
100         {
101             return LOG;
102         }
103 
104         @Override
105         public void handle(RemoteContainerDestroyedEvent event)
106         {
107             if (RemoteContainerSubscriptionManager.this.remoteContextIdentity.equals(event.getRemoteContextIdentity()))
108             {
109                 eventSourceDestroyed(event);
110             }
111             else
112             {
113                 if (getLog().isDebugEnabled())
114                 {
115                     getLog().debug(
116                         "Not handling "
117                             + event
118                             + " because the remote context identity does not match "
119                             + RemoteContainerSubscriptionManager.this.remoteContextIdentity);
120                 }
121             }
122         }
123     }
124 
125     /**
126      * Standard constructor
127      * 
128      * @param context
129      *            the context for this
130      * @param remoteContextIdentity
131      *            the identity of the remote context for the subscriptions that
132      *            this manager handles
133      * @param factory
134      *            the container subscription factory to use
135      */
136     @SuppressWarnings("unchecked")
137     public RemoteContainerSubscriptionManager(IFrameworkContext context,
138         String remoteContextIdentity, ISubscriptionFactory factory)
139     {
140         super(context, factory);
141         nullCheck(remoteContextIdentity, "No remote context identity");
142         this.remoteContextIdentity = remoteContextIdentity;
143         this.eventHandler =
144             new MultiSystemEventListener(this.getClass().getSimpleName(),
145                 context, AbstractEventHandler.getEventHandlerMappings(
146                     new RemoteContainerCreatedEventHandler(),
147                     new RemoteContainerDestroyedEventHandler()));
148     }
149 
150     @Override
151     protected AsyncLog getLog()
152     {
153         return LOG;
154     }
155 
156     @Override
157     protected IContainer doGetEventSource(IAddressable id)
158     {
159         return getContext().getRemoteContainer(remoteContextIdentity,
160             id.getIdentity(), id.getType(), id.getDomain());
161     }
162 
163     @Override
164     protected Collection<IContainer> doGetEventSources()
165     {
166         return getContext().getRemoteContainers(remoteContextIdentity);
167     }
168 
169     @Override
170     protected boolean doEventSourceExists(IAddressable id)
171     {
172         return getContext().containsRemoteContainer(remoteContextIdentity,
173             id.getIdentity(), id.getType(), id.getDomain());
174     }
175 
176     @Override
177     protected void doRaiseSubscribeEvent(ISubscriptionParameters parameters)
178     {
179         // noop - we don't want to raise events for remote subscriptions
180     }
181 
182     @Override
183     protected void doRaiseUnsubscribeEvent(ISubscriptionParameters parameters)
184     {
185         // noop - we don't want to raise events for remote subscriptions
186     }
187 }