001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.processor.util;
027:
028: import java.io.File;
029: import java.io.OutputStream;
030: import java.io.PrintStream;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import com.sun.tools.internal.ws.processor.ProcessorNotificationListener;
036: import com.sun.tools.internal.ws.processor.generator.Names;
037: import com.sun.tools.internal.ws.util.JAXWSClassFactory;
038: import com.sun.xml.internal.ws.util.localization.Localizable;
039:
040: /**
041: *
042: * @author WS Development Team
043: */
044: public class ClientProcessorEnvironment extends
045: ProcessorEnvironmentBase implements ProcessorEnvironment {
046:
047: /**
048: * The stream where error message are printed.
049: */
050: private OutputStream out;
051:
052: /**
053: * A printwriter created lazily in case there are exceptions to report.
054: */
055: private PrintStream outprintstream;
056:
057: /**
058: * listener for error/warning/info notifications
059: */
060: private ProcessorNotificationListener listener;
061:
062: /**
063: * The classpath to use
064: */
065: private String classPath;
066:
067: /**
068: * list of generated source files created in this environment and
069: * its type
070: */
071: private List generatedFiles = new ArrayList();
072:
073: /**
074: * The number of errors and warnings
075: */
076: private int nwarnings;
077: private int nerrors;
078:
079: /**
080: * flags
081: */
082: private int flags;
083:
084: private Names names;
085:
086: /**
087: * Create a ClientProcessorEnvironment with the given class path,
088: * stream for messages and ProcessorNotificationListener.
089: */
090: public ClientProcessorEnvironment(OutputStream out,
091: String classPath, ProcessorNotificationListener listener) {
092:
093: this .out = out;
094: this .classPath = classPath;
095: this .listener = listener;
096: flags = 0;
097:
098: //bug fix:4904604
099: names = JAXWSClassFactory.newInstance().createNames();
100: }
101:
102: /**
103: * Set the environment flags
104: */
105: public void setFlags(int flags) {
106: this .flags = flags;
107: }
108:
109: /**
110: * Get the environment flags
111: */
112: public int getFlags() {
113: return flags;
114: }
115:
116: /**
117: * Get the ClassPath.
118: */
119: public String getClassPath() {
120: return classPath;
121: }
122:
123: /**
124: * Is verbose turned on
125: */
126: public boolean verbose() {
127: return (flags & F_VERBOSE) != 0;
128: }
129:
130: /**
131: * Remember info on generated source file generated so that it
132: * can be removed later, if appropriate.
133: */
134: public void addGeneratedFile(GeneratedFileInfo file) {
135: generatedFiles.add(file);
136: }
137:
138: /**
139: * Return all the generated files and its types.
140: */
141: public Iterator getGeneratedFiles() {
142: return generatedFiles.iterator();
143: }
144:
145: /**
146: * Delete all the generated source files made during the execution
147: * of this environment (those that have been registered with the
148: * "addGeneratedFile" method).
149: */
150: public void deleteGeneratedFiles() {
151: synchronized (generatedFiles) {
152: Iterator iter = generatedFiles.iterator();
153: while (iter.hasNext()) {
154: File file = ((GeneratedFileInfo) iter.next()).getFile();
155: if (file.getName().endsWith(".java")) {
156: file.delete();
157: }
158: }
159: generatedFiles.clear();
160: }
161: }
162:
163: /**
164: * Release resources, if any.
165: */
166: public void shutdown() {
167: listener = null;
168: generatedFiles = null;
169: }
170:
171: public void error(Localizable msg) {
172: if (listener != null) {
173: listener.onError(msg);
174: }
175: nerrors++;
176: }
177:
178: public void warn(Localizable msg) {
179: if (warnings()) {
180: nwarnings++;
181: if (listener != null) {
182: listener.onWarning(msg);
183: }
184: }
185: }
186:
187: public void info(Localizable msg) {
188: if (listener != null) {
189: listener.onInfo(msg);
190: }
191: }
192:
193: public void printStackTrace(Throwable t) {
194: if (outprintstream == null) {
195: outprintstream = new PrintStream(out);
196: }
197: t.printStackTrace(outprintstream);
198: }
199:
200: public Names getNames() {
201: return names;
202: }
203:
204: public int getErrorCount() {
205: return nerrors;
206: }
207:
208: public int getWarningCount() {
209: return nwarnings;
210: }
211:
212: private boolean warnings() {
213: return (flags & F_WARNINGS) != 0;
214: }
215:
216: //bug fix:4904604
217: //to called in compileTool after env is
218: public void setNames(Names names) {
219: this.names = names;
220: }
221:
222: }
|