1 package org.ozoneDB.tools.OPP; 9 10 import java.io.File ; 11 import java.util.*; 12 13 import org.ozoneDB.core.ObjectContainer; 14 import org.ozoneDB.core.helper.ReflectionHelper; 15 import org.ozoneDB.tools.OPP.compiler.JavaCompiler; 16 import org.ozoneDB.tools.OPP.compiler.CompilerException; 17 import org.ozoneDB.tools.OPP.message.MessageWriter; 18 import org.ozoneDB.tools.OPP.message.SummaryMessageWriterDecorator; 19 import org.ozoneDB.tools.OPP.message.StdOutMessageWriter; 20 import org.ozoneDB.tools.OPP.srcgen.ClassDirector; 21 import org.ozoneDB.tools.OPP.srcgen.ClassBuilder; 22 23 29 public class OPP { 30 31 public final static String SIGNATURE_DELIMITER = ReflectionHelper.SIGNATURE_DELIMITER; 32 33 39 public final static String UPDATE_SIGN = "/[*]+ *update *[*]/|// *update"; 40 41 49 public final static String METHOD_PATTERN 50 = "^[ \\t]*public[ \\t]+[_a-zA-Z][_a-zA-Z0-9\\.\\[\\]]*[ \\t]+([_a-zA-Z][\\w]*)[ \\t]*\\("; 51 52 58 public final static String JAVADOC_PATTERN 59 = "^[ \\t]+\\*[ \\t]*@update"; 60 private boolean keepSource = false; 61 private boolean compileSource = true; 62 63 65 private String outputDirName = "." + File.separator; 66 private String sourceDirName = "." + File.separator; 67 private StdOutMessageWriter msgOut = new StdOutMessageWriter(false, false); 68 private OPPBean oppBean = new OPPBean(); 69 private boolean odmg = false; 70 71 73 74 public OPP() { 75 } 76 77 public static void main(String [] args) { 78 Set classes = new HashSet(); 79 if (args.length == 0) { 80 printUsage(); 81 System.exit(0); 82 } 83 OPP opp = new OPP(); 84 boolean preservePackageNames = true; 85 for (int argCount = 0; argCount < args.length; argCount++) { 86 if (args[argCount].equals("-q")) { 87 opp.msgOut.setQuiet(true); 88 } else if (args[argCount].equals("-ks")) { 89 opp.keepSource = true; 90 } else if (args[argCount].equals("-KS")) { 91 opp.keepSource = true; 92 opp.compileSource = false; 93 } else if (args[argCount].equals("-odmg")) { 94 opp.odmg = true; 95 } else if (args[argCount].equals("-nc")) { 96 opp.oppBean.setCache(false); 97 } else if (args[argCount].equals("-nf")) { 98 opp.oppBean.setGenerateFactory(false); 99 } else if (args[argCount].equals("-st")) { 100 opp.oppBean.setPrintStackTrace(true); 101 } else if (args[argCount].equals("-ni")) { 102 opp.oppBean.setUseSource(false); 103 } else if (args[argCount].startsWith("-p")) { 104 opp.oppBean.setUpdateExpression(args[argCount].substring(2)); 105 } else if (args[argCount].equals("-version")) { 106 System.out.println("$Id$"); 107 System.exit(0); 108 } else if (args[argCount].equals("-h")) { 109 opp.printUsage(); 110 System.exit(0); 111 } else if (args[argCount].startsWith("-o")) { 112 opp.outputDirName = args[argCount].substring(2) + File.separator; 113 } else if (args[argCount].startsWith("-s")) { 114 opp.sourceDirName = args[argCount].substring(2) + File.separator; 115 } else if (args[argCount].equals("-ip")) { 116 preservePackageNames = false; 117 } else { 118 if (args[argCount].startsWith("-")) { 119 System.out.println("Unknown option '" + args[argCount] + "'!\n"); 120 printUsage(); 121 System.exit(0); 122 } else { 123 classes.add(args[argCount]); 124 } 125 } 126 } 127 opp.generate(classes, preservePackageNames); 128 } 129 130 private void generate(Collection classes, boolean preservePackageNames) { 131 MessageWriter genListener = new SummaryMessageWriterDecorator(msgOut); 132 oppBean.removeGenrationEventListener(msgOut); 133 oppBean.addGenrationEventListener(genListener); 134 genListener.startGeneration("OPP"); 135 136 try { 140 if (!preservePackageNames) { 141 Iterator iter = classes.iterator(); 142 if (iter.hasNext()) { 143 String className = (String ) iter.next(); 144 if (className != null) { 145 int dotCount = 0; 146 StringTokenizer st = new StringTokenizer(className, "."); 147 while (st.hasMoreTokens()) { 148 dotCount++; 149 st.nextToken(); 150 } 151 outputDirName = OPP.parentDirectoryName(outputDirName, dotCount); 152 sourceDirName = OPP.parentDirectoryName(sourceDirName, dotCount); 153 } 154 } 155 } 156 try { 157 File outputDir = new File (outputDirName); 158 File sourceDir = new File (sourceDirName); 159 makeProxiesAndFactories(classes, sourceDir, outputDir, "Factory", ObjectContainer.PROXYNAME_POSTFIX, genListener); 160 if (compileSource) { 161 compileSource(outputDir, outputDir, genListener, classes); 163 } 164 if (odmg) { 165 genListener.startGeneration("ODMG"); 166 try { 167 for (Iterator iter = classes.iterator(); iter.hasNext();) { 168 String className = (String ) iter.next(); 169 manipulateClass(className, genListener); 170 manipulateClass(className + ObjectContainer.PROXYNAME_POSTFIX, genListener); 171 } 172 } finally { 173 genListener.endGeneration(); 174 } 175 } 176 } catch (Exception e) { 177 e.printStackTrace(System.out); 178 System.exit(1); 179 } 180 } finally { 181 genListener.endGeneration(); 182 } 183 } 184 185 private void manipulateClass(String className, MessageWriter genListener) throws Exception { 186 Class cl = Class.forName(className); 187 ImplManipulator manipulator = new ImplManipulator(outputDirName, genListener, cl.getClassLoader()); 188 String newClassName = cl.getName() + ObjectContainer.IMPLNAME_POSTFIX; 189 manipulator.changeClassFile(null, outputDirName + OPPHelper.classFileBasename(cl) + ".class", newClassName); 190 } 191 192 private void compileSource(File sourceDir, File outputDir, MessageWriter genListener, Collection classes) throws CompilerException { 193 JavaCompiler compiler = OPPCompilerFactory.getCompilerInstance(sourceDir, outputDir); 194 genListener.startGeneration("Compile proxies"); 195 try { 196 compiler.compile(getModifiedNames(sourceDir, classes, ObjectContainer.PROXYNAME_POSTFIX)); 197 } finally { 198 genListener.endGeneration(); 199 } 200 genListener.startGeneration("Compile factories"); 201 try { 202 compiler.compile(getModifiedNames(sourceDir, classes, "Factory")); 203 } finally { 204 genListener.endGeneration(); 205 } 206 } 207 208 private Collection getModifiedNames(File sourceDir, Collection classes, String postfix) { 209 Collection modified = new ArrayList(classes.size()); 210 for (Iterator iter = classes.iterator(); iter.hasNext();) { 211 StringBuffer buffer = new StringBuffer (sourceDir.getPath()); 212 buffer.append(File.separatorChar); 213 buffer.append((iter.next() + postfix).replace('.', File.separatorChar)); 214 buffer.append(".java"); 215 modified.add(buffer.toString()); 216 } 217 return modified; 218 } 219 220 protected static String parentDirectoryName(String directoryName, int dotCount) { 221 StringBuffer b = new StringBuffer (directoryName); 222 for (int i = 0; i < dotCount; i++) { 223 b.append(".."); 224 b.append(File.separatorChar); 225 } 226 return b.toString(); 227 } 228 229 230 protected void makeProxiesAndFactories(Collection classes, File sourceDir, File outputDir, String factoryPost, String proxyPost, MessageWriter msgListener) throws Exception { 231 Iterator it = classes.iterator(); 232 ClassDirector cd = oppBean.createDirector(sourceDir); 233 ClassBuilder cb = oppBean.createBuilder(outputDir, factoryPost, proxyPost); 234 while (it.hasNext()) { 235 String name = (String ) it.next(); 236 msgListener.info("Processing class: " + name); 237 cd.build(name, cb); 238 } 239 } 240 241 public static void printUsage() { 242 System.out.println("Ozone Post Processor"); 243 System.out.println( 244 "usage: opp [-ks] [-st] [-p<pattern>] [-ni] [-nf] [-nc] [-q] [-h] [-o<directory>] [-odmg] [-ip] class [class]*"); 245 System.out.println(" -ks save the generated resolver files"); 246 System.out.println(" -KS save the generated resolver files; do not invoke compiler"); 247 System.out.println(" -st print stack trace"); 248 System.out.println(" -p regular expression to specify update methods"); 249 System.out.println(" -ni do not search interface code for update methods"); 250 System.out.println(" -nf do not create a Factory class"); 251 System.out.println(" -q supress output of any messages"); 252 System.out.println(" -o output directory"); 253 System.out.println(" -s resolver directory"); 254 System.out.println(" -odmg create proxies for the ozone ODMG interface"); 255 System.out.println(" -ip ignore package names"); 256 System.out.println(" -nc do not create code needed for direct invokes and ClientCacheDatabase"); 257 System.out.println(" -version shows version information"); 258 System.out.println(" -h shows this help"); 259 } 260 } | Popular Tags |