KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > WireFormatNegotiator


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;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InterruptedIOException JavaDoc;
22
23 import org.apache.activemq.command.Command;
24 import org.apache.activemq.command.WireFormatInfo;
25 import org.apache.activemq.openwire.OpenWireFormat;
26 import org.apache.activemq.util.IOExceptionSupport;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import java.util.concurrent.CountDownLatch JavaDoc;
31 import java.util.concurrent.TimeUnit JavaDoc;
32 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
33
34
35 /**
36  * Negotiates the wire format with a new connection
37  */

38 public class WireFormatNegotiator extends TransportFilter {
39
40     private static final Log log = LogFactory.getLog(WireFormatNegotiator.class);
41     
42     private OpenWireFormat wireFormat;
43     private final int minimumVersion;
44     private long negotiateTimeout=15000;
45     
46     private final AtomicBoolean JavaDoc firstStart=new AtomicBoolean JavaDoc(true);
47     private final CountDownLatch JavaDoc readyCountDownLatch = new CountDownLatch JavaDoc(1);
48     private final CountDownLatch JavaDoc wireInfoSentDownLatch = new CountDownLatch JavaDoc(1);
49     
50     /**
51      * Negotiator
52      *
53      * @param next
54      */

55     public WireFormatNegotiator(Transport next, OpenWireFormat wireFormat, int minimumVersion) {
56         super(next);
57         this.wireFormat = wireFormat;
58         if (minimumVersion <= 0) {
59             minimumVersion = 1;
60         }
61         this.minimumVersion = minimumVersion;
62     }
63
64     
65     public void start() throws Exception JavaDoc {
66         super.start();
67         if( firstStart.compareAndSet(true, false) ) {
68             try {
69                 WireFormatInfo info = wireFormat.getPreferedWireFormatInfo();
70                 if (log.isDebugEnabled()) {
71                     log.debug("Sending: " + info);
72                 }
73                 sendWireFormat(info);
74             } finally {
75                 wireInfoSentDownLatch.countDown();
76             }
77         }
78     }
79     
80     public void stop() throws Exception JavaDoc {
81         super.stop();
82         readyCountDownLatch.countDown();
83     }
84     
85     public void oneway(Object JavaDoc command) throws IOException JavaDoc {
86         try {
87             if( !readyCountDownLatch.await(negotiateTimeout, TimeUnit.MILLISECONDS) )
88                 throw new IOException JavaDoc("Wire format negotiation timeout: peer did not send his wire format.");
89         } catch (InterruptedException JavaDoc e) {
90             Thread.currentThread().interrupt();
91             throw new InterruptedIOException JavaDoc();
92         }
93         super.oneway(command);
94     }
95
96  
97     public void onCommand(Object JavaDoc o) {
98         Command command = (Command) o;
99         if( command.isWireFormatInfo() ) {
100             WireFormatInfo info = (WireFormatInfo) command;
101             if (log.isDebugEnabled()) {
102                 log.debug("Received WireFormat: " + info);
103             }
104             
105             try {
106                 wireInfoSentDownLatch.await();
107                 
108                 if (log.isDebugEnabled()) {
109                     log.debug(this + " before negotiation: " + wireFormat);
110                 }
111                 if( !info.isValid() ) {
112                     onException(new IOException JavaDoc("Remote wire format magic is invalid"));
113                 } else if( info.getVersion() < minimumVersion ) {
114                     onException(new IOException JavaDoc("Remote wire format ("+info.getVersion()+") is lower the minimum version required ("+minimumVersion+")"));
115                 }
116                 
117                 wireFormat.renegotiateWireFormat(info);
118                 
119                 if (log.isDebugEnabled()) {
120                     log.debug(this + " after negotiation: " + wireFormat);
121                 }
122     
123             } catch (IOException JavaDoc e) {
124                 onException(e);
125             } catch (InterruptedException JavaDoc e) {
126                 onException((IOException JavaDoc) new InterruptedIOException JavaDoc().initCause(e));
127             } catch (Exception JavaDoc e) {
128                 onException(IOExceptionSupport.create(e));
129             }
130             readyCountDownLatch.countDown();
131             onWireFormatNegotiated(info);
132         }
133         getTransportListener().onCommand(command);
134     }
135
136
137     public void onException(IOException JavaDoc error) {
138         readyCountDownLatch.countDown();
139         /*
140         try {
141             super.oneway(new ExceptionResponse(error));
142         }
143         catch (IOException e) {
144             // ignore as we are already throwing an exception
145         }
146         */

147         super.onException(error);
148     }
149     
150     public String JavaDoc toString() {
151         return next.toString();
152     }
153
154     protected void sendWireFormat(WireFormatInfo info) throws IOException JavaDoc {
155         next.oneway(info);
156     }
157     
158     protected void onWireFormatNegotiated(WireFormatInfo info) {
159     }
160
161
162     public long getNegotiateTimeout() {
163         return negotiateTimeout;
164     }
165
166     public void setNegotiateTimeout(long negotiateTimeout) {
167         this.negotiateTimeout = negotiateTimeout;
168     }
169 }
170
Popular Tags