KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > http > InvocationKey


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.http;
31
32 import com.caucho.util.CharBuffer;
33
34 /**
35  * The invocation key is a <host, port, url> triple
36  */

37 public class InvocationKey {
38   private boolean _isSecure;
39   
40   private CharSequence JavaDoc _host;
41   private int _port;
42   
43   private byte []_uri;
44   private int _uriLength;
45
46   /**
47    * Create an empty invocation key.
48    */

49   public InvocationKey()
50   {
51   }
52
53   /**
54    * Create a new invocation key with the given initial values. The
55    * values are copied.
56    *
57    * @param host the request's host
58    * @param uri the raw byte array containing the uri
59    * @param urlLength the length of the uri in the byte array
60    * @param port the request's port
61    */

62   InvocationKey(boolean isSecure,
63         CharSequence JavaDoc host, int port,
64         byte []uri, int uriLength)
65   {
66     _isSecure = isSecure;
67     
68     if (host != null) {
69       CharBuffer cb = new CharBuffer();
70       cb.append(host);
71       _host = cb;
72     }
73     _port = port;
74     
75     _uri = new byte[uriLength];
76     System.arraycopy(uri, 0, _uri, 0, uriLength);
77     _uriLength = uriLength;
78   }
79
80   /**
81    * Initialize the InvocationKey with a new triple.
82    *
83    * @param host the request's host
84    * @param port the request's port
85    * @param uri the raw byte array containing the uri
86    * @param urlLength the length of the uri in the byte array
87    */

88   public void init(boolean isSecure,
89            CharSequence JavaDoc host, int port,
90            byte []uri, int uriLength)
91   {
92     _isSecure = isSecure;
93     
94     _host = host;
95     _port = port;
96     
97     _uri = uri;
98     _uriLength = uriLength;
99   }
100
101   public boolean isSecure()
102   {
103     return _isSecure;
104   }
105
106   /**
107    * Returns the InvocationKey's host.
108    */

109   public CharSequence JavaDoc getHost()
110   {
111     return _host;
112   }
113
114   /**
115    * Sets the InvocationKey's host.
116    */

117   public void setHost(CharSequence JavaDoc host)
118   {
119     _host = host;
120   }
121
122   /**
123    * Returns the InvocationKey's port
124    */

125   public int getPort()
126   {
127     return _port;
128   }
129
130   /**
131    * Sets the InvocationKey's port.
132    */

133   public void setPort(int port)
134   {
135     _port = port;
136   }
137
138   /**
139    * Returns the raw byte array containing the uri
140    */

141   public byte []getUriBuffer()
142   {
143     return _uri;
144   }
145
146   /**
147    * Returns the length of the uri in the byte array.
148    */

149   public int getUriLength()
150   {
151     return _uriLength;
152   }
153
154   /**
155    * Returns a hash code of the key.
156    */

157   public int hashCode()
158   {
159     int hash = _port + (_isSecure ? 65521 : 31);
160     byte []uri = _uri;
161     int length = _uriLength;
162
163     for (int i = length - 1; i >= 0; i--)
164       hash = 65521 * hash + uri[i];
165
166     if (_host != null)
167       hash = 65521 * hash + _host.hashCode();
168
169     return hash;
170   }
171
172   /**
173    * Returns true if the key matches the test key.
174    */

175   public boolean equals(Object JavaDoc b)
176   {
177     if (! (b instanceof InvocationKey))
178       return false;
179     
180     InvocationKey test = (InvocationKey) b;
181
182     if (_isSecure != test._isSecure)
183       return false;
184
185     if (_port != test._port)
186       return false;
187       
188     int length = _uriLength;
189
190     if (length != test._uriLength)
191       return false;
192       
193     byte []uriA = _uri;
194     byte []uriB = test._uri;
195
196     for (int i = length - 1; i >= 0; i--)
197       if (uriA[i] != uriB[i])
198         return false;
199
200     if (_host == null)
201       return test._host == null;
202     else
203       return _host.equals(test._host);
204   }
205
206   public Object JavaDoc clone()
207   {
208     return new InvocationKey(_isSecure, _host, _port, _uri, _uriLength);
209   }
210
211   /**
212    * Returns a printable representation of the key.
213    */

214   public String JavaDoc toString()
215   {
216     if (_host == null)
217       return "InvocationKey[" + new String JavaDoc(_uri, 0, _uriLength) + "]";
218     else
219       return ("InvocationKey[host=" + _host + ",port=" + _port +
220               ",uri=" + new String JavaDoc(_uri, 0, _uriLength) + "]");
221   }
222 }
223
Popular Tags