KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > stomp > StompTransportFilter


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

18 package org.apache.activemq.transport.stomp;
19
20 import java.io.IOException JavaDoc;
21
22 import javax.jms.JMSException JavaDoc;
23
24 import org.apache.activemq.command.Command;
25 import org.apache.activemq.transport.Transport;
26 import org.apache.activemq.transport.TransportFilter;
27 import org.apache.activemq.util.IOExceptionSupport;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * The StompTransportFilter normally sits on top of a TcpTransport
33  * that has been configured with the StompWireFormat and is used to
34  * convert STOMP commands to ActiveMQ commands.
35  *
36  * All of the coversion work is done by delegating to the ProtocolConverter.
37  *
38  * @author <a HREF="http://hiramchirino.com">chirino</a>
39  */

40 public class StompTransportFilter extends TransportFilter {
41     static final private Log log = LogFactory.getLog(StompTransportFilter.class);
42     private final ProtocolConverter protocolConverter;
43
44     private final Object JavaDoc sendToActiveMQMutex = new Object JavaDoc();
45     private final Object JavaDoc sendToStompMutex = new Object JavaDoc();
46
47     private final FrameTranslator frameTranslator;
48
49     private boolean trace;
50     
51     public StompTransportFilter(Transport next, FrameTranslator translator) {
52         super(next);
53         this.frameTranslator = translator;
54         this.protocolConverter = new ProtocolConverter(this, translator);
55     }
56
57     public void oneway(Object JavaDoc o) throws IOException JavaDoc {
58         try {
59             final Command command = (Command) o;
60             protocolConverter.onActiveMQCommad(command);
61         } catch (JMSException JavaDoc e) {
62             throw IOExceptionSupport.create(e);
63         }
64     }
65
66     public void onCommand(Object JavaDoc command) {
67         try {
68             if( trace ) {
69                 log.trace("Received: \n"+command);
70             }
71             protocolConverter.onStompCommad((StompFrame) command);
72         } catch (IOException JavaDoc e) {
73             onException(e);
74         } catch (JMSException JavaDoc e) {
75             onException(IOExceptionSupport.create(e));
76         }
77     }
78
79     public void sendToActiveMQ(Command command) {
80         synchronized(sendToActiveMQMutex) {
81             transportListener.onCommand(command);
82         }
83     }
84
85     public void sendToStomp(StompFrame command) throws IOException JavaDoc {
86         if( trace ) {
87             log.trace("Sending: \n"+command);
88         }
89         synchronized(sendToStompMutex) {
90             next.oneway(command);
91         }
92     }
93
94     public FrameTranslator getFrameTranslator()
95     {
96         return frameTranslator;
97     }
98
99     public boolean isTrace() {
100         return trace;
101     }
102
103     public void setTrace(boolean trace) {
104         this.trace = trace;
105     }
106 }
107
Popular Tags