\r
/**\r
* Manages system dynamic models.\r
- * @author Hannu Niemistö\r
+ * @author Hannu Niemist�\r
*/\r
public class SysdynModelManager {\r
WeakHashMap<Resource, SysdynModel> models =\r
- new WeakHashMap<Resource, SysdynModel>();\r
+ new WeakHashMap<Resource, SysdynModel>(); // FIXME: Resources are weak, there is no guarantee that model exists in this map as long as needed.\r
+ // HashTable with a disposal procedure could be better solution..\r
Session session;\r
\r
public SysdynModelManager(Session session) {\r
* Use only if not inside a transaction\r
*/\r
public SysdynModel getModel(final Resource resource) {\r
- SysdynModel model = models.get(resource);\r
- if(model == null) {\r
- try {\r
- session.syncRequest(new ReadRequest() {\r
- \r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- models.put(resource, new SysdynModel(graph, resource));\r
- \r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
-\r
- \r
- model = models.get(resource);\r
- }\r
- return model;\r
+ synchronized(models) {\r
+ SysdynModel model = models.get(resource);\r
+ if(model == null) {\r
+ try {\r
+ session.syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ models.put(resource, new SysdynModel(graph, resource));\r
+ \r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ \r
+ model = models.get(resource);\r
+ }\r
+ return model;\r
+ }\r
}\r
\r
/**\r
* Should be used if called inside a transaction\r
*/\r
public SysdynModel getModel(ReadGraph g, Resource resource) {\r
- SysdynModel model = models.get(resource);\r
- if(model == null) {\r
- model = new SysdynModel(g, resource);\r
- models.put(resource, model);\r
- }\r
- return model;\r
+ synchronized(models) {\r
+ SysdynModel model = models.get(resource);\r
+ if(model == null) {\r
+ model = new SysdynModel(g, resource);\r
+ models.put(resource, model);\r
+ }\r
+ return model;\r
+ }\r
}\r
\r
- public static SysdynModelManager getInstance(Session session) {\r
+ public synchronized static SysdynModelManager getInstance(Session session) {\r
SysdynModelManager manager =\r
session.peekService(SysdynModelManager.class);\r
if(manager == null) {\r