KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > ResourceId


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/store/ResourceId.java,v 1.4.2.1 2004/09/22 13:38:33 luetzkendorf Exp $
3  * $Revision: 1.4.2.1 $
4  * $Date: 2004/09/22 13:38:33 $
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.store;
25
26 import java.util.Enumeration JavaDoc;
27 import org.apache.slide.common.Namespace;
28 import org.apache.slide.common.Scope;
29 import org.apache.slide.common.ScopeTokenizer;
30 import org.apache.slide.common.ServiceAccessException;
31 import org.apache.slide.common.ServiceConnectionFailedException;
32 import org.apache.slide.common.ServiceMissingOnRootNodeException;
33 import org.apache.slide.common.SlideToken;
34 import org.apache.slide.common.Uri;
35 import org.apache.slide.common.UriPath;
36 import org.apache.slide.content.NodeProperty;
37 import org.apache.slide.store.Store;
38 import org.apache.slide.util.Configuration;
39 import org.apache.slide.util.XMLValue;
40 import org.jdom.Element;
41
42 /**
43  * Uniquely identifies a resource. The main operation with ResourceIds is equals. Immutable.
44  *
45  * Technically, ResourceId extends Uri, but the only reason to do so is compatibility with
46  * existing store interfaces. Logically, a ResourceId is *not* an Uri: Uris have a structure,
47  * resourceId have not. Consequently, almost all Uri methods in this class throw
48  * UnsupportedOperation exceptions.
49  *
50  * @version $Revision: 1.4.2.1 $
51  */

