Clover coverage report - ActiveIO - 1.0 Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT FRAMES NO FRAMES |
file stats: | LOC: | 127 |  | Methods: | 7
| NCLOC: | 62 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
SocketSynchChannelFactory.java | 50% | 79.2% | 71.4% | 74.3% |

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.net;
19 |  |
20 |  |
     import java.io.IOException;
21 |  |
     import java.net.InetAddress;
22 |  |
     import java.net.ServerSocket;
23 |  |
     import java.net.Socket;
24 |  |
     import java.net.URI;
25 |  |
     import java.net.URISyntaxException;
26 |  |
27 |  |
     import javax.net.ServerSocketFactory;
28 |  |
     import javax.net.SocketFactory;
29 |  |
30 |  |
     import org.activeio.SynchChannel;
31 |  |
     import org.activeio.SynchChannelFactory;
32 |  |
     import org.activeio.SynchChannelServer;
33 |  |
34 |  |
     /**
35 |  |
      * A TcpSynchChannelFactory creates {@see org.activeio.net.TcpSynchChannel}
36 |  |
      * and {@see org.activeio.net.TcpSynchChannelServer} objects.
37 |  |
      *
38 |  |
      * @version $Revision$
39 |  |
      */
40 |  |
     public class SocketSynchChannelFactory implements SynchChannelFactory {
41 |  |
42 |  |
         protected static final int DEFAULT_BACKLOG = 500;
43 |  |
44 |  |
         private final SocketFactory socketFactory;
45 |  |
         private final ServerSocketFactory serverSocketFactory;
46 |  |
         private int backlog = DEFAULT_BACKLOG;
47 |  |
48 | 38 |
         public SocketSynchChannelFactory() {
49 | 38 |
             this(SocketFactory.getDefault(), ServerSocketFactory.getDefault());
50 |  |
         }
51 |  |
52 | 44 |
         public SocketSynchChannelFactory(SocketFactory socketFactory, ServerSocketFactory serverSocketFactory) {
53 | 44 |
             this.socketFactory = socketFactory;
54 | 44 |
             this.serverSocketFactory = serverSocketFactory;
55 |  |
         }
56 |  |
57 |  |
         /**
58 |  |
          * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
59 |  |
          *
60 |  |
          * @see org.activeio.SynchChannelFactory#openSynchChannel(java.net.URI)
61 |  |
          */
62 | 34 |
         public SynchChannel openSynchChannel(URI location) throws IOException {
63 | 34 |
             Socket socket=null;
64 | 34 |
             socket = socketFactory.createSocket(location.getHost(), location.getPort());
65 | 34 |
             return createSynchChannel(socket);
66 |  |
         }
67 |  |
68 |  |
         /**
69 |  |
          * @param socket
70 |  |
          * @return
71 |  |
          * @throws IOException
72 |  |
          */
73 | 34 |
         protected SynchChannel createSynchChannel(Socket socket) throws IOException {
74 | 34 |
             return new SocketSynchChannel(socket);
75 |  |
         }
76 |  |
77 |  |
         /**
78 |  |
          * Binds a server socket a the {@param location}'s port.
79 |  |
          *
80 |  |
          * @see org.activeio.SynchChannelFactory#bindSynchChannel(java.net.URI)
81 |  |
          */
82 | 42 |
         public SynchChannelServer bindSynchChannel(URI bindURI) throws IOException {
83 |  |
84 | 42 |
             String host = bindURI.getHost();
85 | 42 |
             InetAddress addr;
86 | 42 |
             if( (host == null || host.length() == 0 || host.equals("localhost") ) ) {
87 | 42 |
                 addr = InetAddress.getLocalHost();
88 |  |
             } else {
89 | 0 |
                 addr = InetAddress.getByName(host);
90 |  |
             }
91 | 42 |
             ServerSocket serverSocket;
92 |  |
93 | 42 |
             if (addr.equals(InetAddress.getLocalHost())) {
94 | 42 |
                 serverSocket = serverSocketFactory.createServerSocket(bindURI.getPort(), backlog);
95 |  |
             } else {
96 | 0 |
                 serverSocket = serverSocketFactory.createServerSocket(bindURI.getPort(), backlog, addr);
97 |  |
             }
98 |  |
99 | 42 |
             URI connectURI=bindURI;
100 | 42 |
             try {
101 | 42 |
                 connectURI = URISupport.changeHost(connectURI, addr.getHostName());
102 | 42 |
                 connectURI = URISupport.changePort(connectURI, serverSocket.getLocalPort());
103 |  |
             } catch (URISyntaxException e) {
104 | 0 |
                 throw (IOException)new IOException("Could build connect URI: "+e).initCause(e);
105 |  |
             }
106 |  |
107 | 42 |
             return new SocketSynchChannelServer(serverSocket, bindURI, connectURI);
108 |  |
         }
109 |  |
110 |  |
         /**
111 |  |
          * @return Returns the backlog.
112 |  |
          */
113 | 0 |
         public int getBacklog() {
114 | 0 |
             return backlog;
115 |  |
         }
116 |  |
117 |  |
         /**
118 |  |
          * @param backlog
119 |  |
          *            The backlog to set.
120 |  |
          */
121 | 0 |
         public void setBacklog(int backlog) {
122 | 0 |
             this.backlog = backlog;
123 |  |
         }
124 |  |
125 |  |
126 |  |
     }
127 |  |