KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > ejbc > NameMapper


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  * NameMapper.java
26  *
27  * Created on December 3, 2001, 5:09 PM
28  */

29
30 package com.sun.jdo.spi.persistence.support.ejb.ejbc;
31
32 import java.util.*;
33
34 import com.sun.enterprise.deployment.*;
35
36 import com.sun.enterprise.deployment.EjbBundleDescriptor;
37 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor;
38
39 import com.sun.jdo.spi.persistence.utility.StringHelper;
40 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper;
41
42 /** This is a subclass of
43  * {@link com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper} (in
44  * the <code>com.sun.jdo.spi.persistence.support.ejb.model.util</code>
45  * package) which implements the abstract methods based on an IAS
46  * implementation.
47  *
48  * @author Rochelle Raccah
49  */

50 public class NameMapper extends
51     com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper
52 {
53     private static String JavaDoc EJB_NAME = "EJB_NAME"; // NOI18N
54
private static String JavaDoc ABSTRACT_SCHEMA_NAME = "ABSTRACT_SCHEMA_NAME"; // NOI18N
55
private static String JavaDoc PERSISTENCE_NAME = "PERSISTENCE_NAME"; // NOI18N
56
private static String JavaDoc LOCAL_NAME = "LOCAL_NAME"; // NOI18N
57
private static String JavaDoc REMOTE_NAME = "REMOTE_NAME"; // NOI18N
58

59     private final boolean _expandPCNames;
60     private Map _nameTypeToNameMap;
61
62     /**
63      * Signature with CVS keyword substitution for identifying the generated code
64      */

65     public static final String JavaDoc SIGNATURE = "$RCSfile: NameMapper.java,v $ $Revision: 1.2 $"; //NOI18N
66

67     /** Creates a new instance of NameMapper
68      * @param bundleDescriptor the IASEjbBundleDescriptor which defines the
69      * universe of names for this application.
70      */

71     public NameMapper (EjbBundleDescriptor bundleDescriptor)
72     {
73         this(bundleDescriptor, true);
74     }
75
76     /** Creates a new instance of NameMapper
77      * @param bundleDescriptor the IASEjbBundleDescriptor which defines the
78      * universe of names for this application.
79      * @param expandPersistenceClassNames flag to indicate whether
80      * persistence class names should differ from bean names
81      */

82     public NameMapper (EjbBundleDescriptor bundleDescriptor,
83         boolean expandPersistenceClassNames)
84     {
85         super(bundleDescriptor);
86         _expandPCNames = expandPersistenceClassNames;
87         initMap();
88     }
89
90     private void initMap ()
91     {
92         Iterator iterator = getBundleDescriptor().getEjbs().iterator();
93         Map ejbMap = new HashMap();
94         Map persistenceClassMap = new HashMap();
95         Set localNames = new HashSet();
96         Set remoteNames = new HashSet();
97         Map abstractSchemaMap = new HashMap();
98
99         _nameTypeToNameMap = new HashMap();
100
101         while (iterator.hasNext())
102         {
103             Object JavaDoc next = iterator.next();
104
105             if (next instanceof IASEjbCMPEntityDescriptor)
106             {
107                 IASEjbCMPEntityDescriptor descriptor =
108                     (IASEjbCMPEntityDescriptor)next;
109                 String JavaDoc ejbName = descriptor.getName();
110
111                 ejbMap.put(ejbName, descriptor);
112                 safePut(persistenceClassMap,
113                     getPersistenceClassForDescriptor(descriptor), ejbName);
114                 safeAdd(localNames, descriptor.getLocalClassName());
115                 safeAdd(remoteNames, descriptor.getRemoteClassName());
116                 safePut(abstractSchemaMap,
117                     descriptor.getAbstractSchemaName(), ejbName);
118             }
119         }
120         _nameTypeToNameMap.put(EJB_NAME, ejbMap);
121         _nameTypeToNameMap.put(PERSISTENCE_NAME, persistenceClassMap);
122         _nameTypeToNameMap.put(LOCAL_NAME, localNames);
123         _nameTypeToNameMap.put(REMOTE_NAME, remoteNames);
124         _nameTypeToNameMap.put(ABSTRACT_SCHEMA_NAME, abstractSchemaMap);
125     }
126
127     // puts a key-value pair in a map as long as the key is not null
128
private void safePut (Map map, Object JavaDoc key, Object JavaDoc value)
129     {
130         if ((key != null) && (map != null))
131             map.put(key, value);
132     }
133     // puts a value in a set as long as the object is not null
134
private void safeAdd (Set set, Object JavaDoc value)
135     {
136         if ((value != null) && (set != null))
137             set.add(value);
138     }
139
140     private Map getMap () { return _nameTypeToNameMap; }
141
142     /** Determines if the specified name represents an ejb.
143      * @param name the fully qualified name to be checked
144      * @return <code>true</code> if this name represents an ejb;
145      * <code>false</code> otherwise.
146      */

147     public boolean isEjbName (String JavaDoc name)
148     {
149         return mapContainsKey(EJB_NAME, name);
150     }
151
152     /** Gets the EjbCMPEntityDescriptor which represents the ejb
153      * with the specified name.
154      * @param name the name of the ejb
155      * @return the EjbCMPEntityDescriptor which represents the ejb.
156      */

157     public EjbCMPEntityDescriptor getDescriptorForEjbName (String JavaDoc name)
158     {
159         Map ejbMap = (Map)getMap().get(EJB_NAME);
160         Object JavaDoc descriptor = ejbMap.get(name);
161
162         return (((descriptor != null) &&
163             (descriptor instanceof EjbCMPEntityDescriptor)) ?
164             (EjbCMPEntityDescriptor)descriptor : null);
165     }
166
167     private IASEjbCMPEntityDescriptor getIASDescriptorForEjbName (String JavaDoc name)
168     {
169         EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
170
171         return (((descriptor != null) &&
172             (descriptor instanceof IASEjbCMPEntityDescriptor)) ?
173             (IASEjbCMPEntityDescriptor)descriptor : null);
174     }
175
176     /** Gets the name of the abstract bean class which corresponds to the
177      * specified ejb name.
178      * @param name the name of the ejb
179      * @return the name of the abstract bean for the specified ejb
180      */

181     public String JavaDoc getAbstractBeanClassForEjbName (String JavaDoc name)
182     {
183         EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
184
185         return ((descriptor != null) ? descriptor.getEjbClassName() : null);
186     }
187
188     /** Gets the name of the key class which corresponds to the specified
189      * ejb name.
190      * @param name the name of the ejb
191      * @return the name of the key class for the ejb
192      */

193     public String JavaDoc getKeyClassForEjbName (String JavaDoc name)
194     {
195         EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
196
197         return ((descriptor != null) ?
198             descriptor.getPrimaryKeyClassName() : null);
199     }
200
201     /** Gets the name of the ejb which corresponds to the specified abstract
202      * schema name.
203      * @param schemaName the name of the abstract schema
204      * @return the name of the ejb for the specified abstract schema
205      */

206     public String JavaDoc getEjbNameForAbstractSchema (String JavaDoc schemaName)
207     {
208         Map abstractSchemaMap = (Map)getMap().get(ABSTRACT_SCHEMA_NAME);
209
210         return (String JavaDoc)abstractSchemaMap.get(schemaName);
211     }
212
213     /** Gets the name of the abstract schema which corresponds to the
214      * specified ejb.
215      * @param name the name of the ejb
216      * @return the name of the abstract schema for the specified ejb
217      */

218     public String JavaDoc getAbstractSchemaForEjbName (String JavaDoc name)
219     {
220         EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
221
222         return ((descriptor != null) ?
223             descriptor.getAbstractSchemaName() : null);
224     }
225
226     /** Gets the name of the concrete bean class which corresponds to the
227      * specified ejb.
228      * @param name the name of the ejb
229      * @return the name of the concrete bean for the specified ejb
230      */

231     public String JavaDoc getConcreteBeanClassForEjbName (String JavaDoc name)
232     {
233         IASEjbCMPEntityDescriptor descriptor =
234             getIASDescriptorForEjbName(name);
235
236         return ((descriptor != null) ? getQualifiedName(
237             getAbstractBeanClassForEjbName(name),
238             descriptor.getConcreteImplClassName()) : null);
239     }
240
241     private String JavaDoc getQualifiedName (String JavaDoc classNameWithPackage,
242         String JavaDoc classNameToQualify)
243     {
244         if (!StringHelper.isEmpty(classNameToQualify))
245         {
246             String JavaDoc packageName =
247                 JavaTypeHelper.getPackageName(classNameToQualify);
248             
249             if (StringHelper.isEmpty(packageName)) // not already qualified
250
{
251                 packageName =
252                     JavaTypeHelper.getPackageName(classNameWithPackage);
253
254                 if (!StringHelper.isEmpty(packageName))
255                     return packageName + '.' + classNameToQualify;
256             }
257         }
258
259         return classNameToQualify;
260     }
261
262     /** Gets the name of the ejb name which corresponds to the
263      * specified persistence-capable class name.
264      * @param className the name of the persistence-capable
265      * @return the name of the ejb for the specified persistence-capable
266      */

267     public String JavaDoc getEjbNameForPersistenceClass (String JavaDoc className)
268     {
269         Map pcMap = (Map)getMap().get(PERSISTENCE_NAME);
270
271         return (String JavaDoc)pcMap.get(className);
272     }
273
274     /** Gets the name of the persistence-capable class which corresponds to
275      * the specified ejb name.
276      * @param name the name of the ejb
277      * @return the name of the persistence-capable for the specified ejb
278      */

279     public String JavaDoc getPersistenceClassForEjbName (String JavaDoc name)
280     {
281         EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
282
283         return ((descriptor != null) ?
284             getPersistenceClassForDescriptor(descriptor) : null);
285     }
286
287     private String JavaDoc getPersistenceClassForDescriptor (
288         EjbCMPEntityDescriptor descriptor)
289     {
290         String JavaDoc pcName = ((descriptor instanceof IASEjbCMPEntityDescriptor) ?
291             ((IASEjbCMPEntityDescriptor)descriptor).getPcImplClassName() :
292             null);
293
294         // use the package name, keep the ejb name
295
if ((pcName != null) && !_expandPCNames)
296         {
297             pcName = JavaTypeHelper.getPackageName(pcName) +
298                 '.' + descriptor.getName();
299         }
300
301         return pcName;
302     }
303
304     private boolean mapContainsKey (String JavaDoc stringIndex, String JavaDoc name)
305     {
306         Object JavaDoc mapObject = getMap().get(stringIndex);
307         Set testSet = ((mapObject instanceof Set) ? (Set)mapObject :
308             ((Map)mapObject).keySet());
309
310         return ((name != null) ? testSet.contains(name) : false);
311     }
312
313     /** Determines if the specified name represents a local interface.
314      * @param name the fully qualified name to be checked
315      * @return <code>true</code> if this name represents a local interface;
316      * <code>false</code> otherwise.
317      */

318     public boolean isLocalInterface (String JavaDoc name)
319     {
320         return mapContainsKey(LOCAL_NAME, name);
321     }
322
323     /** Gets the name of the ejb which corresponds to the specified
324      * local interface name.
325      * @param ejbName the name of the ejb which contains fieldName
326      * from which to find relationship and therefore the local interface
327      * @param fieldName the name of the field in the ejb
328      * @param interfaceName the name of the local interface
329      * @return the name of the ejb for the specified local interface
330      */

331     public String JavaDoc getEjbNameForLocalInterface (String JavaDoc ejbName,
332         String JavaDoc fieldName, String JavaDoc interfaceName)
333     {
334         EjbCMPEntityDescriptor descriptor =
335             getRelatedEjbDescriptor(ejbName, fieldName);
336
337         return (((descriptor != null) && !StringHelper.isEmpty(interfaceName)
338             && interfaceName.equals(descriptor.getLocalClassName())) ?
339             descriptor.getName() : null);
340     }
341
342     /** Gets the name of the local interface which corresponds to the
343      * specified ejb name.
344      * @param name the name of the ejb
345      * @return the name of the local interface for the specified ejb
346      */

347     public String JavaDoc getLocalInterfaceForEjbName (String JavaDoc name)
348     {
349         EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
350
351         return ((descriptor != null) ? descriptor.getLocalClassName() : null);
352     }
353
354     /** Determines if the specified name represents a remote interface.
355      * @param name the fully qualified name to be checked
356      * @return <code>true</code> if this name represents a remote interface;
357      * <code>false</code> otherwise.
358      */

359     public boolean isRemoteInterface (String JavaDoc name)
360     {
361         return mapContainsKey(REMOTE_NAME, name);
362     }
363
364     /** Gets the name of the ejb which corresponds to the specified
365      * remote interface name.
366      * @param ejbName the name of the ejb which contains fieldName
367      * from which to find relationship and therefore the remote interface
368      * @param fieldName the name of the field in the ejb
369      * @param interfaceName the name of the remote interface
370      * @return the name of the ejb for the specified remote interface
371      */

372     public String JavaDoc getEjbNameForRemoteInterface (String JavaDoc ejbName,
373         String JavaDoc fieldName, String JavaDoc interfaceName)
374     {
375         EjbCMPEntityDescriptor descriptor =
376             getRelatedEjbDescriptor(ejbName, fieldName);
377
378         return (((descriptor != null) && !StringHelper.isEmpty(interfaceName)
379             && interfaceName.equals(descriptor.getRemoteClassName())) ?
380             descriptor.getName() : null);
381     }
382
383     /** Gets the name of the remote interface which corresponds to the
384      * specified ejb name.
385      * @param name the name of the ejb
386      * @return the name of the remote interface for the specified ejb
387      */

388     public String JavaDoc getRemoteInterfaceForEjbName (String JavaDoc name)
389     {
390         EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
391
392         return ((descriptor != null) ? descriptor.getRemoteClassName() : null);
393     }
394
395     private EjbCMPEntityDescriptor getRelatedEjbDescriptor (
396         String JavaDoc ejbName, String JavaDoc ejbFieldName)
397     {
398         EjbCMPEntityDescriptor descriptor = ((ejbName != null) ?
399             getDescriptorForEjbName(ejbName) : null);
400
401         if (descriptor != null)
402         {
403             PersistenceDescriptor persistenceDescriptor =
404                 descriptor.getPersistenceDescriptor();
405             CMRFieldInfo cmrf =
406                 persistenceDescriptor.getCMRFieldInfoByName(ejbFieldName);
407             
408             return cmrf.role.getPartner().getOwner();
409         }
410
411         return null;
412     }
413
414     /** Gets the name of the field in the ejb which corresponds to the
415      * specified persistence-capable class name and field name pair.
416      * @param className the name of the persistence-capable
417      * @param fieldName the name of the field in the persistence-capable
418      * @return the name of the field in the ejb for the specified
419      * persistence-capable field
420      */

421     public String JavaDoc getEjbFieldForPersistenceField (String JavaDoc className,
422         String JavaDoc fieldName)
423     {
424         return fieldName;
425     }
426
427     /** Gets the name of the field in the persistence-capable class which
428      * corresponds to the specified ejb name and field name pair.
429      * @param name the name of the ejb
430      * @param fieldName the name of the field in the ejb
431      * @return the name of the field in the persistence-capable for the
432      * specified ejb field
433      */

434     public String JavaDoc getPersistenceFieldForEjbField (String JavaDoc name, String JavaDoc fieldName)
435     {
436         return fieldName;
437     }
438 }
439
Popular Tags