View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Hiram Chirino
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.activeio.adapter;
19  
20  import java.io.IOException;
21  import java.net.URI;
22  
23  import org.activeio.AsynchChannel;
24  import org.activeio.AsynchChannelFactory;
25  import org.activeio.AsynchChannelServer;
26  import org.activeio.ChannelFactory;
27  import org.activeio.SynchChannelFactory;
28  
29  import EDU.oswego.cs.dl.util.concurrent.Executor;
30  
31  /***
32   * @version $Revision$
33   */
34  public class SynchToAsynchChannelFactoryAdaptor implements AsynchChannelFactory {
35      
36      private final SynchChannelFactory synchChannelFactory;
37      private final Executor executor;
38          
39      static public AsynchChannelFactory adapt(SynchChannelFactory channelFactory) {
40          return adapt(channelFactory, ChannelFactory.DEFAULT_EXECUTOR);
41      }
42      
43      static public AsynchChannelFactory adapt(SynchChannelFactory channelFactory, Executor executor ) {
44  
45          // It might not need adapting
46          if( channelFactory instanceof AsynchChannelFactory ) {
47              return (AsynchChannelFactory) channelFactory;
48          }
49  
50          // Can we just just undo the adaptor
51          if( channelFactory.getClass() == AsynchToSynchChannelFactoryAdaptor.class ) {
52              return ((AsynchToSynchChannelFactoryAdaptor)channelFactory).getAsynchChannelFactory();
53          }
54          
55          return new SynchToAsynchChannelFactoryAdaptor((SynchChannelFactory)channelFactory, executor);        
56      }
57      
58      /***
59       * @deprecated {@see #adapt(SynchChannelFactory)}
60       */
61      public SynchToAsynchChannelFactoryAdaptor(final SynchChannelFactory next) {
62          this(next, ChannelFactory.DEFAULT_EXECUTOR);
63      }
64      
65      /***
66       * @deprecated {@see #adapt(SynchChannelFactory, Executor)}
67       */
68      public SynchToAsynchChannelFactoryAdaptor(final SynchChannelFactory next, Executor executor) {
69          this.synchChannelFactory = next;
70          this.executor = executor;
71      }
72          
73      public AsynchChannel openAsynchChannel(URI location) throws IOException {
74          return SynchToAsynchChannelAdapter.adapt(synchChannelFactory.openSynchChannel(location),executor);
75      }
76  
77      public AsynchChannelServer bindAsynchChannel(URI location) throws IOException {
78          return new SynchToAsynchChannelServerAdapter(synchChannelFactory.bindSynchChannel(location),executor);
79      }
80      
81      public SynchChannelFactory getSynchChannelFactory() {
82          return synchChannelFactory;
83      }
84  }