View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with 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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.transport.vmpipe;
21  
22  import java.io.IOException;
23  import java.net.SocketAddress;
24  
25  import org.apache.mina.common.ConnectFuture;
26  import org.apache.mina.common.ExceptionMonitor;
27  import org.apache.mina.common.IoFilterChain;
28  import org.apache.mina.common.IoHandler;
29  import org.apache.mina.common.IoServiceConfig;
30  import org.apache.mina.common.IoSessionConfig;
31  import org.apache.mina.common.support.AbstractIoFilterChain;
32  import org.apache.mina.common.support.BaseIoConnector;
33  import org.apache.mina.common.support.BaseIoConnectorConfig;
34  import org.apache.mina.common.support.BaseIoSessionConfig;
35  import org.apache.mina.common.support.DefaultConnectFuture;
36  import org.apache.mina.transport.vmpipe.support.VmPipe;
37  import org.apache.mina.transport.vmpipe.support.VmPipeFilterChain;
38  import org.apache.mina.transport.vmpipe.support.VmPipeIdleStatusChecker;
39  import org.apache.mina.transport.vmpipe.support.VmPipeSessionImpl;
40  import org.apache.mina.util.AnonymousSocketAddress;
41  
42  /**
43   * Connects to {@link IoHandler}s which is bound on the specified
44   * {@link VmPipeAddress}.
45   * 
46   * @author The Apache Directory Project (mina-dev@directory.apache.org)
47   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
48   */
49  public class VmPipeConnector extends BaseIoConnector {
50      private static final IoSessionConfig CONFIG = new BaseIoSessionConfig() {
51      };
52  
53      private final IoServiceConfig defaultConfig = new BaseIoConnectorConfig() {
54          public IoSessionConfig getSessionConfig() {
55              return CONFIG;
56          }
57      };
58  
59      /**
60       * Creates a new instance.
61       */
62      public VmPipeConnector() {
63      }
64  
65      public ConnectFuture connect(SocketAddress address, IoHandler handler,
66              IoServiceConfig config) {
67          return connect(address, null, handler, config);
68      }
69  
70      public ConnectFuture connect(SocketAddress address,
71              SocketAddress localAddress, IoHandler handler,
72              IoServiceConfig config) {
73          if (address == null)
74              throw new NullPointerException("address");
75          if (handler == null)
76              throw new NullPointerException("handler");
77          if (!(address instanceof VmPipeAddress))
78              throw new IllegalArgumentException("address must be VmPipeAddress.");
79  
80          if (config == null) {
81              config = getDefaultConfig();
82          }
83  
84          VmPipe entry = (VmPipe) VmPipeAcceptor.boundHandlers.get(address);
85          if (entry == null) {
86              return DefaultConnectFuture.newFailedFuture(new IOException(
87                      "Endpoint unavailable: " + address));
88          }
89  
90          DefaultConnectFuture future = new DefaultConnectFuture();
91          VmPipeSessionImpl localSession = new VmPipeSessionImpl(this, config,
92                  getListeners(), new Object(), // lock
93                  new AnonymousSocketAddress(), handler, entry);
94  
95          // initialize connector session
96          try {
97              IoFilterChain filterChain = localSession.getFilterChain();
98              this.getFilterChainBuilder().buildFilterChain(filterChain);
99              config.getFilterChainBuilder().buildFilterChain(filterChain);
100             config.getThreadModel().buildFilterChain(filterChain);
101 
102             // The following sentences don't throw any exceptions.
103             localSession.setAttribute(AbstractIoFilterChain.CONNECT_FUTURE,
104                     future);
105             getListeners().fireSessionCreated(localSession);
106             VmPipeIdleStatusChecker.getInstance().addSession(localSession);
107         } catch (Throwable t) {
108             future.setException(t);
109             return future;
110         }
111 
112         // initialize acceptor session
113         VmPipeSessionImpl remoteSession = localSession.getRemoteSession();
114         try {
115             IoFilterChain filterChain = remoteSession.getFilterChain();
116             entry.getAcceptor().getFilterChainBuilder().buildFilterChain(
117                     filterChain);
118             entry.getConfig().getFilterChainBuilder().buildFilterChain(
119                     filterChain);
120             entry.getConfig().getThreadModel().buildFilterChain(filterChain);
121 
122             // The following sentences don't throw any exceptions.
123             entry.getListeners().fireSessionCreated(remoteSession);
124             VmPipeIdleStatusChecker.getInstance().addSession(remoteSession);
125         } catch (Throwable t) {
126             ExceptionMonitor.getInstance().exceptionCaught(t);
127             remoteSession.close();
128         }
129 
130         ((VmPipeFilterChain) localSession.getFilterChain()).start();
131         ((VmPipeFilterChain) remoteSession.getFilterChain()).start();
132 
133         return future;
134     }
135 
136     public IoServiceConfig getDefaultConfig() {
137         return defaultConfig;
138     }
139 }