001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common.model;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.logging.Logger;
026:
027: import javax.jws.soap.SOAPBinding;
028: import javax.wsdl.OperationType;
029:
030: import org.apache.cxf.common.i18n.Message;
031: import org.apache.cxf.common.logging.LogUtils;
032: import org.apache.cxf.tools.common.ToolException;
033:
034: public class JavaMethod implements JavaAnnotatable {
035: private static final Logger LOG = LogUtils
036: .getL7dLogger(JavaMethod.class);
037: private String name;
038: private String operationName;
039: private JavaReturn javaReturn;
040: private OperationType style;
041: private String soapAction;
042: private SOAPBinding.Style soapStyle;
043: private SOAPBinding.Use soapUse;
044: private boolean wrapperStyle;
045: private boolean enableMime;
046: private JavaInterface javaInterface;
047: private final List<JavaParameter> parameters = new ArrayList<JavaParameter>();
048: private final List<JavaException> exceptions = new ArrayList<JavaException>();
049: private final Map<String, JavaAnnotation> annotations = new HashMap<String, JavaAnnotation>();
050:
051: private JavaCodeBlock block;
052:
053: public JavaMethod() {
054: this (new JavaInterface());
055: }
056:
057: public JavaMethod(JavaInterface i) {
058: this .javaInterface = i;
059: this .javaReturn = new JavaReturn();
060: }
061:
062: public void clear() {
063: parameters.clear();
064: javaReturn = new JavaReturn();
065: }
066:
067: public String getSignature() {
068: StringBuffer sb = new StringBuffer();
069: sb.append(javaReturn.getName());
070: sb.append("#");
071: sb.append(javaInterface.getPackageName());
072: sb.append(".");
073: sb.append(javaInterface.getName());
074: sb.append("#");
075: sb.append(name);
076: sb.append("[");
077: for (JavaParameter param : parameters) {
078: sb.append(param.getName());
079: sb.append(",");
080: }
081: sb.append("]");
082: return sb.toString();
083: }
084:
085: public JavaInterface getInterface() {
086: return this .javaInterface;
087: }
088:
089: public String getName() {
090: return name;
091: }
092:
093: public void setName(String n) {
094: this .name = n;
095: }
096:
097: public String getOperationName() {
098: return this .operationName;
099: }
100:
101: public void setOperationName(String arg) {
102: this .operationName = arg;
103: }
104:
105: public JavaReturn getReturn() {
106: return javaReturn;
107: }
108:
109: public void setReturn(JavaReturn rt) {
110: if (rt != null && rt.getType() == null
111: && rt.getClassName() == null) {
112: Message msg = new Message(
113: "FAIL_TO_CREATE_JAVA_OUTPUT_PARAMETER", LOG,
114: rt.name, this .getName());
115: throw new ToolException(msg);
116: }
117: this .javaReturn = rt;
118: }
119:
120: public boolean hasParameter(String paramName) {
121: for (int i = 0; i < parameters.size(); i++) {
122: if (paramName.equals((parameters.get(i)).getName())) {
123: return true;
124: }
125: }
126: return false;
127: }
128:
129: private void replaceParameter(JavaParameter p1, JavaParameter p2) {
130: int index = ((ArrayList) parameters).indexOf(p1);
131: parameters.remove(index);
132: parameters.add(index, p2);
133: }
134:
135: public void addParameter(JavaParameter param) {
136: if (hasParameter(param.getName())) {
137: JavaParameter paramInList = getParameter(param.getName());
138: if (paramInList.isIN() || paramInList.isINOUT()) {
139: //removeParameter(paramInList);
140: replaceParameter(paramInList, param);
141: return;
142: } else {
143: Message message = new Message(
144: "PARAMETER_ALREADY_EXIST", LOG, param.getName());
145: throw new ToolException(message);
146: }
147: }
148:
149: if (param.getType() == null && param.getClassName() == null) {
150: Message msg = new Message("FAIL_TO_CREATE_JAVA_PARAMETER",
151: LOG, param.name, this .getName());
152: throw new ToolException(msg);
153: }
154:
155: parameters.add(param);
156: }
157:
158: public JavaParameter getParameter(String paramName) {
159: for (int i = 0; i < parameters.size(); i++) {
160: JavaParameter jParam = parameters.get(i);
161: if (paramName.equals(jParam.getName())) {
162: return jParam;
163: }
164: }
165: return null;
166: }
167:
168: public List<JavaParameter> getParameters() {
169: return parameters;
170: }
171:
172: public int getParameterCount() {
173: return parameters.size();
174: }
175:
176: public boolean hasException(JavaException exception) {
177: return exceptions.contains(exception);
178: }
179:
180: public void addException(JavaException exception) {
181: if (hasException(exception)) {
182: Message message = new Message("EXCEPTION_ALREADY_EXIST",
183: LOG, exception.getName());
184: throw new ToolException(message);
185: }
186: exceptions.add(exception);
187: }
188:
189: public List<JavaException> getExceptions() {
190: return exceptions;
191: }
192:
193: public OperationType getStyle() {
194: return this .style;
195: }
196:
197: public void setStyle(OperationType ot) {
198: this .style = ot;
199: }
200:
201: public boolean isOneWay() {
202: return OperationType.ONE_WAY.equals(getStyle());
203: }
204:
205: public boolean isWrapperStyle() {
206: return this .wrapperStyle;
207: }
208:
209: public void setWrapperStyle(boolean w) {
210: this .wrapperStyle = w;
211: }
212:
213: public boolean enableMime() {
214: return this .enableMime;
215: }
216:
217: public void setMimeEnable(boolean arg) {
218: enableMime = arg;
219: }
220:
221: public void setSoapStyle(SOAPBinding.Style sty) {
222: this .soapStyle = sty;
223: }
224:
225: public SOAPBinding.Style getSoapStyle() {
226: return this .soapStyle;
227: }
228:
229: public void setSoapAction(String action) {
230: this .soapAction = action;
231: }
232:
233: public String getSoapAction() {
234: return this .soapAction;
235: }
236:
237: public void setSoapUse(SOAPBinding.Use u) {
238: this .soapUse = u;
239: }
240:
241: public SOAPBinding.Use getSoapUse() {
242: return this .soapUse;
243: }
244:
245: public void addAnnotation(String tag, JavaAnnotation annotation) {
246: if (annotation == null) {
247: return;
248: }
249: this .annotations.put(tag, annotation);
250: }
251:
252: public Collection<JavaAnnotation> getAnnotations() {
253: return this .annotations.values();
254: }
255:
256: public Map<String, JavaAnnotation> getAnnotationMap() {
257: return this .annotations;
258: }
259:
260: public List<String> getParameterList() {
261: return getParameterList(true);
262: }
263:
264: public List<String> getParameterListWithoutAnnotation() {
265: return getParameterList(false);
266: }
267:
268: public List<String> getParameterList(boolean includeAnnotation) {
269: List<String> list = new ArrayList<String>();
270: StringBuffer sb = new StringBuffer();
271: for (int i = 0; i < parameters.size(); i++) {
272: JavaParameter parameter = parameters.get(i);
273: if (includeAnnotation) {
274: list.add(parameter.getAnnotation().toString());
275: }
276: sb.setLength(0);
277: if (parameter.isHolder()) {
278: sb.append(parameter.getHolderName());
279: sb.append("<");
280: sb.append(parameter.getClassName());
281: sb.append(">");
282: } else {
283: sb.append(parameter.getClassName());
284: }
285: sb.append(" ");
286: sb.append(parameter.getName());
287: if (i != (parameters.size() - 1)) {
288: sb.append(',');
289: }
290: list.add(sb.toString());
291: }
292: return list;
293: }
294:
295: public String toString() {
296: StringBuffer sb = new StringBuffer();
297: sb.append("\n========================\n");
298: sb.append("\nMethod:");
299: sb.append(getName());
300: sb.append("\n-----------\n");
301: sb.append("\nReturn:");
302: sb.append(getReturn());
303: sb.append("\n------------\n");
304: sb.append("\nParameter:");
305: sb.append(getParameterList());
306: sb.append("\n------------\n");
307: sb.append("\nAnnotations:");
308: sb.append(getAnnotations());
309: sb.append("\n========================\n");
310: return sb.toString();
311: }
312:
313: public void setInterface(JavaInterface intf) {
314: this .javaInterface = intf;
315: }
316:
317: public void annotate(Annotator annotator) {
318: annotator.annotate(this );
319: }
320:
321: public void setJavaCodeBlock(JavaCodeBlock b) {
322: this .block = b;
323: }
324:
325: public JavaCodeBlock getJavaCodeBlock() {
326: return this.block;
327: }
328: }
|