View Javadoc

1   package com.bm.ejb3metadata.annotations.metadata;
2   
3   import java.util.Collection;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import javax.ejb.ApplicationException;
8   
9   import com.bm.ejb3metadata.xml.struct.EJB3;
10  
11  /**
12   * This class represents the annotation metadata of all classes of an EjbJar
13   * file. From this class, we can get metadata of all beans.
14   * 
15   * @author Daniel Wiese
16   */
17  public class EjbJarAnnotationMetadata {
18  
19  	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory
20  			.getLogger(EjbJarAnnotationMetadata.class);
21  
22  	/**
23  	 * List of class annotations metadata.
24  	 */
25  	private Map<String, ClassAnnotationMetadata> classesAnnotationMetadata = null;
26  
27  	/**
28  	 * Map of interface names and the first possible implementation Currently we
29  	 * don't manage different implementations of a session bean TODO check the
30  	 * spect: if a Local/Romete interface has 2 different implementation
31  	 */
32  	private Map<String, String> interface2implemantation = null;
33  
34  	/**
35  	 * Link to the Deployment Descriptor object.
36  	 */
37  	private EJB3 ejb3 = null;
38  
39  	/**
40  	 * List of application exceptions used on this ejb-jar.
41  	 */
42  	private Map<String, ApplicationException> applicationExceptions = null;
43  
44  	/**
45  	 * Constructor.
46  	 */
47  	public EjbJarAnnotationMetadata() {
48  		classesAnnotationMetadata = new HashMap<String, ClassAnnotationMetadata>();
49  	}
50  
51  	/**
52  	 * Merge the items from another
53  	 * 
54  	 * @param other
55  	 */
56  	public void mergeClassAnnotationMetadata(EjbJarAnnotationMetadata other) {
57  		classesAnnotationMetadata.putAll(other.classesAnnotationMetadata);
58  	}
59  
60  	/**
61  	 * Add annotation metadata for a given class.
62  	 * 
63  	 * @param classAnnotationMetadata
64  	 *            annotation metadata of a class.
65  	 */
66  	public void addClassAnnotationMetadata(final ClassAnnotationMetadata classAnnotationMetadata) {
67  		String key = classAnnotationMetadata.getClassName();
68  		// already exists ?
69  		if (classesAnnotationMetadata.containsKey(key)) {
70  			String msg = "EjbJarAnnotationMetadata.addClassAnnotationMetadata.alreadyPresent";
71  			logger.trace(msg);
72  			// throw new IllegalStateException(msg);
73  		}
74  		classesAnnotationMetadata.put(key, classAnnotationMetadata);
75  	}
76  
77  	/**
78  	 * Returns the name of the bean class by passing the rmote/ local interface
79  	 * name.
80  	 * 
81  	 * @return the name of the bean class by passing the rmote/ local interface
82  	 *         name.
83  	 * @param interfaceName -
84  	 *            the name of the local / remote interface
85  	 */
86  	public String getBeanImplementationForInterface(String interfaceName) {
87  		return this.interface2implemantation.get(interfaceName);
88  	}
89  
90  	/**
91  	 * Returns the name of the bean class by passing the rmote/ local interface
92  	 * name.
93  	 * 
94  	 * @return the name of the bean class by passing the rmote/ local interface
95  	 *         name.
96  	 * @param interfaceName -
97  	 *            the name of the local / remote interface
98  	 */
99  	public String getBeanImplementationForInterface(Class interfaceName) {
100 		final String name = interfaceName.getName();
101 		if (interface2implemantation == null) {
102 			buildInterfaceImplementationMap();
103 		}
104 		return this.interface2implemantation.get(name.replace('.', '/'));
105 	}
106 
107 	/**
108 	 * Builds a map of Local/Remote interfaces and it's implementations.
109 	 */
110 	private void buildInterfaceImplementationMap() {
111 		interface2implemantation = new HashMap<String, String>();
112 		for (ClassAnnotationMetadata current : this.getClassAnnotationMetadataCollection()) {
113 			if (current.getLocalInterfaces() != null) {
114 				for (String interfaze : current.getLocalInterfaces().getInterfaces()) {
115 					// build no implementation map for anonymus classes and
116 					// inner classes
117 					if (current.getClassName().indexOf('$') < 0) {
118 						this.interface2implemantation.put(interfaze, current.getClassName());
119 					}
120 				}
121 			}
122 			if (current.getRemoteInterfaces() != null) {
123 				for (String interfaze : current.getRemoteInterfaces().getInterfaces()) {
124 					// build no implementation map for anonymus classes and
125 					// inner classes
126 					if (current.getClassName().indexOf('$') < 0) {
127 						this.interface2implemantation.put(interfaze, current.getClassName());
128 					}
129 				}
130 			}
131 		}
132 	}
133 
134 	/**
135 	 * Get class annotation metadata.
136 	 * 
137 	 * @param className
138 	 *            key of the map of annotations bean.
139 	 * @return Bean annotation metadata of a given name.
140 	 */
141 	public ClassAnnotationMetadata getClassAnnotationMetadata(final String className) {
142 		return classesAnnotationMetadata.get(className);
143 	}
144 
145 	/**
146 	 * Get collections of bean annotation metadata.
147 	 * 
148 	 * @return collections of bean annotation metadata.
149 	 */
150 	public Collection<ClassAnnotationMetadata> getClassAnnotationMetadataCollection() {
151 		return classesAnnotationMetadata.values();
152 	}
153 
154 	/**
155 	 * @return the ejb3 deployment descriptor object.
156 	 */
157 	public EJB3 getEjb3() {
158 		return ejb3;
159 	}
160 
161 	/**
162 	 * Sets the ejb3 deployment descriptor object.
163 	 * 
164 	 * @param ejb3
165 	 *            the ejb3 deployment descriptor object.
166 	 */
167 	public void setEjb3(final EJB3 ejb3) {
168 		this.ejb3 = ejb3;
169 	}
170 
171 	/**
172 	 * Gets the list of application exceptions defined on this ejb jar metadata.
173 	 * 
174 	 * @return the list of application exceptions defined on this ejb jar
175 	 *         metadata.
176 	 */
177 	public Map<String, ApplicationException> getApplicationExceptions() {
178 		if (applicationExceptions != null) {
179 			return applicationExceptions;
180 		}
181 
182 		// compute it
183 		applicationExceptions = new HashMap<String, ApplicationException>();
184 
185 		// For each class, look if it is an application exception
186 		for (ClassAnnotationMetadata classMetadata : getClassAnnotationMetadataCollection()) {
187 			ApplicationException appException = classMetadata.getApplicationException();
188 			// found it then add it in the map.
189 			if (appException != null) {
190 				applicationExceptions.put(classMetadata.getClassName().replaceAll("/", "."),
191 						appException);
192 			}
193 		}
194 		return applicationExceptions;
195 	}
196 
197 	/**
198 	 * Returns the interface2implemantation.
199 	 * 
200 	 * @return Returns the interface2implemantation.
201 	 */
202 	public Map<String, String> getInterface2implemantation() {
203 		if (interface2implemantation == null) {
204 			buildInterfaceImplementationMap();
205 		}
206 		return interface2implemantation;
207 	}
208 
209 }