KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > ws > rm > providers > RMProvider


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.ws.rm.providers;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.MessageContext;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.handlers.soap.SOAPService;
23 import org.apache.axis.message.SOAPEnvelope;
24 import org.apache.axis.message.addressing.Action;
25 import org.apache.axis.message.addressing.AddressingHeaders;
26 import org.apache.axis.providers.java.RPCProvider;
27 import org.apache.commons.logging.Log;
28 import org.apache.sandesha.Constants;
29 import org.apache.sandesha.IStorageManager;
30 import org.apache.sandesha.RMMessageContext;
31 import org.apache.sandesha.SandeshaContext;
32 import org.apache.sandesha.server.MessageValidator;
33 import org.apache.sandesha.server.RMMessageProcessorIdentifier;
34 import org.apache.sandesha.server.msgprocessors.FaultProcessor;
35 import org.apache.sandesha.server.msgprocessors.IRMMessageProcessor;
36 import org.apache.sandesha.storage.Callback;
37 import org.apache.sandesha.storage.CallbackData;
38 import org.apache.sandesha.ws.rm.RMHeaders;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * class RMProvider
47  *
48  * @author Amila Navarathna
49  * @author Jaliya Ekanayaka
50  * @author Sudar Nimalan
51  */

52
53 public class RMProvider extends RPCProvider {
54
55     private boolean client;
56     private static final Log log = LogFactory.getLog(RMProvider.class.getName());
57     private static Callback callback = null;
58
59     public static void setCallback(Callback cb) {
60         callback = cb;
61     }
62
63     public static void removeCallback() {
64         callback = null;
65     }
66
67     public void processMessage(MessageContext msgContext, SOAPEnvelope reqEnv, SOAPEnvelope resEnv,
68                                Object JavaDoc obj) throws Exception JavaDoc {
69         if (log.isDebugEnabled())
70             RMProvider.log.debug(Constants.InfomationMessage.PROVIDER_RECEIVED_MSG);
71         //Some actions may need to be ignored. e.g. http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT
72
//user can specify them in the server-config.wsdd as parameters to the service
73
//parameter names should be in ignoreAction1, ignoreAction2 format.
74

75
76         if (isIgnorableMessage(msgContext)) {
77             RPCProvider rpcProvider = new RPCProvider();
78             rpcProvider.invoke(msgContext);
79         } else {
80             IStorageManager storageManager = SandeshaContext.init(client);
81             storageManager.init();
82
83             RMMessageContext rmMessageContext = new RMMessageContext();
84             rmMessageContext.setMsgContext(msgContext);
85             try {
86                 MessageValidator.validate(rmMessageContext, client);
87             } catch (AxisFault af) {
88                 FaultProcessor faultProcessor = new FaultProcessor(storageManager, af);
89                 if (!faultProcessor.sendFault(rmMessageContext)) {
90                     msgContext.setPastPivot(true);
91                     msgContext.setResponseMessage(null);
92                     return;
93                 }
94                 return;
95             }
96             RMHeaders rmHeaders = rmMessageContext.getRMHeaders();
97             AddressingHeaders addrHeaders = rmMessageContext.getAddressingHeaders();
98
99             if (null != rmHeaders.getSequence()) {
100                 rmMessageContext.setSequenceID(rmHeaders.getSequence().getIdentifier().toString());
101                 if (null != rmHeaders.getSequence().getLastMessage()) {
102                     rmMessageContext.setLastMessage(true);
103                 }
104             }
105
106             if (addrHeaders.getMessageID() == null) {
107                 rmMessageContext.setMessageID((new Long JavaDoc(System.currentTimeMillis())).toString());
108             } else {
109                 rmMessageContext.setMessageID(addrHeaders.getMessageID().toString());
110             }
111
112             IRMMessageProcessor rmMessageProcessor = RMMessageProcessorIdentifier.getMessageProcessor(rmMessageContext, storageManager);
113
114             if (callback != null) {
115                 CallbackData cbData = getCallbackData(rmMessageContext);
116                 callback.onIncomingMessage(cbData);
117             }
118
119             try {
120                 if (!rmMessageProcessor.processMessage(rmMessageContext)) {
121                     msgContext.setPastPivot(true);
122                     msgContext.setResponseMessage(null);
123                 } else {
124                     msgContext.setPastPivot(true);
125                 }
126             } catch (AxisFault af) {
127                 RMProvider.log.error(af);
128                 FaultProcessor faultProcessor = new FaultProcessor(storageManager, af);
129                 if (!faultProcessor.sendFault(rmMessageContext)) {
130                     msgContext.setPastPivot(true);
131                     msgContext.setResponseMessage(null);
132                     return;
133                 }
134                 return;
135             }
136         }
137     }
138
139     //This is used by the Client to set the
140
//set the side that the RMProvider is used.
141
public void setClient(boolean client) {
142         this.client = client;
143     }
144
145     private boolean isIgnorableMessage(MessageContext msgContext) throws Exception JavaDoc {
146         boolean result = false;
147         AddressingHeaders addrH = new AddressingHeaders(msgContext.getRequestMessage().getSOAPEnvelope());
148         List JavaDoc lst = getIgnorableActions(msgContext);
149         if (lst != null && addrH.getAction() != null) {
150             Iterator JavaDoc ite = lst.iterator();
151             while (ite.hasNext()) {
152                 String JavaDoc str = (String JavaDoc) ite.next();
153                 if (str.equals(addrH.getAction().toString()))
154                     result = true;
155             }
156         }
157
158         return result;
159     }
160
161     private List JavaDoc getIgnorableActions(MessageContext msgContext) {
162         SOAPService soapService = msgContext.getService();
163         Hashtable JavaDoc options = soapService.getOptions();
164         Iterator JavaDoc ite = options.keySet().iterator();
165         List JavaDoc actionList = new ArrayList JavaDoc();
166         while (ite.hasNext()) {
167             String JavaDoc key = (String JavaDoc) ite.next();
168
169             if (key.regionMatches(0, Constants.IGNORE_ACTION, 0, Constants.IGNORE_ACTION.length()))
170                 actionList.add(options.get(key));
171         }
172         return actionList;
173     }
174
175     //for callback
176

177     private CallbackData getCallbackData(RMMessageContext rmMsgContext) {
178         CallbackData cbData = new CallbackData();
179
180         // setting callback data;
181
cbData.setSequenceId(rmMsgContext.getSequenceID());
182         cbData.setMessageId(rmMsgContext.getMessageID());
183         cbData.setMessageType(rmMsgContext.getMessageType());
184
185         Action action = rmMsgContext.getAddressingHeaders().getAction();
186         if (action != null) {
187             cbData.setAction(action.toString());
188         }
189
190         return cbData;
191     }
192     //end callback
193

194 }
Popular Tags