52 public final class ResourceId extends Uri {
53     
54     public static String JavaDoc RESOURCE_ID_SCHEMA = "urn:uuid:";
55     
56     
57     /**
58      ** There are probably no two jvm's working on the same database that initialitzing this
59      ** class at the same mill second
60      **/

61     private static final String JavaDoc TIMESTAMP = String.valueOf(System.currentTimeMillis());
62     
63     /**
64      ** Number of uuri created in this JVM session
65      **/

66     private static int counter = 0;
67     
68     /**
69      * @pre uriStr has to contain store prefix, i.e. extractStoreUri has to succeed
70      */

71     public static ResourceId createNew(Uri uri) {
72         String JavaDoc newUuri;
73         boolean useBinding = Configuration.useBinding(uri.getStore());
74         
75         if (useBinding) {
76             String JavaDoc scopeSlash = uri.getScope().toString();
77             if (!"/".equals(scopeSlash)) {
78                 scopeSlash += "/";
79             }
80             synchronized (ResourceId.class) {
81                 newUuri = uri.isStoreRoot()
82                     ? scopeSlash
83                     : scopeSlash+TIMESTAMP+"."+counter++;
84             }
85         } else {
86             newUuri = uri.toString();
87         }
88         return new ResourceId(uri, newUuri);
89     }
90     
91     public static ResourceId create(Uri uri, String JavaDoc uuri) {
92         return new ResourceId(uri, uuri);
93     }
94     
95     private static String JavaDoc resourceIdSchema(Store store) {
96         if (Configuration.useBinding(store)) {
97             return RESOURCE_ID_SCHEMA;
98         }
99         else {
100             return "";
101         }
102     }
103     
104     private final String JavaDoc uuri;
105     
106     /**
107      * Constructor
108      *
109      * @param uri an Uri
110      * @param id /scope/identifier
111      *
112      */

113     private ResourceId(Uri uri, String JavaDoc uuri) {
114         super(uri.getToken(), uri.getNamespace(), uri.toString());
115         this.uuri = uuri;
116         parseUuri(uuri);
117     }
118     
119     /**
120      * Tests equivalence of two ResourceIds.
121      *
122      * @param obj Object to test
123      * @return boolean
124      */

125     public boolean equals(Object JavaDoc obj) {
126         ResourceId resourceId;
127         
128         if (obj instanceof ResourceId) {
129             resourceId = (ResourceId) obj;
130             return getNamespace() == resourceId.getNamespace() &&
131                 getStore() == resourceId.getStore() && uuri.equals(resourceId.uuri);
132         } else {
133             return false;
134         }
135     }
136     
137     public String JavaDoc toString() {
138         return uuri;
139     }
140     
141     /**
142      * Intentionally does not return anything like the origianl uri.
143      *
144      * @return String
145      */

146     public String JavaDoc getUuri() {
147         return uuri;
148     }
149     
150     /**
151      * Hash code.
152      *
153      * @return int hash code
154      */

155     public int hashCode() {
156         return this.uuri.hashCode();
157     }
158     
159     public Scope getScope() {
160         return super.getScope();
161     }
162     
163     public Store getStore() {
164         return super.getStore();
165     }
166     
167     public SlideToken getToken() {
168         return super.getToken();
169     }
170     
171     public Namespace getNamespace() {
172         return super.getNamespace();
173     }
174     
175     public boolean isStoreRoot() {
176         UriPath thisPath = new UriPath(uuri);
177         UriPath scopePath = new UriPath(scope.toString());
178         return thisPath.equals(scopePath);
179     }
180     
181     // -------------------------------------------------------------
182

183     public void setUri(String JavaDoc uri) {
184         throw new UnsupportedOperationException JavaDoc();
185     }
186     
187     public Enumeration JavaDoc getScopes() {
188         throw new UnsupportedOperationException JavaDoc();
189     }
190     
191     
192     public void setToken(SlideToken token) {
193         throw new UnsupportedOperationException JavaDoc();
194     }
195     
196     public Uri getParentUri() {
197         throw new UnsupportedOperationException JavaDoc();
198     }
199     
200     public void invalidateServices() {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203     
204     public void reconnectServices() {
205         throw new UnsupportedOperationException JavaDoc();
206     }
207     
208     public String JavaDoc getRelative() {
209         throw new UnsupportedOperationException JavaDoc();
210     }
211     
212     /**
213      * Do not clone ResourceId's, use aliasing.
214      */

215     public Uri cloneObject() {
216         throw new UnsupportedOperationException JavaDoc();
217     }
218     
219     public boolean isParent(Uri uri) {
220         throw new UnsupportedOperationException JavaDoc();
221     }
222     
223     public String JavaDoc asXml() {
224         XMLValue r = new XMLValue();
225         Element hrefElm = new Element("href", NodeProperty.NamespaceCache.DEFAULT_NAMESPACE);
226         hrefElm.setText(resourceIdSchema(getStore())+getUuri());
227         r.add(hrefElm);
228         return r.toString();
229     }
230     
231     /**
232      * This function is called by the constructor and when uri is changed.
233      *
234      * @param uri Uri to parse
235      */

236     private void parseUuri(String JavaDoc uuri) {
237         // We first try to tokenize the uri string.
238
scopes = new ScopeTokenizer(token, namespace, uuri);
239         
240         // Find the qualifiying stuff from the registry.
241
// Then we contentStore the scope of the found Data Source
242
// within scope.
243
store = null;
244         while ((store == null) && (scopes.hasMoreElements())) {
245             Scope courScope = scopes.nextScope();
246             try {
247                 if (token == null) {
248                     store = namespace.retrieveStore(courScope, null);
249                 } else {
250                     store = namespace.retrieveStore(courScope, token.getCredentialsToken());
251                 }
252                 
253                 if (store != null) {
254                     scope = courScope;
255                 }
256             }
257             catch (ServiceConnectionFailedException e) {
258                 // Problem ...
259
// FIXME : Throw a RuntimeException ??
260
}
261             catch (ServiceAccessException e) {
262                 // Problem ...
263
// FIXME : Throw a RuntimeException ??
264
}
265         }
266         
267         // If descriptorsStore or contentStore is still null, then no valid
268
// scope is defined in the namespace ...
269
if (store == null) {
270             throw new ServiceMissingOnRootNodeException();
271         }
272         
273     }
274 }
275
276
Popular Tags