KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > naming > lib > KFPNCManager


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.jorm.naming.lib;
19
20 import org.objectweb.jorm.api.PMapperListener;
21 import org.objectweb.jorm.api.PMappingRequiredEvent;
22 import org.objectweb.jorm.api.ClassMappedEvent;
23 import org.objectweb.jorm.api.PMapper;
24 import org.objectweb.jorm.api.PClassMapping;
25 import org.objectweb.jorm.api.PException;
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.jorm.type.lib.PTypePAAH;
28 import org.objectweb.jorm.naming.api.NamingFilterKeyProvider;
29 import org.objectweb.jorm.naming.api.KeyFilteredNamingContext;
30 import org.objectweb.jorm.naming.api.PBinder;
31 import org.objectweb.jorm.util.api.Loggable;
32 import org.objectweb.util.monolog.api.Logger;
33 import org.objectweb.util.monolog.api.BasicLevel;
34 import org.objectweb.util.monolog.api.LoggerFactory;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Iterator JavaDoc;
43
44 /**
45  * Is a manager of FKPNC instances. It is in charge of the initialization of the
46  * couple (binder, key) into KFPNC. This manager keeps an association table
47  * between the class name of the root inheritance tree, and the KFPNC instance.
48  *
49  * @author S.Chassande-Barrioz
50  */

