View Javadoc

1   /*
2    *   @(#) $Id: Service.java 210062 2005-07-11 03:52:38Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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   */
19  package org.apache.mina.registry;
20  
21  import java.io.Serializable;
22  import java.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  
25  import org.apache.mina.common.TransportType;
26  import org.apache.mina.protocol.vmpipe.VmPipeAddress;
27  
28  /***
29   * Represents a service that is registered to {@link ServiceRegistry}.
30   * 
31   * @author Trustin Lee (trustin@apache.org)
32   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
33   */
34  public class Service implements Serializable, Cloneable
35  {
36      private static final long serialVersionUID = 3258407344110383155L;
37  
38      private final String name;
39  
40      private final TransportType transportType;
41  
42      private final SocketAddress address;
43  
44      /***
45       * Creates a new instance with the specified protocol name, transport type,
46       * and port number to be bound.
47       */
48      public Service( String name, TransportType transportType, int port )
49      {
50          this( name, transportType, new InetSocketAddress( port ) );
51      }
52  
53      /***
54       * Creates a new instance with the specified protocol name, transport type,
55       * and socket address to be bound.
56       */
57      public Service( String name, TransportType transportType, SocketAddress address )
58      {
59          if( name == null )
60              throw new NullPointerException( "name" );
61          if( transportType == null )
62              throw new NullPointerException( "transportType" );
63          if( address == null )
64              throw new NullPointerException( "address" );
65  
66          if( transportType == TransportType.VM_PIPE &&
67              !( address instanceof VmPipeAddress ) )
68          {
69              throw new IllegalArgumentException(
70                      "VM_PIPE transport type accepts only VmPipeAddress: " + address.getClass() );
71          }
72  
73          this.name = name;
74          this.transportType = transportType;
75          this.address = address;
76      }
77  
78      /***
79       * Returns the name of this service (protocol).
80       */
81      public String getName()
82      {
83          return name;
84      }
85  
86      /***
87       * Returns the transport type this service uses.
88       */
89      public TransportType getTransportType()
90      {
91          return transportType;
92      }
93  
94      /***
95       * Returns the socket address this service is bound on.
96       */
97      public SocketAddress getAddress()
98      {
99          return address;
100     }
101 
102     public int hashCode()
103     {
104         return ( ( name.hashCode() * 37 ) ^ transportType.hashCode() * 37 )
105                 ^ address.hashCode();
106     }
107 
108     public boolean equals( Object o )
109     {
110         if( o == null )
111             return false;
112         if( this == o )
113             return true;
114         if( !( o instanceof Service ) )
115             return false;
116 
117         Service that = ( Service ) o;
118         return this.name.equals( that.name )
119                 && this.transportType == that.transportType
120                 && this.address.equals( that.address );
121     }
122 
123     public Object clone()
124     {
125         try
126         {
127             return super.clone();
128         }
129         catch( CloneNotSupportedException e )
130         {
131             throw new InternalError();
132         }
133     }
134     
135     public String toString() {
136         return "(" + transportType + ", " + name + ", " + address + ')';
137     }
138 }