]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/internal/TimedSessionCache.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / internal / TimedSessionCache.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.internal;\r
13 \r
14 import java.util.Hashtable;\r
15 \r
16 import org.osgi.framework.BundleContext;\r
17 import org.osgi.framework.ServiceRegistration;\r
18 import org.simantics.db.Disposable;\r
19 import org.simantics.db.management.ISessionContext;\r
20 import org.simantics.utils.datastructures.cache.SoftTimedCache;\r
21 import org.simantics.utils.datastructures.disposable.IDisposable;\r
22 /**\r
23  * @author Tuukka Lehtonen\r
24  */\r
25 public class TimedSessionCache extends SoftTimedCache</*ServerAddress*/Object, ISessionContext> {\r
26 \r
27     private static ServiceRegistration<?> service       = null;\r
28     private static BundleContext          bundleContext = null;\r
29     private static boolean                disposed      = false;\r
30 \r
31     TimedSessionCache() {\r
32         super("Database Session Cache Timer");\r
33     }\r
34 \r
35     /**\r
36      * Invoked by the bundle activator to initialize the cache service.\r
37      * \r
38      * @param context the bundle context to register the service with\r
39      */\r
40     @SuppressWarnings({ "unchecked", "rawtypes" })\r
41     public synchronized static void initialize(BundleContext context) {\r
42         bundleContext = context;\r
43         service = context.registerService(TimedSessionCache.class.getName(), new TimedSessionCache(), new Hashtable());\r
44         disposed = false;\r
45     }\r
46 \r
47     /**\r
48      * Invoked by the bundle activator to close the cache service.\r
49      */\r
50     public synchronized static void close() {\r
51         if (service != null) {\r
52             TimedSessionCache cache = getCache();\r
53             cache.clear();\r
54             \r
55             service.unregister();\r
56             service = null;\r
57             disposed = true;\r
58         }\r
59         bundleContext = null;\r
60     }\r
61 \r
62     public static synchronized TimedSessionCache getCache() {\r
63         if (disposed)\r
64             throw new IllegalStateException("cache service is disposed");\r
65         if (service == null)\r
66             throw new IllegalStateException("cache service not initialized");\r
67         return (TimedSessionCache) bundleContext.getService(service.getReference());\r
68     }\r
69 \r
70     @Override\r
71     protected void disposeValue(ISessionContext v) {\r
72         super.disposeValue(v);\r
73         // Implementing Disposable allows immediate disposal of the value\r
74         // object instead of leaving the disposal up to garbage\r
75         // collection and finalization.\r
76         if (v instanceof IDisposable) {\r
77             ((IDisposable) v).safeDispose();\r
78         } else if (v instanceof Disposable) {\r
79             ((Disposable) v).dispose();\r
80         }\r
81     }\r
82 \r
83 }\r