]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/uri/URIToResource.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / uri / URIToResource.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.uri;
13
14 import java.util.Arrays;
15 import java.util.Map;
16
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;
23
24 /**
25  * TODO: should this be considered deprecated and favor {@link Queries#resource(String)} ?
26  */
27 public class URIToResource implements AsyncRead<Resource> {
28         String[] nameSequence;
29         
30         public URIToResource(String[] nameSequence) {
31                 this.nameSequence = nameSequence;
32         }
33         
34         public URIToResource(String uri) {
35                 this.nameSequence = URIEscape.splitURI(uri);
36         }
37
38         @Override
39         public void perform(AsyncReadGraph g, final AsyncProcedure<Resource> procedure) {
40                 if(nameSequence.length == 0)
41                         procedure.execute(g, g.getRootLibrary());
42                 else
43                         g.asyncRequest(new URIToResource(Arrays.copyOf(nameSequence, nameSequence.length-1)),
44                                 new AsyncProcedure<Resource>() {
45
46                                         @Override
47                                         public void execute(AsyncReadGraph g, Resource result) {
48                                                 g.asyncRequest(new EscapedChildMapOfResource(result),
49                                                         new AsyncProcedure<Map<String, Resource>>() {
50
51                                                                 @Override
52                                                                 public void execute(AsyncReadGraph g, Map<String, Resource> map) {
53                                                                         Resource r = map.get(nameSequence[nameSequence.length-1]);
54                                                                         if(r != null)
55                                                                                 procedure.execute(g, r);
56                                                                         else
57                                                                                 procedure.exception(g, new ResourceNotFoundException(
58                                                                                                 URIEscape.joinURI(nameSequence)));
59                                                                 }
60                                                         
61                                                                 @Override
62                                                                 public void exception(AsyncReadGraph g, Throwable t) {
63                                                                         procedure.exception(g, t);                                                                                                                      
64                                                                 }
65                                                         
66                                                         }
67                                                 );
68                                         }
69                                 
70                                         @Override
71                                         public void exception(AsyncReadGraph g, Throwable t) {
72                                                 procedure.exception(g, t);                                              
73                                         }
74                                 
75                                 }
76                         );
77                 
78         }
79         
80         @Override
81         public int hashCode() {
82                 return Arrays.hashCode(nameSequence);
83         }
84
85     @Override
86     public int threadHash() {
87         return hashCode();
88     }
89
90         @Override
91         public boolean equals(Object obj) {
92                 if(this == obj)
93                         return true;
94                 if(!(obj instanceof URIToResource))
95                         return false;
96                 return Arrays.equals(nameSequence, ((URIToResource)obj).nameSequence);
97         }
98     
99     @Override
100     public int getFlags() {
101         return 0;
102     }
103
104 }