001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.j2mews.sg;
028:
029: import java.io.*;
030: import java.util.*;
031: import com.sun.xml.rpc.tools.wscompile.CompileTool;
032: import com.sun.xml.rpc.processor.model.*;
033: import com.sun.xml.rpc.processor.util.*;
034: import com.sun.xml.rpc.processor.*;
035: import com.sun.xml.rpc.processor.util.GeneratedFileInfo;
036: import com.sun.xml.rpc.tools.wscompile.ActionConstants;
037: import com.sun.xml.rpc.util.localization.*;
038:
039: /**
040: * We extend the JAX-RPC SI's CompileTool class and replace their
041: * generators with ours where needed.
042: *
043: */
044: public class CompileTool172 extends CompileTool {
045: protected StringBuffer errorMessages = new StringBuffer();
046: protected String cldcVersion = "1.0";
047: protected boolean expandArguments = true;
048: protected LocalizableMessageFactory messageFactory172;
049: protected boolean doCompilation = true;
050: protected RemoteInterfaceGenerator rig;
051: protected StubGenerator sg;
052:
053: private Map originalTypes;
054:
055: public CompileTool172(OutputStream out, String program) {
056: super (out, program);
057: originalTypes = new HashMap();
058: messageFactory172 = new LocalizableMessageFactory(
059: "com.sun.j2mews.sg.wscompile172");
060:
061: installReplacementActions();
062: }
063:
064: protected void installReplacementActions() {
065: rig = new RemoteInterfaceGenerator();
066: replaceAction(
067: ActionConstants.ACTION_REMOTE_INTERFACE_GENERATOR, rig);
068:
069: sg = new StubGenerator();
070: sg.setOriginalTypes(originalTypes);
071: replaceAction(ActionConstants.ACTION_STUB_GENERATOR, sg);
072:
073: // And tell these processors to do nothing
074: NullProcessorAction nullProcessor = new NullProcessorAction();
075: replaceAction(
076: ActionConstants.ACTION_ENUMERATION_ENCODER_GENERATOR,
077: nullProcessor);
078: replaceAction(
079: ActionConstants.ACTION_INTERFACE_SERIALIZER_GENERATOR,
080: nullProcessor);
081: replaceAction(
082: ActionConstants.ACTION_SOAP_OBJECT_SERIALIZER_GENERATOR,
083: nullProcessor);
084: replaceAction(
085: ActionConstants.ACTION_SOAP_OBJECT_BUILDER_GENERATOR,
086: nullProcessor);
087: replaceAction(
088: ActionConstants.ACTION_LITERAL_OBJECT_SERIALIZER_GENERATOR,
089: nullProcessor);
090: replaceAction(
091: ActionConstants.ACTION_SOAP_FAULT_SERIALIZER_GENERATOR,
092: nullProcessor);
093: replaceAction(
094: ActionConstants.ACTION_FAULT_EXCEPTION_BUILDER_GENERATOR,
095: nullProcessor);
096: replaceAction(
097: ActionConstants.ACTION_SERIALIZER_REGISTRY_GENERATOR,
098: nullProcessor);
099: replaceAction(ActionConstants.ACTION_SERVICE_GENERATOR,
100: nullProcessor);
101: replaceAction(
102: ActionConstants.ACTION_SERVICE_INTERFACE_GENERATOR,
103: nullProcessor);
104: }
105:
106: /**
107: * For example,
108: * replaceAction(ActionConstants.ACTION_STUB_GENERATOR, new StubGenerator());
109: */
110: public void replaceAction(String name, ProcessorAction pa) {
111: actions.put(name, pa);
112: }
113:
114: /**
115: * Add a few parameters/features specific to JSR-172.
116: * Remove a few parameters/features that don't apply.
117: * Always make sure the wsi feature is on.
118: */
119: protected boolean parseArguments(String[] args) {
120: boolean hasFeatures = false;
121: boolean optimize = false;
122: boolean showCldc1_0Info = false;
123: boolean hasKeep = false;
124: for (int i = 0; i < args.length; ++i) {
125: String arg = args[i].intern();
126: if (arg == "-cldc1.0") {
127: cldcVersion = "1.0";
128: } else if (arg == "-cldc1.1") {
129: cldcVersion = "1.1";
130: } else if (arg == "-cldc1.0info") {
131: showCldc1_0Info = true;
132: } else if (arg == "-define" || arg == "-gen:server"
133: || arg == "-gen:both") {
134: // The server side only features are not supported here.
135: onError(get172Message("wscompile172.noServiceSide"));
136: return false;
137: } else if (args[i].startsWith("-f:")
138: || args[i].startsWith("-features:")) {
139: hasFeatures = true;
140: boolean hasWSIFeatureSet = false;
141: String featureString = args[i].substring(args[i]
142: .startsWith("-f:") ? 3 : 10);
143: StringTokenizer tokenizer = new StringTokenizer(
144: featureString, ",");
145: String unprocessedFeatures = null;
146: while (tokenizer.hasMoreTokens()) {
147: String feature = tokenizer.nextToken().trim()
148: .intern();
149: if (feature == "rpcliteral") {
150: onError(get172Message(
151: "wscompile172.featureNotSupported",
152: "rpcliteral"));
153: return false;
154: } else if (feature == "wsi") {
155: hasWSIFeatureSet = true;
156: } else if (feature == "donotunwrap") {
157: expandArguments = false;
158: continue;
159: }
160: if (unprocessedFeatures == null)
161: unprocessedFeatures = "-f:";
162: else
163: unprocessedFeatures += ",";
164: unprocessedFeatures += feature;
165: }
166: if (!hasWSIFeatureSet) {
167: if (unprocessedFeatures == null)
168: unprocessedFeatures = "-f:";
169: unprocessedFeatures += ",wsi";
170: hasWSIFeatureSet = true;
171: }
172: if (unprocessedFeatures != null)
173: args[i] = unprocessedFeatures;
174: continue;
175: } else if (arg == "-O") {
176: optimize = true;
177: continue;
178: } else if (arg == "-keep") {
179: hasKeep = true;
180: continue;
181: } else {
182: // Skip the resetting of the argument.
183: continue;
184: }
185: args[i] = "";
186: }
187: if (!hasFeatures) {
188: String[] newArgs = new String[args.length + 1];
189: System.arraycopy(args, 0, newArgs, 0, args.length);
190: newArgs[args.length] = "-f:wsi";
191: args = newArgs;
192: }
193: if (!hasKeep) {
194: String[] newArgs = new String[args.length + 1];
195: System.arraycopy(args, 0, newArgs, 0, args.length);
196: newArgs[args.length] = "-keep";
197: args = newArgs;
198: }
199: if (cldcVersion.equals("1.0"))
200: properties.setProperty(
201: StubGenerator.FLOAT_DOUBLE_TO_STRING, "true");
202: else
203: properties.setProperty(
204: StubGenerator.FLOAT_DOUBLE_TO_STRING, "false");
205: if (showCldc1_0Info)
206: properties.setProperty(StubGenerator.SHOW_ALL_CLDC1_0_INFO,
207: "true");
208: else
209: properties.setProperty(StubGenerator.SHOW_ALL_CLDC1_0_INFO,
210: "false");
211: if (expandArguments)
212: properties.setProperty(StubGenerator.EXPAND_ARGUMENTS,
213: "true");
214: else
215: properties.setProperty(StubGenerator.EXPAND_ARGUMENTS,
216: "false");
217: if (optimize)
218: properties.setProperty(StubGenerator.OPTIMIZE, "true");
219: else
220: properties.setProperty(StubGenerator.OPTIMIZE, "false");
221:
222: return super .parseArguments(args);
223: }
224:
225: public Localizable getVersion() {
226: return get172Message("wscompile172.version", super .getVersion());
227: }
228:
229: protected void compileGeneratedClasses() {
230: if (doCompilation) {
231: super .compileGeneratedClasses();
232: } else {
233: // Do nothing, so as to skip the compilation step.
234: }
235: }
236:
237: protected void registerProcessorActions(Processor processor) {
238: rig.setLocalizer(localizer);
239: rig.setEnvironment(environment);
240: sg.setLocalizer(localizer);
241: sg.setEnvironment(environment);
242: if (cldcVersion.equals("1.0")) {
243: MakeCldc1_0 coercer = new MakeCldc1_0(originalTypes);
244: coercer.setEnvironment(environment);
245: processor.add(coercer);
246: }
247: super .registerProcessorActions(processor);
248: }
249:
250: public Map getOriginalTypes() {
251: return originalTypes;
252: }
253:
254: public Iterator getGeneratedFiles() {
255: return environment.getGeneratedFiles();
256: }
257:
258: public Model getCompileToolModel() {
259: return (Model) processor.getModel();
260: }
261:
262: public Object getCompileToolEnvironment() {
263: return environment;
264: }
265:
266: public void onError(Localizable msg) {
267: String text = localizer.localize(msg);
268: errorMessages.append(text);
269: errorMessages.append("\n");
270: super .onError(msg);
271: }
272:
273: public String getErrorMessages() {
274: return errorMessages.toString();
275: }
276:
277: protected Localizable get172Message(String key) {
278: return messageFactory172.getMessage(key);
279: }
280:
281: protected Localizable get172Message(String key, String arg) {
282: return messageFactory172.getMessage(key, new Object[] { arg });
283: }
284:
285: protected Localizable get172Message(String key, Localizable l) {
286: return messageFactory172.getMessage(key, new Object[] { l });
287: }
288:
289: protected Localizable get172Message(String key, Object[] args) {
290: return messageFactory172.getMessage(key, args);
291: }
292:
293: protected void usage() {
294: report(get172Message("wscompile172.usage", program));
295: }
296: }
|