KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > facility > naming > polymorphid > PolymorphIdMgrImpl


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.facility.naming.polymorphid;
25
26 import org.objectweb.jorm.api.*;
27 import org.objectweb.jorm.facility.naming.generator.LongGen;
28 import org.objectweb.jorm.facility.naming.generator.LongGenMgr;
29 import org.objectweb.jorm.facility.naming.generator.LongGenMgrRegistry;
30 import org.objectweb.jorm.facility.naming.basidir.BasidBinder;
31 import org.objectweb.jorm.naming.api.PBinder;
32 import org.objectweb.jorm.naming.api.PNamingContext;
33 import org.objectweb.jorm.naming.api.PBinder;
34
35 import org.objectweb.util.monolog.api.Logger;
36 import org.objectweb.util.monolog.api.BasicLevel;
37
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * @author P. Dechamboux
43  */

44 public class PolymorphIdMgrImpl implements PolymorphIdMgr {
45     private static final String JavaDoc POLYMORPHIDBINDERCLASS
46             = "org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinderInfo";
47     // Static binding to LongGenIncrMgr: TO BE REMOVED
48
private final static String JavaDoc LONGGENMGRCLASS
49             = "org.objectweb.jorm.facility.naming.generator.LongGenIncrMapping";
50     private HashMap JavaDoc cn2binders = new HashMap JavaDoc();
51     private HashMap JavaDoc id2binders = new HashMap JavaDoc();
52     private HashMap JavaDoc refNC = new HashMap JavaDoc();
53     private LongGenMgr lgManager = null;
54     private LongGen binderLG;
55
56     protected Logger logger = null;
57
58     public void setLogger(Logger l) {
59         logger = l;
60     }
61
62     // IMPLEMENTATION OF METHODS FROM THE PolymorphIdMgr INTERFACE
63

64     /**
65      * Notifies the manager that a PType has been associated with this binder.
66      */

67     public void binderTypeDef(PolymorphIdBinderInfo b) {
68         if (Debug.ON && logger != null) {
69             if (b.getPType() == null)
70                 logger.log(BasicLevel.DEBUG, b.getPType().getJormName());
71             else
72                 logger.log(BasicLevel.DEBUG, "Null type of the binder: " + b);
73         }
74         Iterator JavaDoc it = refNC.values().iterator();
75         while (it.hasNext()) {
76             PolymorphRefNC p = (PolymorphRefNC) it.next();
77             if (p.getPType() == null)
78                 continue;
79             if (Debug.ON && logger != null)
80                 logger.log(BasicLevel.DEBUG, "compare to " + p.getPType().getJormName());
81             if (b.getPType().isa(p.getPType())) {
82                 p.addValidBinder(b);
83                 if (Debug.ON && logger != null) logger.log(BasicLevel.DEBUG, "added");
84             }
85         }
86     }
87
88     /**
89      * Retrieves the LongGenMgr associated with this PolymorphIdMgr.
90      */

91     public LongGenMgr getLongGenMgr() {
92         return lgManager;
93     }
94
95     /**
96      * Retrieves a PBinder that manages polymorphic identifiers for the class
97      * with the given name. If it does not exist, creates it.
98      * @param cn The class name.
99      */

100     public PBinder getPBinder(String JavaDoc cn) throws PException {
101         return getPBinder(cn, null);
102     }
103
104     /**
105      * Retrieves a PBinder that manages polymorphic identifiers for the class
106      * with the given name. If it does not exist, creates it.
107      * @param cn The class name.
108      */

109     public PBinder getPBinder(String JavaDoc cn, Object JavaDoc conn) throws PException {
110         PolymorphIdBinderInfo res = (PolymorphIdBinderInfo) cn2binders.get(cn);
111         if (res == null) {
112             try {
113                 res = (PolymorphIdBinderInfo) Class.forName(POLYMORPHIDBINDERCLASS)
114                         .newInstance();
115             } catch (Exception JavaDoc e) {
116                 throw new PException(e, "Cannot allocate the Binder (probably a ClassLoader problem).");
117             }
118             if (conn == null)
119                 res.init(lgManager.getPMapper(), cn, this);
120             else
121                 res.init(lgManager.getPMapper(), cn, this, conn);
122             cn2binders.put(res.className, res);
123             id2binders.put(res.classId, res);
124         }
125         return res;
126     }
127
128     /**
129      * Retrieves a PBinder that manages polymorphic identifiers for the class
130      * with the given identifier. If it does not exist, creates it.
131      * @param id The class identifier.
132      */

133     public PBinder getPBinder(long id) throws PException {
134         return getPBinder(id, null);
135     }
136
137     /**
138      * Retrieves a PBinder that manages polymorphic identifiers for the class
139      * with the given identifier. If it does not exist, creates it.
140      * @param id The class name.
141      * @param conn The connection to access the data store.
142      */

143     public PBinder getPBinder(long id, Object JavaDoc conn) throws PException {
144         PolymorphIdBinderInfo res = (PolymorphIdBinderInfo) id2binders.get(new Long JavaDoc(id));
145         if (res == null) {
146             try {
147                 res = (PolymorphIdBinderInfo) Class.forName(POLYMORPHIDBINDERCLASS)
148                         .newInstance();
149             } catch (Exception JavaDoc e) {
150                 throw new PException(e,
151                                      "Cannot allocate the Binder (probably a ClassLoader problem).");
152             }
153             res.init(lgManager.getPMapper(), id, this, conn);
154             cn2binders.put(res.className, res);
155             id2binders.put(res.classId, res);
156         }
157         return res;
158     }
159
160     /**
161      * Retrieves the mapper associated with this PolymorphIdMgr.
162      */

163     public PMapper getPMapper() {
164         return lgManager.getPMapper();
165     }
166
167     /**
168      * Retrieves a PNamingContext that manages polymorphic references for the
169      * class with the given name. This means that such references can point to
170      * object of this class or any of its sub-classes. If it does not exist,
171      * creates it.
172      * @param cn The class name.
173      */

174     public PNamingContext getRefNC(String JavaDoc cn) {
175         PolymorphRefNC res = (PolymorphRefNC) refNC.get(cn);
176         if (res == null) {
177             res = new PolymorphRefNC();
178             res.init(this);
179             refNC.put(cn, res);
180         }
181         return res;
182     }
183
184     /**
185      * Initializes a polymorphic identifier manager by mapping relevant classes
186      * to the given mapper, and setting up relevant initial persistent
187      * information.
188      */

189     public void init(PMapper pm, byte clact) throws PException {
190         synchronized (LongGenMgrRegistry.class) {
191             lgManager = LongGenMgrRegistry.getLongGenMgr(pm);
192             if (lgManager == null) {
193                 String JavaDoc cn = pm.cn2mn(LONGGENMGRCLASS);
194                 try {
195                     lgManager = (LongGenMgr) Class.forName(cn).newInstance();
196                 } catch (Exception JavaDoc e) {
197                     throw new PException(e, "Cannot create LongGenMgr (probably a ClassLoader problem): " + cn);
198                 }
199                 lgManager.init(pm, clact);
200                 LongGenMgrRegistry.registerLonGenMgr(lgManager);
201             }
202         }
203         binderLG = lgManager.getLongGen(this.getClass().getPackage().getName() +
204                                         "." + this.getClass().getName());
205         String JavaDoc cn = "??";
206         try {
207             PBinder binder = new BasidBinder(PNamingContext.CTSTRING);
208             cn = pm.cn2mn(PolymorphIdBinderInfo.CLASSIDN) + PMapper.PCLASSMAPPINGAPPENDER;
209             PClassMapping pcm = (PClassMapping) Class.forName(cn).newInstance();
210             pcm.setPBinder(binder);
211             binder.setPClassMapping(pcm);
212             pm.map(pcm);
213             PMapCluster cl = pm.getPMapCluster(pcm.getClassName());
214             switch (clact) {
215             case PClassMapping.CREATE_STRUCTURE_IF_NEEDED:
216                 cl.createMappingStructures(false);
217                 break;
218             case PClassMapping.CLEANUP_REMOVEALL:
219                 cl.deleteMappingStructures();
220                 cl.createMappingStructures(false);
221                 break;
222             case PClassMapping.CLEANUP_REMOVEDATA:
223                 cl.deleteData();
224                 break;
225             case PClassMapping.CLEANUP_DONOTHING:
226                 break;
227             }
228
229             binder = new BasidBinder(PNamingContext.CTLONG);
230             cn = pm.cn2mn(PolymorphIdBinderInfo.IDCLASSN) + PMapper.PCLASSMAPPINGAPPENDER;
231             pcm = (PClassMapping) Class.forName(cn).newInstance();
232             pcm.setPBinder(binder);
233             binder.setPClassMapping(pcm);
234             pm.map(pcm);
235             cl = pm.getPMapCluster(pcm.getClassName());
236             switch (clact) {
237             case PClassMapping.CREATE_STRUCTURE_IF_NEEDED:
238                 cl.createMappingStructures(false);
239                 break;
240             case PClassMapping.CLEANUP_REMOVEALL:
241                 cl.deleteMappingStructures();
242                 cl.createMappingStructures(false);
243                 break;
244             case PClassMapping.CLEANUP_REMOVEDATA:
245                 cl.deleteData();
246                 break;
247             case PClassMapping.CLEANUP_DONOTHING:
248                 break;
249             }
250         } catch (InstantiationException JavaDoc e) {
251             throw new PException(e,
252                                  "Cannot create PClassMapping for [" + cn + "].");
253         } catch (IllegalAccessException JavaDoc e) {
254             throw new PException(e,
255                                  "Cannot access PClassMapping class [" + cn + "].");
256         } catch (ClassNotFoundException JavaDoc e) {
257             throw new PException(e,
258                                  "Cannot load PClassMapping class [" + cn + "].");
259         } catch (PException e) {
260             throw e;
261         }
262     }
263
264     /**
265      * Allocates a new persisttent class identifier.
266      */

267     public long newClassId() throws PException {
268         return binderLG.genId();
269     }
270
271     public long newClassId(Object JavaDoc conn) throws PException {
272         return binderLG.genId(conn);
273     }
274
275     /**
276      * Notifies the manager that a PType has been associated with this binder.
277      */

278     public void ncTypeDef(PolymorphRefNC p) {
279         if (Debug.ON && logger != null) {
280             if (p.getPType() == null)
281                 logger.log(BasicLevel.DEBUG, p.getPType().getJormName());
282             else
283                 logger.log(BasicLevel.DEBUG, "Null type of the pnc: " + p);
284         }
285         Iterator JavaDoc it = cn2binders.values().iterator();
286         while (it.hasNext()) {
287             PolymorphIdBinderInfo b = (PolymorphIdBinderInfo) it.next();
288             if (b.getPType() == null)
289                 continue;
290             if (Debug.ON && logger != null)
291                 logger.log(BasicLevel.DEBUG, "compare to " + b.getPType().getJormName());
292             if (b.getPType().isa(p.getPType())) {
293                 p.addValidBinder(b);
294                 if (Debug.ON && logger != null) logger.log(BasicLevel.DEBUG, "added");
295             }
296         }
297     }
298 }
299
Popular Tags