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.util.Enumeration;
045: import java.util.jar.JarEntry;
046: import java.util.jar.JarFile;
047:
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectUtils;
050: import org.netbeans.modules.classfile.ClassFile;
051: import org.netbeans.modules.compapp.javaee.sunresources.ResourceAggregator;
052: import org.netbeans.modules.compapp.javaee.sunresources.SunResourcesUtil;
053: import org.netbeans.modules.compapp.javaee.sunresources.tool.annotation.JavaEEAnnotationProcessor;
054: import org.netbeans.modules.compapp.javaee.sunresources.tool.cmap.CMap;
055:
056: import org.netbeans.modules.compapp.javaee.sunresources.tool.graph.JAXBHandler;
057: import org.netbeans.modules.compapp.projects.jbi.jeese.actions.ServerResourcesAction;
058: import org.openide.filesystems.FileObject;
059: import org.openide.util.NbBundle;
060:
061: /**
062: * @author echou
063: *
064: */
065: public class EJBArchive extends Archive {
066:
067: private Project p;
068: private JavaEEAnnotationProcessor annoProcessor;
069: private CMap cmap;
070: private String name;
071: private ResourceAggregator resAggregator;;
072:
073: private WebservicesDDJaxbHandler webservicesDD;
074: private SunEjbDDJaxbHandler sunEjbDD;
075: private EjbDDJaxbHandler ejbDD;
076: private FileObject resourceDirFO;
077:
078: /*
079: * this constructor is called if it is not stand-alone EJB
080: */
081: public EJBArchive(Project p, CMap cmap,
082: JavaEEAnnotationProcessor annoProcessor) throws Exception {
083: this .p = p;
084: this .name = ProjectUtils.getInformation(p).getName();
085: this .cmap = cmap;
086: this .annoProcessor = annoProcessor;
087: this .resAggregator = new ResourceAggregator(p);
088: this .distJarPropName = "dist.jar"; // NOI18N
089: }
090:
091: /*
092: * this constructor is called if it is a stand-alone EJB
093: */
094: public EJBArchive(Project p) throws Exception {
095: this .p = p;
096: this .name = ProjectUtils.getInformation(p).getName();
097: this .cmap = new CMap(name);
098: this .resAggregator = new ResourceAggregator(p);
099: this .annoProcessor = new JavaEEAnnotationProcessor(cmap,
100: resAggregator);
101: this .distJarPropName = "dist.jar"; // NOI18N
102: }
103:
104: @Override
105: public String getName() {
106: return this .name;
107: }
108:
109: @Override
110: public void open() throws Exception {
111: // aggregate *.sun-resource files into memory here
112: resourceDirFO = SunResourcesUtil.getResourceDir(this .p);
113: FileObject[] children = resourceDirFO.getChildren();
114: for (int i = 0; i < children.length; i++) {
115: FileObject fo = children[i];
116: if (!fo.isFolder()
117: && fo.getExt().equalsIgnoreCase("sun-resource")) { // NOI18N
118: resAggregator.addResource(fo);
119: }
120: }
121:
122: // scan annotations
123: FileObject ejbJarFO = SunResourcesUtil.getProjectDistJar(
124: this .p, this .distJarPropName);
125: if (ejbJarFO == null) {
126: throw new ServerResourcesAction.ProjectNotBuiltException(
127: NbBundle.getMessage(EJBArchive.class,
128: "EXC_project_not_build", this .name));
129: }
130:
131: JarFile jf = null;
132: try {
133: jf = new JarFile(org.openide.filesystems.FileUtil
134: .toFile(ejbJarFO));
135: for (Enumeration<JarEntry> e = jf.entries(); e
136: .hasMoreElements();) {
137: JarEntry jarEntry = e.nextElement();
138: if (jarEntry.getName().endsWith(".class")) { // NOI18N
139: ClassFile classfile = new ClassFile(jf
140: .getInputStream(jarEntry));
141: annoProcessor.process(classfile);
142: }
143: }
144: } finally {
145: FileUtil.safecloseJar(jf);
146: }
147:
148: annoProcessor.postProcess();
149:
150: //readDescriptors();
151: }
152:
153: /*
154: private void readDescriptors() {
155: webservicesDD = readWebservicesDD();
156: sunEjbDD = readSunEjbDD();
157: ejbDD = readEjbDD();
158: }
159:
160: private EjbDDJaxbHandler readEjbDD() {
161: try {
162: // find DD in this archive
163: URL url = getJarURL(ArchiveConstants.EJB_DESCRIPTOR_PATH);
164:
165: JAXBContext jc = JAXBContext.newInstance("com.sun.wasilla.jaxb.ejb21",
166: this.getClass().getClassLoader());
167: Unmarshaller u = jc.createUnmarshaller();
168: JAXBElement<?> root = (JAXBElement<?>) u.unmarshal(url);
169:
170: EjbDDJaxbHandler ejbDD =
171: new EjbDDJaxbHandler(root.getValue(), sunEjbDD, webservicesDD);
172: return ejbDD;
173: } catch (Exception e) {
174: //e.printStackTrace();
175: return null;
176: }
177: }
178:
179: private SunEjbDDJaxbHandler readSunEjbDD() {
180: try {
181: // find DD in this archive
182: URL url = getJarURL(ArchiveConstants.SUN_EJB_DESCRIPTOR_PATH);
183:
184: JAXBContext jc = JAXBContext.newInstance("com.sun.wasilla.jaxb.sunejb30",
185: this.getClass().getClassLoader());
186: Unmarshaller u = jc.createUnmarshaller();
187: Object root = u.unmarshal(url);
188:
189: SunEjbDDJaxbHandler sunEjbDD = new SunEjbDDJaxbHandler(root);
190: return sunEjbDD;
191: } catch (Exception e) {
192: //e.printStackTrace();
193: }
194: return null;
195: }
196:
197: private WebservicesDDJaxbHandler readWebservicesDD() {
198: try {
199: // find DD in this archive
200: URL url = getJarURL(ArchiveConstants.WEB_SERVICES_DESCRIPTOR_PATH);
201:
202: JAXBContext jc = JAXBContext.newInstance("com.sun.wasilla.jaxb.webservices11",
203: this.getClass().getClassLoader());
204: Unmarshaller u = jc.createUnmarshaller();
205: JAXBElement<?> root = (JAXBElement<?>) u.unmarshal(url);
206:
207: WebservicesDDJaxbHandler webservicesDD = new WebservicesDDJaxbHandler(this, root.getValue());
208: return webservicesDD;
209: } catch (Exception e) {
210: //e.printStackTrace();
211: }
212: return null;
213: }
214:
215: public ArrayList<CMapNode> getCMapNodes() {
216: if (this.ejbDD == null) {
217: return null;
218: } else {
219: return this.ejbDD.getNodes();
220: }
221: }
222: */
223:
224: /* (non-Javadoc)
225: * @see com.sun.wasilla.tool.archive.Archive#getJAXBHandler()
226: */
227: @Override
228: public JAXBHandler getJAXBHandler() {
229: // TODO Auto-generated method stub
230: return null;
231: }
232:
233: /* (non-Javadoc)
234: * @see com.sun.wasilla.tool.archive.Archive#close()
235: */
236: @Override
237: public void close() throws Exception {
238: // TODO Auto-generated method stub
239:
240: }
241:
242: public ResourceAggregator getResourceAggregator() {
243: return this .resAggregator;
244: }
245:
246: public CMap getCMap() {
247: return this .cmap;
248: }
249:
250: public FileObject getResourceDir() {
251: return this.resourceDirFO;
252: }
253: }
|