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: * ClientBeanInfoGenerator.java
043: *
044: * Created on May 12, 2004, 8:37 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import java.io.File;
050: import java.io.FileOutputStream;
051: import java.io.PrintWriter;
052: import java.util.ArrayList;
053: import java.util.Date;
054: import java.util.Iterator;
055: import org.openide.ErrorManager;
056:
057: /**
058: * Generates the BeanInfo class for the wrapper Bean
059: *
060: * @author cao
061: */
062: public class ClientBeanInfoGenerator {
063: // Hardcode the iconFileName here
064: public static final String EJB_ICON_FILE_NAME = "/org/netbeans/modules/visualweb/ejb/resources/session_bean.png";
065:
066: /**
067: * The bean class which this BeanInfo is for
068: */
069: private String fullBeanClassName;
070:
071: private ArrayList properties;
072:
073: private String ejbName;
074:
075: public ClientBeanInfoGenerator(String ejbName,
076: String fullBeanClassName, ArrayList properties) {
077: this .fullBeanClassName = fullBeanClassName;
078: this .properties = properties;
079: this .ejbName = ejbName;
080: }
081:
082: /**
083: * Generates the BeanInfo class
084: *
085: * @param srcDir Where the java source code will be saved
086: * @return the ClassDescriptor for the BeanInfo class just generated
087: */
088: public ClassDescriptor generateClass(String srcDir)
089: throws EjbLoadException {
090: // Declare it outside the try-catch so that the file name can be logged in case of exception
091: File javaFile = null;
092:
093: try {
094: // Figure out the package name, class name and directory/file name
095:
096: int i = fullBeanClassName.lastIndexOf(".");
097: String beanClassName = fullBeanClassName.substring(i + 1);
098:
099: // Package name
100: if (i == -1) // No package
101: i = 0;
102: String packageName = fullBeanClassName.substring(0, i);
103:
104: String beanInfoClassName = beanClassName + "BeanInfo";
105:
106: String classDir = packageName.replace('.',
107: File.separatorChar);
108: File dirF = new File(srcDir + File.separator + classDir);
109: if (!dirF.exists()) {
110: if (!dirF.mkdirs())
111: System.out.println(".....failed to make dir"
112: + srcDir + File.separator + classDir);
113: }
114:
115: String beanInfoClassFile = beanInfoClassName + ".java";
116: javaFile = new File(dirF, beanInfoClassFile);
117: javaFile.createNewFile();
118:
119: ClassDescriptor classDescriptor = new ClassDescriptor(
120: beanInfoClassName, packageName, javaFile
121: .getAbsolutePath(), classDir
122: + File.separator + beanInfoClassFile);
123:
124: // Generate java code
125:
126: PrintWriter out = new PrintWriter(new FileOutputStream(
127: javaFile));
128:
129: // pacage
130: if (packageName != null && packageName.length() != 0) {
131: out.println("package " + packageName + ";");
132: out.println();
133: }
134:
135: // comments
136: out.println("/**");
137: out.println(" * Source code created on " + new Date());
138: out.println(" */");
139: out.println();
140:
141: // Import
142: out.println("import java.awt.Image;");
143: out.println("import java.beans.BeanDescriptor;");
144: out.println("import java.beans.PropertyDescriptor;");
145: out.println("import java.beans.SimpleBeanInfo;");
146: out.println();
147:
148: // Start class
149: out.println("public class " + beanInfoClassName
150: + " extends SimpleBeanInfo {");
151: out.println();
152:
153: // Private variables
154: String beanClassVariable = "beanClass";
155: String iconFileNameVariable = "iconFileName";
156: String beanDescriptorVariable = "beanDescriptor";
157: String propDescriptorsVariable = "propDescriptors";
158: out.println(" private Class " + beanClassVariable
159: + " = " + fullBeanClassName + ".class;");
160: out.println(" private String " + iconFileNameVariable
161: + " = \"" + EJB_ICON_FILE_NAME + "\";");
162: out.println(" private BeanDescriptor "
163: + beanDescriptorVariable + " = null;");
164: out.println(" private PropertyDescriptor[] "
165: + propDescriptorsVariable + " = null; ");
166: out.println();
167:
168: // Method - getBeanDescriptor()
169: out
170: .println(" public BeanDescriptor getBeanDescriptor() {");
171: out.println(" if( " + beanDescriptorVariable
172: + " == null ) {");
173: out.println(" " + beanDescriptorVariable
174: + " = new BeanDescriptor( " + beanClassVariable
175: + " );");
176: out.println(" " + beanDescriptorVariable
177: + ".setValue( \"trayComponent\", Boolean.TRUE );");
178: out.println(" }");
179: out.println(" return " + beanDescriptorVariable
180: + ";");
181: out.println(" }");
182: out.println();
183:
184: // Method - getPropertyDescriptors()
185: out
186: .println(" public PropertyDescriptor[] getPropertyDescriptors() {");
187: if (properties == null || properties.isEmpty()) {
188: // No try-catch
189: out.println(" if( "
190: + propDescriptorsVariable + " == null ) { ");
191: out.println(" "
192: + propDescriptorsVariable
193: + " = new PropertyDescriptor[] {");
194: out.println(" }; ");
195: out.println(" }");
196: out.println(" return "
197: + propDescriptorsVariable + ";");
198: } else {
199: // Need try-catch block
200: out.println(" try { ");
201: out.println(" if( "
202: + propDescriptorsVariable + " == null ) { ");
203: out.println(" "
204: + propDescriptorsVariable
205: + " = new PropertyDescriptor[] {");
206:
207: // Virtual JavaBean properties
208: for (Iterator iter = properties.iterator(); iter
209: .hasNext();) {
210: String prop = (String) iter.next();
211: out
212: .println(" new PropertyDescriptor( \""
213: + prop
214: + "\", "
215: + beanClassVariable
216: + "), ");
217: }
218:
219: out.println(" }; ");
220: out.println(" }");
221: out.println(" return "
222: + propDescriptorsVariable + ";");
223: out
224: .println(" } catch( java.beans.IntrospectionException e ) {");
225: out.println(" return "
226: + propDescriptorsVariable + ";");
227: out.println(" }");
228: }
229: out.println(" }");
230: out.println();
231:
232: // Method - getIcon()
233: out.println(" public Image getIcon(int iconKind) {");
234: out.println(" return loadImage( "
235: + iconFileNameVariable + " );");
236: out.println(" }");
237: out.println();
238:
239: // End of class
240: out.println("}");
241:
242: out.flush();
243: out.close();
244:
245: return classDescriptor;
246: } catch (java.io.FileNotFoundException ex) {
247: // Log error
248: String errMsg = "Error occurred when trying to generate the wrapper bean class for EJB "
249: + ejbName
250: + ". Could not find file "
251: + javaFile.getAbsolutePath();
252: ErrorManager
253: .getDefault()
254: .getInstance(
255: "org.netbeans.modules.visualweb.ejb.load.ClientBeanInfoGenerator")
256: .log(errMsg);
257: ex.printStackTrace();
258:
259: // Throw up as a SYSTEM_ERROR
260: throw new EjbLoadException(ex.getMessage());
261: } catch (java.io.IOException ex) {
262: // Log error
263: String errMsg = "Error occurred when trying to generate the wrapper bean class for EJB "
264: + ejbName
265: + ". Could not create file "
266: + javaFile.getAbsolutePath();
267: ErrorManager
268: .getDefault()
269: .getInstance(
270: "org.netbeans.modules.visualweb.ejb.load.ClientBeanInfoGenerator")
271: .log(errMsg);
272: ex.printStackTrace();
273:
274: // Throw up as a SYSTEM_ERROR
275: throw new EjbLoadException(ex.getMessage());
276: }
277: }
278: }
|