001: //========================================================================
002: //$Id: LifeCycleCallbackCollection.java 1448 2006-12-29 20:46:57Z janb $
003: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.plus.annotation;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.mortbay.log.Log;
022:
023: /**
024: * LifeCycleCallbackCollection
025: *
026: *
027: */
028: public class LifeCycleCallbackCollection {
029: private HashMap postConstructCallbacksMap = new HashMap();
030: private HashMap preDestroyCallbacksMap = new HashMap();
031:
032: public void add(LifeCycleCallback callback) {
033: if ((callback == null) || (callback.getClassName() == null)
034: || (callback.getTarget() == null))
035: return;
036:
037: Log.debug("Adding callback for class="
038: + callback.getClassName() + " of type "
039: + callback.getClass().getName());
040: Map map = null;
041: if (callback instanceof PreDestroyCallback)
042: map = preDestroyCallbacksMap;
043: if (callback instanceof PostConstructCallback)
044: map = postConstructCallbacksMap;
045:
046: if (map == null)
047: throw new IllegalArgumentException(
048: "Unsupported lifecycle callback type: " + callback);
049:
050: //check that there is not already a callback of the same type
051: //already registered for this class
052: LifeCycleCallback existing = (LifeCycleCallback) map
053: .get(callback.getClassName());
054: if (existing != null)
055: throw new IllegalStateException(
056: "Callback already registered for class "
057: + callback.getClassName() + " on method "
058: + existing.getTarget().getName());
059: map.put(callback.getClassName(), callback);
060: }
061:
062: /**
063: * Call the method, if one exists, that is annotated with PostConstruct
064: * or with <post-construct> in web.xml
065: * @param o the object on which to attempt the callback
066: * @throws Exception
067: */
068: public void callPostConstructCallback(Object o) throws Exception {
069: if (o == null)
070: return;
071:
072: LifeCycleCallback callback = (LifeCycleCallback) postConstructCallbacksMap
073: .get(o.getClass().getName());
074:
075: Log.debug("Got callback=" + callback + " for class: "
076: + o.getClass().getName());
077: if (callback == null)
078: return;
079:
080: callback.callback(o);
081: }
082:
083: /**
084: * Call the method, if one exists, that is annotated with PreDestroy
085: * or with <pre-destroy> in web.xml
086: * @param o the object on which to attempt the callback
087: */
088: public void callPreDestroyCallback(Object o) {
089: if (o == null)
090: return;
091:
092: PreDestroyCallback callback = (PreDestroyCallback) preDestroyCallbacksMap
093: .get(o.getClass().getName());
094: Log.debug("Got callback=" + callback + " for class: "
095: + o.getClass().getName());
096: if (callback == null)
097: return;
098: callback.callback(o);
099: }
100: }
|