KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > server > RMInvokerWork


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
18 package org.apache.sandesha.server;
19
20 import org.apache.axis.MessageContext;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.components.uuid.UUIDGen;
23 import org.apache.axis.components.uuid.UUIDGenFactory;
24 import org.apache.axis.message.addressing.AddressingHeaders;
25 import org.apache.commons.logging.Log;
26 import org.apache.sandesha.Constants;
27 import org.apache.sandesha.IStorageManager;
28 import org.apache.sandesha.RMMessageContext;
29 import org.apache.sandesha.util.RMMessageCreator;
30
31 /**
32  * This is the worker class for the RMInvoker. RMInvoker instantiate several RMInvokerWorkers to
33  * keep on monitoring SandeshaQueue and invoke the web services. The run method is synchronized
34  * to get the IN-ORDER message invocation.
35  */

36 public class RMInvokerWork {
37
38     private IStorageManager storageManager;
39     private static final Log log = LogFactory.getLog(RMInvokerWork.class.getName());
40     private static final UUIDGen uuidGen = UUIDGenFactory.getUUIDGen();
41     private InvokeHandler invoker = null;
42
43     public RMInvokerWork() {
44         setStorageManager(new ServerStorageManager());
45         getStorageManager().init();
46     }
47
48
49     protected boolean doRealInvoke(MessageContext aMessageContext) throws Exception JavaDoc {
50         if (invoker == null) {
51             invoker = InvokerFactory.getInstance().createInvokeHandler();
52         }
53         return invoker.handleInvoke(aMessageContext);
54     }
55
56
57     public void executeInvoke() throws Exception JavaDoc {
58
59         Object JavaDoc seq = getStorageManager().getNextSeqToProcess();
60         if (seq != null) {
61             synchronized (seq) {
62                 RMMessageContext rmMessageContext = getStorageManager().getNextMessageToProcess(seq);
63                 doWork(rmMessageContext);
64             }
65         }
66     }
67
68
69     /**
70      * @param storageManager The storageManager to set.
71      */

72     protected void setStorageManager(IStorageManager storageManager) {
73         this.storageManager = storageManager;
74     }
75
76     /**
77      * @return Returns the storageManager.
78      */

79     protected IStorageManager getStorageManager() {
80         return storageManager;
81     }
82
83     protected void doWork(RMMessageContext rmMessageContext) throws Exception JavaDoc {
84         if (rmMessageContext != null) {
85             AddressingHeaders addrHeaders = rmMessageContext.getAddressingHeaders();
86             boolean isVoid = doRealInvoke(rmMessageContext.getMsgContext());
87
88             if (!isVoid) {
89
90                 String JavaDoc oldAction = rmMessageContext.getAddressingHeaders().getAction()
91                         .toString();
92                 rmMessageContext.getAddressingHeaders().setAction(oldAction + Constants.RESPONSE);
93                 if (rmMessageContext.isLastMessage()) {
94                     //Insert Terminate Sequnce.
95
if (addrHeaders.getReplyTo() != null) {
96                         String JavaDoc replyTo = addrHeaders.getReplyTo().getAddress().toString();
97                         RMMessageContext terminateMsg = RMMessageCreator.createTerminateSeqMsg(rmMessageContext, Constants.SERVER);
98                         terminateMsg.setOutGoingAddress(replyTo);
99                         getStorageManager().insertTerminateSeqMessage(terminateMsg);
100                     } else {
101                         RMInvokerWork.log.error(Constants.ErrorMessages.CANNOT_SEND_THE_TERMINATE_SEQ);
102                     }
103                 }
104                 //Store the message in the response queue. If there is an application
105
// response then that response is always sent using a new HTTP connection
106
// and the <replyTo> header is used in this case. This is done by the
107
// RMSender.
108
rmMessageContext.setMessageType(Constants.MSG_TYPE_SERVICE_RESPONSE);
109
110                 boolean hasResponseSeq = getStorageManager().isResponseSequenceExist(rmMessageContext.getSequenceID());
111                 boolean firstMsgOfResponseSeq = false;
112                 if (!(hasResponseSeq && rmMessageContext.getRMHeaders().getSequence()
113                         .getMessageNumber().getMessageNumber() == 1)) {
114                     firstMsgOfResponseSeq = !hasResponseSeq;
115                 }
116
117                 rmMessageContext.setMsgNumber(getStorageManager().getNextMessageNumber(rmMessageContext.getSequenceID()));
118                 getStorageManager().insertOutgoingMessage(rmMessageContext);
119
120
121                 if (firstMsgOfResponseSeq) {
122                     String JavaDoc msgIdStr = Constants.UUID + RMInvokerWork.uuidGen.nextUUID();
123
124                     RMMessageContext csRMMsgCtx = RMMessageCreator.createCreateSeqMsg(rmMessageContext, Constants.SERVER, msgIdStr, null);
125                     csRMMsgCtx.setOutGoingAddress(rmMessageContext.getAddressingHeaders()
126                             .getReplyTo().getAddress().toString());
127
128                     csRMMsgCtx.addToMsgIdList(msgIdStr);
129                     csRMMsgCtx.setMessageID(msgIdStr);
130
131                     getStorageManager().setTemporaryOutSequence(csRMMsgCtx.getSequenceID(),
132                             msgIdStr);
133                     getStorageManager().addCreateSequenceRequest(csRMMsgCtx);
134                 }
135             }
136         }
137     }
138 }
139
Popular Tags