1 23 24 package org.apache.slide.store; 25 26 import java.util.Enumeration ; 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 52 public final class ResourceId extends Uri { 53 54 public static String RESOURCE_ID_SCHEMA = "urn:uuid:"; 55 56 57 61 private static final String TIMESTAMP = String.valueOf(System.currentTimeMillis()); 62 63 66 private static int counter = 0; 67 68 71 public static ResourceId createNew(Uri uri) { 72 String newUuri; 73 boolean useBinding = Configuration.useBinding(uri.getStore()); 74 75 if (useBinding) { 76 String 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 uuri) { 92 return new ResourceId(uri, uuri); 93 } 94 95 private static String 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 uuri; 105 106 113 private ResourceId(Uri uri, String uuri) { 114 super(uri.getToken(), uri.getNamespace(), uri.toString()); 115 this.uuri = uuri; 116 parseUuri(uuri); 117 } 118 119 125 public boolean equals(Object 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 toString() { 138 return uuri; 139 } 140 141 146 public String getUuri() { 147 return uuri; 148 } 149 150 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 183 public void setUri(String uri) { 184 throw new UnsupportedOperationException (); 185 } 186 187 public Enumeration getScopes() { 188 throw new UnsupportedOperationException (); 189 } 190 191 192 public void setToken(SlideToken token) { 193 throw new UnsupportedOperationException (); 194 } 195 196 public Uri getParentUri() { 197 throw new UnsupportedOperationException (); 198 } 199 200 public void invalidateServices() { 201 throw new UnsupportedOperationException (); 202 } 203 204 public void reconnectServices() { 205 throw new UnsupportedOperationException (); 206 } 207 208 public String getRelative() { 209 throw new UnsupportedOperationException (); 210 } 211 212 215 public Uri cloneObject() { 216 throw new UnsupportedOperationException (); 217 } 218 219 public boolean isParent(Uri uri) { 220 throw new UnsupportedOperationException (); 221 } 222 223 public String 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 236 private void parseUuri(String uuri) { 237 scopes = new ScopeTokenizer(token, namespace, uuri); 239 240 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 } 261 catch (ServiceAccessException e) { 262 } 265 } 266 267 if (store == null) { 270 throw new ServiceMissingOnRootNodeException(); 271 } 272 273 } 274 } 275 276 | Popular Tags |