KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > model > ConfigCacheImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.spi.persistence.support.sqlstore.model;
26
27 import com.sun.jdo.spi.persistence.support.sqlstore.ConfigCache;
28 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceConfig;
29 import com.sun.jdo.spi.persistence.support.sqlstore.VersionConsistencyCache;
30 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.ApplicationLifeCycleEventListener;
31 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper;
32 import com.sun.jdo.spi.persistence.support.sqlstore.query.util.type.TypeTable;
33 import com.sun.jdo.api.persistence.model.Model;
34
35 import java.util.*;
36
37 /**
38  * Caches SQLStore config information.
39  *
40  * @author Mitesh Meswani
41  */

42 public class ConfigCacheImpl
43         implements ConfigCache, ApplicationLifeCycleEventListener {
44
45     /**
46      * Map of class types to PersistenceConfig.
47      */

48     private Map classConfigs;
49
50     /**
51      * Map of OID classes to PersistenceCapable classes.
52      */

53     private Map oidClassToClassType;
54
55     /**
56      * This cache should be notified every time a VC class
57      * is loaded or unloaded.
58      */

59     private VersionConsistencyCache vcCache;
60
61     /**
62      * Map of class loaders to a list of PersistenceCapable
63      * classes loaded by the classloader.
64      */

65     private Map classLoaderToClassType;
66
67     public ConfigCacheImpl() {
68         classConfigs = new HashMap();
69         classLoaderToClassType = new HashMap();
70         oidClassToClassType = new HashMap();
71
72         // Register for call backs on application loader events.
73
EJBHelper.registerApplicationLifeCycleEventListener(this);
74     }
75
76     /**
77      * Get the PersistenceConfig for given pcClass. The config is looked up
78      * from a cache. If a config can not be found in cache, a new
79      * instance is created and returned.
80      *
81      * @param pcClass The input pcClass.
82      * @return PersistenceConfig for given pcClass.
83      */

84     public synchronized PersistenceConfig getPersistenceConfig(Class JavaDoc pcClass) {
85         ClassDesc sqlConfig =
86                 (ClassDesc) classConfigs.get(pcClass);
87         if (sqlConfig == null) {
88             // The order of the below operations is important.
89
// Initialize is called after puting sqlConfig into the
90
// cache so that ClassDesc#fixupForeignReferences does not
91
// cause infinite recursion.
92
// After the initialisation, oidClassToClassType.put is
93
// called so that sqlConfig.getOidClass() returns correct
94
// value.
95

96             sqlConfig = ClassDesc.newInstance(pcClass);
97             classConfigs.put(pcClass, sqlConfig);
98             sqlConfig.initialize(this);
99             oidClassToClassType.put(sqlConfig.getOidClass(), pcClass);
100             addToClassLoaderMap(pcClass);
101         }
102         return sqlConfig;
103     }
104
105     /**
106      * Gets the Class instance corresponding to given oidType.
107      *
108      * @param oidType The input oidType.
109      * @return The Class instance corresponding to given oidType.
110      */

111     public Class JavaDoc getClassByOidClass(Class JavaDoc oidType) {
112         return (Class JavaDoc) oidClassToClassType.get(oidType);
113     }
114
115     /**
116      * @inheritDoc
117      */

118     public void notifyApplicationUnloaded(ClassLoader JavaDoc classLoader) {
119         // Clean up classConfigs and oidClassToClassType for the given
120
// classLoader.
121
synchronized (this) {
122             List pcClasses = (List) classLoaderToClassType.get(classLoader);
123             if (pcClasses != null) {
124                 Iterator it = pcClasses.iterator();
125                 while (it.hasNext()) {
126                     Class JavaDoc classType = (Class JavaDoc) it.next();
127                     ClassDesc config =
128                             (ClassDesc) classConfigs.remove(classType);
129                     Class JavaDoc oidClass = config.getOidClass();
130                     oidClassToClassType.remove(oidClass);
131                     if (config.hasVersionConsistency() && vcCache != null) {
132                         vcCache.removePCType(classType);
133                     }
134                 }
135                 // Data about this classLoader is no longer needed.
136
// Remove it from cache.
137
classLoaderToClassType.remove(classLoader);
138             }
139         }
140
141         // Notify others to cleanup
142
TypeTable.removeInstance(classLoader);
143         Model model = Model.RUNTIME;
144         model.removeResourcesFromCaches(classLoader);
145
146     }
147
148     /**
149      * Sets VersionConsistencyCache field.
150      *
151      * @param vcCache the VersionConsistencyCache instance.
152      */

153     public synchronized void setVersionConsistencyCache(
154             VersionConsistencyCache vcCache) {
155         this.vcCache = vcCache;
156     }
157
158     /**
159      * Add pcClass to a classLoaderToClassType map. The only call to
160      * this private method is from getPersistenceConfig(). As that method is
161      * synchronized we don't need to synchronize here.
162      *
163      * @param pcClass The pcClass to be added.
164      */

165     private void addToClassLoaderMap(Class JavaDoc pcClass) {
166         ClassLoader JavaDoc classLoader = pcClass.getClassLoader();
167         List classes = (List) classLoaderToClassType.get(classLoader);
168         if (classes == null) {
169             // First entry for a given ClassLoader, initialize the ArrayList.
170
classes = new ArrayList();
171             classLoaderToClassType.put(classLoader, classes);
172         }
173         classes.add(pcClass);
174     }
175 }
176
Popular Tags