Merge commit 'ffdf837'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / BuiltinSupportImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import gnu.trove.map.hash.THashMap;\r
4 \r
5 import java.lang.ref.SoftReference;\r
6 import java.util.HashSet;\r
7 \r
8 import org.simantics.db.common.utils.Logger;\r
9 import org.simantics.db.exception.DatabaseException;\r
10 import org.simantics.db.exception.ValidationException;\r
11 import org.simantics.db.impl.ClusterBase;\r
12 import org.simantics.db.impl.ClusterSupport;\r
13 import org.simantics.db.impl.ResourceImpl;\r
14 import org.simantics.db.impl.support.BuiltinSupport;\r
15 import org.simantics.db.procore.cluster.ClusterTraits;\r
16 import org.simantics.db.service.ClusterUID;\r
17 import org.simantics.db.service.InitSupport;\r
18 import org.simantics.graph.db.CoreInitialization;\r
19 \r
20 public class BuiltinSupportImpl implements BuiltinSupport {\r
21     private final boolean DEBUG = false;\r
22     private final SessionImplSocket session;\r
23     private ClusterBase builtinCluster;\r
24     private THashMap<String, BuiltinData> builtinMap;\r
25     BuiltinSupportImpl(SessionImplSocket session) {\r
26         this.session = session;\r
27         this.builtinMap = null;\r
28         this.builtinCluster = null;\r
29     }\r
30     @Override\r
31     public int getBuiltin(String uri) {\r
32         try {\r
33             ResourceImpl impl = getBuiltinResource(uri);\r
34             if (impl != null)\r
35                 return impl.id;\r
36         } catch (DatabaseException e) {\r
37             if (DEBUG)\r
38                 e.printStackTrace();\r
39             Logger.defaultLogError("Failed to get builtin for " + uri, e);\r
40         }\r
41         return 0;\r
42     }\r
43     private ResourceImpl getBuiltinResource(String name)\r
44     throws DatabaseException {\r
45         Logger.defaultLogTrace("Asking for builtin " + name);\r
46         synchronized (this) {\r
47             if (null == builtinMap) {\r
48                 ClusterSupport cs = session.getService(ClusterSupport.class);\r
49                 builtinCluster = cs.getClusterByClusterUIDOrMake(ClusterUID.Builtin);\r
50                 builtinMap = initBuiltinMap();\r
51             }\r
52         }\r
53         ResourceImpl resource = null;\r
54         BuiltinData builtin = null;\r
55         synchronized (this) {\r
56             builtin = builtinMap.get(name);\r
57             if (null == builtin) {\r
58                 Logger.defaultLogInfo("Asking for missing builtin " + name);\r
59                 return null;\r
60             }\r
61             if (null != builtin.weakResource) { \r
62                 resource = builtin.weakResource.get();\r
63                 if (resource != null)\r
64                     return resource;\r
65             }\r
66         }\r
67         int id = ClusterTraits.createResourceKey(builtinCluster.getClusterKey(), builtin.index);\r
68         resource = session.getResource(id);\r
69         if (null == resource)\r
70             throw new ValidationException("Builtin not found index=" + builtin.index);\r
71         builtin.weakResource = new SoftReference<ResourceImpl>(resource); \r
72         return resource;\r
73     }\r
74     private THashMap<String, BuiltinData> initBuiltinMap()\r
75             throws DatabaseException {\r
76         THashMap<String, BuiltinData> builtins = new THashMap<String, BuiltinData>();\r
77         HashSet<InitSupport.Builtin> t = CoreInitialization.getBuiltins();\r
78         int size = t.size();\r
79         for (InitSupport.Builtin b : t) {\r
80             if (b.index < 1 || b.index > size)\r
81                 throw new ValidationException("Illegal builtin resource index=" + b.index);\r
82             BuiltinData bd = new BuiltinData(b.index);\r
83             builtins.put(b.uri, bd);\r
84         }\r
85         return builtins;\r
86     }\r
87     static class BuiltinData {\r
88         final int index; // resource index\r
89         SoftReference<ResourceImpl> weakResource = null;\r
90         BuiltinData(int index) {\r
91             this.index = index;\r
92         }\r
93     }\r
94 }\r