001: package com.bm.ejb3metadata.annotations.metadata;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005: import java.util.Map;
006:
007: import javax.ejb.ApplicationException;
008:
009: import com.bm.ejb3metadata.xml.struct.EJB3;
010:
011: /**
012: * This class represents the annotation metadata of all classes of an EjbJar
013: * file. From this class, we can get metadata of all beans.
014: *
015: * @author Daniel Wiese
016: */
017: public class EjbJarAnnotationMetadata {
018:
019: private final static org.apache.log4j.Logger logger = org.apache.log4j.Logger
020: .getLogger(EjbJarAnnotationMetadata.class);
021:
022: /**
023: * List of class annotations metadata.
024: */
025: private Map<String, ClassAnnotationMetadata> classesAnnotationMetadata = null;
026:
027: /**
028: * Map of interface names and the first possible implementation Currently we
029: * donīt manage different implementations of a session bean TODO check the
030: * spect: if a Local/Romete interface has 2 different implementation
031: */
032: private Map<String, String> interface2implemantation = null;
033:
034: /**
035: * Link to the Deployment Descriptor object.
036: */
037: private EJB3 ejb3 = null;
038:
039: /**
040: * List of application exceptions used on this ejb-jar.
041: */
042: private Map<String, ApplicationException> applicationExceptions = null;
043:
044: /**
045: * Constructor.
046: */
047: public EjbJarAnnotationMetadata() {
048: classesAnnotationMetadata = new HashMap<String, ClassAnnotationMetadata>();
049: }
050:
051: /**
052: * Merge the items from another
053: *
054: * @param other
055: */
056: public void mergeClassAnnotationMetadata(
057: EjbJarAnnotationMetadata other) {
058: classesAnnotationMetadata
059: .putAll(other.classesAnnotationMetadata);
060: }
061:
062: /**
063: * Add annotation metadata for a given class.
064: *
065: * @param classAnnotationMetadata
066: * annotation metadata of a class.
067: */
068: public void addClassAnnotationMetadata(
069: final ClassAnnotationMetadata classAnnotationMetadata) {
070: String key = classAnnotationMetadata.getClassName();
071: // already exists ?
072: if (classesAnnotationMetadata.containsKey(key)) {
073: String msg = "EjbJarAnnotationMetadata.addClassAnnotationMetadata.alreadyPresent";
074: logger.debug(msg);
075: // throw new IllegalStateException(msg);
076: }
077: classesAnnotationMetadata.put(key, classAnnotationMetadata);
078: }
079:
080: /**
081: * Returns the name of the bean class by passing the rmote/ local interface
082: * name.
083: *
084: * @return the name of the bean class by passing the rmote/ local interface
085: * name.
086: * @param interfaceName -
087: * the name of the local / remote interface
088: */
089: public String getBeanImplementationForInterface(String interfaceName) {
090: return this .interface2implemantation.get(interfaceName);
091: }
092:
093: /**
094: * Returns the name of the bean class by passing the rmote/ local interface
095: * name.
096: *
097: * @return the name of the bean class by passing the rmote/ local interface
098: * name.
099: * @param interfaceName -
100: * the name of the local / remote interface
101: */
102: public String getBeanImplementationForInterface(Class interfaceName) {
103: final String name = interfaceName.getName();
104: if (interface2implemantation == null) {
105: buildInterfaceImplementationMap();
106: }
107: return this .interface2implemantation
108: .get(name.replace('.', '/'));
109: }
110:
111: /**
112: * Builds a map of Local/Remote interfaces and itīs implementations.
113: */
114: private void buildInterfaceImplementationMap() {
115: interface2implemantation = new HashMap<String, String>();
116: for (ClassAnnotationMetadata current : this
117: .getClassAnnotationMetadataCollection()) {
118: if (current.getLocalInterfaces() != null) {
119: for (String interfaze : current.getLocalInterfaces()
120: .getInterfaces()) {
121: // build no implementation map for anonymus classes and
122: // inner classes
123: if (current.getClassName().indexOf('$') < 0) {
124: this .interface2implemantation.put(interfaze,
125: current.getClassName());
126: }
127: }
128: }
129: if (current.getRemoteInterfaces() != null) {
130: for (String interfaze : current.getRemoteInterfaces()
131: .getInterfaces()) {
132: // build no implementation map for anonymus classes and
133: // inner classes
134: if (current.getClassName().indexOf('$') < 0) {
135: this .interface2implemantation.put(interfaze,
136: current.getClassName());
137: }
138: }
139: }
140: }
141: }
142:
143: /**
144: * Get class annotation metadata.
145: *
146: * @param className
147: * key of the map of annotations bean.
148: * @return Bean annotation metadata of a given name.
149: */
150: public ClassAnnotationMetadata getClassAnnotationMetadata(
151: final String className) {
152: return classesAnnotationMetadata.get(className);
153: }
154:
155: /**
156: * Get collections of bean annotation metadata.
157: *
158: * @return collections of bean annotation metadata.
159: */
160: public Collection<ClassAnnotationMetadata> getClassAnnotationMetadataCollection() {
161: return classesAnnotationMetadata.values();
162: }
163:
164: /**
165: * @return the ejb3 deployment descriptor object.
166: */
167: public EJB3 getEjb3() {
168: return ejb3;
169: }
170:
171: /**
172: * Sets the ejb3 deployment descriptor object.
173: *
174: * @param ejb3
175: * the ejb3 deployment descriptor object.
176: */
177: public void setEjb3(final EJB3 ejb3) {
178: this .ejb3 = ejb3;
179: }
180:
181: /**
182: * Gets the list of application exceptions defined on this ejb jar metadata.
183: *
184: * @return the list of application exceptions defined on this ejb jar
185: * metadata.
186: */
187: public Map<String, ApplicationException> getApplicationExceptions() {
188: if (applicationExceptions != null) {
189: return applicationExceptions;
190: }
191:
192: // compute it
193: applicationExceptions = new HashMap<String, ApplicationException>();
194:
195: // For each class, look if it is an application exception
196: for (ClassAnnotationMetadata classMetadata : getClassAnnotationMetadataCollection()) {
197: ApplicationException appException = classMetadata
198: .getApplicationException();
199: // found it then add it in the map.
200: if (appException != null) {
201: applicationExceptions.put(classMetadata.getClassName()
202: .replaceAll("/", "."), appException);
203: }
204: }
205: return applicationExceptions;
206: }
207:
208: /**
209: * Returns the interface2implemantation.
210: *
211: * @return Returns the interface2implemantation.
212: */
213: public Map<String, String> getInterface2implemantation() {
214: if (interface2implemantation == null) {
215: buildInterfaceImplementationMap();
216: }
217: return interface2implemantation;
218: }
219:
220: }
|