51 public class KFPNCManager
52     implements PMapperListener, Loggable {
53
54     private Logger logger;
55     private LoggerFactory loggerFactory;
56     private boolean debug;
57
58     private Map JavaDoc cn2pnc;
59     
60     /**
61      * Key=SuperClassName Value=List of Object[]
62      * where Object[0] = binder
63      * Object[1] = namingKey
64      */

65     private Map JavaDoc scn2binderKey = new HashMap JavaDoc();
66
67     public KFPNCManager() {
68         cn2pnc = new HashMap JavaDoc();
69     }
70
71     public KFPNCManager(Map JavaDoc cn2pnc) {
72         this.cn2pnc = cn2pnc;
73     }
74
75     public KeyFilteredNamingContext getKFPNC(String JavaDoc className) {
76         Object JavaDoc pnc = cn2pnc.get(className);
77         return (KeyFilteredNamingContext )
78             (pnc instanceof KeyFilteredNamingContext ? pnc : null);
79     }
80
81
82     // IMPLEMENTATION OF THE PMapperListener INTERFACE //
83
//-------------------------------------------------//
84

85     public void mappingRequired(PMappingRequiredEvent e) {
86         //nothing to do
87
}
88
89     /**
90      * If the class is a root of an inheritance class tree, a KFPNC is created.
91      * Otherwise if the PClassMapping of the new persistent class is also
92      * a NamingFilterKeyProvider, the couple (binder, key) is registered into
93      * the KFPNC of the ancestors.
94      */

95     public void classMapped(ClassMappedEvent e) {
96         String JavaDoc className = e.getJormClassName();
97         PMapper mapper = e.getPMapper();
98         PType type = mapper.getPTypeSpace().getPType(className);
99         if (!(type instanceof PTypePAAH)) {
100             return;
101         }
102         PClassMapping pcm = mapper.lookup(className);
103         PTypePAAH classType = (PTypePAAH) type;
104         PType[] superTypes = classType.getInheritedPType();
105         if (superTypes == null || superTypes.length == 0) { //no parent
106
//fetch the KFPNC
107
Object JavaDoc pnc = cn2pnc.get(className);
108             if (!(pnc instanceof KeyFilteredNamingContext)) {
109                 return;
110             }
111             KeyFilteredNamingContext kfpnc = (KeyFilteredNamingContext) pnc;
112             // fetch all children
113
HashSet JavaDoc subPCMs = new HashSet JavaDoc(); //contains also the current PCM
114
findSubPCM(className, subPCMs, mapper);
115
116             //resgister the couple (binder,key) into the PNC
117
for(Iterator JavaDoc it = subPCMs.iterator(); it.hasNext();) {
118                 PClassMapping subpcm = (PClassMapping) it.next();
119                 if (subpcm instanceof NamingFilterKeyProvider) {
120                     Object JavaDoc key = ((NamingFilterKeyProvider) subpcm).getNamingFilterKey();
121                     if (debug) {
122                         logger.log(BasicLevel.DEBUG,
123                             "Register the binder of the '"
124                             + subpcm.getClassName()
125                             + "' sub class with the following key: " + key);
126                     }
127                     try {
128                         kfpnc.exportClass(subpcm.getPBinder(), key);
129                     } catch (PException e1) {
130                         logger.log(BasicLevel.ERROR,
131                             "Impossible to export a Binder into the KFPNC of the class "
132                             + className, e1);
133                     }
134                 }
135             }
136             List JavaDoc list = (List JavaDoc) scn2binderKey.remove(className);
137             if (list != null) {
138                 while (!list.isEmpty()) {
139                     Object JavaDoc[] obj = (Object JavaDoc[]) list.remove(0);
140                     if (debug) {
141                         logger.log(BasicLevel.DEBUG,
142                             "Register the binder of the '"
143                             + ((PBinder)obj[0]).getBinderClassMapping().getClassName()
144                             + "' sub class with the following key: " + obj[1]);
145                     }
146                     try {
147                         kfpnc.exportClass((PBinder)obj[0], obj[1]);
148                     } catch (PException e1) {
149                         logger.log(BasicLevel.ERROR,
150                             "Impossible to export a Binder into the KFPNC of the class "
151                             + className, e1);
152                     }
153                 }
154             }
155         } else if (pcm instanceof NamingFilterKeyProvider) {
156             Object JavaDoc key = ((NamingFilterKeyProvider) pcm).getNamingFilterKey();
157             if (debug) {
158                 logger.log(BasicLevel.DEBUG,
159                     "The '" + pcm.getClassName()
160                     + "' provides the following key: " + key);
161             }
162             //has a parent at least & provide naming filter key
163
HashSet JavaDoc superClassNames = new HashSet JavaDoc();
164
165             //find ancestors
166
findAncestorNames(className, superClassNames, mapper);
167             PBinder binder = pcm.getPBinder();
168
169             //Register the new mapped class into the KFPNC
170
for(Iterator JavaDoc it = superClassNames.iterator(); it.hasNext();) {
171                 String JavaDoc superClassName = (String JavaDoc) it.next();
172                 KeyFilteredNamingContext kfpnc = getKFPNC(superClassName);
173                 if (kfpnc == null) {
174                     List JavaDoc list = (List JavaDoc) scn2binderKey.get(superClassName);
175                     if (list == null) {
176                         list = new ArrayList JavaDoc();
177                         scn2binderKey.put(superClassName, list);
178                     }
179                     list.add(new Object JavaDoc[]{binder, key});
180                     if (debug) {
181                         logger.log(BasicLevel.DEBUG,
182                             "No KPNC found for the ancestor " + superClassName
183                             + ". The register of the class "
184                             + pcm.getClassName() + " will be done later");
185                     }
186                 } else {
187                     if (debug) {
188                         logger.log(BasicLevel.DEBUG,
189                             "Register the binder of the '"
190                             + pcm.getClassName()
191                             + "' class with the following key: " + key
192                             + " into the KFPNc of the ancestor " + superClassName);
193                     }
194                     try {
195                         kfpnc.exportClass(binder, key);
196                     } catch (PException e1) {
197                         logger.log(BasicLevel.ERROR,
198                             "Impossible to export a Binder into the KFPNC of the class "
199                             + superClassName, e1);
200                     }
201                 }
202             }
203         }
204     }
205
206     /**
207      * Find PClassMapping of all sub persistent classes of a persistent class.
208      * The type tree is used to find the sub class.
209      *
210      * @param className is the name of the persistent class
211      * @param result is the result parameter where the PCM instance must be
212      * added.
213      */

214     private void findSubPCM(String JavaDoc className, Set JavaDoc result, PMapper mapper) {
215         PClassMapping pcm = mapper.lookup(className);
216         if (pcm != null && result.add(pcm)) {
217             PType classType = mapper.getPTypeSpace().getPType(className);
218             PType[] subTypes = classType.getSubTypes();
219             for(int i=0; i<subTypes.length; i++) {
220                 findSubPCM(subTypes[i].getJavaName(), result, mapper);
221             }
222         } else {
223             if (debug) {
224                 logger.log(BasicLevel.DEBUG, "The sub class "
225                     + className + " is not yet mapped");
226             }
227         }
228     }
229
230     /**
231      * Find class name of ancestor of a persistent class
232      * @param className is the name of the persistent class
233      * @param result is the result parameter where the ancestor class name
234      * must be added.
235      */

236     private void findAncestorNames(String JavaDoc className, Set JavaDoc result, PMapper mapper) {
237         PType type = mapper.getPTypeSpace().getPType(className);
238         if (!(type instanceof PTypePAAH)) {
239             return;
240         }
241         PTypePAAH classType = (PTypePAAH) type;
242         PType[] superTypes = classType.getInheritedPType();
243         if (superTypes == null || superTypes.length == 0) { //no parent
244
result.add(className);
245         } else { //has Parent
246
for(int i=0; i<superTypes.length; i++) {
247                 findAncestorNames(superTypes[i].getJormName(), result, mapper);
248             }
249         }
250     }
251
252
253     // IMPLEMENTATION OF THE Loggable INTERFACE //
254
//------------------------------------------//
255

256     public LoggerFactory getLoggerFactory() {
257         return loggerFactory;
258     }
259
260     public void setLoggerFactory(LoggerFactory loggerFactory) {
261         this.loggerFactory = loggerFactory;
262     }
263
264     public Logger getLogger() {
265         return logger;
266     }
267
268     public void setLogger(Logger logger) {
269         this.logger = logger;
270         debug = logger.isLoggable(BasicLevel.DEBUG);
271     }
272 }
273
Popular Tags