KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > client > ClientPropertyValidator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.sandesha.client;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.client.Call;
21 import org.apache.sandesha.Constants;
22 import org.apache.sandesha.RMMessageContext;
23 import org.apache.sandesha.SandeshaContext;
24
25 import javax.xml.namespace.QName JavaDoc;
26
27 /**
28  * This class is used to get the client side properties and to set them to the RMMessageContext.
29  *
30  * @author Jaliya Ekanayake
31  */

32 public class ClientPropertyValidator {
33
34     public static synchronized RMMessageContext validate(Call call) throws AxisFault {
35
36         RMMessageContext rmMessageContext = null;
37
38         boolean inOut = getInOut(call);
39         long msgNumber = getMessageNumber(call);
40         boolean lastMessage = getLastMessage(call);
41         boolean sync = getSync(call);
42         String JavaDoc action = getAction(call);
43         String JavaDoc sourceURL = null;
44         String JavaDoc from = getFrom(call);
45         String JavaDoc replyTo = getReplyTo(call);
46         String JavaDoc acksTo = getAcksTo(call);
47         String JavaDoc to = getTo(call);
48         String JavaDoc faultTo = getFaultTo(call);
49         boolean sendOffer = getOffer(call);
50         String JavaDoc key = getKey(call);
51         SandeshaContext ctx = getCtx(call);
52
53         try {
54             sourceURL = getSourceURL(call);
55         } catch (Exception JavaDoc e) {
56             throw new AxisFault(e.getMessage());
57         }
58
59         String JavaDoc errorMsg = getValidated(msgNumber, action, replyTo, sync, inOut);
60         if (errorMsg == null) {
61             rmMessageContext = new RMMessageContext();
62
63             rmMessageContext.setSync(sync);
64             rmMessageContext.setHasResponse(inOut);
65             rmMessageContext.setMsgNumber(msgNumber);
66             rmMessageContext.setLastMessage(lastMessage);
67             rmMessageContext.setSourceURL(sourceURL);
68             rmMessageContext.setSequenceID(key);
69             rmMessageContext.setReplyTo(replyTo);
70             rmMessageContext.setFrom(from);
71             rmMessageContext.setAction(action);
72             rmMessageContext.setAcksTo(acksTo);
73             rmMessageContext.setTo(to);
74             rmMessageContext.setFaultTo(faultTo);
75             rmMessageContext.setSendOffer(sendOffer);
76             rmMessageContext.setCtx(ctx);
77             return rmMessageContext;
78
79         } else
80             throw new AxisFault(errorMsg);
81
82     }
83
84     private static SandeshaContext getCtx(Call call) {
85         return (SandeshaContext) call.getProperty("context");
86     }
87
88     private static String JavaDoc getKey(Call call) {
89         return (String JavaDoc) call.getProperty(Constants.ClientProperties.CALL_KEY);
90     }
91
92     private static boolean getOffer(Call call) {
93         Boolean JavaDoc sendOffer = (Boolean JavaDoc) call.getProperty(Constants.ClientProperties.SEND_OFFER);
94         if (sendOffer != null) {
95             return sendOffer.booleanValue();
96         } else
97             return false;
98     }
99
100     private static String JavaDoc getFaultTo(Call call) {
101         String JavaDoc faultTo = (String JavaDoc) call.getProperty(Constants.ClientProperties.FAULT_TO);
102         if (faultTo != null)
103             return faultTo;
104         else
105             return null;
106     }
107
108     private static String JavaDoc getTo(Call call) {
109         String JavaDoc to = (String JavaDoc) call.getProperty(Constants.ClientProperties.TO);
110         if (to != null)
111             return to;
112         else
113             return call.getTargetEndpointAddress();
114     }
115
116
117     /**
118      * This will decide whether we have an IN-OUT style service request
119      * or IN-ONLY service request by checking the value of the QName
120      * returned by the call.getReturnType().
121      *
122      * @param call
123      * @return
124      */

125     private static boolean getInOut(Call call) {
126         QName JavaDoc returnQName = (QName JavaDoc) call.getReturnType();
127         if (returnQName != null)
128             return true;
129         else
130             return false;
131
132     }
133
134
135     private static String JavaDoc getAction(Call call) {
136         String JavaDoc action = (String JavaDoc) call.getProperty(Constants.ClientProperties.ACTION);
137         if (action != null)
138             return action;
139         else
140             return null;
141     }
142
143     private static String JavaDoc getAcksTo(Call call) {
144         String JavaDoc acksTo = (String JavaDoc) call.getProperty(Constants.ClientProperties.ACKS_TO);
145         if (acksTo != null)
146             return acksTo;
147         else
148             return null;
149     }
150
151     private static boolean getSync(Call call) {
152         Boolean JavaDoc synchronous = (Boolean JavaDoc) call.getProperty(Constants.ClientProperties.SYNC);
153         if (synchronous != null) {
154             return synchronous.booleanValue();
155         } else
156             return false;//If the user has not specified the synchronous
157
}
158
159
160     private static String JavaDoc getSourceURL(Call call) throws Exception JavaDoc {
161         String JavaDoc sourceURL = null;
162         sourceURL = (String JavaDoc) call.getProperty(Constants.ClientProperties.SOURCE_URL);
163         if (sourceURL != null) {
164             return sourceURL;
165         } else {
166             throw new Exception JavaDoc("No Source URL specified");
167
168         }
169     }
170
171     /**
172      * @param call
173      * @return
174      */

175     private static long getMessageNumber(Call call) {
176         Object JavaDoc temp = call.getProperty(Constants.ClientProperties.MSG_NUMBER);
177         SandeshaContext ctx = (SandeshaContext) call.getProperty(Constants.CONTEXT);
178         long msgNo = ctx.getMessageNumber();
179         if (temp == null) {
180             ctx.setMessageNumber(++msgNo);
181         } else {
182             msgNo = ((Long JavaDoc) call.getProperty(Constants.ClientProperties.MSG_NUMBER)).longValue();
183
184         }
185
186         return msgNo;
187     }
188
189     private static boolean getLastMessage(Call call) {
190         Boolean JavaDoc lastMessage = (Boolean JavaDoc) call.getProperty(Constants.ClientProperties.LAST_MESSAGE);
191         if (lastMessage != null)
192             return lastMessage.booleanValue();
193         else
194             return false;
195
196     }
197
198     private static String JavaDoc getValidated(long msgNumber, String JavaDoc action, String JavaDoc replyTo, boolean sync,
199                                        boolean inOut) {
200         String JavaDoc errorMsg = null;
201
202         if (sync && inOut && replyTo == null) {
203             errorMsg = Constants.ErrorMessages.CLIENT_PROPERTY_VALIDATION_ERROR;
204             return errorMsg;
205         }
206
207         if ((action == null)) {
208             errorMsg = Constants.ErrorMessages.MESSAGE_NUMBER_NOT_SPECIFIED;
209             return errorMsg;
210         }
211         return errorMsg;
212     }
213
214     private static String JavaDoc getFrom(Call call) {
215         return (String JavaDoc) call.getProperty(Constants.ClientProperties.FROM);
216     }
217
218     private static String JavaDoc getReplyTo(Call call) {
219         return (String JavaDoc) call.getProperty(Constants.ClientProperties.REPLY_TO);
220     }
221
222 }
Popular Tags