]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/internal/TimedSessionCache.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / internal / TimedSessionCache.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.internal;
13
14 import java.util.Hashtable;
15
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;
22 /**
23  * @author Tuukka Lehtonen
24  */
25 public class TimedSessionCache extends SoftTimedCache</*ServerAddress*/Object, ISessionContext> {
26
27     private static ServiceRegistration<?> service       = null;
28     private static BundleContext          bundleContext = null;
29     private static boolean                disposed      = false;
30
31     TimedSessionCache() {
32         super("Database Session Cache Timer");
33     }
34
35     /**
36      * Invoked by the bundle activator to initialize the cache service.
37      * 
38      * @param context the bundle context to register the service with
39      */
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());
44         disposed = false;
45     }
46
47     /**
48      * Invoked by the bundle activator to close the cache service.
49      */
50     public synchronized static void close() {
51         if (service != null) {
52             TimedSessionCache cache = getCache();
53             cache.clear();
54             
55             service.unregister();
56             service = null;
57             disposed = true;
58         }
59         bundleContext = null;
60     }
61
62     public static synchronized TimedSessionCache getCache() {
63         if (disposed)
64             throw new IllegalStateException("cache service is disposed");
65         if (service == null)
66             throw new IllegalStateException("cache service not initialized");
67         return (TimedSessionCache) bundleContext.getService(service.getReference());
68     }
69
70     @Override
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();
80         }
81     }
82
83 }