org/objectweb/proactive/ext/util/StubGenerator.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.util;
00032 
00033 import java.io.File;
00034 import java.io.FileOutputStream;
00035 
00036 //import org.objectweb.proactive.core.mop.ASMBytecodeStubBuilder;
00037 import org.objectweb.proactive.core.mop.JavassistByteCodeStubBuilder;
00038 import org.objectweb.proactive.core.mop.MOPClassLoader;
00039 import org.objectweb.proactive.core.mop.Utils;
00040 
00041 
00042 public class StubGenerator {
00043 
00048     protected static String processClassName(String name) {
00049         int i = name.indexOf(".class");
00050         String tmp = name;
00051         if (i < 0) {
00052             return name;
00053         }
00054         tmp = name.substring(0, i);
00055 
00056         String tmp2 = tmp.replace(File.separatorChar, '.');
00057 
00058         if (tmp2.indexOf('.') == 0) {
00059             return tmp2.substring(1);
00060         }
00061         return tmp2;
00062     }
00063 
00064     public static void printUsageAndExit() {
00065         System.err.println("usage: java " + StubGenerator.class.getName() +
00066             " <classes> ");
00067         System.exit(0);
00068     }
00069 
00070     public static void main(String[] args) {
00071         // This is the file into which we are about to write the bytecode for the stub
00072         String fileName = null;
00073 
00074         //the index of the first className
00075         int classIndex = 0;
00076 
00077         // Check number of arguments
00078         if (args.length <= 0) {
00079             printUsageAndExit();
00080         }
00081 
00082         String directoryName = "./";
00083 
00084         if (args[0].equals("-d")) {
00085             directoryName = args[1];
00086             classIndex = 2;
00087         }
00088 
00089         // If the directory name does not end with a file separator, add one
00090         if (!directoryName.endsWith(System.getProperty("file.separator"))) {
00091             directoryName = directoryName +
00092                 System.getProperty("file.separator");
00093         }
00094 
00095         // Name of the class
00096         for (int i = classIndex; i < args.length; i++) {
00097             try {
00098                 generateClass(args[i], directoryName);
00099             } catch (Throwable e) {
00100                 System.err.println(e);
00101             }
00102         }
00103     }
00104 
00109     protected static void generateClass(String arg, String directoryName) {
00110         String className = processClassName(arg);
00111         String fileName = null;
00112 
00113         String stubClassName;
00114 
00115         try {
00116             // Generates the bytecode for the class
00117             //ASM is now the default bytecode manipulator
00118             byte[] data;
00119 
00120 //            if (MOPClassLoader.BYTE_CODE_MANIPULATOR.equals("ASM")) {
00121 //                ASMBytecodeStubBuilder bsb = new ASMBytecodeStubBuilder(className);
00122 //                data = bsb.create();
00123 //                stubClassName = Utils.convertClassNameToStubClassName(className);
00124 //            } else 
00125                 if (MOPClassLoader.BYTE_CODE_MANIPULATOR.equals("javassist")) {
00126                         
00127                 data = JavassistByteCodeStubBuilder.create(className, null);
00128                 stubClassName = Utils.convertClassNameToStubClassName(className, null);
00129             } else {
00130                 // that shouldn't happen, unless someone manually sets the BYTE_CODE_MANIPULATOR static variable
00131                 System.err.println(
00132                 "byteCodeManipulator argument is optionnal. If specified, it can only be set to javassist (ASM is no longer supported).");
00133                 System.err.println(
00134                 "Any other setting will result in the use of javassist, the default bytecode manipulator framework");
00135                 stubClassName = null;
00136                 data = null;
00137             }
00138 
00139             char sep = System.getProperty("file.separator").toCharArray()[0];
00140             fileName = directoryName + stubClassName.replace('.', sep) +
00141                 ".class";
00142 
00143             // And writes it to a file
00144             new File(fileName.substring(0, fileName.lastIndexOf(sep))).mkdirs();
00145 
00146             //  String fileName = directoryName + System.getProperty ("file.separator") + 
00147             File f = new File(fileName);
00148             FileOutputStream fos = new FileOutputStream(f);
00149             fos.write(data);
00150             fos.flush();
00151             fos.close();
00152         } catch (Exception e) {
00153             System.err.println("Cannot write file " + fileName);
00154             System.err.println("Reason is " + e);
00155         }
00156     }
00157 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1