KickJava   Java API By Example, From Geeks To Geeks.

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


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.Application;
27 import com.sun.enterprise.deployment.backend.DeploymentEventInfo;
28 import com.sun.enterprise.deployment.backend.DeploymentRequest;
29 import com.sun.enterprise.deployment.backend.DeploymentStatus;
30 import com.sun.enterprise.deployment.backend.IASDeploymentException;
31 import com.sun.enterprise.util.io.FileUtils;
32
33 import com.sun.jdo.spi.persistence.utility.I18NHelper;
34 import com.sun.jdo.spi.persistence.utility.database.DatabaseConstants;
35 import com.sun.jdo.spi.persistence.utility.logging.Logger;
36
37 import java.io.BufferedReader JavaDoc;
38 import java.io.File JavaDoc;
39 import java.io.FileReader JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.sql.Connection JavaDoc;
42 import java.sql.Statement JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.ResourceBundle JavaDoc;
46
47 /**
48  *
49  * @author pramodg
50  */

51 abstract public class BaseProcessor
52         implements DatabaseConstants {
53
54     /** The logger */
55     protected static final Logger logger = LogHelperEJBCompiler.getLogger();
56     
57     /** I18N message handler */
58     protected final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
59         "com.sun.jdo.spi.persistence.support.ejb.ejbc.Bundle", // NOI18N
60
BaseProcessor.class.getClassLoader());
61
62     protected Application application;
63     protected DeploymentEventInfo info;
64     protected DeploymentRequest request;
65     protected DeploymentStatus status;
66     
67     /**
68      * True if this is a deploy event.
69      */

70     protected boolean deploy;
71     protected boolean redeploy;
72     protected String JavaDoc cliCreateTables;
73     protected String JavaDoc cliDropAndCreateTables;
74     protected String JavaDoc cliDropTables;
75     /**
76      * Name with which the application is registered.
77      */

78     protected String JavaDoc appRegisteredName;
79     
80     protected String JavaDoc appDeployedLocation;
81     protected String JavaDoc appGeneratedLocation;
82     /**
83      * The string name of the create jdbc ddl file.
84      */

85     protected String JavaDoc createJdbcFileName;
86     /**
87      * The string name of the drop jdbc ddl file.
88      */

89     protected String JavaDoc dropJdbcFileName;
90
91     protected boolean debug = logger.isLoggable(logger.FINE);
92     
93     /**
94      * Creates a new instance of BaseProcessor.
95      * @param info the deployment info object.
96      * @param deploy true if this is a deploy event.
97      * @param redeploy true if this application is being redeployed.
98      * @param cliCreateTables the cli string related to creating tables
99      * at deployment time.
100      * @param cliDropAndCreateTables the cli string that indicates that
101      * old tables have to be dropped and new tables created.
102      * @param cliDropTables the cli string to indicate that the tables
103      * have to dropped at undeploy time.
104      */

105     public BaseProcessor(DeploymentEventInfo info,
106             boolean deploy, boolean redeploy, String JavaDoc cliCreateTables,
107             String JavaDoc cliDropAndCreateTables, String JavaDoc cliDropTables) {
108         initializeVariables(info, deploy, redeploy, cliCreateTables,
109             cliDropAndCreateTables, cliDropTables);
110     }
111
112     private void initializeVariables(
113             DeploymentEventInfo info, boolean deploy,
114             boolean redeploy, String JavaDoc cliCreateTables,
115             String JavaDoc cliDropAndCreateTables, String JavaDoc cliDropTables) {
116         this.info = info;
117         this.application = this.info.getApplicationDescriptor();
118         this.appRegisteredName = this.application.getRegistrationName();
119         this.status =
120                 this.info.getDeploymentRequest().getCurrentDeploymentStatus();
121         
122         this.deploy = deploy;
123         this.redeploy = redeploy;
124         this.cliCreateTables = cliCreateTables;
125         this.cliDropAndCreateTables = cliDropAndCreateTables;
126         this.cliDropTables = cliDropTables;
127     }
128     
129     abstract protected void processApplication();
130
131     
132        /**
133         * Read the create ddl file from the disk location.
134         * @param createJdbcFileName the string name of the file.
135         * @return the create jdbc ddl file.
136         */

