KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DeploymentEventListenerImpl.java
26  *
27  * Created on April 8, 2003.
28  */

29
30 package com.sun.jdo.spi.persistence.support.ejb.ejbc;
31
32 import java.io.BufferedReader JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileReader JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import java.sql.Connection JavaDoc;
38 import java.sql.Statement JavaDoc;
39 import java.sql.SQLException JavaDoc;
40
41 import java.util.Iterator JavaDoc;
42 import java.util.ResourceBundle JavaDoc;
43 import java.util.Properties JavaDoc;
44 import java.util.Collection JavaDoc;
45
46 import com.sun.enterprise.util.io.FileUtils;
47
48 import com.sun.enterprise.deployment.Application;
49 import com.sun.enterprise.deployment.EjbBundleDescriptor;
50 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor;
51 import com.sun.enterprise.deployment.PersistenceUnitsDescriptor;
52 import com.sun.enterprise.deployment.PersistenceUnitDescriptor;
53 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
54
55 import com.sun.enterprise.deployment.backend.DeploymentEvent;
56 import com.sun.enterprise.deployment.backend.DeploymentEventInfo;
57 import com.sun.enterprise.deployment.backend.DeploymentEventListener;
58 import com.sun.enterprise.deployment.backend.DeploymentEventManager;
59 import com.sun.enterprise.deployment.backend.DeploymentRequest;
60 import com.sun.enterprise.deployment.backend.DeploymentStatus;
61 import com.sun.enterprise.deployment.backend.IASDeploymentException;
62
63 import com.sun.enterprise.server.Constants;
64
65 import com.sun.jdo.api.persistence.support.JDOFatalUserException;
66
67 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperSQLStore;
68
69 import com.sun.jdo.spi.persistence.utility.logging.Logger;
70 import com.sun.jdo.spi.persistence.utility.I18NHelper;
71 import com.sun.jdo.spi.persistence.utility.database.DatabaseConstants;
72 //import com.sun.jdo.spi.persistence.support.sqlstore.ejb.*;
73

74 /** Implementation of the DeploymentEventListener interface for
75 * creating and dropping database schema definitions at the appropriate
76 * deployment/undeployment events.
77 *
78 */

79 public class DeploymentEventListenerImpl
80     implements DeploymentEventListener, DatabaseConstants {
81
82     /** I18N message handler */
83     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
84         "com.sun.jdo.spi.persistence.support.ejb.ejbc.Bundle", // NOI18N
85
DeploymentEventListenerImpl.class.getClassLoader());
86
87     /** The logger */
88     private static Logger logger = LogHelperSQLStore.getLogger();
89
90     /** Garantees singleton.
91      * Registers itself during initial load
92      */

93     static {
94         DeploymentEventManager.addListener (new DeploymentEventListenerImpl());
95     }
96
97     /** Default constructor should not be public */
98     DeploymentEventListenerImpl() { }
99
100     /**
101      * This method is called when a <code>DeploymentEventManager</code>
102      * needs to deliver a code>DeploymentEvent</code> event.
103      * @param event the DeploymentEvent to be delivered.
104      */

105     public void notifyDeploymentEvent(DeploymentEvent event) {
106         int type = event.getEventType();
107         switch (type) {
108             case DeploymentEvent.POST_DEPLOY:
109                 processEvent(event.getEventInfo(), true);
110                 break;
111             case DeploymentEvent.PRE_UNDEPLOY:
112                 processEvent(event.getEventInfo(), false);
113                 break;
114             default:
115                 break;
116         }
117     }
118
119     /** Event handling.
120      * @param source the event source.
121      * @param deploy true if this is part of the deploy event.
122      */

123     private void processEvent(DeploymentEventInfo info, boolean deploy) {
124         // Get the CLI overrides.
125
String JavaDoc cliCreateTables = null;
126         String JavaDoc cliDropAndCreateTables = null;
127         String JavaDoc cliDropTables = null;
128
129         DeploymentRequest request = info.getDeploymentRequest();
130         Properties JavaDoc cliOverrides = request.getOptionalArguments();
131         if (deploy) {
132             cliDropAndCreateTables = cliOverrides.getProperty(
133                 Constants.CMP_DROP_AND_CREATE_TABLES, Constants.UNDEFINED);
134
135             cliCreateTables = cliOverrides.getProperty(
136                 Constants.CMP_CREATE_TABLES, Constants.UNDEFINED);
137
138             if (cliCreateTables.equals(Constants.UNDEFINED)) {
139                 // It might have been specified as CMP_DROP_AND_CREATE_TABLES.
140
cliCreateTables = cliDropAndCreateTables;
141             }
142         } else {
143             cliDropTables = cliOverrides.getProperty(
144                 Constants.CMP_DROP_TABLES, Constants.UNDEFINED);
145         }
146
147         Application application = info.getApplicationDescriptor();
148         if ( application == null) {
149             return;
150         }
151
152         processApplication(request, info, deploy, cliCreateTables,
153             cliDropAndCreateTables, cliDropTables);
154     } //processEvent
155

156     /**
157      * This is the method that does the actual processing of the deployment event.
158      * For each application process the cmp 2.x beans if any followed by processing
159      * the ejb 3.0 beans.
160      * @param request the deployment request object
161      * @param info the event source
162      * @param deploy true if this is part of a deploy event
163      * @param cliCreateTables the cli option for creating tables
164      * @param cliDropAndCreateTables the cli option for dropping old tables and creating new tables
165      * @param cliDropTables the cli option to drop tables at undeploy time
166      */

167     private void processApplication(DeploymentRequest request,
168             DeploymentEventInfo info, boolean deploy,
169             String JavaDoc cliCreateTables, String JavaDoc cliDropAndCreateTables,
170             String JavaDoc cliDropTables) {
171         boolean debug = logger.isLoggable(logger.FINE);
172         boolean redeploy = isRedeploy(deploy, request);
173         
174         if (debug) {
175             logger.fine("ejb.DeploymentEventListenerImpl.processingevent", //NOI18N
176
((deploy)? ((redeploy)? "redeploy" : "deploy") : "undeploy"), //NOI18N
177
info.getApplicationDescriptor().getRegistrationName());
178         }
179         
180         // Get status value for our use and initialize it.
181
DeploymentStatus status = request.getCurrentDeploymentStatus();
182         status.setStageStatus(DeploymentStatus.SUCCESS);
183         status.setStageStatusMessage("");
184         
185         BaseProcessor processor =
186                 new CMPProcessor(info, deploy, redeploy, cliCreateTables,
187             cliDropAndCreateTables, cliDropTables);
188         processor.processApplication();
189
190         processor =
191                 new PersistenceProcessor(info, deploy, redeploy, cliCreateTables,
192             cliDropAndCreateTables, cliDropTables);
193         processor.processApplication();
194     }
195
196     /**
197      * This method returns a boolean value that determines if we are
198      * trying to do a redeployment of an existing application.
199      * @param deploy true if the event is part of deploy
200      * @param request the deployment request object
201      * @return true if we are trying to redeploy an existing application
202      */

203     private boolean isRedeploy(boolean deploy, DeploymentRequest request) {
204         boolean redeploy = deploy;
205         try {
206             if (request != null) {
207                 redeploy = request.isReDeploy();
208             }
209         } catch (IASDeploymentException e) {
210             // Ignore? This is a strange exception.
211
}
212         return redeploy;
213     }
214
215 } //DeploymentEventListenerImpl
216
Popular Tags