KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.jdo.spi.persistence.support.ejb.ejbc;
25
26 import com.sun.enterprise.deployment.EjbBundleDescriptor;
27 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
28 import com.sun.enterprise.deployment.io.DescriptorConstants;
29 import com.sun.enterprise.deployment.backend.DeploymentEventInfo;
30 import com.sun.enterprise.deployment.backend.DeploymentStatus;
31 import com.sun.enterprise.server.Constants;
32
33 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.DeploymentHelper;
34 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper;
35 import com.sun.jdo.api.persistence.support.JDOFatalUserException;
36 import com.sun.jdo.spi.persistence.utility.I18NHelper;
37
38 import java.io.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.sql.Connection JavaDoc;
41 import java.sql.Statement JavaDoc;
42 import java.sql.SQLException JavaDoc;
43 import java.util.Collection JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 /**
47  * If the application contains cmp 2.x beans process them. Check if
48  * tables have to created or dropped depending on where we are called
49  * in a deploy/undeploy case.
50  * @author pramodg
51  */

52 public class CMPProcessor
53     extends BaseProcessor {
54     
55     /**
56      * Creates a new instance of CMPProcessor
57      * @param info the deployment info object.
58      * @param deploy true if this is a deploy event.
59      * @param redeploy true if this application is being redeployed.
60      * @param cliCreateTables the cli string related to creating tables
61      * at deployment time.
62      * @param cliDropAndCreateTables the cli string that indicates that
63      * old tables have to be dropped and new tables created.
64      * @param cliDropTables the cli string to indicate that the tables
65      * have to dropped at undeploy time.
66      */

67     public CMPProcessor(
68             DeploymentEventInfo info, boolean deploy, boolean redeploy,
69             String JavaDoc cliCreateTables, String JavaDoc cliDropAndCreateTables, String JavaDoc cliDropTables) {
70         super(info, deploy, redeploy, cliCreateTables,
71             cliDropAndCreateTables, cliDropTables);
72     }
73    
74     /**
75      * The entry point into this class. Process
76      * any ejb bundle descriptors if defined
77      * for this application.
78      */

79     protected void processApplication() {
80         Collection JavaDoc bundleCollection = application.getEjbBundleDescriptors();
81         if ( bundleCollection == null) {
82             return;
83         }
84         Iterator JavaDoc bundleItr = bundleCollection.iterator();
85
86         // Process only those Bundle descriptors that contain CMP beans.
87
while ( bundleItr.hasNext() ) {
88              processAppBundle((EjbBundleDescriptor)bundleItr.next());
89         }
90     }
91     
92     /**
93      * This method does all the work of checking
94      * and processing each ejb bundle descriptor.
95      * @param bundle the ejb bundle descriptor that is being worked on.
96      */

97    private void processAppBundle(EjbBundleDescriptor bundle) {
98            if (!bundle.containsCMPEntity()) {
99                 return;
100             }
101
102             ResourceReferenceDescriptor cmpResource =
103                     bundle.getCMPResourceReference();
104
105             // If this bundle's beans are not created by Java2DB, then skip to
106
// next bundle.
107
if (!DeploymentHelper.isJavaToDatabase(
108                     cmpResource.getSchemaGeneratorProperties())) {
109                 return;
110             }
111       
112             boolean createTables = getCreateTablesValue(cmpResource) ;
113             boolean dropTables = getDropTablesValue(cmpResource);
114
115             if (debug) {
116                 logger.fine("ejb.CMPProcessor.createanddroptables", //NOI18N
117
new Object JavaDoc[] {new Boolean JavaDoc(createTables), new Boolean JavaDoc(dropTables)});
118             }
119
120             if (!createTables && !dropTables) {
121                 // Nothing to do.
122
return;
123             }
124         
125             // At this point of time we are sure that we would need to create
126
// the sql/jdbc files required to create or drop objects from the
127
// database. Hence setup the required directories from the info object.
128
setApplicationLocation();
129             setGeneratedLocation();
130             
131             constructJdbcFileNames(bundle);
132             if (debug) {
133                 logger.fine("ejb.CMPProcessor.createanddropfilenames",
134                     createJdbcFileName, dropJdbcFileName); //NOI18N
135
}
136             dropTablesFromDB(dropTables, cmpResource.getJndiName());
137             createTablesInDB(createTables, cmpResource.getJndiName());
138        
139    }
140
141    /**
142      * We need to create tables only on deploy, and
143      * only if the CLI options cliCreateTables or
144      * cliDropAndCreateTables are not set to false.
145      * If those options are not set (UNDEFINED)
146      * the value is taken from the
147      * create-tables-at-deploy element of the
148      * sun-ejb-jar.xml
149      * (cmpResource.isCreateTablesAtDeploy()).
150      * @param cmpResource the cmp resource reference descriptor.
151      * @return true if tables have to created.
152      */

153     protected boolean getCreateTablesValue(
154             ResourceReferenceDescriptor cmpResource) {
155             boolean createTables =
156                 deploy
157                     && (cliCreateTables.equals(Constants.TRUE)
158                         || (cmpResource.isCreateTablesAtDeploy()
159                             && cliCreateTables.equals(Constants.UNDEFINED)));
160             return createTables;
161     }
162
163     /**
164      * We need to drop tables on deploy and redeploy,
165      * if the corresponding CLI options cliDropAndCreateTables
166      * (for redeploy) or cliDropTables (for undeploy) are
167      * not set to false.
168      * If the corresponding option is not set (UNDEFINED)
169      * the value is taken from the drop-tables-at-undeploy
170      * element of the sun-ejb-jar.xml
171      * (cmpResource.isDropTablesAtUndeploy()).
172      * @param cmpResource the cmp resource reference descriptor.
173      * @return true if the tables have to be dropped.
174      */

175     protected boolean getDropTablesValue(
176             ResourceReferenceDescriptor cmpResource) {
177         boolean dropTables =
178             (redeploy
179                 && (cliDropAndCreateTables.equals(Constants.TRUE)
180                     || (cmpResource.isDropTablesAtUndeploy()
181                         && cliDropAndCreateTables.equals(Constants.UNDEFINED))))
182             || (!deploy
183                 && (cliDropTables.equals(Constants.TRUE)
184                     || (cmpResource.isDropTablesAtUndeploy()
185                         && cliDropTables.equals(Constants.UNDEFINED))));
186        return dropTables;
187     }
188     
189     /**
190      * Construct the name of the create and
191      * drop jdbc ddl files that would be
192      * created. These name would be either
193      * obtained from the persistence.xml file
194      * (if the user has defined them) or we would
195      * create default filenames
196      * @param ejbBundle the ejb bundle descriptor being worked on.
197      */

198     private void constructJdbcFileNames(EjbBundleDescriptor ejbBundle) {
199         String JavaDoc filePrefix = EJBHelper.getDDLNamePrefix(ejbBundle);
200         
201         createJdbcFileName = filePrefix + CREATE_DDL_JDBC_FILE_SUFFIX;
202         dropJdbcFileName = filePrefix + DROP_DDL_JDBC_FILE_SUFFIX;
203     }
204     
205     /**
206      * If the drop file is present, drop the tables
207      * from the database.
208      * @param dropTables true if the table need to be dropped.
209      * @param resourceName the cmp resource string.
210      * @return true if tables were successfully dropped.
211      */

212     private boolean dropTablesFromDB(
213             boolean dropTables, String JavaDoc resourceName) {
214          boolean dropResult = false;
215         File JavaDoc dropFile = null;
216         
217         if(dropTables) {
218             dropFile = getDropDDLFile(dropJdbcFileName, deploy);
219             if (dropFile.exists()) {
220                 dropResult = executeDDLStatement(dropFile, resourceName);
221             } else {
222                 if (debug) {
223                     logger.fine("ejb.BaseProcessor.cannotdroptables", dropFile.getName()); //NOI18N
224
}
225             }
226         }
227         return dropResult;
228     }
229     
230     /**
231      * Get the create jdbc ddl file and execute it
232      * against the database
233      * @param createTables true if tables have to be created.
234      * @param resourceName the cmp resource name
235      * @return true if tables were successfully created.
236      */

237     private boolean createTablesInDB
238             (boolean createTables, String JavaDoc resourceName) {
239         boolean createResult = false;
240         File JavaDoc createFile = null;
241         
242         if(createTables) {
243             createFile = getCreateDDLFile(
244                     appGeneratedLocation + createJdbcFileName);
245             if(createFile.exists()) {
246                 createResult = executeDDLStatement(createFile, resourceName);
247             } else {
248                 if (debug) {
249                     logger.fine("ejb.BaseProcessor.cannotcreatetables", createFile.getName()); //NOI18N
250
}
251             }
252         }
253         return createResult;
254     }
255     
256     /**
257      * Get the ddl files eventually executed
258      * against the database. This method deals
259      * with both create and drop ddl files.
260      * @param fileName the create or drop jdbc ddl file.
261      * @param resourceName the jdbc resource name that would be used get a connection to the database.
262      * @return true if the tables were successfully
263      * created/dropped from the database.
264      */

265     private boolean executeDDLStatement(File JavaDoc fileName, String JavaDoc resourceName) {
266         boolean result = false;
267         Connection JavaDoc conn = null;
268         Statement JavaDoc sql = null;
269         try {
270             try {
271                     conn = DeploymentHelper.getConnection(resourceName);
272                     sql = conn.createStatement();
273                     result = true;
274                 } catch (SQLException JavaDoc ex) {
275                     cannotConnect(resourceName, ex);
276                 } catch (JDOFatalUserException ex) {
277                     cannotConnect(resourceName, ex);
278                 }
279         
280                 if(result) {
281                     executeDDLs(fileName, sql);
282                 }
283         } catch (IOException JavaDoc e) {
284             fileIOError(application.getRegistrationName(), e);
285         } finally {
286             closeConn(conn);
287         }
288         return result;
289     }
290         
291 }
292
Popular Tags