001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037:
038: package oracle.toplink.essentials.weaving;
039:
040: import java.io.File;
041: import java.io.FileWriter;
042: import java.io.PrintStream;
043: import java.io.Writer;
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import java.net.URLClassLoader;
047:
048: import oracle.toplink.essentials.exceptions.StaticWeaveException;
049: import oracle.toplink.essentials.internal.localization.ExceptionLocalization;
050: import oracle.toplink.essentials.internal.localization.ToStringLocalization;
051: import oracle.toplink.essentials.logging.AbstractSessionLog;
052: import oracle.toplink.essentials.logging.SessionLog;
053:
054: /**
055: * <p>
056: * <b>Description</b>: This is the static weave command line processing class that verifies command options and invokes
057: * StaticWeaveProcessor to statically weave the classes.
058: * <p>
059: * <b>Usage</b>:<br>
060: * StaticWeave [options] source target<br>
061: * <b>Options</b>:<br>
062: * -classpath<br>
063: * Set the user class path, use ";" as the delimiter in Window system and ":" in Unix system.<br>
064: * -log <br>
065: * The path of log file, the standard output will be the default.<br>
066: * -loglevel<br>
067: * Specify a literal value for toplink log level(OFF,SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST). The default value is OFF.<br>
068: * -persistenceinfo<br>
069: * The path contains META-INF/persistence.xml. This is ONLY required when the source does not include it.
070: * The classpath must contain all the classes necessary in oder to perform weaving.<br><br>
071: * The weaving will be performed in place if source and target point to the same location. Weaving in place is ONLY applicable for directory-based sources.<br>
072: *<b>Example</b>:<br>
073: * To weave all entites contained in c:\foo-source.jar with its persistence.xml contained within c:\foo-containing-persistence-xml.jar, and output to c:\\foo-target.jar,<br>
074: * StaticWeave -persistenceinfo c:\foo-containing-persistencexml.jar -classpath c:\classpath1;c:\classpath2 c:\foo-source.jar c:\foo-target.jar
075: *
076: **/
077:
078: public class StaticWeave {
079:
080: // command line arguments
081: private String[] argv;
082:
083: // The location path of the source, null if none was given
084: private String source;
085:
086: // The location path containing persistence.xml, null if none was given
087: private String persistenceinfopath;
088:
089: // The location path of the target, null if none was given
090: private String target;
091:
092: private int loglevel = SessionLog.OFF;
093:
094: private Writer logWriter;
095:
096: private PrintStream vout = System.out;
097:
098: private String[] classpaths;
099:
100: public static void main(String[] argv) {
101:
102: StaticWeave staticweaver = new StaticWeave(argv);
103:
104: try {
105: // Verify the command line arguments
106: staticweaver.processCommandLine();
107: staticweaver.start();
108: } catch (Exception e) {
109: throw StaticWeaveException.exceptionPerformWeaving(e);
110: }
111: }
112:
113: public StaticWeave(String[] argv) {
114: this .argv = argv;
115: }
116:
117: /**
118: * Invoke StaticWeaveProcessor to perform weaving.
119: */
120: public void start() throws Exception {
121:
122: //perform weaving
123: StaticWeaveProcessor staticWeaverProcessor = new StaticWeaveProcessor(
124: this .source, this .target);
125: if (persistenceinfopath != null) {
126: staticWeaverProcessor
127: .setPersistenceInfo(this .persistenceinfopath);
128: }
129: if (classpaths != null) {
130: staticWeaverProcessor.setClassLoader(getClassLoader());
131: }
132: if (logWriter != null) {
133: staticWeaverProcessor.setLog(logWriter);
134: }
135: staticWeaverProcessor.setLogLevel(loglevel);
136: staticWeaverProcessor.performWeaving();
137: }
138:
139: /*
140: * Verify command line option.
141: */
142: void processCommandLine() throws Exception {
143: if (argv.length < 2 || argv.length > 10) {
144: printUsage();
145: System.exit(1);
146: }
147: for (int i = 0; i < this .argv.length; i++) {
148: if (argv[i].equalsIgnoreCase("-classpath")) {
149: // Make sure we did not run out of arguments
150: if ((i + 1) >= argv.length) {
151: printUsage();
152: System.exit(1);
153: }
154: classpaths = argv[i + 1].split(File.pathSeparator);
155: i++;
156: continue;
157: }
158:
159: if (argv[i].equalsIgnoreCase("-persistenceinfo")) {
160: if ((i + 1) >= argv.length) {
161: printUsage();
162: System.exit(1);
163: }
164: persistenceinfopath = argv[i + 1];
165: i++;
166: continue;
167: }
168:
169: if (argv[i].equalsIgnoreCase("-log")) {
170: if ((i + 1) >= argv.length) {
171: printUsage();
172: System.exit(1);
173: }
174: logWriter = new FileWriter(argv[i + 1]);
175: i++;
176: continue;
177: }
178:
179: if (argv[i].equalsIgnoreCase("-loglevel")) {
180: if ((i + 1) >= argv.length) {
181: printUsage();
182: System.exit(1);
183: }
184:
185: if (argv[i + 1].equalsIgnoreCase("OFF")
186: || argv[i + 1].equalsIgnoreCase("SEVERE")
187: || argv[i + 1].equalsIgnoreCase("WARNING")
188: || argv[i + 1].equalsIgnoreCase("INFO")
189: || argv[i + 1].equalsIgnoreCase("CONFIG")
190: || argv[i + 1].equalsIgnoreCase("FINE")
191: || argv[i + 1].equalsIgnoreCase("FINER")
192: || argv[i + 1].equalsIgnoreCase("FINEST")
193: || argv[i + 1].equalsIgnoreCase("ALL")) {
194: loglevel = AbstractSessionLog
195: .translateStringToLoggingLevel(argv[i + 1]
196: .toUpperCase());
197: } else {
198: printUsage();
199: System.exit(1);
200: }
201: i++;
202: continue;
203: }
204:
205: if (source != null) {
206: printUsage();
207: System.exit(1);
208: }
209:
210: if (target != null) {
211: printUsage();
212: System.exit(1);
213: }
214:
215: source = argv[i];
216: if ((i + 1) >= argv.length) {
217: printUsage();
218: System.exit(1);
219: }
220: i++;
221: if (i >= argv.length) {
222: printUsage();
223: System.exit(1);
224: }
225: target = argv[i];
226: i++;
227: }
228:
229: //Ensure source and target have been specified
230: if (source == null) {
231: printUsage();
232: throw StaticWeaveException.missingSource();
233: }
234: if (target == null) {
235: printUsage();
236: throw StaticWeaveException.missingTarget();
237: }
238: }
239:
240: /*
241: * print command help message
242: */
243: private void printUsage() {
244: PrintStream o = vout;
245: o.println(ToStringLocalization.buildMessage(
246: "staticweave_commandline_help_message",
247: new Object[] { null }));
248: }
249:
250: /*
251: * Convert the specified classpath arrary to URL array where new classloader will build on.
252: */
253: private ClassLoader getClassLoader() throws MalformedURLException {
254: if (classpaths != null) {
255: URL[] urls = new URL[classpaths.length];
256: for (int i = 0; i < classpaths.length; i++) {
257: urls[i] = (new File(classpaths[i])).toURL();
258: }
259: return new URLClassLoader(urls, Thread.currentThread()
260: .getContextClassLoader());
261: } else {
262: return null;
263: }
264: }
265: }
|