001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.compapp.javaee.sunresources.tool.annotation;
043:
044: import java.util.ArrayList;
045: import java.util.Hashtable;
046: import java.util.Iterator;
047:
048: import org.netbeans.modules.classfile.Access;
049: import org.netbeans.modules.classfile.ClassFile;
050: import org.netbeans.modules.classfile.ClassName;
051: import org.netbeans.modules.compapp.javaee.sunresources.ResourceAggregator;
052:
053: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.CMap;
054: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.CMapNode;
055: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.EJBInterface;
056: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.EJBNode;
057: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.MDBNode;
058: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.CMapNode.CMapNodeType;
059: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.EJBInterface.EJBInterfaceType;
060: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.ServletNode;
061:
062: /**
063: * @author echou
064: *
065: */
066: public class JavaEEAnnotationProcessor {
067:
068: public static final String LOCAL_CLASSTYPE = "javax/ejb/Local"; // NOI18N
069: public static final String REMOTE_CLASSTYPE = "javax/ejb/Remote"; // NOI18N
070: public static final String STATEFUL_CLASSTYPE = "javax/ejb/Stateful"; // NOI18N
071: public static final String STATELESS_CLASSTYPE = "javax/ejb/Stateless"; // NOI18N
072: public static final String MDB_CLASSTYPE = "javax/ejb/MessageDriven"; // NOI18N
073: public static final String WEBSERVICE_CLASSTYPE = "javax/jws/WebService"; // NOI18N
074: public static final String EJB_CLASSTYPE = "javax/ejb/EJB"; // NOI18N
075: public static final String EJBS_CLASSTYPE = "javax/ejb/EJBs"; // NOI18N
076: public static final String RESOURCE_CLASSTYPE = "javax/annotation/Resource"; // NOI18N
077: public static final String RESOURCES_CLASSTYPE = "javax/annotation/Resources"; // NOI18N
078: public static final String WEBSERVICEREF_CLASSTYPE = "javax/xml/ws/WebServiceRef"; // NOI18N
079: public static final String WEBSERVICEREFS_CLASSTYPE = "javax/xml/ws/WebServiceRefs"; // NOI18N
080: public static final String SERVLET_CLASSTYPE = "javax/servlet/Servlet"; // NOI18N
081: public static final String GENERIC_SERVLET_CLASSTYPE = "javax/servlet/GenericServlet"; // NOI18N
082: public static final String HTTP_SERVLET_CLASSTYPE = "javax/servlet/http/HttpServlet"; // NOI18N
083:
084: private EJBAnnotation ejbAnnotation;
085: private DependencyAnnotation dependencyAnnotation;
086: private CMap cmap;
087:
088: // keep track of Local/Remote interfaces
089: private Hashtable<String, EJBInterfaceType> intfTable = new Hashtable<String, EJBInterfaceType>();
090:
091: public JavaEEAnnotationProcessor(CMap cmap,
092: ResourceAggregator resAggregator) {
093: this .ejbAnnotation = new EJBAnnotation(resAggregator);
094: this .dependencyAnnotation = new DependencyAnnotation(
095: resAggregator);
096: this .cmap = cmap;
097: }
098:
099: /**
100: *
101: * @param cls declaring class of annotations to process
102: * @throws Exception
103: */
104: /*
105: public void process(Class<?> cls) throws Exception {
106: if (cls.isInterface()) { // process Interface
107: if (cls.isAnnotationPresent(Local.class)) {
108: intfTable.put(cls.getName(), EJBInterfaceType.LOCAL);
109: } else if (cls.isAnnotationPresent(Remote.class)) {
110: intfTable.put(cls.getName(), EJBInterfaceType.REMOTE);
111: }
112: } else { // process Class
113: if (cls.isAnnotationPresent(WebService.class) &&
114: cls.isAnnotationPresent(Stateless.class)) {
115: EJBNode node = new EJBNode(cls, CMapNodeType.STATELESS);
116: node.setIsWebService(true);
117: ejbAnnotation.fillWebServiceNode(node, cls);
118: dependencyAnnotation.fillDependencyInfo(node, cls);
119: cmap.addNode(cls.getName(), node);
120: } else if (cls.isAnnotationPresent(Stateless.class)) {
121: EJBNode node = new EJBNode(cls, CMapNodeType.STATELESS);
122: ejbAnnotation.fillStatelessNode(node, cls);
123: dependencyAnnotation.fillDependencyInfo(node, cls);
124: cmap.addNode(cls.getName(), node);
125: } else if (cls.isAnnotationPresent(Stateful.class)) {
126: EJBNode node = new EJBNode(cls, CMapNodeType.STATEFUL);
127: ejbAnnotation.fillStatefulNode(node, cls);
128: dependencyAnnotation.fillDependencyInfo(node, cls);
129: cmap.addNode(cls.getName(), node);
130: } else if (cls.isAnnotationPresent(MessageDriven.class)) {
131: MDBNode node = new MDBNode(cls, CMapNodeType.MDB);
132: ejbAnnotation.fillMessageDrivenNode(node, cls);
133: dependencyAnnotation.fillDependencyInfo(node, cls);
134: cmap.addNode(cls.getName(), node);
135: }
136: }
137: }
138: **/
139:
140: /*
141: * process annotation using NetBeans ClassFile API
142: */
143: public void process(ClassFile cls) throws Exception {
144: if ((cls.getAccess() & Access.INTERFACE) == Access.INTERFACE) { // process Interface
145: if (cls.isAnnotationPresent(ClassName
146: .getClassName(LOCAL_CLASSTYPE))) {
147: intfTable.put(cls.getName().getExternalName(),
148: EJBInterfaceType.LOCAL);
149: } else if (cls.isAnnotationPresent(ClassName
150: .getClassName(REMOTE_CLASSTYPE))) {
151: intfTable.put(cls.getName().getExternalName(),
152: EJBInterfaceType.REMOTE);
153: }
154: } else { // process Class
155: if (cls.isAnnotationPresent(ClassName
156: .getClassName(WEBSERVICE_CLASSTYPE))
157: && cls.isAnnotationPresent(ClassName
158: .getClassName(STATELESS_CLASSTYPE))) {
159: EJBNode node = new EJBNode(cls, CMapNodeType.STATELESS);
160: node.setIsWebService(true);
161: ejbAnnotation.fillWebServiceNode(node, cls);
162: dependencyAnnotation.fillDependencyInfo(node, cls);
163: cmap.addNode(cls.getName().getExternalName(), node);
164: } else if (cls.isAnnotationPresent(ClassName
165: .getClassName(STATELESS_CLASSTYPE))) {
166: EJBNode node = new EJBNode(cls, CMapNodeType.STATELESS);
167: ejbAnnotation.fillStatelessNode(node, cls);
168: dependencyAnnotation.fillDependencyInfo(node, cls);
169: cmap.addNode(cls.getName().getExternalName(), node);
170: } else if (cls.isAnnotationPresent(ClassName
171: .getClassName(STATEFUL_CLASSTYPE))) {
172: EJBNode node = new EJBNode(cls, CMapNodeType.STATEFUL);
173: ejbAnnotation.fillStatefulNode(node, cls);
174: dependencyAnnotation.fillDependencyInfo(node, cls);
175: cmap.addNode(cls.getName().getExternalName(), node);
176: } else if (cls.isAnnotationPresent(ClassName
177: .getClassName(MDB_CLASSTYPE))) {
178: MDBNode node = new MDBNode(cls, CMapNodeType.MDB);
179: ejbAnnotation.fillMessageDrivenNode(node, cls);
180: dependencyAnnotation.fillDependencyInfo(node, cls);
181: cmap.addNode(cls.getName().getExternalName(), node);
182: } else if (isServlet(cls)) {
183: ServletNode node = new ServletNode(cls,
184: CMapNodeType.SERVLET);
185: dependencyAnnotation.fillDependencyInfo(node, cls);
186: cmap.addNode(cls.getName().getExternalName(), node);
187: }
188: }
189: }
190:
191: /*
192: * post processing
193: * need to find out if EJB interface is Local or Remote
194: */
195: public void postProcess() throws Exception {
196: // check global interface table to see if need to set Local/Remote type
197: // on the ejb bean implemented interfaces
198: for (Iterator<CMapNode> iter = cmap.getNodes(); iter.hasNext();) {
199: CMapNode node = iter.next();
200: if (node instanceof EJBNode) {
201: EJBNode ejbNode = (EJBNode) node;
202: ArrayList<EJBInterface> ejbIntfs = ejbNode
203: .getImplementedIntfs();
204: for (int i = 0; i < ejbIntfs.size(); i++) {
205: EJBInterface ejbIntf = ejbIntfs.get(i);
206: EJBInterfaceType type = intfTable.get(ejbIntf
207: .getIntf());
208: if (type != null && type != ejbIntf.getIntfType()) {
209: ejbIntf.setIntfType(type);
210: }
211: }
212: }
213: }
214:
215: cmap.postProcess();
216: }
217:
218: private boolean isServlet(ClassFile cls) {
219: for (Iterator iter = cls.getInterfaces().iterator(); iter
220: .hasNext();) {
221: String s = (String) iter.next();
222: if (SERVLET_CLASSTYPE.equals(s)) {
223: return true;
224: }
225: }
226: // check superclass
227: if (cls.getSuperClass().equals(
228: ClassName.getClassName(GENERIC_SERVLET_CLASSTYPE))
229: || cls.getSuperClass().equals(
230: ClassName.getClassName(HTTP_SERVLET_CLASSTYPE))) {
231: return true;
232: }
233: return false;
234: }
235: }
|