001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.bayeux;
017:
018: import java.util.Arrays;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.directwebremoting.dwrp.Batch;
025: import org.directwebremoting.dwrp.PlainCallMarshaller;
026: import org.directwebremoting.extend.Call;
027: import org.directwebremoting.extend.Calls;
028: import org.directwebremoting.extend.ConverterManager;
029: import org.directwebremoting.extend.EnginePrivate;
030: import org.directwebremoting.extend.FormField;
031: import org.directwebremoting.extend.Remoter;
032: import org.directwebremoting.extend.Replies;
033: import org.directwebremoting.extend.Reply;
034: import org.directwebremoting.extend.ScriptConduit;
035:
036: import dojox.cometd.Bayeux;
037: import dojox.cometd.Client;
038: import dojox.cometd.Listener;
039:
040: /**
041: * @author Greg Wilkins [gregw at webtide dot com]
042: * @author Joe Walker [joe at getahead dot ltd dot uk]
043: */
044: public class BayeuxClient implements Listener {
045: public BayeuxClient(Bayeux bayeux) {
046: this .bayeux = bayeux;
047:
048: // At this point BayeuxClient is fully initialized so it is safe to
049: // allow other classes to see and use us.
050: //noinspection ThisEscapedInObjectConstruction
051: this .client = bayeux.newClient("dwr", this );
052: bayeux.subscribe("/dwr", client);
053: }
054:
055: /* (non-Javadoc)
056: * @see dojox.cometd.Listener#deliver(dojox.cometd.Client, java.lang.String, java.lang.Object, java.lang.String)
057: */
058: public void deliver(Client fromClient, String toChannel,
059: Object message, String msgId) {
060: try {
061: @SuppressWarnings("unchecked")
062: Map<String, Object> msgParams = (Map<String, Object>) message;
063: Map<String, FormField> fileParams = new HashMap<String, FormField>(
064: msgParams.size());
065: for (Map.Entry<String, Object> entry : msgParams.entrySet()) {
066: String param = (String) entry.getValue();
067: FormField formField = new FormField(param);
068: fileParams.put(entry.getKey(), formField);
069: }
070:
071: Batch batch = new Batch(fileParams);
072: Calls calls = plainCallMarshaller.marshallInbound(batch);
073:
074: log.debug("Calls=" + calls);
075:
076: for (int i = 0; i < calls.getCallCount(); i++) {
077: Call call = calls.getCall(i);
078: Object[] params = call.getParameters();
079: log.debug("Call["
080: + i
081: + "]="
082: + call.getScriptName()
083: + "."
084: + call.getMethodName()
085: + (params == null ? "[]" : Arrays
086: .asList(params)));
087: }
088:
089: Replies replies = remoter.execute(calls);
090:
091: ScriptConduit conduit = new BayeuxScriptConduit(
092: converterManager, JSON_OUTPUT);
093: for (Reply reply : replies) {
094: String batchId = calls.getBatchId();
095: log.debug("Reply=" + reply + " BatchId=" + batchId);
096:
097: if (reply.getThrowable() != null) {
098: Throwable ex = reply.getThrowable();
099: EnginePrivate.remoteHandleException(conduit,
100: batchId, reply.getCallId(), ex);
101:
102: log.warn("--Erroring: batchId[" + batchId
103: + "] message[" + ex.toString() + ']');
104: } else {
105: Object data = reply.getReply();
106: log.debug("data=" + data);
107: EnginePrivate.remoteHandleCallback(conduit,
108: batchId, reply.getCallId(), data);
109: }
110: }
111:
112: String output = conduit.toString();
113: log.debug("<< " + output);
114: bayeux.publish(client, "/dwr/" + fromClient.getId(),
115: output, calls.getBatchId());
116: } catch (Exception ex) {
117: log.warn("Protocol Error", ex);
118: }
119: }
120:
121: /* (non-Javadoc)
122: * @see dojox.cometd.Listener#removed(java.lang.String, boolean)
123: */
124: public void removed(String clientId, boolean timeout) {
125: }
126:
127: /**
128: * @param remoter
129: */
130: public void setRemoter(Remoter remoter) {
131: this .remoter = remoter;
132: }
133:
134: /**
135: * @param converterManager
136: */
137: public void setConverterManager(ConverterManager converterManager) {
138: this .converterManager = converterManager;
139: }
140:
141: /**
142: * @param plainCallMarshaller
143: */
144: public void setPlainCallMarshaller(
145: PlainCallMarshaller plainCallMarshaller) {
146: this .plainCallMarshaller = plainCallMarshaller;
147: }
148:
149: /**
150: * We're note constraining our output to JSON at the moment
151: */
152: private static final boolean JSON_OUTPUT = false;
153:
154: private Bayeux bayeux;
155:
156: private Client client;
157:
158: private Remoter remoter;
159:
160: private ConverterManager converterManager;
161:
162: private PlainCallMarshaller plainCallMarshaller;
163:
164: /**
165: * The log stream
166: */
167: private static final Log log = LogFactory
168: .getLog(BayeuxClient.class);
169: }
|