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: * ClientBeanWrapperJarGenerator.java
043: *
044: * Created on May 14, 2004, 3:42 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import org.netbeans.modules.visualweb.ejb.datamodel.EjbGroup;
050: import org.netbeans.modules.visualweb.ejb.util.Util;
051: import java.io.BufferedInputStream;
052: import java.io.BufferedOutputStream;
053: import java.io.DataInputStream;
054: import java.io.FileInputStream;
055: import java.io.FileOutputStream;
056: import java.net.URL;
057: import java.util.ArrayList;
058: import java.util.jar.Attributes;
059: import java.util.jar.JarEntry;
060: import java.util.jar.JarOutputStream;
061: import java.util.jar.Manifest;
062: import org.openide.ErrorManager;
063:
064: /**
065: *
066: * @author cao
067: */
068: public class ClientBeanWrapperJarGenerator {
069: private static final int BUFFER = 2048;
070:
071: /**
072: * Jar all the given class in the given jar file
073: *
074: * @param destJarFile Where all the classes will be added to
075: * @param classDescriptors All the classes will be jarred. It is a Collection of ClassDescriptors
076: */
077: public static void jarThemUp(String ejbGroupName,
078: EjbGroup ejbGroup, ArrayList classDescriptors)
079: throws EjbLoadException {
080: // Split the classes into two pile
081: // - DesignInfo classes
082: // - not DesignInfo classes
083:
084: ArrayList designInfoClasses = new ArrayList();
085: ArrayList clientWrapperBeanClasses = new ArrayList();
086:
087: for (int i = 0; i < classDescriptors.size(); i++) {
088: ClassDescriptor classDescriptor = (ClassDescriptor) classDescriptors
089: .get(i);
090:
091: if (classDescriptor.getClassName().indexOf("DesignInfo") != -1) // NOI18N
092: designInfoClasses.add(classDescriptor);
093: else
094: clientWrapperBeanClasses.add(classDescriptor);
095: }
096:
097: // Make a jar containing the client wrapper classes (including the BeanInfo classes )
098: makeJar(ejbGroupName, ejbGroup.getClientWrapperBeanJar(),
099: clientWrapperBeanClasses, true);
100:
101: // Make a jar containing the DesignInfo classes
102: if (!designInfoClasses.isEmpty())
103: makeJar(ejbGroupName, ejbGroup.getDesignInfoJar(),
104: designInfoClasses, false);
105: else
106: // For very corner case - for example, the client jar only has one ejb with one method returning void
107: ejbGroup.setDesignInfoJar(null);
108: }
109:
110: private static void makeJar(String ejbGroupName,
111: String destJarFile, ArrayList classDescriptors,
112: boolean includeImages) throws EjbLoadException {
113: try {
114: // The manifest file contains version and time stamp
115: Manifest manifest = new Manifest();
116: Attributes attrs = manifest.getMainAttributes();
117: attrs.putValue(Attributes.Name.MANIFEST_VERSION.toString(),
118: "2.0"); // Always 2.0. It matches the Creator
119: attrs.putValue("Generated-time-in-millies", Long
120: .toString(System.currentTimeMillis())); // NOI18N
121: FileOutputStream dest = new FileOutputStream(destJarFile);
122: JarOutputStream jarOutputStream = new JarOutputStream(
123: new BufferedOutputStream(dest), manifest);
124: jarOutputStream.setMethod(JarOutputStream.DEFLATED);
125:
126: byte data[] = new byte[BUFFER];
127: BufferedInputStream origin = null;
128:
129: // Add the classes to the jar file - exclude out the DesignInfo classes
130: for (int i = 0; i < classDescriptors.size(); i++) {
131: ClassDescriptor classDescriptor = (ClassDescriptor) classDescriptors
132: .get(i);
133:
134: // Note: I have to replace "\" with "/". Otherwise, the class loader can open the jar file. Weird ...
135: FileInputStream fileInputStream = new FileInputStream(
136: classDescriptor.getFullPathFileName().replace(
137: '\\', '/'));
138: origin = new BufferedInputStream(fileInputStream,
139: BUFFER);
140:
141: JarEntry entry = new JarEntry(classDescriptor
142: .getPackageFileName().replace('\\', '/'));
143: jarOutputStream.putNextEntry(entry);
144:
145: int count;
146: while ((count = origin.read(data, 0, BUFFER)) != -1) {
147: jarOutputStream.write(data, 0, count);
148: }
149:
150: origin.close();
151: }
152:
153: if (includeImages) {
154: // Add the session bean icon and data provider icon to the wrapper jar file
155:
156: URL[] imageUrls = new URL[] {
157: ClientBeanWrapperJarGenerator.class
158: .getResource("/org/netbeans/modules/visualweb/ejb/resources/session_bean.png"),
159: ClientBeanWrapperJarGenerator.class
160: .getResource("/org/netbeans/modules/visualweb/ejb/resources/methodPublic.gif"),
161: ClientBeanWrapperJarGenerator.class
162: .getResource("/org/netbeans/modules/visualweb/ejb/resources/table_dp_badge.png") };
163: String[] entryNames = new String[] {
164: Util
165: .getFileName(ClientBeanInfoGenerator.EJB_ICON_FILE_NAME),
166: Util
167: .getFileName(DataProviderBeanInfoGenerator.DATA_PROVIDER_ICON_FILE_NAME),
168: Util
169: .getFileName(DataProviderBeanInfoGenerator.DATA_PROVIDER_ICON_FILE_NAME2) };
170:
171: for (int i = 0; i < imageUrls.length; i++) {
172: DataInputStream beanImageIn = new DataInputStream(
173: imageUrls[i].openStream());
174: origin = new BufferedInputStream(beanImageIn,
175: BUFFER);
176:
177: // The image entry should be something like org/netbeans/modules/visualweb/ejb/resources/session_bean.png
178: JarEntry beanImageEntry = new JarEntry(
179: EjbLoader.CLIENT_WRAPPER_PACKAGE_NAME
180: .replace('.', '/')
181: + "/resources/" + entryNames[i]); // NOI18N
182: jarOutputStream.putNextEntry(beanImageEntry);
183:
184: int count;
185: while ((count = origin.read(data, 0, BUFFER)) != -1) {
186: jarOutputStream.write(data, 0, count);
187: }
188:
189: origin.close();
190: }
191: }
192:
193: // Done with the jar file. Flush...close..
194:
195: jarOutputStream.flush();
196: jarOutputStream.close();
197:
198: } catch (java.io.FileNotFoundException ex) {
199: // Log error
200: String errMsg = "Error occurred when trying to jar the wrapper bean classes for EJB set "
201: + ejbGroupName
202: + ". Could not find file "
203: + destJarFile;
204: ErrorManager
205: .getDefault()
206: .getInstance(
207: "org.netbeans.modules.visualweb.ejb.load.ClientWrapperJarGenerator")
208: .log(errMsg);
209: ex.printStackTrace();
210:
211: // Throw up as a SYSTEM_ERROR
212: throw new EjbLoadException(ex.getMessage());
213: } catch (java.io.IOException ex) {
214: // Log error
215: String errMsg = "Error occurred when trying to jar the wrapper bean classes for EJB set "
216: + ejbGroupName
217: + ". Could not create file "
218: + destJarFile;
219: ErrorManager
220: .getDefault()
221: .getInstance(
222: "org.netbeans.modules.visualweb.ejb.load.ClientWrapperJarGenerator")
223: .log(errMsg);
224: ex.printStackTrace();
225:
226: // Throw up as a SYSTEM_ERROR
227: throw new EjbLoadException(ex.getMessage());
228: }
229: }
230: }
|