001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: OPP.java,v 1.4 2002/06/08 00:49:39 mediumnet Exp $
008:
009: package org.ozoneDB.tools.OPP;
010:
011: import java.io.File;
012:
013: import org.ozoneDB.DxLib.DxHashSet;
014: import org.ozoneDB.DxLib.DxIterator;
015: import org.ozoneDB.core.ObjectContainer;
016:
017: /**
018: * Command line driver of the OPP tool.
019: *
020: * @author <a href="http://www.softwarebuero.de/">SMB</a>
021: * @version $Revision: 1.4 $Date: 2002/06/08 00:49:39 $
022: */
023: public class OPP {
024:
025: public final static String SIGNATURE_DELIMITER = "|";
026:
027: /**
028: * Update methods can marked with a
029: * <pre>
030: * /*update * /
031: * </pre>
032: * in the lines following the method signature.
033: */
034: public final static String UPDATE_SIGN = "/[*]+ *update *[*]/|// *update";
035:
036: /**
037: * All method signatures in java interfaces must look like:
038: * <pre>
039: * public package.return.Class[] methodname (
040: * </pre>
041: * Otherwise OPP is unable to find them.
042: */
043: public final static String METHOD_PATTERN = "^[ \\t]*public[ \\t]+[_a-zA-Z][_a-zA-Z0-9\\.\\[\\]]*[ \\t]+([_a-zA-Z][\\w]*)[ \\t]*\\(";
044:
045: /**
046: * Update methods can marked with a
047: * <pre>
048: * @update
049: * </pre>
050: * in its appropriate Javadoc Comment.
051: */
052: public final static String JAVADOC_PATTERN = "^[ \\t]+\\*[ \\t]*@update";
053:
054: static boolean odmg = false;
055: static boolean quiet = false;
056: static boolean cache = true;
057: static boolean keepSource = false;
058: static boolean compileSource = true;
059: static boolean printStackTrace = false;
060: static boolean searchInterfaceSource = true;
061: static String methodPattern = ".*_update";
062: static String outputDirName = "." + File.separator;
063: static String sourceDirName = "." + File.separator;
064:
065: public static void main(String[] args) {
066:
067: DxHashSet classes = new DxHashSet();
068:
069: if (args.length == 0) {
070: printUsage();
071: System.exit(0);
072: }
073:
074: boolean preservePackageNames = true;
075:
076: for (int argCount = 0; argCount < args.length; argCount++) {
077: if (args[argCount].equals("-q")) {
078: quiet = true;
079: } else if (args[argCount].equals("-ks")) {
080: keepSource = true;
081: } else if (args[argCount].equals("-KS")) {
082: keepSource = true;
083: compileSource = false;
084: } else if (args[argCount].equals("-odmg")) {
085: odmg = true;
086: } else if (args[argCount].equals("-nc")) {
087: cache = false;
088: } else if (args[argCount].equals("-st")) {
089: printStackTrace = true;
090: } else if (args[argCount].equals("-ni")) {
091: searchInterfaceSource = false;
092: } else if (args[argCount].startsWith("-p")) {
093: methodPattern = args[argCount].substring(2);
094: } else if (args[argCount].equals("-version")) {
095: System.out
096: .println("$Id: OPP.java,v 1.4 2002/06/08 00:49:39 mediumnet Exp $");
097: System.exit(0);
098: } else if (args[argCount].equals("-h")) {
099: printUsage();
100: System.exit(0);
101: } else if (args[argCount].startsWith("-o")) {
102: outputDirName = args[argCount].substring(2)
103: + File.separator;
104: } else if (args[argCount].startsWith("-s")) {
105: sourceDirName = args[argCount].substring(2)
106: + File.separator;
107: } else if (args[argCount].equals("-ip")) {
108: preservePackageNames = false;
109: } else {
110: if (args[argCount].startsWith("-")) {
111: System.out.println("Unknown option '"
112: + args[argCount] + "'!\n");
113: printUsage();
114: System.exit(0);
115: } else {
116: classes.add(args[argCount]);
117: }
118: }
119: }
120: // We internally use the new directory parameter format,
121: // but the user has specified the directory parameters in the old format
122: // where packages were ignored
123: if (!preservePackageNames) {
124: DxIterator i = classes.iterator();
125:
126: if (i.next() != null) {
127: String className = (String) i.key();
128:
129: if (className != null) {
130: int dotCount = 0;
131: int index = 0;
132:
133: while ((index = className.indexOf('.', index)) != -1) {
134: dotCount++;
135: index++;
136: }
137:
138: outputDirName = parentDirectoryName(outputDirName,
139: dotCount);
140: sourceDirName = parentDirectoryName(sourceDirName,
141: dotCount);
142: }
143: }
144: }
145: try {
146: if (odmg) {
147: makeODMGProxies(classes);
148: } else {
149: makeProxies(classes);
150: }
151: } catch (Exception e) {
152: e.printStackTrace(System.out);
153: System.exit(1);
154: }
155: }
156:
157: protected static String parentDirectoryName(String directoryName,
158: int dotCount) {
159: StringBuffer b = new StringBuffer(directoryName);
160: for (int i = 0; i < dotCount; i++) {
161: b.append("..");
162: b.append(File.separatorChar);
163: }
164:
165: return b.toString();
166: }
167:
168: protected static void makeODMGProxies(DxHashSet classes)
169: throws Exception {
170:
171: DxIterator it = classes.iterator();
172: while (it.next() != null) {
173: String name = (String) it.object();
174:
175: OPPHelper.progressMsg(name + ":", quiet);
176:
177: // create the *_Impl class of the original class
178: Class cl = Class.forName(name);
179: String newClassName = cl.getName()
180: + ObjectContainer.IMPLNAME_POSTFIX;
181:
182: ImplManipulator manipulator = new ImplManipulator(cl,
183: outputDirName, quiet);
184: manipulator.changeClassFile(classes, outputDirName
185: + OPPHelper.classFileBasename(cl) + ".class",
186: newClassName);
187:
188: // generate source and class file of the *_Proxy proxy class
189: makeProxy(name);
190:
191: // create the *_Impl class of the original class
192: cl = Class
193: .forName(name + ObjectContainer.PROXYNAME_POSTFIX);
194: newClassName = name;
195: manipulator.changeClassFile(null, outputDirName
196: + OPPHelper.classFileBasename(cl) + ".class",
197: newClassName);
198: }
199: }
200:
201: protected static void makeProxies(DxHashSet classes)
202: throws Exception {
203: DxIterator it = classes.iterator();
204: while (it.next() != null) {
205: String name = (String) it.object();
206:
207: OPPHelper.progressMsg(name + ":", quiet);
208:
209: makeProxy(name);
210: }
211: }
212:
213: protected static void makeODMGProxy(String arg) throws Exception {
214:
215: // create the *_Impl class of the original class
216: Class cl = Class.forName(arg);
217: ImplManipulator manipulator = new ImplManipulator(cl,
218: outputDirName, quiet);
219: String newClassName = cl.getName()
220: + ObjectContainer.IMPLNAME_POSTFIX;
221: manipulator.changeClassFile(null, outputDirName
222: + OPPHelper.classFileBasename(cl) + ".class",
223: newClassName);
224:
225: // generate source and class file of the *_Proxy proxy class
226: makeProxy(arg);
227:
228: // create the *_Impl class of the original class
229: cl = Class.forName(arg + ObjectContainer.PROXYNAME_POSTFIX);
230: manipulator = new ImplManipulator(cl, outputDirName, quiet);
231: newClassName = arg;
232: manipulator.changeClassFile(null, outputDirName
233: + OPPHelper.classFileBasename(cl) + ".class",
234: newClassName);
235: }
236:
237: protected static void makeProxy(String arg) throws Exception {
238: Class cl = Class.forName(arg);
239:
240: ProxyGenerator generator = new ProxyGenerator(cl,
241: methodPattern, outputDirName, quiet, cache);
242: generator.generateSource(searchInterfaceSource);
243:
244: if (compileSource) {
245: generator.compileSource();
246: }
247:
248: if (!keepSource) {
249: generator.deleteSource();
250: }
251: }
252:
253: public static void printUsage() {
254: System.out.println("Ozone Post Processor");
255: System.out
256: .println("usage: opp [-ks] [-st] [-p<pattern>] [-ni] [-nc] [-q] [-h] [-o<directory>] [-odmg] [-ip] class [class]*");
257: System.out
258: .println(" -ks save the generated source files");
259: System.out
260: .println(" -KS save the generated source files; do not invoke compiler");
261: System.out.println(" -st print stack trace");
262: System.out
263: .println(" -p regular expression to specify update methods");
264: System.out
265: .println(" -ni do not search interface code for update methods");
266: System.out
267: .println(" -q supress output of any messages");
268: System.out.println(" -o output directory");
269: System.out.println(" -s source directory");
270: System.out
271: .println(" -odmg create proxies for the ozone ODMG interface");
272: System.out.println(" -ip ignore package names");
273: System.out
274: .println(" -nc do not create code needed for direct invokes and ClientCacheDatabase");
275: System.out.println(" -version shows version information");
276: System.out.println(" -h shows this help");
277: }
278:
279: }
|