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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.internal;
\r
14 import java.util.Hashtable;
\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
23 * @author Tuukka Lehtonen
\r
25 public class TimedSessionCache extends SoftTimedCache</*ServerAddress*/Object, ISessionContext> {
\r
27 private static ServiceRegistration<?> service = null;
\r
28 private static BundleContext bundleContext = null;
\r
29 private static boolean disposed = false;
\r
31 TimedSessionCache() {
\r
32 super("Database Session Cache Timer");
\r
36 * Invoked by the bundle activator to initialize the cache service.
\r
38 * @param context the bundle context to register the service with
\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
48 * Invoked by the bundle activator to close the cache service.
\r
50 public synchronized static void close() {
\r
51 if (service != null) {
\r
52 TimedSessionCache cache = getCache();
\r
55 service.unregister();
\r
59 bundleContext = null;
\r
62 public static synchronized TimedSessionCache getCache() {
\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
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