1 //========================================================================
2 //$Id: LifeCycleCallbackCollection.java 1540 2007-01-19 12:24:10Z janb $
3 //Copyright 2006 Mort Bay Consulting Pty. Ltd.
4 //------------------------------------------------------------------------
5 //Licensed under the Apache License, Version 2.0 (the "License");
6 //you may not use this file except in compliance with the License.
7 //You may obtain a copy of the License at
8 //http://www.apache.org/licenses/LICENSE-2.0
9 //Unless required by applicable law or agreed to in writing, software
10 //distributed under the License is distributed on an "AS IS" BASIS,
11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //See the License for the specific language governing permissions and
13 //limitations under the License.
14 //========================================================================
15
16 package org.mortbay.jetty.plus.annotation;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.mortbay.log.Log;
25
26
27 /**
28 * LifeCycleCallbackCollection
29 *
30 *
31 */
32 public class LifeCycleCallbackCollection
33 {
34 private HashMap postConstructCallbacksMap = new HashMap();
35 private HashMap preDestroyCallbacksMap = new HashMap();
36
37
38
39
40
41 /**
42 * Add a Callback to the list of callbacks.
43 *
44 * @param callback
45 */
46 public void add (LifeCycleCallback callback)
47 {
48 if ((callback==null) || (callback.getTargetClass()==null) || (callback.getTarget()==null))
49 return;
50
51 if (Log.isDebugEnabled())
52 Log.debug("Adding callback for class="+callback.getTargetClass()+ " on "+callback.getTarget());
53 Map map = null;
54 if (callback instanceof PreDestroyCallback)
55 map = preDestroyCallbacksMap;
56 if (callback instanceof PostConstructCallback)
57 map = postConstructCallbacksMap;
58
59 if (map == null)
60 throw new IllegalArgumentException ("Unsupported lifecycle callback type: "+callback);
61
62
63 List callbacks = (List)map.get(callback.getTargetClass());
64 if (callbacks==null)
65 {
66 callbacks = new ArrayList();
67 map.put(callback.getTargetClass(), callbacks);
68 }
69
70 //don't add another callback for exactly the same method
71 if (!callbacks.contains(callback))
72 callbacks.add(callback);
73 }
74
75
76 /**
77 * Call the method, if one exists, that is annotated with PostConstruct
78 * or with <post-construct> in web.xml
79 * @param o the object on which to attempt the callback
80 * @throws Exception
81 */
82 public void callPostConstructCallback (Object o)
83 throws Exception
84 {
85 if (o == null)
86 return;
87
88 List callbacks = (List)postConstructCallbacksMap.get(o.getClass());
89
90 if (callbacks == null)
91 return;
92
93 for (int i=0;i<callbacks.size();i++)
94 ((LifeCycleCallback)callbacks.get(i)).callback(o);
95 }
96
97
98 /**
99 * Call the method, if one exists, that is annotated with PreDestroy
100 * or with <pre-destroy> in web.xml
101 * @param o the object on which to attempt the callback
102 */
103 public void callPreDestroyCallback (Object o)
104 throws Exception
105 {
106 if (o == null)
107 return;
108
109 List callbacks = (List)preDestroyCallbacksMap.get(o.getClass());
110 if (callbacks == null)
111 return;
112
113 for (int i=0;i<callbacks.size();i++)
114 ((LifeCycleCallback)callbacks.get(i)).callback(o);
115 }
116 }