KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > AppUnDeployer


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  * AppDeployer.java
26  *
27  * Created on December 11, 2001, 5:37 PM
28  */

29
30 package com.sun.enterprise.deployment.backend;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.logging.*;
35 import java.security.AccessController JavaDoc;
36 import java.security.PrivilegedAction JavaDoc;
37
38 import com.sun.enterprise.config.ConfigException;
39 import com.sun.enterprise.deployment.Application;
40 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
41 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
42 import com.sun.enterprise.instance.ApplicationEnvironment;
43 import com.sun.enterprise.instance.AppsManager;
44 import com.sun.enterprise.instance.BaseManager;
45 import com.sun.enterprise.instance.InstanceEnvironment;
46 import com.sun.enterprise.util.io.FileSource;
47 import com.sun.enterprise.util.io.FileUtils;
48 import com.sun.enterprise.util.zip.ZipFileException;
49 import com.sun.enterprise.util.i18n.StringManager;
50 import com.sun.enterprise.util.diagnostics.Reminder;
51 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils;
52
53 /** AppUnDeployer is responsible for Undeploying Applications.
54  *
55  * @author bnevins
56  * @version
57  */

58
59 public class AppUnDeployer extends AppDeployerBase
60 {
61     AppUnDeployer(DeploymentRequest r) throws IASDeploymentException
62     {
63         super(r);
64     }
65
66     ///////////////////////////////////////////////////////////////////////////
67

68     public void doRequest() throws IASDeploymentException
69     {
70         doRequestPrepare();
71         doRequestFinish();
72     }
73
74     ///////////////////////////////////////////////////////////////////////////
75

76     public void doRequestPrepare() throws IASDeploymentException
77     {
78         try
79         {
80             begin();
81         }
82         catch(IASDeploymentException e)
83         {
84             throw e;
85         }
86         catch(Exception JavaDoc e)
87         {
88             String JavaDoc msg = localStrings.getString(
89                     "enterprise.deployment.backend.dorequest_exception");
90             logger.log(Level.WARNING, msg, e);
91             throw new IASDeploymentException(msg, e);
92         }
93     }
94
95     ///////////////////////////////////////////////////////////////////////////
96

97     public void doRequestFinish() throws IASDeploymentException
98     {
99         try
100         {
101             predeploy();
102             localBegin(); // sets cmp drop table variables
103
undeploy();
104     removePolicy();
105         }
106         catch(IASDeploymentException e)
107         {
108             throw e;
109         }
110         catch(Exception JavaDoc e)
111         {
112             String JavaDoc msg = localStrings.getString(
113                     "enterprise.deployment.backend.dorequest_exception");
114             logger.log(Level.WARNING, msg, e);
115             throw new IASDeploymentException(msg, e);
116         }
117         finally
118         {
119             finish();
120         }
121     }
122
123     /**
124      * Attempt to delete the deployed directory-tree.
125      * <p> This method call is <b>guaranteed</b> to never throw any kind of Exception
126      */

127     public void cleanup_internal() {
128         try {
129             if(isMaybeCMPDropTables)
130                 dropTables();
131             
132             liquidate();
133         }
134         catch(Exception JavaDoc e) {
135             logger.warning("Caught an Exception in cleanup_internal: " + e);
136         }
137     }
138     
139     ///////////////////////////////////////////////////////////////////////////
140

141     protected final File JavaDoc setAppDir() throws IASDeploymentException {
142         try {
143             return new File JavaDoc(DeploymentServiceUtils.getLocation(getAppName(),
144                 request.getType()));
145         }
146         catch(Exception JavaDoc e) {
147             String JavaDoc msg = localStrings.getString(
148             "enterprise.deployment.backend.error_getting_app_directory",
149             e);
150             throw new IASDeploymentException(msg, e);
151         }
152     }
153     
154     ///////////////////////////////////////////////////////////////////////////
155

156     private void undeploy() throws IASDeploymentException {
157         try {
158             getManager().unregisterDescriptor(getAppName());
159             addToSummary(successMessage + getAppName());
160         }
161         catch(Exception JavaDoc e) {
162             if(e instanceof IASDeploymentException)
163                 throw (IASDeploymentException)e;
164             else
165                 throw new IASDeploymentException(e);
166         }
167     }
168     
169     ///////////////////////////////////////////////////////////////////////////
170

171     private void liquidate() throws IASDeploymentException, ConfigException
172     {
173         
174         File JavaDoc aDir = getAppDir();
175         File JavaDoc sDir = getStubsDir();
176         File JavaDoc jDir = getJSPDir();
177         File JavaDoc xDir = getXMLDir();
178         File JavaDoc jwsDir = getJWSDir();
179         assert aDir != null && sDir != null && jDir != null;
180         
181         // if it's an archive deployment
182
if (! (DeploymentServiceUtils.isDirectoryDeployed(getAppName(),
183                 request.getType()) || request.isReload()) )
184         {
185             if(FileUtils.safeIsDirectory(aDir)) {
186                 FileUtils.whack(aDir);
187                 logger.fine("Deleted Application Directory: " + aDir.getPath());
188             }
189             else {
190                 logger.warning("Can't delete Application Directory -- it isn't a directory: "
191                 + aDir.getPath());
192             }
193         }
194         else {
195             logger.fine("Did NOT delete Application Directory (Directory-Deployment): " + aDir);
196         }
197         
198         if(FileUtils.safeIsDirectory(sDir)) {
199             FileUtils.whack(sDir);
200         }
201         else {
202             logger.finest("Can't delete Stubs Directory -- it isn't a directory: "
203             + FileUtils.safeGetCanonicalPath(sDir));
204         }
205         
206         if(FileUtils.safeIsDirectory(jDir)) {
207             FileUtils.whack(jDir);
208         }
209         else {
210             logger.finest("Can't delete JSP Directory -- it isn't a directory: "
211             + FileUtils.safeGetCanonicalPath(jDir));
212         }
213
214         if(FileUtils.safeIsDirectory(xDir)) {
215             FileUtils.whack(xDir);
216         }
217         else {
218             logger.finest("Can't delete XML Directory -- it isn't a directory: "
219             + FileUtils.safeGetCanonicalPath(xDir));
220         }
221
222         if(FileUtils.safeIsDirectory(jwsDir)) {
223             FileUtils.whack(jwsDir);
224         }
225         else {
226             logger.finest("Can't delete JWS Directory -- it isn't a directory: "
227             + FileUtils.safeGetCanonicalPath(jwsDir));
228         }
229
230     }
231     
232     /**
233      * localBegin -- if we need to drop cmp tables later, create an Application
234      * object NOW before the registration information disappears. The reason we don't
235      * drop the tables now is that it can NOT be rolled back. So we delay doing it as long
236      * as possible -- which is just before the files are deleted.
237      */

238     private void localBegin() {
239         // Why is this stuff complicating localBegin() when it could simply be called
240
// from cleanup_internal()? Because there may be problems with doing the actual
241
// drop tables late in the process (namely after unregistering. If that turns out to be the
242
// case then the drop tables call will be moved here. If not, we can clean this code up by
243
// moving it into dropTables() later.
244
// bnevins April 2003
245

246         try {
247             if(getRequest().isMaybeCMPDropTables()) {
248                 isMaybeCMPDropTables = true;
249                 
250                 if (FileUtils.safeIsDirectory(getXMLDir())) {
251                     applicationDD =
252                         DeploymentUtils.getAppDescriptor(getXMLDir().getAbsolutePath());
253                         FileArchive archive = new FileArchive();
254                         archive.open(getAppDir().getAbsolutePath());
255                         try {
256                             ApplicationArchivist.
257                                 readPersistenceDeploymentDescriptorsRecursively(
258                                     archive, applicationDD);
259                         } finally {
260                             archive.close();
261                         }
262                 } else {
263                     applicationDD =
264                         DeploymentUtils.getAppDescriptor(getAppDir().getAbsolutePath());
265                 }
266
267                 applicationDD.setRegistrationName(getAppName());
268             }
269         }
270         catch(Exception JavaDoc e) {
271             logger.warning("Caught an Exception in localBegin: " + e);
272         }
273     }
274     
275     /**
276      * Call into CMP to drop tables. This is called just before files are deleted as part
277      * of the cleanup() call. We do it very late in the process since dropping tables
278      * can't be rolled-back. Thus all of the other steps that can have errors that require a
279      * rollback have already been done successfully - or we wouldn't be here.
280      * bnevins April 2003
281      * <p> This method call is <b>guaranteed</b> to never throw any kind of Exception
282      */

283     private void dropTables() {
284         assert isMaybeCMPDropTables; // programmer error if this is false!
285

286         try {
287             DeploymentEventInfo info = new DeploymentEventInfo(getAppDir(),
288                 getStubsDir(), applicationDD, getRequest());
289             DeploymentEvent ev = new DeploymentEvent(DeploymentEventType.PRE_UNDEPLOY, info);
290             DeploymentEventManager.notifyDeploymentEvent(ev);
291         }
292         catch(Throwable JavaDoc t) {
293             logger.warning("Caught a Throwable in dropTables: " + t);
294         }
295     }
296     
297     ///////////////////////////////////////////////////////////////////////////
298

299     private String JavaDoc failureMessage = "Undeployment Failed -- rolled back undeployment";
300     private String JavaDoc successMessage = "Undeployment Successful for ";
301     private boolean isMaybeCMPDropTables = false;
302     private Application applicationDD = null;
303     private static StringManager localStrings = StringManager.getManager( AppUnDeployer.class );
304 }
305
Popular Tags