]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / 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.impl.query;
13
14 import org.simantics.databoard.util.URIStringUtils;
15 import org.simantics.db.ObjectResourceIdMap;
16 import org.simantics.db.common.exception.DebugException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.ResourceNotFoundException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21
22 public class URIToResource extends StringQuery<InternalProcedure<Integer>> implements InternalProcedure<Integer> {
23
24     URIToResource(final String id) {
25         super(id);
26     }
27
28     @Override
29     final public void removeEntry(QueryProcessor provider) {
30         provider.cache.remove(this);
31     }
32     
33     //@Override
34     public Object compute(ReadGraphImpl graph, final InternalProcedure<Integer> procedure) throws DatabaseException {
35         computeForEach(graph, id, this, procedure);
36         return getResult();
37     }
38
39     static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure_) throws DatabaseException {
40         
41         InternalProcedure<Integer> procedure = entry != null ? entry : procedure_;
42         
43         if("http://".equals(id) || "http:/".equals(id)) {
44             
45                 QueryProcessor processor = graph.processor;
46             procedure.execute(graph, processor.getRootLibrary());
47
48         } else {
49             
50             final String[] parts = URIStringUtils.splitURI(id);
51             if (parts != null) {
52
53                 QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure<Integer>() {
54
55                     @Override
56                     public void execute(ReadGraphImpl graph, Integer parentId) throws DatabaseException {
57                         
58                         ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
59                         assert(map != null);
60                         int result = map.getId(URIStringUtils.unescape(parts[1]));
61                         if (result == 0) {
62                             ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
63                             procedure.exception(graph, e);
64                         } else {
65                             procedure.execute(graph, result);
66                         }
67                     }
68
69                     @Override
70                     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
71                         procedure.exception(graph, throwable);
72                     }
73                     
74                 });
75                 
76                         
77             } else {
78                 ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
79                 procedure.exception(graph, e);
80             }
81
82         }
83         
84         if(entry != null) entry.performFromCache(graph, procedure_);
85         
86     }
87     
88     public void addOrSet(Integer result) {
89
90         assert(isPending());
91
92         synchronized(this) {
93             setResult(result);
94             setReady();
95         }
96         
97     }
98     
99     @Override
100     public String toString() {
101         return "URIToResource[" + id + "]";
102     }
103
104     @Override
105     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
106         
107         assert(isReady());
108         
109         if(handleException(graph, procedure)) return (Throwable)getResult();
110
111         Integer result = (Integer)getResult();
112         procedure.execute(graph, result);
113         return result;
114         
115     }
116     
117     @Override
118     public void recompute(ReadGraphImpl graph) throws DatabaseException {
119         
120         compute(graph, new InternalProcedure<Integer>() {
121
122             @Override
123             public void execute(ReadGraphImpl graph, Integer result) {
124             }
125             
126             @Override
127             public void exception(ReadGraphImpl graph, Throwable t) {
128                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
129                 throw new Error("Error in recompute.", t);
130             }
131
132         });
133         
134     }
135
136     @Override
137     public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException {
138         synchronized(this) {
139             setResult(result);
140             setReady();
141         }
142     }
143
144     @Override
145     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
146         except(throwable);
147     }
148         
149 }