137     protected File JavaDoc getCreateDDLFile( String JavaDoc createJdbcFileName) {
138         File JavaDoc createFile = null;
139         try {
140             createFile = new File JavaDoc(createJdbcFileName);
141             if (!createFile.exists()) {
142                 logI18NWarnMessage(
143                  "ejb.BaseProcessor.cannotcreatetables",
144                 appRegisteredName, createFile.getName(), null);
145             }
146
147             if (debug) {
148                 logger.fine("ejb.BaseProcessor.createfilename", //NOI18N
149
createFile.getName());
150             }
151         } catch (Exception JavaDoc e) {
152             logI18NWarnMessage(
153                  "Exception caught in BaseProcessor.getCreateDDLFile()",
154                 appRegisteredName, null, e);
155         }
156         return createFile;
157     }
158     
159        /**
160         * Read the drop ddl file from the disk location.
161         * @param dropJdbcFileName the string name of the file.
162         * @param deploy true if this is a deploy event.
163         * @return the drop jdbc ddl file.
164         */

165        protected File JavaDoc getDropDDLFile(String JavaDoc dropJdbcFileName, boolean deploy) {
166         File JavaDoc dropFile = null;
167         try {
168
169             File JavaDoc dir = (deploy)? info.getOldStubsDir() : info.getStubsDir();
170             if (dir == null) {
171                 // Just in case it's not available for redeploy.
172
dir = info.getStubsDir();
173             }
174             if(null != dropJdbcFileName) {
175                 dropFile = new File JavaDoc(dir, dropJdbcFileName);
176             }
177             if(!dropFile.exists()) {
178                 logI18NWarnMessage(
179                      "ejb.BaseProcessor.cannotdroptables",
180                     appRegisteredName, dropFile.getName(), null);
181             }
182
183             if (debug) {
184                 logger.fine("ejb.BaseProcessor.dropfilename", //NOI18N
185
dropFile.getName());
186             }
187
188         } catch (Exception JavaDoc e) {
189             logI18NWarnMessage(
190                     "Exception caught in BaseProcessor.getDropDDLFile()",
191                 appRegisteredName, null, e);
192         }
193         return dropFile;
194     }
195        
196         /**
197      * Open a DDL file and execute each line as a SQL statement.
198      * @throw IOException if there is a problem with reading the file.
199      * @param f the File object to use.
200      * @param sql the Statement to use for execution.
201      * @throws java.io.IOException if there is a problem with reading the file.
202      */

203     protected void executeDDLs(File JavaDoc f, Statement JavaDoc sql)
204             throws IOException JavaDoc {
205
206         BufferedReader JavaDoc reader = null;
207         StringBuffer JavaDoc warningBuf = new StringBuffer JavaDoc();
208
209         try {
210             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
211             String JavaDoc s;
212             while ((s = reader.readLine()) != null) {
213                 try {
214                     if (debug) {
215                         logger.fine("ejb.BaseProcessor.executestatement", s); //NOI18N
216
}
217                     sql.execute(s);
218
219                 } catch(SQLException JavaDoc ex) {
220                     String JavaDoc msg = getI18NMessage("ejb.BaseProcessor.sqlexception",
221                             s, null, ex);
222                     logger.warning(msg);
223                     warningBuf.append("\n\t").append(msg); // NOI18N
224
}
225             }
226         } finally {
227             if (reader != null) {
228                 try {
229                     reader.close();
230                 } catch(IOException JavaDoc ex) {
231                     // Ignore.
232
}
233             }
234             if (warningBuf.length() > 0) {
235                 String JavaDoc warning =
236                         getI18NMessage("ejb.BaseProcessor.tablewarning");
237                 warnUser(warning + warningBuf.toString());
238             }
239         }
240     }
241
242     /**
243      * Provide a warning message to the user. The message is appended to any
244      * already-existing warning message text.
245      * @param status DeploymentStatus via which user is warned
246      * @param msg Message for user.
247      */

