1 package org.codehaus.xfire.service;
2
3 import java.lang.reflect.Method;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.Map;
9
10 import javax.xml.namespace.QName;
11
12 /***
13 * Represents the description of a service operation. An operation has a name, and consists of a number of in and out
14 * parameters.
15 * <p/>
16 * Operations are created using the {@link ServiceInfo#addOperation} method.
17 *
18 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
19 * @author <a href="mailto:poutsma@mac.com">Arjen Poutsma</a>
20 */
21 public class OperationInfo
22 implements Visitable
23 {
24 private String name;
25 private String action;
26 private ServiceInfo service;
27 private String mep;
28 private boolean async;
29 private MessageInfo inputMessage;
30 private MessageInfo outputMessage;
31
32 private Map faults = new HashMap();
33 private Method method;
34
35 /***
36 * Initializes a new instance of the <code>OperationInfo</code> class with the given name and service.
37 *
38 * @param name the name of the operation.
39 * @param service the service.
40 */
41 OperationInfo(String name, Method method, ServiceInfo service)
42 {
43 this.name = name;
44 this.service = service;
45 this.method = method;
46 }
47
48 /***
49 * Returns the name of the operation.
50 *
51 * @return the name of the operation.
52 */
53 public String getName()
54 {
55 return name;
56 }
57
58 /***
59 * Sets the name of the operation.
60 *
61 * @param name the new name of the operation.
62 */
63 public void setName(String name)
64 {
65 if ((name == null) || (name.length() == 0))
66 {
67 throw new IllegalArgumentException("Invalid name [" + name + "]");
68 }
69
70 service.removeOperation(this.name);
71 this.name = name;
72 service.addOperation(this);
73 }
74
75 public Method getMethod()
76 {
77 return method;
78 }
79
80 /***
81 * Whether or not the operation should be invoked asynchronously.
82 *
83 * @return
84 */
85 public boolean isAsync()
86 {
87 return async;
88 }
89
90 public void setAsync(boolean async)
91 {
92 this.async = async;
93 }
94
95 /***
96 * Get the message exchange pattern of this operation.
97 * @return
98 */
99 public String getMEP()
100 {
101 return mep;
102 }
103
104 public void setMEP(String mep)
105 {
106 this.mep = mep;
107 }
108
109 /***
110 * Returns the service descriptor of this operation.
111 *
112 * @return the service.
113 */
114 public ServiceInfo getService()
115 {
116 return service;
117 }
118
119 /***
120 * Creates a new message. This message can be set as either {@link #setInputMessage(MessageInfo) input message} or
121 * {@link #setOutputMessage(MessageInfo) output message}.
122 *
123 * @param name the name of the message.
124 * @return the created message.
125 */
126 public MessageInfo createMessage(QName name)
127 {
128 MessageInfo message = new MessageInfo(name, this);
129 return message;
130 }
131
132 /***
133 * Returns the input message info.
134 *
135 * @return the input message info.
136 */
137 public MessageInfo getInputMessage()
138 {
139 return inputMessage;
140 }
141
142 /***
143 * Sets the input message info.
144 *
145 * @param inputMessage the input message info.
146 */
147 public void setInputMessage(MessageInfo inputMessage)
148 {
149 this.inputMessage = inputMessage;
150 }
151
152 /***
153 * Returns the output message info.
154 *
155 * @return the output message info.
156 */
157 public MessageInfo getOutputMessage()
158 {
159 return outputMessage;
160 }
161
162 /***
163 * Sets the output message info.
164 *
165 * @param outputMessage the output message info.
166 */
167 public void setOutputMessage(MessageInfo outputMessage)
168 {
169 this.outputMessage = outputMessage;
170 }
171
172 /***
173 * Adds an fault to this operation.
174 *
175 * @param name the fault name.
176 */
177 public FaultInfo addFault(String name)
178 {
179 if ((name == null) || (name.length() == 0))
180 {
181 throw new IllegalArgumentException("Invalid name [" + name + "]");
182 }
183 if (faults.containsKey(name))
184 {
185 throw new IllegalArgumentException("A fault with name [" + name + "] already exists in this operation");
186 }
187 FaultInfo fault = new FaultInfo(name, this);
188 addFault(fault);
189 return fault;
190 }
191
192 /***
193 * Adds a fault to this operation.
194 *
195 * @param fault the fault.
196 */
197 void addFault(FaultInfo fault)
198 {
199 faults.put(fault.getName(), fault);
200 }
201
202 /***
203 * Removes a fault from this operation.
204 *
205 * @param name the qualified fault name.
206 */
207 public void removeFault(String name)
208 {
209 faults.remove(name);
210 }
211
212 /***
213 * Returns the fault with the given name, if found.
214 *
215 * @param name the name.
216 * @return the fault; or <code>null</code> if not found.
217 */
218 public FaultInfo getFault(String name)
219 {
220 return (FaultInfo) faults.get(name);
221 }
222
223 /***
224 * Returns all faults for this operation.
225 *
226 * @return all faults.
227 */
228 public Collection getFaults()
229 {
230 return Collections.unmodifiableCollection(faults.values());
231 }
232
233 public String getAction()
234 {
235 return action;
236 }
237
238 public void setAction(String action)
239 {
240 this.action = action;
241 }
242
243 /***
244 * Acceps the given visitor. Iterates over the input and output messages, if set.
245 *
246 * @param visitor the visitor.
247 */
248 public void accept(Visitor visitor)
249 {
250 visitor.startOperation(this);
251 if (inputMessage != null)
252 {
253 inputMessage.accept(visitor);
254 }
255 if (outputMessage != null)
256 {
257 outputMessage.accept(visitor);
258 }
259 for (Iterator iterator = faults.values().iterator(); iterator.hasNext();)
260 {
261 FaultInfo faultInfo = (FaultInfo) iterator.next();
262 faultInfo.accept(visitor);
263 }
264 visitor.endOperation(this);
265 }
266 }