KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > vm > VMTransportServer


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.vm;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetSocketAddress JavaDoc;
22 import java.net.URI JavaDoc;
23
24 import org.apache.activemq.command.BrokerInfo;
25 import org.apache.activemq.transport.MutexTransport;
26 import org.apache.activemq.transport.ResponseCorrelator;
27 import org.apache.activemq.transport.Transport;
28 import org.apache.activemq.transport.TransportAcceptListener;
29 import org.apache.activemq.transport.TransportServer;
30
31 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
32
33 /**
34  * Broker side of the VMTransport
35  *
36  */

37 public class VMTransportServer implements TransportServer {
38
39     private TransportAcceptListener acceptListener;
40     private final URI JavaDoc location;
41     private boolean disposed;
42     
43     private final AtomicInteger JavaDoc connectionCount = new AtomicInteger JavaDoc(0);
44     private final boolean disposeOnDisconnect;
45
46     /**
47      * @param location
48      * @param disposeOnDisconnect
49      */

50     public VMTransportServer(URI JavaDoc location, boolean disposeOnDisconnect) {
51         this.location = location;
52         this.disposeOnDisconnect=disposeOnDisconnect;
53     }
54     
55     /**
56      *@return a pretty print of this
57      */

58     public String JavaDoc toString(){
59         return "VMTransportServer(" + location +")";
60     }
61     
62     /**
63      * @return new VMTransport
64      * @throws IOException
65      */

66     public VMTransport connect() throws IOException JavaDoc {
67         TransportAcceptListener al;
68         synchronized (this) {
69             if( disposed )
70                 throw new IOException JavaDoc("Server has been disposed.");
71             al = acceptListener;
72         }
73         if( al == null)
74             throw new IOException JavaDoc("Server TransportAcceptListener is null.");
75             
76         connectionCount.incrementAndGet();
77         VMTransport client = new VMTransport(location) {
78             public void stop() throws Exception JavaDoc {
79                 if( disposed )
80                     return;
81                 super.stop();
82                 if( connectionCount.decrementAndGet()==0 && disposeOnDisconnect ) {
83                     VMTransportServer.this.stop();
84                 }
85             };
86         };
87         
88         VMTransport server = new VMTransport(location);
89         client.setPeer(server);
90         server.setPeer(client);
91         al.onAccept(configure(server));
92         return client;
93     }
94     
95     /**
96      * Configure transport
97      * @param transport
98      * @return the Transport
99      */

100     public static Transport configure(Transport transport) {
101         transport = new MutexTransport(transport);
102         transport = new ResponseCorrelator(transport);
103         return transport;
104     }
105
106
107     /**
108      * Set the Transport accept listener for new Connections
109      * @param acceptListener
110      *
111      */

112     synchronized public void setAcceptListener(TransportAcceptListener acceptListener) {
113         this.acceptListener = acceptListener;
114     }
115
116     public void start() throws IOException JavaDoc {
117     }
118
119     public void stop() throws IOException JavaDoc {
120         VMTransportFactory.stopped(this);
121     }
122
123     public URI JavaDoc getConnectURI() {
124         return location;
125     }
126     
127     public URI JavaDoc getBindURI() {
128         return location;
129     }
130
131     public void setBrokerInfo(BrokerInfo brokerInfo) {
132     }
133
134     public InetSocketAddress JavaDoc getSocketAddress() {
135         return null;
136     }
137 }
138
Popular Tags