248     protected void warnUser(String JavaDoc msg) {
249         status.setStageStatus(DeploymentStatus.WARNING);
250         status.setStageStatusMessage(
251                 status.getStageStatusMessage() + "\n" + msg); // NOI18N
252
}
253
254     /**
255      * Provide a warning message to the user about inability to connect to the
256      * database. The message is created from the cmpResource's JNDI name and
257      * the exception.
258      * @param status DeploymentStatus via which user is warned
259      * @param cmpResource For obtaining JNDI name
260      * @param ex Exception which is cause for inability to connect.
261      */

262     protected void cannotConnect(String JavaDoc connName,
263             Throwable JavaDoc ex) {
264         logI18NWarnMessage( "ejb.BaseProcessor.cannotConnect",
265                 connName, null, ex);
266     }
267     
268     protected void fileIOError(String JavaDoc regName,
269             Throwable JavaDoc ex) {
270         logI18NWarnMessage("ejb.BaseProcessor.ioexception",
271                 regName, null, ex);
272     }
273     
274     /**
275      * Close the connection that was opened
276      * to the database
277      * @param conn the database connection.
278      */

279     protected static void closeConn(Connection JavaDoc conn) {
280         if (conn != null) {
281             try {
282                 conn.close();
283             } catch(SQLException JavaDoc ex) {
284                 // Ignore.
285
}
286         }
287     }
288     
289     protected void logI18NInfoMessage(
290             String JavaDoc errorCode, String JavaDoc regName,
291             String JavaDoc fileName, Throwable JavaDoc ex) {
292         String JavaDoc msg = getI18NMessage(errorCode,
293                 regName, fileName, ex);
294         logger.info(msg);
295     }
296     
297     protected void logI18NWarnMessage(
298             String JavaDoc errorCode, String JavaDoc regName,
299             String JavaDoc fileName, Throwable JavaDoc ex) {
300         String JavaDoc msg = getI18NMessage(errorCode,
301                 regName, fileName, ex);
302         logger.warning(msg);
303         warnUser(msg);
304     }
305     
306     /**
307      *
308      * @param errorCode
309      * @return
310      */

311     protected String JavaDoc getI18NMessage(String JavaDoc errorCode) {
312         return getI18NMessage(
313              errorCode, null, null, null);
314     }
315     protected String JavaDoc getI18NMessage(
316             String JavaDoc errorCode, String JavaDoc regName,
317             String JavaDoc fileName, Throwable JavaDoc ex) {
318         String JavaDoc msg = null;
319         if(null != ex)
320                msg = I18NHelper.getMessage(
321                     messages, errorCode, regName, ex.toString());
322         else if(null != fileName )
323             msg = I18NHelper.getMessage(
324                     messages, errorCode, regName, fileName);
325         else
326              msg = I18NHelper.getMessage(messages, errorCode);
327         
328         return msg;
329     }
330
331
332     /**
333      * The location where the application has been deployed. This would ideally
334      * be the domains/domain1/applications directory. This information is obtained
335      * from the DeploymentEventListener object that is passed in.
336      */

337     protected void setApplicationLocation() {
338         if(null != this.appDeployedLocation)
339             return;
340         
341         this.appDeployedLocation =
342             info.getDeploymentRequest().getDeployedDirectory().getAbsolutePath()
343             + File.separator;
344     }
345     
346     /**
347      * The location where files have been generated as part of the application
348      * deployment cycle. This is where we write out the sql/jdbc files used to
349      * create or drop objects from the database. This information is obtained
350      * from the DeploymentEventListener object that is passed in.
351      */

352     protected void setGeneratedLocation() {
353         if(null != this.appGeneratedLocation)
354             return;
355         this.appGeneratedLocation =
356                 info.getStubsDir().getAbsolutePath() + File.separator;
357     }
358 }
359
Popular Tags