001: //========================================================================
002: //$Id: InjectionCollection.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.lang.reflect.Field;
019: import java.lang.reflect.Member;
020: import java.lang.reflect.Method;
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.mortbay.log.Log;
029:
030: /**
031: * InjectionCollection
032: *
033: *
034: */
035: public class InjectionCollection {
036: private HashMap fieldInjectionsMap = new HashMap();
037: private HashMap methodInjectionsMap = new HashMap();
038:
039: public void add(Injection injection) {
040: if ((injection == null) || (injection.getTarget() == null)
041: || (injection.getClassName() == null))
042: return;
043:
044: Log.debug("Adding injection for class="
045: + injection.getClassName() + " on a "
046: + injection.getTarget().getClass());
047: Map injectionsMap = null;
048: if (injection.getTarget() instanceof Field)
049: injectionsMap = fieldInjectionsMap;
050: if (injection.getTarget() instanceof Method)
051: injectionsMap = methodInjectionsMap;
052:
053: List injections = (List) injectionsMap.get(injection
054: .getClassName());
055: if (injections == null) {
056: injections = new ArrayList();
057: injectionsMap.put(injection.getClassName(), injections);
058: }
059:
060: injections.add(injection);
061: }
062:
063: public void inject(Object injectable) throws Exception {
064: if (injectable == null)
065: return;
066:
067: //TODO: ensure that overridden methods and fields are correctly visible
068:
069: ArrayList injections = new ArrayList();
070: injections.addAll(getMatchingInjections(injectable.getClass()
071: .getDeclaredFields(), (List) fieldInjectionsMap
072: .get(injectable.getClass().getName())));
073:
074: injections.addAll(getMatchingInjections(injectable.getClass()
075: .getDeclaredMethods(), (List) methodInjectionsMap
076: .get(injectable.getClass().getName())));
077:
078: Iterator itor = injections.iterator();
079: while (itor.hasNext())
080: ((Injection) itor.next()).inject(injectable);
081: }
082:
083: public List getMatchingInjections(Member[] members, List injections) {
084: if ((injections == null) || (members == null))
085: return Collections.EMPTY_LIST;
086:
087: List results = new ArrayList();
088:
089: Iterator itor = injections.iterator();
090: while (itor.hasNext()) {
091: Injection injection = (Injection) itor.next();
092: //find the member in the injectable matching
093: boolean found = false;
094: for (int i = 0; i < members.length && !found; i++) {
095: if (members[i].equals(injection.getTarget())) {
096: found = true;
097: results.add(injection);
098: }
099: }
100: }
101: return results;
102: }
103: }
|