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: * ClientBeanWrapperCompiler.java
043: *
044: * Created on May 13, 2004, 1:52 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import org.netbeans.modules.visualweb.ejb.util.RunCommand;
050: import java.io.File;
051: import java.util.*;
052: import org.openide.ErrorManager;
053: import org.openide.modules.InstalledFileLocator;
054:
055: /**
056: * To compile the wrapper client bean and bean info classes for the EJB session beans.
057: * Using com.sun.tools.java.Main for now. Probably should use org.openide.compiler. Look
058: * into later ...
059: *
060: * @author cao
061: */
062: public class ClientBeanWrapperCompiler {
063: private ArrayList cmdArray;
064:
065: public ClientBeanWrapperCompiler() {
066: }
067:
068: /**
069: * Compile the given java classes
070: *
071: * @param srcDir The director where the src code exists
072: * @param classDescriptors A list of ClassDescriptors, one per java class
073: * @param jarFiles A list jars which should be in the classpath
074: */
075: public void compile(String srcDir, ArrayList classDescriptors,
076: ArrayList jarFiles) throws EjbLoadException {
077: // The command array contains
078: // javac
079: // -source
080: // value of the source version, which is hardcoded to 1.4 for now
081: // -target
082: // value of the target JVM version, which is 1.4
083: // -classpath
084: // value of the classpath
085: // src1
086: // src2
087: // ....
088:
089: cmdArray = new ArrayList();
090:
091: cmdArray.add(System.getProperty("jdk.home") + "/bin/javac");
092: cmdArray.add("-source");
093: cmdArray.add("1.4");
094: cmdArray.add("-target");
095: cmdArray.add("1.4");
096: cmdArray.add("-classpath");
097:
098: // Now what should be in the classpath?
099: // The source dirs and all the passed in jar files
100:
101: String classpath = srcDir;
102: for (Iterator iter = jarFiles.iterator(); iter.hasNext();) {
103: String jarFile = (String) iter.next();
104: classpath += File.pathSeparator + jarFile;
105: }
106:
107: cmdArray.add(classpath);
108:
109: // Now the java source files
110: int i = 3;
111: for (Iterator iter = classDescriptors.iterator(); iter
112: .hasNext();) {
113: ClassDescriptor srcClass = (ClassDescriptor) iter.next();
114:
115: if (!srcClass.isInnerClass())
116: cmdArray.add(srcClass.getFullPathFileName());
117: }
118:
119: try {
120: RunCommand runCommand = new RunCommand();
121: runCommand.execute((String[]) cmdArray
122: .toArray(new String[0]));
123: if (runCommand.getReturnStatus() != 0) {
124: // Something went wrong. Log the attempted command here
125: ErrorManager
126: .getDefault()
127: .getInstance(
128: "org.netbeans.modules.visualweb.ejb.load.ClientBeanWrapperCompiler")
129: .log(ErrorManager.ERROR,
130: "Failed to compile warpper bean classes");
131:
132: // throw up as SYSTEM_ERROR
133: throw new EjbLoadException(
134: "Failed to compile wrapper bean classes"); // NOI18N
135: }
136:
137: // Need to create ClassDescriptors for the compiled .class's. One per src file
138: ArrayList compiledClasses = new ArrayList();
139: for (Iterator iter = classDescriptors.iterator(); iter
140: .hasNext();) {
141: ClassDescriptor srcClass = (ClassDescriptor) iter
142: .next();
143:
144: // We need to check the full path file name. If it is null, that means
145: // the class is a inner class. In that case, we don't need to add a class
146: // descriptor for the .class. The original one is enough
147:
148: if (!srcClass.isInnerClass()) {
149: // Replace ".java" with ".class" for the compiled class files
150: int lastIndexJavaFullPath = srcClass
151: .getFullPathFileName().lastIndexOf('.');
152: int lastIndexJavaPkg = srcClass
153: .getPackageFileName().lastIndexOf('.');
154: String classFullPath = srcClass
155: .getFullPathFileName().substring(0,
156: lastIndexJavaFullPath)
157: + ".class";// NOI18N
158: String classPkgFileName = srcClass
159: .getPackageFileName().substring(0,
160: lastIndexJavaPkg)
161: + ".class";// NOI18N
162:
163: compiledClasses.add(new ClassDescriptor(srcClass
164: .getClassName(), srcClass.getPackageName(),
165: classFullPath, classPkgFileName));
166: }
167:
168: }
169:
170: classDescriptors.addAll(compiledClasses);
171: } catch (Exception ex) {
172: // Log error
173: String logMsg = "Error occurred when trying to execute command: "
174: + getCommand();
175: ErrorManager
176: .getDefault()
177: .getInstance(
178: "org.netbeans.modules.visualweb.ejb.load.ClientBeanWrapperCompiler")
179: .log(ErrorManager.ERROR, logMsg);
180: ex.printStackTrace();
181:
182: // throw up as SYSTEM_ERROR
183: throw new EjbLoadException(ex.getMessage());
184: }
185: }
186:
187: private String getCommand() {
188: StringBuffer cmd = new StringBuffer();
189: for (int i = 0; i < cmdArray.size(); i++) {
190: cmd.append((String) cmdArray.get(i));
191: cmd.append(" ");
192: }
193:
194: return cmd.toString();
195: }
196: }
|