001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.injection;
023:
024: import java.lang.reflect.Field;
025: import java.lang.reflect.Method;
026: import java.lang.reflect.AccessibleObject;
027: import java.util.Map;
028: import javax.management.ObjectName;
029: import javax.management.MalformedObjectNameException;
030:
031: import org.jboss.annotation.ejb.Depends;
032: import org.jboss.logging.Logger;
033: import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
034:
035: /**
036: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
037: * @version $Revision: 60455 $
038: */
039: public class DependsHandler implements InjectionHandler {
040: private static final Logger log = Logger
041: .getLogger(DependsHandler.class);
042:
043: public void loadXml(EnvironmentRefGroup xml,
044: InjectionContainer container) {
045: }
046:
047: public void handleMethodAnnotations(Method method,
048: InjectionContainer container,
049: Map<AccessibleObject, Injector> injectors) {
050: Depends dep = container.getAnnotation(Depends.class, method
051: .getDeclaringClass(), method);
052: if (dep != null) {
053: if (!method.getName().startsWith("set"))
054: throw new RuntimeException(
055: "@EJB can only be used with a set method: "
056: + method);
057: String[] names = dep.value();
058: if (names.length != 1)
059: throw new RuntimeException(
060: "@Depends on a field can only take one object name: "
061: + method);
062: ObjectName on = null;
063: try {
064: on = new ObjectName(names[0]);
065: } catch (MalformedObjectNameException e) {
066: throw new RuntimeException(e);
067: }
068:
069: // don't replace other injections
070: if (injectors.get(method) == null)
071: injectors.put(method, new DependsMethodInjector(method,
072: on));
073:
074: container.getDependencyPolicy().addDependency(names[0]);
075: }
076: }
077:
078: public void handleFieldAnnotations(Field field,
079: InjectionContainer container,
080: Map<AccessibleObject, Injector> injectors) {
081: Depends dep = container.getAnnotation(Depends.class, field
082: .getDeclaringClass(), field);
083: if (dep != null) {
084: String[] names = dep.value();
085: if (names.length != 1)
086: throw new RuntimeException(
087: "@Depends on a field can only take one object name: "
088: + field);
089: ObjectName on = null;
090: try {
091: on = new ObjectName(names[0]);
092: } catch (MalformedObjectNameException e) {
093: throw new RuntimeException(e);
094: }
095: // don't replace other injections
096: if (injectors.get(field) == null)
097: injectors.put(field,
098: new DependsFieldInjector(field, on));
099:
100: container.getDependencyPolicy().addDependency(names[0]);
101: }
102: }
103:
104: public void handleClassAnnotations(Class clazz,
105: InjectionContainer container) {
106: Depends dep = (Depends) container.getAnnotation(Depends.class,
107: clazz);
108: if (dep == null)
109: return;
110: for (String dependency : dep.value()) {
111: container.getDependencyPolicy().addDependency(dependency);
112: }
113: }
114:
115: }
|