]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInputs.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceInputs.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.ui.workbench;
13
14 import java.lang.ref.Reference;
15 import java.lang.ref.SoftReference;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20
21 import org.simantics.Simantics;
22 import org.simantics.db.RequestProcessor;
23 import org.simantics.db.Resource;
24 import org.simantics.db.Session;
25 import org.simantics.db.common.ResourceArray;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.exception.RuntimeDatabaseException;
28 import org.simantics.db.service.LifecycleSupport;
29 import org.simantics.db.service.SerialisationSupport;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 final class ResourceInputs {
35
36     public static <T> Reference<T> makeReference(T t) {
37         return new SoftReference<T>(t);
38     }
39
40     public static Resource resolveResource(RequestProcessor processor, String resourceId) throws DatabaseException {
41         try {
42             SerialisationSupport ss = processor.getService(SerialisationSupport.class);
43             return ss.getResource(Long.parseLong(resourceId));
44         } catch (NumberFormatException e) {
45             throw new DatabaseException(e);
46         }
47     }
48
49     public static ResourceArray makeResourceArray(RequestProcessor processor, Collection<String> resourceIds) throws DatabaseException {
50         try {
51             SerialisationSupport ss = processor.getService(SerialisationSupport.class);
52             List<Resource> rs = new ArrayList<Resource>();
53             for (String id : resourceIds)
54                 rs.add(ss.getResource(Long.parseLong(id)));
55             return new ResourceArray(rs);
56         } catch (NumberFormatException e) {
57             throw new DatabaseException(e);
58         }
59     }
60
61     public static String getRandomAccessId(Resource r) {
62         try {
63             SerialisationSupport support = getSession().getService(SerialisationSupport.class);
64             return String.valueOf( support.getRandomAccessId(r) );
65         } catch (DatabaseException e) {
66             throw new RuntimeDatabaseException(e);
67         }
68     }
69
70     public static List<String> getRandomAccessIds(ResourceArray ra) {
71         try {
72             List<String> randoms = new ArrayList<String>();
73             SerialisationSupport support = getSession().getService(SerialisationSupport.class);
74             for (Resource r : ra.resources)
75                 randoms.add(String.valueOf(support.getRandomAccessId(r)));
76             return Collections.unmodifiableList(randoms);
77         } catch (DatabaseException e) {
78             throw new RuntimeDatabaseException(e);
79         }
80     }
81
82     /**
83      * @return a graph instance if it exists and has not yet been disposed,
84      *         <code>null</code> otherwise
85      */
86     public static Session getSession() {
87         Session s = Simantics.getSession();
88         if (s.getService(LifecycleSupport.class).isClosed())
89             throw new IllegalStateException("database session is closed");
90         return s;
91     }
92
93     /**
94      * @return a graph instance if it exists and has not yet been disposed,
95      *         <code>null</code> otherwise
96      */
97     public static Session peekSession() {
98         Session s = Simantics.peekSession();
99         if (s == null)
100             return null;
101         if (s.getService(LifecycleSupport.class).isClosed())
102             throw new IllegalStateException("database session is closed");
103         return s;
104     }
105
106 }