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.archive;
043:
044: import java.net.URLClassLoader;
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectUtils;
051: import org.netbeans.modules.compapp.javaee.sunresources.ResourceAggregator;
052: import org.netbeans.modules.compapp.javaee.sunresources.SunResourcesUtil;
053:
054: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.CMap;
055: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.CMapNode;
056: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.EJBNode;
057: import org.netbeans.modules.compapp.javaee.sunresources.tool.graph.JAXBHandler;
058: import org.openide.filesystems.FileObject;
059: import org.openide.util.NbBundle;
060:
061: /**
062: * @author echou
063: *
064: */
065: public class ApplicationArchive extends Archive {
066:
067: private Project p;
068: private String name;
069: private ResourceAggregator resAggregator;
070:
071: private CMap cmap;
072: private SunResourcesDDJaxbHandler sunResourcesDD;
073: private JAXBHandler jaxbHandler;
074: private List<Archive> subProjects = new ArrayList<Archive>();
075: private URLClassLoader appClassLoader;
076:
077: public ApplicationArchive(Project p) throws Exception {
078: this .p = p;
079: this .name = ProjectUtils.getInformation(p).getName();
080: this .resAggregator = new ResourceAggregator(p);
081: this .cmap = new CMap(this .name);
082:
083: List<Project> childrenProjects = SunResourcesUtil
084: .getSubProjects(p);
085: for (int i = 0; i < childrenProjects.size(); i++) {
086: Project childProject = childrenProjects.get(i);
087: ArchiveConstants.ArchiveType projType = SunResourcesUtil
088: .getJavaEEProjectType(childProject);
089:
090: if (projType == ArchiveConstants.ArchiveType.EJB) {
091: EJBArchive ejbArchive = new EJBArchive(childProject);
092: subProjects.add(ejbArchive);
093: } else if (projType == ArchiveConstants.ArchiveType.WAR) {
094: WebArchive webArchive = new WebArchive(childProject);
095: subProjects.add(webArchive);
096: } else {
097: throw new Exception(NbBundle.getMessage(
098: ApplicationArchive.class,
099: "EXC_bad_project_type", projType));
100: }
101: }
102:
103: /*
104: this.jaxbHandler =
105: new JAXBHandler(new File(archive, ArchiveConstants.GRAPH_DESCRIPTOR_PATH));
106: **/
107: }
108:
109: @Override
110: public String getName() {
111: return this .name;
112: }
113:
114: public void open() throws Exception {
115: // aggregate *.sun-resource files into memory here
116: FileObject resourceDirFO = SunResourcesUtil
117: .getResourceDir(this .p);
118: FileObject[] children = resourceDirFO.getChildren();
119: for (int i = 0; i < children.length; i++) {
120: FileObject fo = children[i];
121: if (!fo.isFolder()
122: && fo.getExt().equalsIgnoreCase("sun-resource")) { // NOI18N
123: resAggregator.addResource(fo);
124: }
125: }
126:
127: for (int i = 0; i < subProjects.size(); i++) {
128: Archive subProject = subProjects.get(i);
129: subProject.open();
130: }
131:
132: //process();
133: }
134:
135: /*
136: private void process() throws Exception {
137: processAnnotations();
138: processDD();
139: postProcess();
140: }
141:
142:
143: private void processAnnotations() throws Exception {
144: JavaEEAnnotationProcessor annoProcessor =
145: new JavaEEAnnotationProcessor(cmap, sunResourcesDD);
146: for (int i = 0; i < subProjects.size(); i++) {
147: EJBArchive archive = subProjects.get(i);
148: for (int j = 0; j < archive.getClassNames().size(); j++) {
149: String className = archive.getClassNames().get(j);
150: Class<?> cls = Class.forName(className, true, appClassLoader);
151: // process each class object
152: annoProcessor.process(cls);
153: }
154: }
155: annoProcessor.postProcess();
156:
157: }
158:
159:
160: private void processDD() throws Exception {
161: for (int i = 0; i < subProjects.size(); i++) {
162: EJBArchive archive = subProjects.get(i);
163: ArrayList<CMapNode> nodes = archive.getCMapNodes();
164: if (nodes != null) {
165: for (int j = 0; j < nodes.size(); j++) {
166: CMapNode node = nodes.get(j);
167: cmap.addNode(node.getNodeClass(), node);
168: }
169: }
170: }
171: }
172:
173: private void postProcess() throws Exception {
174: // find all the target node for EJB dependency
175: for (Iterator<CMapNode> iter = cmap.getNodes(); iter.hasNext(); ) {
176: CMapNode node = iter.next();
177: ArrayList<EJBDepend> ejbDepends = node.getEjbDepends();
178: for (int i = 0; i < ejbDepends.size(); i++) {
179: EJBDepend ejbDepend = ejbDepends.get(i);
180: CMapNode targetNode = findEJBNodeImplIntf(ejbDepend.getTargetIntfName());
181: ejbDepend.setTarget(targetNode);
182: }
183: }
184:
185: // find all the target node for Resource dependency, if none exists,
186: // create one, since resource nodes are abstract
187: for (Iterator<CMapNode> iter = cmap.getNodes(); iter.hasNext(); ) {
188: CMapNode node = iter.next();
189: ArrayList<ResourceDepend> resDepends = node.getResDepends();
190: for (int i = 0; i < resDepends.size(); i++) {
191: ResourceDepend resDepend = resDepends.get(i);
192: ResourceNode resNode = cmap.findResNode(resDepend);
193: if (resNode == null) {
194: String logicalName;
195: if (resDepend.getMappedName() == null ||
196: resDepend.getMappedName().equals("")) {
197: logicalName = resDepend.getTargetResJndiName();
198: } else {
199: logicalName = resDepend.getMappedName();
200: }
201: resNode = new ResourceNode(logicalName,
202: resDepend.getTargetResType(),
203: resDepend.getTargetResJndiName(),
204: resDepend.getType(),
205: resDepend.getProps());
206: cmap.addResNode(resNode);
207: }
208: resDepend.setTarget(resNode);
209: }
210: }
211:
212: // after all the Resource nodes are created, we need to find out
213: // which resource node is each MDB listening to
214: for (Iterator<CMapNode> iter = cmap.getNodes(); iter.hasNext(); ) {
215: CMapNode node = iter.next();
216: if (node instanceof MDBNode) {
217: MDBNode mdbNode = (MDBNode) node;
218: ResourceNode resNode = cmap.findResNodeByLogicalName(mdbNode.getMappedName());
219: if (resNode == null) {
220: // if the resource node does not exist, then create it
221: resNode = new ResourceNode(mdbNode.getMappedName(),
222: null,
223: null,
224: ResourceType.JMS,
225: null);
226: cmap.addResNode(resNode);
227: }
228: mdbNode.setTargetListenNode(resNode);
229: }
230: }
231: }
232: */
233:
234: private EJBNode findEJBNodeImplIntf(String targetIntfName) {
235: for (Iterator<CMapNode> iter = cmap.getNodes(); iter.hasNext();) {
236: CMapNode node = iter.next();
237: if (node instanceof EJBNode) {
238: EJBNode ejbNode = (EJBNode) node;
239: if (ejbNode.implements Interface(targetIntfName)) {
240: return ejbNode;
241: }
242: }
243: }
244: return null;
245: }
246:
247: public void close() throws Exception {
248: this .appClassLoader = null;
249: this .resAggregator.close();
250: this .resAggregator = null;
251: }
252:
253: public String toString() {
254: return this .name;
255: }
256:
257: public CMap getCMap() {
258: return this .cmap;
259: }
260:
261: @Override
262: public JAXBHandler getJAXBHandler() {
263: return this .jaxbHandler;
264: }
265:
266: public ResourceAggregator getResourceAggregator() {
267: return this .resAggregator;
268: }
269:
270: public FileObject getResourceDir() {
271: return null;
272: }
273: }
|