KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > injection > DependsHandler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.injection;
23
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.AccessibleObject JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30
31 import org.jboss.annotation.ejb.Depends;
32 import org.jboss.logging.Logger;
33 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
34
35 /**
36  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
37  * @version $Revision: 55144 $
38  */

39 public class DependsHandler implements InjectionHandler
40 {
41    private static final Logger log = Logger.getLogger(DependsHandler.class);
42
43    public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
44    {
45    }
46
47    public void handleMethodAnnotations(Method JavaDoc method, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
48    {
49       Depends dep = container.getAnnotation(Depends.class, method.getDeclaringClass(), method);
50       if (dep != null)
51       {
52          if (!method.getName().startsWith("set"))
53             throw new RuntimeException JavaDoc("@EJB can only be used with a set method: " + method);
54          String JavaDoc[] names = dep.value();
55          if (names.length != 1)
56             throw new RuntimeException JavaDoc("@Depends on a field can only take one object name: " + method);
57          ObjectName JavaDoc on = null;
58          try
59          {
60             on = new ObjectName JavaDoc(names[0]);
61          }
62          catch (MalformedObjectNameException JavaDoc e)
63          {
64             throw new RuntimeException JavaDoc(e);
65          }
66          injectors.put(method, new DependsMethodInjector(method, on));
67          container.getDependencyPolicy().addDependency(names[0]);
68       }
69    }
70
71    public void handleFieldAnnotations(Field JavaDoc field, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
72    {
73       Depends dep = container.getAnnotation(Depends.class, field.getDeclaringClass(), field);
74       if (dep != null)
75       {
76          String JavaDoc[] names = dep.value();
77          if (names.length != 1)
78             throw new RuntimeException JavaDoc("@Depends on a field can only take one object name: " + field);
79          ObjectName JavaDoc on = null;
80          try
81          {
82             on = new ObjectName JavaDoc(names[0]);
83          }
84          catch (MalformedObjectNameException JavaDoc e)
85          {
86             throw new RuntimeException JavaDoc(e);
87          }
88          injectors.put(field, new DependsFieldInjector(field, on));
89          container.getDependencyPolicy().addDependency(names[0]);
90       }
91    }
92
93    public void handleClassAnnotations(Class JavaDoc clazz, InjectionContainer container)
94    {
95       Depends dep = (Depends)container.getAnnotation(Depends.class, clazz);
96       if (dep == null) return;
97       for (String JavaDoc dependency : dep.value())
98       {
99          container.getDependencyPolicy().addDependency(dependency);
100       }
101    }
102
103 }
104
Popular Tags