1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.logicblaze.lingo.jms.marshall;
19
20 import org.logicblaze.lingo.LingoInvocation;
21 import org.logicblaze.lingo.jms.Requestor;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.springframework.remoting.support.RemoteInvocation;
25 import org.springframework.remoting.support.RemoteInvocationResult;
26
27 import javax.jms.JMSException;
28 import javax.jms.Message;
29 import javax.jms.ObjectMessage;
30
31 /***
32 * Represents the strategy of marshalling of requests and responses in and out of JMS messages
33 *
34 * @version $Revision: 1.2 $
35 */
36 public class DefaultMarshaller implements Marshaller {
37
38 private static final Log log = LogFactory.getLog(DefaultMarshaller.class);
39
40 private boolean ignoreInvalidMessages;
41
42 public Message createRequestMessage(Requestor requestor, LingoInvocation invocation) throws JMSException {
43 ObjectMessage message = requestor.getSession().createObjectMessage(invocation);
44 appendMessageHeaders(message, requestor, invocation);
45 return message;
46 }
47
48 public RemoteInvocationResult extractInvocationResult(Message message) throws JMSException {
49 handleInvocationResultHeaders(message);
50 if (message instanceof ObjectMessage) {
51 ObjectMessage objectMessage = (ObjectMessage) message;
52 Object body = objectMessage.getObject();
53 if (body instanceof RemoteInvocationResult) {
54 return (RemoteInvocationResult) body;
55 }
56 }
57 return onInvalidClientMessage(message);
58 }
59
60 public RemoteInvocation readRemoteInvocation(Message message) throws JMSException {
61 handleInvocationHeaders(message);
62 if (message instanceof ObjectMessage) {
63 ObjectMessage objectMessage = (ObjectMessage) message;
64 Object body = objectMessage.getObject();
65 if (body instanceof RemoteInvocation) {
66 return (RemoteInvocation) body;
67 }
68 }
69 return onInvalidMessage(message);
70 }
71
72
73
74 public boolean isIgnoreInvalidMessages() {
75 return ignoreInvalidMessages;
76 }
77
78 /***
79 * Sets whether invalidly formatted messages should be silently ignored or not
80 */
81 public void setIgnoreInvalidMessages(boolean ignoreInvalidMessages) {
82 this.ignoreInvalidMessages = ignoreInvalidMessages;
83 }
84
85
86
87 protected RemoteInvocationResult onInvalidClientMessage(Message message) throws JMSException {
88 throw new JMSException("Invalid response message: " + message);
89 }
90
91 /***
92 * Handle invalid messages by just logging, though a different implementation
93 * may wish to throw exceptions
94 */
95 protected RemoteInvocation onInvalidMessage(Message message) {
96 String text = "Invalid message will be discarded: " + message;
97 log.info(text);
98 if (!ignoreInvalidMessages) {
99 throw new RuntimeException(text);
100 }
101 return null;
102 }
103
104 /***
105 * A strategy method for derived classes to allow them a plugin point to perform custom header processing
106 */
107 protected void appendMessageHeaders(Message message, Requestor requestor, LingoInvocation invocation) {
108 }
109
110
111 /***
112 * A strategy method to allow derived classes to process the headers in a special way
113 */
114 protected void handleInvocationHeaders(Message message) {
115 }
116
117 /***
118 * A strategy method to allow derived classes to process the headers in a special way
119 */
120 protected void handleInvocationResultHeaders(Message message) {
121 }
122
123 }