--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.internal;\r
+\r
+import java.util.Hashtable;\r
+\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.ServiceRegistration;\r
+import org.simantics.db.Disposable;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.utils.datastructures.cache.SoftTimedCache;\r
+import org.simantics.utils.datastructures.disposable.IDisposable;\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TimedSessionCache extends SoftTimedCache</*ServerAddress*/Object, ISessionContext> {\r
+\r
+ private static ServiceRegistration<?> service = null;\r
+ private static BundleContext bundleContext = null;\r
+ private static boolean disposed = false;\r
+\r
+ TimedSessionCache() {\r
+ super("Database Session Cache Timer");\r
+ }\r
+\r
+ /**\r
+ * Invoked by the bundle activator to initialize the cache service.\r
+ * \r
+ * @param context the bundle context to register the service with\r
+ */\r
+ @SuppressWarnings({ "unchecked", "rawtypes" })\r
+ public synchronized static void initialize(BundleContext context) {\r
+ bundleContext = context;\r
+ service = context.registerService(TimedSessionCache.class.getName(), new TimedSessionCache(), new Hashtable());\r
+ disposed = false;\r
+ }\r
+\r
+ /**\r
+ * Invoked by the bundle activator to close the cache service.\r
+ */\r
+ public synchronized static void close() {\r
+ if (service != null) {\r
+ TimedSessionCache cache = getCache();\r
+ cache.clear();\r
+ \r
+ service.unregister();\r
+ service = null;\r
+ disposed = true;\r
+ }\r
+ bundleContext = null;\r
+ }\r
+\r
+ public static synchronized TimedSessionCache getCache() {\r
+ if (disposed)\r
+ throw new IllegalStateException("cache service is disposed");\r
+ if (service == null)\r
+ throw new IllegalStateException("cache service not initialized");\r
+ return (TimedSessionCache) bundleContext.getService(service.getReference());\r
+ }\r
+\r
+ @Override\r
+ protected void disposeValue(ISessionContext v) {\r
+ super.disposeValue(v);\r
+ // Implementing Disposable allows immediate disposal of the value\r
+ // object instead of leaving the disposal up to garbage\r
+ // collection and finalization.\r
+ if (v instanceof IDisposable) {\r
+ ((IDisposable) v).safeDispose();\r
+ } else if (v instanceof Disposable) {\r
+ ((Disposable) v).dispose();\r
+ }\r
+ }\r
+\r
+}\r