1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.internal;
14 import java.util.Hashtable;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceRegistration;
18 import org.simantics.db.Disposable;
19 import org.simantics.db.management.ISessionContext;
20 import org.simantics.utils.datastructures.cache.SoftTimedCache;
21 import org.simantics.utils.datastructures.disposable.IDisposable;
23 * @author Tuukka Lehtonen
25 public class TimedSessionCache extends SoftTimedCache</*ServerAddress*/Object, ISessionContext> {
27 private static ServiceRegistration<?> service = null;
28 private static BundleContext bundleContext = null;
29 private static boolean disposed = false;
32 super("Database Session Cache Timer");
36 * Invoked by the bundle activator to initialize the cache service.
38 * @param context the bundle context to register the service with
40 @SuppressWarnings({ "unchecked", "rawtypes" })
41 public synchronized static void initialize(BundleContext context) {
42 bundleContext = context;
43 service = context.registerService(TimedSessionCache.class.getName(), new TimedSessionCache(), new Hashtable());
48 * Invoked by the bundle activator to close the cache service.
50 public synchronized static void close() {
51 if (service != null) {
52 TimedSessionCache cache = getCache();
62 public static synchronized TimedSessionCache getCache() {
64 throw new IllegalStateException("cache service is disposed");
66 throw new IllegalStateException("cache service not initialized");
67 return (TimedSessionCache) bundleContext.getService(service.getReference());
71 protected void disposeValue(ISessionContext v) {
72 super.disposeValue(v);
73 // Implementing Disposable allows immediate disposal of the value
74 // object instead of leaving the disposal up to garbage
75 // collection and finalization.
76 if (v instanceof IDisposable) {
77 ((IDisposable) v).safeDispose();
78 } else if (v instanceof Disposable) {
79 ((Disposable) v).dispose();