001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EjbJarAnnotationMetadata.java 2059 2007-11-22 17:22:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.metadata;
025:
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030:
031: import javax.ejb.ApplicationException;
032:
033: import org.ow2.easybeans.deployment.annotations.InterceptorType;
034: import org.ow2.easybeans.deployment.annotations.JClassInterceptor;
035: import org.ow2.easybeans.deployment.annotations.impl.JApplicationException;
036: import org.ow2.easybeans.deployment.annotations.impl.JCommonBean;
037: import org.ow2.easybeans.deployment.annotations.impl.JInterceptors;
038: import org.ow2.easybeans.deployment.xml.struct.EJB3;
039: import org.ow2.util.log.Log;
040: import org.ow2.util.log.LogFactory;
041:
042: /**
043: * This class represents the annotation metadata of all classes of an EjbJar
044: * file. From this class, we can get metadata of all beans.
045: * @author Florent Benoit
046: */
047: public class EjbJarAnnotationMetadata {
048:
049: /**
050: * Logger.
051: */
052: private static Log logger = LogFactory
053: .getLog(EjbJarAnnotationMetadata.class);
054:
055: /**
056: * List of class annotations metadata.
057: */
058: private Map<String, ClassAnnotationMetadata> classesAnnotationMetadata = null;
059:
060: /**
061: * Link to the Deployment Descriptor object.
062: */
063: private EJB3 ejb3 = null;
064:
065: /**
066: * List of application exceptions used on this ejb-jar.
067: */
068: private Map<String, ApplicationException> applicationExceptions = null;
069:
070: /**
071: * List of default interceptors by type (business or lifecycle).
072: */
073: private Map<InterceptorType, List<JClassInterceptor>> defaultInterceptors = null;
074:
075: /**
076: * List of default interceptors classes.
077: */
078: private JInterceptors defaultInterceptorsClasses = null;
079:
080: /**
081: * Constructor.
082: */
083: public EjbJarAnnotationMetadata() {
084: classesAnnotationMetadata = new HashMap<String, ClassAnnotationMetadata>();
085: }
086:
087: /**
088: * Add annotation metadata for a given class.
089: * @param classAnnotationMetadata annotation metadata of a class.
090: */
091: public void addClassAnnotationMetadata(
092: final ClassAnnotationMetadata classAnnotationMetadata) {
093: String key = classAnnotationMetadata.getClassName();
094: // already exists ?
095: if (classesAnnotationMetadata.containsKey(key)) {
096: String msg = logger
097: .getI18n()
098: .getMessage(
099: "EjbJarAnnotationMetadata.addClassAnnotationMetadata.alreadyPresent",
100: key);
101: logger.debug(msg);
102: throw new IllegalStateException(msg);
103: }
104: classesAnnotationMetadata.put(key, classAnnotationMetadata);
105: }
106:
107: /**
108: * Gets the class metadata for the given ejb-name.
109: * @return class metadata or null if not found
110: * @param ejbName the name of the EJB.
111: */
112: public ClassAnnotationMetadata getClassAnnotationMetadataForEjbName(
113: final String ejbName) {
114: for (ClassAnnotationMetadata metadata : classesAnnotationMetadata
115: .values()) {
116: JCommonBean bean = metadata.getJCommonBean();
117: if (bean != null && ejbName.equals(bean.getName())) {
118: return metadata;
119: }
120: }
121: return null;
122: }
123:
124: /**
125: * Get class annotation metadata.
126: * @param className key of the map of annotations bean.
127: * @return Bean annotation metadata of a given name.
128: */
129: public ClassAnnotationMetadata getClassAnnotationMetadata(
130: final String className) {
131: return classesAnnotationMetadata.get(className);
132: }
133:
134: /**
135: * Get collections of bean annotation metadata.
136: * @return collections of bean annotation metadata.
137: */
138: public Collection<ClassAnnotationMetadata> getClassAnnotationMetadataCollection() {
139: return classesAnnotationMetadata.values();
140: }
141:
142: /**
143: * @return the ejb3 deployment descriptor object.
144: */
145: public EJB3 getEjb3() {
146: return ejb3;
147: }
148:
149: /**
150: * Sets the ejb3 deployment descriptor object.
151: * @param ejb3 the ejb3 deployment descriptor object.
152: */
153: public void setEjb3(final EJB3 ejb3) {
154: this .ejb3 = ejb3;
155: }
156:
157: /**
158: * Gets the list of application exceptions defined on this ejb jar metadata.
159: * @return the list of application exceptions defined on this ejb jar
160: * metadata.
161: */
162: public Map<String, ApplicationException> getApplicationExceptions() {
163: if (applicationExceptions != null) {
164: return applicationExceptions;
165: }
166:
167: // compute it
168: applicationExceptions = new HashMap<String, ApplicationException>();
169:
170: // For each class, look if it is an application exception
171: for (ClassAnnotationMetadata classMetadata : getClassAnnotationMetadataCollection()) {
172: ApplicationException appException = classMetadata
173: .getApplicationException();
174: // found it then add it in the map.
175: if (appException != null) {
176: applicationExceptions.put(classMetadata.getClassName()
177: .replaceAll("/", "."), appException);
178: }
179: }
180:
181: // Add a default application exception (for checked exception)
182: applicationExceptions.put("DEFAULT",
183: new JApplicationException());
184:
185: return applicationExceptions;
186: }
187:
188: /**
189: * @return Map<interceptor type <--> List of methods/class corresponding to the interceptor> (interceptor classes)
190: * of default interceptors that enhancer will use.
191: */
192: public Map<InterceptorType, List<JClassInterceptor>> getDefaultInterceptors() {
193: return defaultInterceptors;
194: }
195:
196: /**
197: * Sets the list of default interceptors that enhancers will use.<br>
198: * These interceptors are defined by XML DD.
199: * @param defaultInterceptors list of interceptors that enhancer will use.
200: */
201: public void setDefaultInterceptors(
202: final Map<InterceptorType, List<JClassInterceptor>> defaultInterceptors) {
203: this .defaultInterceptors = defaultInterceptors;
204: }
205:
206: /**
207: * @return object representing list of default interceptor classes.
208: */
209: public JInterceptors getDefaultInterceptorsClasses() {
210: return defaultInterceptorsClasses;
211: }
212:
213: /**
214: * Sets the object representing the default interceptor classes.
215: * @param defaultInterceptorsClasses list of classes
216: */
217: public void setDefaultInterceptorsClasses(
218: final JInterceptors defaultInterceptorsClasses) {
219: this.defaultInterceptorsClasses = defaultInterceptorsClasses;
220: }
221:
222: }
|