KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > runtime > ProxyCollection


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

17 package org.apache.geronimo.gbean.runtime;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.geronimo.gbean.AbstractName;
22 import org.apache.geronimo.gbean.ReferenceCollection;
23 import org.apache.geronimo.gbean.ReferenceCollectionEvent;
24 import org.apache.geronimo.gbean.ReferenceCollectionListener;
25 import org.apache.geronimo.kernel.Kernel;
26 import org.apache.geronimo.kernel.GBeanNotFoundException;
27
28 import javax.management.ObjectName JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.NoSuchElementException JavaDoc;
36 import java.util.Set JavaDoc;
37
38 /**
39  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
40  */

41 class ProxyCollection implements ReferenceCollection {
42     private static final Log log = LogFactory.getLog(ProxyCollection.class);
43     private final String JavaDoc name;
44     private final Kernel kernel;
45     private final Map JavaDoc proxies = new HashMap JavaDoc();
46     private final Set JavaDoc listeners = new HashSet JavaDoc();
47     private boolean stopped = false;
48     private final Class JavaDoc type;
49
50     public ProxyCollection(String JavaDoc name, Class JavaDoc type, Set JavaDoc targets, Kernel kernel) {
51         this.name = name;
52         this.kernel = kernel;
53         this.type = type;
54
55         for (Iterator JavaDoc iterator = targets.iterator(); iterator.hasNext();) {
56             addTarget((AbstractName) iterator.next());
57         }
58     }
59
60     synchronized void destroy() {
61         stopped = true;
62         if (!AbstractGBeanReference.NO_PROXY) {
63             for (Iterator JavaDoc iterator = proxies.values().iterator(); iterator.hasNext();) {
64                 kernel.getProxyManager().destroyProxy(iterator.next());
65             }
66         }
67         proxies.clear();
68         listeners.clear();
69     }
70
71     void addTarget(AbstractName target) {
72         Object JavaDoc proxy;
73         ArrayList JavaDoc listenerCopy;
74         synchronized (this) {
75             // if this is not a new target return
76
if (proxies.containsKey(target)) {
77                 return;
78             }
79
80             // create and add the proxy
81
if (AbstractGBeanReference.NO_PROXY) {
82                 try {
83                     proxy = kernel.getGBean(target);
84                 } catch (GBeanNotFoundException e) {
85                     // gbean disappeard on us
86
log.debug("GBean was unloaded before it could be added to reference collections: " + target);
87                     return;
88                 }
89             } else {
90                 proxy = kernel.getProxyManager().createProxy(target, type);
91             }
92             proxies.put(target, proxy);
93
94             // make a snapshot of the listeners
95
listenerCopy = new ArrayList JavaDoc(listeners);
96         }
97
98         // fire the member added event
99
for (Iterator JavaDoc iterator = listenerCopy.iterator(); iterator.hasNext();) {
100             ReferenceCollectionListener listener = (ReferenceCollectionListener) iterator.next();
101             try {
102                 listener.memberAdded(new ReferenceCollectionEvent(name, proxy));
103             } catch (Throwable JavaDoc t) {
104                 log.error("Listener threw exception", t);
105             }
106         }
107     }
108
109     void removeTarget(AbstractName target) {
110         Object JavaDoc proxy;
111         ArrayList JavaDoc listenerCopy;
112         synchronized (this) {
113             // remove the proxy
114
proxy = proxies.remove(target);
115
116             // if this was not a target return
117
if (proxy == null) {
118                 return;
119             }
120
121             // make a snapshot of the listeners
122
listenerCopy = new ArrayList JavaDoc(listeners);
123         }
124
125         // fire the member removed event
126
for (Iterator JavaDoc iterator = listenerCopy.iterator(); iterator.hasNext();) {
127             ReferenceCollectionListener listener = (ReferenceCollectionListener) iterator.next();
128             try {
129                 listener.memberRemoved(new ReferenceCollectionEvent(name, proxy));
130             } catch (Throwable JavaDoc t) {
131                 log.error("Listener threw exception", t);
132             }
133         }
134
135         // destroy the proxy
136
if (!AbstractGBeanReference.NO_PROXY) {
137             kernel.getProxyManager().destroyProxy(proxy);
138         }
139     }
140
141     public synchronized ObjectName JavaDoc[] getMemberObjectNames() {
142         return (ObjectName JavaDoc[])proxies.keySet().toArray(new ObjectName JavaDoc[0]);
143     }
144
145     public synchronized boolean isStopped() {
146         return stopped;
147     }
148
149     public synchronized void addReferenceCollectionListener(ReferenceCollectionListener listener) {
150         listeners.add(listener);
151     }
152
153     public synchronized void removeReferenceCollectionListener(ReferenceCollectionListener listener) {
154         listeners.remove(listener);
155     }
156
157     public synchronized int size() {
158         if (stopped) {
159             return 0;
160         }
161         return proxies.size();
162     }
163
164     public synchronized boolean isEmpty() {
165         if (stopped) {
166             return true;
167         }
168         return proxies.isEmpty();
169     }
170
171     public synchronized boolean contains(Object JavaDoc o) {
172         if (stopped) {
173             return false;
174         }
175         return proxies.containsValue(o);
176     }
177
178     public synchronized Iterator JavaDoc iterator() {
179         if (stopped) {
180             return new Iterator JavaDoc() {
181                 public boolean hasNext() {
182                     return false;
183                 }
184
185                 public Object JavaDoc next() {
186                     throw new NoSuchElementException JavaDoc();
187                 }
188
189                 public void remove() {
190                     throw new UnsupportedOperationException JavaDoc();
191                 }
192             };
193         }
194
195         return new Iterator JavaDoc() {
196             // copy the proxies, so the client can iterate without concurrent modification
197
// this is necssary since the client has nothing to synchronize on
198
private final Iterator JavaDoc iterator = new ArrayList JavaDoc(proxies.values()).iterator();
199
200             public boolean hasNext() {
201                 return iterator.hasNext();
202             }
203
204             public Object JavaDoc next() {
205                 return iterator.next();
206             }
207
208             public void remove() {
209                 throw new UnsupportedOperationException JavaDoc();
210             }
211         };
212     }
213
214     public synchronized Object JavaDoc[] toArray() {
215         if (stopped) {
216             return new Object JavaDoc[0];
217         }
218         return proxies.values().toArray();
219     }
220
221     public synchronized Object JavaDoc[] toArray(Object JavaDoc a[]) {
222         if (stopped) {
223             if (a.length > 0) {
224                 a[0] = null;
225             }
226             return a;
227         }
228         return proxies.values().toArray(a);
229     }
230
231     public synchronized boolean containsAll(Collection c) {
232         if (stopped) {
233             return c.isEmpty();
234         }
235         return proxies.values().containsAll(c);
236     }
237
238     public boolean add(Object JavaDoc o) {
239         throw new UnsupportedOperationException JavaDoc();
240     }
241
242     public boolean remove(Object JavaDoc o) {
243         throw new UnsupportedOperationException JavaDoc();
244     }
245
246     public boolean addAll(Collection c) {
247         throw new UnsupportedOperationException JavaDoc();
248     }
249
250     public boolean removeAll(Collection c) {
251         throw new UnsupportedOperationException JavaDoc();
252     }
253
254     public boolean retainAll(Collection c) {
255         throw new UnsupportedOperationException JavaDoc();
256     }
257
258     public void clear() {
259         throw new UnsupportedOperationException JavaDoc();
260     }
261 }
262
Popular Tags