1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.uri;
14 import java.util.Arrays;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.Queries;
20 import org.simantics.db.exception.ResourceNotFoundException;
21 import org.simantics.db.procedure.AsyncProcedure;
22 import org.simantics.db.request.AsyncRead;
25 * TODO: should this be considered deprecated and favor {@link Queries#resource(String)} ?
27 public class URIToResource implements AsyncRead<Resource> {
28 String[] nameSequence;
30 public URIToResource(String[] nameSequence) {
31 this.nameSequence = nameSequence;
34 public URIToResource(String uri) {
35 this.nameSequence = URIEscape.splitURI(uri);
39 public void perform(AsyncReadGraph g, final AsyncProcedure<Resource> procedure) {
40 if(nameSequence.length == 0)
41 procedure.execute(g, g.getRootLibrary());
43 g.asyncRequest(new URIToResource(Arrays.copyOf(nameSequence, nameSequence.length-1)),
44 new AsyncProcedure<Resource>() {
47 public void execute(AsyncReadGraph g, Resource result) {
48 g.asyncRequest(new EscapedChildMapOfResource(result),
49 new AsyncProcedure<Map<String, Resource>>() {
52 public void execute(AsyncReadGraph g, Map<String, Resource> map) {
53 Resource r = map.get(nameSequence[nameSequence.length-1]);
55 procedure.execute(g, r);
57 procedure.exception(g, new ResourceNotFoundException(
58 URIEscape.joinURI(nameSequence)));
62 public void exception(AsyncReadGraph g, Throwable t) {
63 procedure.exception(g, t);
71 public void exception(AsyncReadGraph g, Throwable t) {
72 procedure.exception(g, t);
81 public int hashCode() {
82 return Arrays.hashCode(nameSequence);
86 public int threadHash() {
91 public boolean equals(Object obj) {
94 if(!(obj instanceof URIToResource))
96 return Arrays.equals(nameSequence, ((URIToResource)obj).nameSequence);
100 public int getFlags() {