KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > Uri


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/Uri.java,v 1.19.2.2 2004/11/24 11:38:15 ozeigermann Exp $
3  * $Revision: 1.19.2.2 $
4  * $Date: 2004/11/24 11:38:15 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.common;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 import org.apache.slide.store.Store;
30
31 /**
32  * This class manages the unique identifier of an object which is
33  * manipulated by Slide.
34  *
35  * @version $Revision: 1.19.2.2 $
36  */

37 public class Uri implements Cloneable JavaDoc, java.io.Serializable JavaDoc {
38     
39     
40     // ----------------------------------------------------------- Constructors
41

42     
43     /**
44      * Constructor.
45      *
46      * @param namespace Namespace associated with this Uri
47      * @param uri Uri
48      *
49      * @deprecated use signature with SlideToken instead
50      */

51     public Uri(Namespace namespace, String JavaDoc uri) {
52         this(null, namespace, uri);
53     }
54     
55     
56     /**
57      * Constructor.
58      *
59      * @param token Slide token
60      * @param namespace Namespace associated with this Uri
61      * @param uri Uri
62      */

63     public Uri(SlideToken token, Namespace namespace, String JavaDoc uri) {
64         this.token = token;
65         this.namespace = namespace;
66         parseUri(uri);
67     }
68     
69     
70     // ----------------------------------------------------- Instance Variables
71

72     
73     /**
74      * Uri's namespace.
75      */

76     protected transient Namespace namespace;
77     
78     
79     /**
80      * uri MUST be unique.
81      */

82     protected transient String JavaDoc uri;
83     
84     
85     /**
86      * Scopes to which this Uri belongs.
87      */

88     protected transient ScopeTokenizer scopes;
89     
90     
91     /**
92      * FIXME : Is that still used ?
93      */

94     protected transient Scope scope;
95     
96     
97     /**
98      * Associated Store instance.
99      */

100     protected transient Store store;
101     
102     
103     /**
104      * Associated SlideToken.
105      */

106     protected transient SlideToken token;
107     
108     
109     // ------------------------------------------------------------- Properties
110

111     
112     /**
113      * Uri mutator.
114      *
115      * @param uri New uri
116      */

117     public void setUri(String JavaDoc uri) {
118         parseUri(uri);
119     }
120     
121     
122     /**
123      * Method getRoot
124      *
125      * @param uri an Uri
126      *
127      * @return an Uri
128      *
129      */

130     public static Uri getRoot(Uri uri) {
131         return new Uri(uri.getToken(), uri.getNamespace(), uri.getScope().toString());
132     }
133     
134     
135     /**
136      * Scope accessor.
137      *
138      * @return StringTokenizer
139      */

140     public Scope getScope() {
141         return this.scope;
142     }
143     
144     
145     /**
146      * Returns the scopes tokenized by the ScopeTokenizer associated
147      * with this Uri object.
148      *
149      * @return Enumeration
150      */

151     public Enumeration JavaDoc getScopes() {
152         return this.scopes.elements();
153     }
154     
155     
156     /**
157      * Store accessor.
158      *
159      * @return Store
160      */

161     public Store getStore() {
162         return this.store;
163     }
164     
165     
166     /**
167      * Token accessor.
168      *
169      * @return The SlideToken, or null if no token has been set
170      * (which is valid)
171      */

172     public SlideToken getToken() {
173         return token;
174     }
175     
176     
177     /**
178      * Token mutator.
179      *
180      * @param token New Slide token
181      */

182     public void setToken(SlideToken token) {
183         this.token = token;
184     }
185     
186     
187     // --------------------------------------------------------- Public Methods
188

189     
190     /**
191      * Return the namespace to which this Uri belongs.
192      *
193      * @return Namespace
194      */

195     public Namespace getNamespace() {
196         return namespace;
197     }
198     
199     
200     /**
201      * Get the parent uri.
202      *
203      * @return Uri
204      */

205     public Uri getParentUri() {
206         Uri result = scopes.getParentUri();
207         if (result != null)
208             result.setToken(token);
209         return result;
210     }
211     
212     
213     /**
214      * Invalidate the current Services and PK. Used if there are changes in
215      * the namespace at runtime.
216      */

217     public void invalidateServices() {
218         store = null;
219         parseUri(this.uri);
220     }
221     
222     
223     /**
224      * Reconnect the Uri services.
225      */

226     public void reconnectServices() {
227         try {
228             if (token == null) {
229                 store.connectIfNeeded(null);
230             } else {
231                 store.connectIfNeeded(token.getCredentialsToken());
232             }
233         } catch (ServiceConnectionFailedException e) {
234             parseUri(this.uri);
235         } catch (ServiceAccessException e) {
236             parseUri(this.uri);
237         }
238     }
239     
240     
241     /**
242      * Get the relative path to the matched scope.
243      */

244     public String JavaDoc getRelative() {
245         return (uri.substring(scope.toString().length()));
246     }
247     
248     /**
249      * Test whether this Uri is equivalent to its scope
250      * @return a boolean
251      */

252     public boolean isStoreRoot() {
253         UriPath thisPath = new UriPath(uri);
254         UriPath scopePath = new UriPath(scope.toString());
255         return thisPath.equals(scopePath);
256     }
257     
258     // --------------------------------------------------------- Object Methods
259

260     
261     /**
262      * String representation of the uri object.
263      *
264      * @return String
265      */

266     public String JavaDoc toString() {
267         return uri;
268     }
269     
270     
271     /**
272      * Hash code.
273      *
274      * @return int hash code
275      */

276     public int hashCode() {
277         return this.uri.hashCode();
278     }
279     
280     
281     /**
282      * Tests equivalence of two Uris.
283      *
284      * @param obj Object to test
285      * @return boolean
286      */

287     public boolean equals(Object JavaDoc obj) {
288         if ((obj != null) && (obj instanceof Uri)) {
289             return (uri.equals(obj.toString()));
290         } else {
291             return false;
292         }
293     }
294     
295     
296     /**
297      * Do a fast clone of the Uri object.
298      */

299     public Uri cloneObject() {
300         Uri result = null;
301         try {
302             result = (Uri) super.clone();
303         } catch (CloneNotSupportedException JavaDoc e) {
304             e.printStackTrace();
305         }
306         return result;
307     }
308     
309     
310     /**
311      * Tests if the given uri represents a parent object.
312      *
313      * @param uri Uri to test
314      * @return boolean
315      */

316     public boolean isParent(Uri uri) {
317         return this.uri.startsWith(uri.toString());
318     }
319     
320     
321     // -------------------------------------------------------- Private Methods
322

323     
324     /**
325      * This function is called by the constructor and when uri is changed.
326      *
327      * @param uri Uri to parse
328      */

329     private void parseUri(String JavaDoc uri) {
330         // We first try to tokenize the uri string.
331

332         scopes = new ScopeTokenizer(token, namespace, uri);
333         
334         this.uri = scopes.getUri();
335         
336         // Find the qualifiying stuff from the registry.
337
// Then we contentStore the scope of the found Data Source
338
// within scope.
339
store = null;
340         while ((store == null) && (scopes.hasMoreElements())) {
341             Scope courScope = scopes.nextScope();
342             try {
343                 if (store == null) {
344                     if (token == null) {
345                         store = namespace.retrieveStore(courScope, null);
346                     } else {
347                         store = namespace.retrieveStore(courScope, token.getCredentialsToken());
348                     }
349                     
350                     if (store != null) {
351                         scope = courScope;
352                     }
353                 }
354             } catch (ServiceConnectionFailedException e) {
355                 // Problem ...
356
// FIXME : Throw a RuntimeException ??
357
} catch (ServiceAccessException e) {
358                 // Problem ...
359
// FIXME : Throw a RuntimeException ??
360
}
361         }
362         
363         // If descriptorsStore or contentStore is still null, then no valid
364
// scope is defined in the namespace ...
365
if (store == null) {
366             throw new ServiceMissingOnRootNodeException();
367         }
368         
369     }
370     
371     
372     // ---------------------------------------------------------- Serialization
373

374     
375     /**
376      * Read serialized object.
377      */

378     private void readObject(java.io.ObjectInputStream JavaDoc in)
379         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
380         String JavaDoc namespaceName = (String JavaDoc) in.readObject();
381         namespace = Domain.getNamespace(namespaceName);
382         parseUri((String JavaDoc) in.readObject());
383     }
384     
385     
386     /**
387      * Write serialized object.
388      */

389     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
390         throws IOException JavaDoc {
391         out.writeObject(namespace.getName());
392         out.writeObject(uri);
393     }
394     
395 }
396
Popular Tags