KickJava   Java API By Example, From Geeks To Geeks.

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


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  * EJBBundleInfoHelper.java
26  *
27  * Created on October 15, 2004, 1:51 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 import org.netbeans.modules.dbschema.SchemaElement;
36
37 import com.sun.jdo.api.persistence.model.Model;
38 import com.sun.jdo.api.persistence.mapping.ejb.*;
39 import com.sun.jdo.spi.persistence.support.ejb.model.DeploymentDescriptorModel;
40 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.DeploymentHelper;
41
42 /** This is a class which implements the EJBInfoHelper interface
43  * based on EjbBundleDescriptor and other DOL classes.
44  *
45  * @author Rochelle Raccah
46  */

47 public class EJBBundleInfoHelper implements EJBInfoHelper {
48     private static final char UNDERLINE = '_'; // NOI18N
49
private static final char DOT = '.'; // NOI18N
50

51     private final EjbBundleDescriptor bundleDescriptor;
52     private Collection availableSchemaNames;
53     private NameMapper nameMapper; // standard one
54
private Model model;
55
56     /** Creates a new instance of EJBBundleInfoHelper
57      * @param bundleDescriptor the EjbBundleDescriptor which defines the
58      * universe of names for this application.
59      * @param availableSchemaNames a Collection of available schemas
60      * in the application - used only during development
61      */

62     public EJBBundleInfoHelper(EjbBundleDescriptor bundleDescriptor,
63             Collection availableSchemaNames) {
64         this(bundleDescriptor, null, null, availableSchemaNames);
65     }
66
67     /** Creates a new instance of EJBBundleInfoHelper
68      * @param bundleDescriptor the EjbBundleDescriptor which defines the
69      * universe of names for this application.
70      * @param nameMapper the NameMapper object to be used - allows
71      * a client to supply its own mapper to the helper rather than
72      * have the helper construct a new instance
73      * @param model the Model object to be used - allows
74      * a client to supply its own mapper to the helper rather than
75      * have the helper construct a new instance
76      * @param availableSchemaNames a Collection of available schemas
77      * in the application - used only during development
78      */

79     EJBBundleInfoHelper(EjbBundleDescriptor bundleDescriptor,
80             NameMapper nameMapper, Model model,
81             Collection availableSchemaNames) {
82         this.bundleDescriptor = bundleDescriptor;
83         this.nameMapper = nameMapper;
84         this.model = model;
85         this.availableSchemaNames = availableSchemaNames;
86     }
87
88     /** Gets the EjbBundleDescriptor which defines the universe of
89      * names for this application.
90      * @return the EjbBundleDescriptor which defines the universe of
91      * names for this application.
92      */

93     private EjbBundleDescriptor getBundleDescriptor() {
94         return bundleDescriptor;
95     }
96
97     /**
98      * @see EJBInfoHelper#getEjbJarDisplayName
99      */

100     public String JavaDoc getEjbJarDisplayName() {
101         return bundleDescriptor.getName();
102     }
103
104     /** Gets a collection of names of schemas defined in this
105      * ejb jar. This implementation simply returns the list passed in
106      * the constructor or <code>null</code> if there was none supplied.
107      * @return a collection schema names
108      */

109     public Collection getAvailableSchemaNames () {
110         return availableSchemaNames;
111     }
112
113     /** Gets the name to use for schema generation. This implementation
114      * uses a combo of app name, module name, etc.
115      * @return the name to use for schema generation
116      */

117     public String JavaDoc getSchemaNameToGenerate() {
118         // make sure there is no '.' in schema name
119
return DeploymentHelper.getDDLNamePrefix(
120             getBundleDescriptor()).replace(DOT, UNDERLINE);
121     }
122
123     /** Gets the schema with the specified name, loading it if necessary.
124      * This implementation uses the class loader as the extra context
125      * information used to load.
126      * @param schemaName the name of the schema to be loaded
127      * @return the schema object
128      */

129     public SchemaElement getSchema(String JavaDoc schemaName) {
130         return SchemaElement.forName(schemaName, getClassLoader());
131     }
132
133     /**
134      * @see EJBInfoHelper#getEjbNames
135      */

136     public Collection getEjbNames() {
137         Iterator iterator = getBundleDescriptor().getEjbs().iterator();
138         ArrayList returnList = new ArrayList();
139
140         while (iterator.hasNext()) {
141             EjbDescriptor ejb = (EjbDescriptor)iterator.next();
142
143             if (ejb instanceof EjbCMPEntityDescriptor)
144                 returnList.add(ejb.getName());
145         }
146         
147         return returnList;
148     }
149
150     /**
151      * @see EJBInfoHelper#getFieldsForEjb
152      */

153     public Collection getFieldsForEjb(String JavaDoc ejbName) {
154         Iterator iterator = getModel().getFields(ejbName).iterator();
155         ArrayList returnList = new ArrayList();
156
157         while (iterator.hasNext())
158             returnList.add(iterator.next());
159
160         return returnList;
161     }
162
163     /**
164      * @see EJBInfoHelper#getRelationshipsForEjb
165      */

166     public Collection getRelationshipsForEjb(String JavaDoc ejbName) {
167         Iterator iterator = getBundleDescriptor().getRelationships().iterator();
168         ArrayList returnList = new ArrayList();
169
170         // TODO: issue of usage of this - several iterations of this if
171
// iterating all the bean - but, I think it can change, so can't
172
// cache it in a map (same comment applies to getEjbNames and
173
// getFieldsForEjb)
174
while (iterator.hasNext()) {
175             RelationshipDescriptor relD =
176                 (RelationshipDescriptor)iterator.next();
177             RelationRoleDescriptor testRole = relD.getSource();
178             String JavaDoc cmrField = null;
179
180             if (ejbName.equals(testRole.getOwner().getName())) {
181                 cmrField = testRole.getCMRField();
182                 if (cmrField != null)
183                     returnList.add(cmrField);
184             }
185
186             testRole = relD.getSink();
187             if (ejbName.equals(testRole.getOwner().getName())) {
188                 cmrField = testRole.getCMRField();
189                 if (cmrField != null)
190                     returnList.add(cmrField);
191             }
192         }
193
194         return returnList;
195     }
196
197     /** Gets the class loader which corresponds to this ejb bundle.
198      * @return the class loader which corresponds to this ejb bundle
199      */

200     public ClassLoader JavaDoc getClassLoader() {
201         return bundleDescriptor.getClassLoader();
202     }
203
204     /**
205      * @see EJBInfoHelper#getNameMapper
206      */

207     public AbstractNameMapper getNameMapper() {
208         return getNameMapperInternal();
209     }
210
211     /**
212      * @see EJBInfoHelper#createUniqueNameMapper
213      */

214     public AbstractNameMapper createUniqueNameMapper() {
215         return new NameMapper(bundleDescriptor);
216     }
217
218     private NameMapper getNameMapperInternal() {
219         if (nameMapper == null)
220             nameMapper = new NameMapper(bundleDescriptor, false);
221
222         return nameMapper;
223     }
224
225     /**
226      * @see EJBInfoHelper#createConversionHelper
227      */

228     public ConversionHelper createConversionHelper() {
229         return new EjbConversionHelper(getNameMapperInternal());
230     }
231
232     /**
233      * @see EJBInfoHelper#getModel
234      */

235     public Model getModel() {
236         if (model == null) {
237             model = new DeploymentDescriptorModel(getNameMapperInternal(),
238                 getClassLoader());
239         }
240
241         return model;
242     }
243 }
244
Popular Tags