KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AppReDeployer.java
26  *
27  * Created on April 27, 2002, 3:38 PM
28  *
29  * @author bnevins
30  * <BR> <I>$Source: /cvs/glassfish/appserv-core/src/java/com/sun/enterprise/deployment/backend/AppReDeployer.java,v $
31  *
32  * Indentation Information:
33  * 0. Please (try to) preserve these settings.
34  * 1. Tabs are preferred over spaces.
35  * 2. In vi/vim -
36  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
37  * 3. In S1 Studio -
38  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
39  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = False.
40  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
41  */

42
43 package com.sun.enterprise.deployment.backend;
44
45 import java.io.*;
46 import java.util.logging.*;
47 import com.sun.enterprise.instance.ApplicationEnvironment;
48 import com.sun.enterprise.instance.InstanceEnvironment;
49 import com.sun.enterprise.util.io.FileSource;
50 import com.sun.enterprise.util.io.FileUtils;
51 import com.sun.enterprise.config.ConfigException;
52 import com.sun.enterprise.util.i18n.StringManager;
53 import com.sun.enterprise.util.diagnostics.Reminder;
54 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils;
55
56 public class AppReDeployer extends AppDeployer
57 {
58     ///////////////////////////////////////////////////////////////////////////
59
////////// Constructor //////////////////
60
///////////////////////////////////////////////////////////////////////////
61

62     AppReDeployer(DeploymentRequest r) throws IASDeploymentException
63     {
64         super(r);
65     }
66
67     ///////////////////////////////////////////////////////////////////////////
68
////////// Public Methods //////////////////
69
///////////////////////////////////////////////////////////////////////////
70

71     /**
72      * Attempt to delete previously deployed directory-tree.
73      * <p> This method call is <b>guaranteed</b> to never throw any kind of Exception
74      */

75     public void cleanup_internal()
76     {
77         try
78         {
79             // don't whack Directory-Redeployments, because the new dir == previous dir!
80
if(isArchive() && oldAppDir != null)
81             {
82                 FileUtils.whack(oldAppDir);
83
84                 if(oldAppDir.exists())
85                     logger.info("Unable to delete previous deployment directory: " + oldAppDir.getPath());
86                 else
87                     logger.info("Deleted previous deployment directory: " + oldAppDir.getPath());
88             }
89         }
90         catch(Exception JavaDoc e)
91         {
92             logger.info("Unable to delete previous deployment directory: " + oldAppDir.getPath());
93         }
94     }
95
96     ///////////////////////////////////////////////////////////////////////////
97
////////// Protected and Package Visibility Methods //////////////////
98
///////////////////////////////////////////////////////////////////////////
99

100     protected void begin() throws IASDeploymentException
101     {
102         // this is here because by the time we get to predeploy() -- the app will no
103
// longer be registered. So we need to save the app-dir right NOW!
104

105         super.begin();
106         
107         try
108         {
109                         originalAppDir = new File(DeploymentServiceUtils.getLocation(getAppName(), request.getType()));
110                         getManager().unregisterDescriptor(getAppName());
111                         removePolicy();
112         }
113         catch(Exception JavaDoc e)
114         {
115             String JavaDoc msg = localStrings.getString(
116                     "enterprise.deployment.backend.error_getting_app_location",
117                     getAppName() );
118             throw new IASDeploymentException( msg, e);
119         }
120     }
121
122     ///////////////////////////////////////////////////////////////////////////
123

124     protected final void predeploy() throws IASDeploymentException
125     {
126         appWasUnregistered = true;
127         super.predeploy();
128         setOldDirs();
129     }
130
131     ///////////////////////////////////////////////////////////////////////////
132

133     protected final File setAppDir() throws IASDeploymentException
134     {
135         File newAppDir = null;
136         
137         if(isArchive())
138         {
139             newAppDir = setAppDirArchive();
140         }
141         else if(isDirectory())
142         {
143             newAppDir = setAppDirDirectory();
144         }
145         else
146         {
147             String JavaDoc msg = localStrings.getString(
148                     "enterprise.deployment.backend.redeployment_not_dir_or_archive" );
149             throw new IASDeploymentException( msg );
150         }
151         
152         newAppDir.mkdirs();
153         return newAppDir;
154     }
155
156     ///////////////////////////////////////////////////////////////////////////
157

158     protected final File getOldStubsDir()
159     {
160         // this is here AND in the super-class so that ejbc-calling code
161
// can exist in the super-class only.
162
return oldStubsDir;
163     }
164     
165     ///////////////////////////////////////////////////////////////////////////
166

167     protected final File getOldAppDir()
168     {
169         // this is here AND in the super-class so that ejbc-calling code
170
// can exist in the super-class only.
171
return oldAppDir;
172     }
173
174     ///////////////////////////////////////////////////////////////////////////
175

176     protected void postDeploy() throws ConfigException, IASDeploymentException
177     {
178         super.postDeploy();
179
180         if(FileUtils.safeIsDirectory(oldStubsDir))
181             FileUtils.whack(oldStubsDir);
182
183         if(FileUtils.safeIsDirectory(oldJSPDir))
184             FileUtils.whack(oldJSPDir);
185
186                 if(FileUtils.safeIsDirectory(oldXMLDir))
187                         FileUtils.whack(oldXMLDir);
188
189                 if(FileUtils.safeIsDirectory(oldJWSDir))
190                         FileUtils.whack(oldJWSDir);
191     }
192     
193     ///////////////////////////////////////////////////////////////////////////
194

195     
196     protected final void rollback()
197     {
198         // careful -- this method is called from inside of a catch block.
199
// If we throw an exception from here, it'll be BIG trouble!
200
try
201         {
202             if(isArchive())
203                 rollbackArchive();
204             else
205                 rollbackDirectory();
206         }
207         catch(Exception JavaDoc e)
208         {
209             // Can't do anything about it!!
210
logger.log( Level.WARNING,
211                     "enterprise.deployment_rollback_error", e );
212         }
213     }
214     ///////////////////////////////////////////////////////////////////////////
215

216     protected String JavaDoc whatAreYou()
217     {
218         return "Redeployment";
219     }
220
221     ///////////////////////////////////////////////////////////////////////////
222
////////// Private Methods and Data //////////////////
223
///////////////////////////////////////////////////////////////////////////
224

225     private final void rollbackArchive() throws IASDeploymentException, ConfigException
226     {
227         // blow away new dirs, rename old ones back to new ones...
228
// This is inherently complex. Sigh.
229

230         // updated comment August 2003 bnevins
231
// redeploy has changed. Right now we have a completely UNREGISTERED app
232
// We need to
233
// 1) rename the _old dir back to the original
234
// 2) Register it -- because it was unregistered in order to unload it long ago.
235

236         if(appdirWasRenamed)
237         {
238             // hose-down new generated directories
239
DeleteOrKeepFailedStubs(getStubsDir());
240
241             if(FileUtils.safeIsDirectory(getJSPDir()))
242                 FileUtils.whack(getJSPDir());
243
244                         if(FileUtils.safeIsDirectory(getXMLDir()))
245                                 FileUtils.whack(getXMLDir());
246
247                         if(FileUtils.safeIsDirectory(getJWSDir()))
248                                 FileUtils.whack(getJWSDir());
249
250             // restore the generated directories back to original...
251
if(FileUtils.safeIsDirectory(oldStubsDir))
252                 oldStubsDir.renameTo(getStubsDir());
253
254             if(FileUtils.safeIsDirectory(oldJSPDir))
255                 oldJSPDir.renameTo(getJSPDir());
256                        if(FileUtils.safeIsDirectory(oldXMLDir))
257                                 oldXMLDir.renameTo(getXMLDir());
258
259                        if(FileUtils.safeIsDirectory(oldJWSDir))
260                                 oldJWSDir.renameTo(getJWSDir());
261
262             boolean success = false;
263             File appdir = getAppDir();
264
265             if(FileUtils.safeIsDirectory(appdir) && FileUtils.safeIsDirectory(oldAppDir))
266             {
267                 FileUtils.whack(appdir);
268                 success = oldAppDir.renameTo(appdir);
269             }
270
271             if(!success)
272             {
273                 FileUtils.whack(oldAppDir);
274                 logger.log( Level.SEVERE, "enterprise.deployment.backend.directoryRenameFailure",
275                                    new Object JavaDoc[] {oldAppDir.getAbsolutePath() , appdir.getAbsolutePath()});
276             }
277
278         }
279
280                 getManager().registerDescriptor(request.getName(), request.getDescriptor());
281
282     }
283     
284     ///////////////////////////////////////////////////////////////////////////
285

286     private final void rollbackDirectory() // throws IASDeploymentException, ConfigException
287
{
288         // bnevins -- On 9/8/2003 it was decided by the deploy team to NOT attempt
289
// to do a rollback to a previous working version. So we just fail everything,
290
// undeploy, and keep the files right where they are.
291

292         request.setReRegisterOnFailure(false);
293     }
294
295     ///////////////////////////////////////////////////////////////////////////
296

297     private final File setAppDirDirectory() throws IASDeploymentException
298     {
299         FileSource fileSource = request.getFileSource();
300
301         if(!fileSource.exists())
302         {
303             String JavaDoc msg = localStrings.getString("enterprise.deployment.backend.file_source_does_not_exist", fileSource );
304             throw new IASDeploymentException( msg );
305         }
306
307         assert fileSource.isDirectory();
308         File appDirectory = fileSource.getFile();
309
310         return appDirectory;
311     }
312
313     ///////////////////////////////////////////////////////////////////////////
314

315     private final File setAppDirArchive() throws IASDeploymentException
316     {
317         assert originalAppDir != null;
318         File newAppDirectory = originalAppDir;
319         oldAppDir = new File(newAppDirectory.getAbsolutePath() + "_old");
320
321         
322         // wipe it out if it happens to already exist
323
FileUtils.whack(oldAppDir);
324
325         // note -- java does NOT change the path of newAppDirectory, it just renames the file on disk!
326
// i.e. "newAppDirectory" will still be pointing at the same file after the call as before the call.
327
appdirWasRenamed = FileUtils.renameFile(newAppDirectory, oldAppDir);
328
329         if(!appdirWasRenamed)
330         {
331             String JavaDoc msg = localStrings.getString("enterprise.deployment.backend.directory_rename_error",
332                 newAppDirectory.getAbsolutePath(), oldAppDir.getAbsolutePath());
333             throw new IASDeploymentException( msg );
334         }
335
336         return newAppDirectory;
337     }
338     
339     ///////////////////////////////////////////////////////////////////////////
340

341     private final void setOldDirs() throws IASDeploymentException
342     {
343         File stubsDirectory = getStubsDir();
344         File jspDirectory = getJSPDir();
345         File xmlDirectory = getXMLDir();
346         File jwsDirectory = getJWSDir();
347         
348         if(FileUtils.safeIsDirectory(stubsDirectory))
349         {
350             oldStubsDir = new File(stubsDirectory.getPath() + "_old");
351             
352             if(oldStubsDir.exists())
353             {
354                 // what if there is a regular file with the name we need? Let's check...
355

356                 if(oldStubsDir.isDirectory())
357                     FileUtils.whack(oldStubsDir);
358                 else
359                     oldStubsDir.delete();
360             }
361             
362             if(!stubsDirectory.renameTo(oldStubsDir)) {
363                 String JavaDoc msg = localStrings.getString(
364                         "enterprise.deployment.backend.directory_rename_error",
365                         stubsDirectory.getPath(),
366                         oldStubsDir.getPath() );
367                 throw new IASDeploymentException( msg );
368             }
369         }
370
371         // WBN 4-18-2002 I don't know when JSP files are generated. To be safe I'm
372
// renaming the old ones -- just for rollback purposes...
373
if(FileUtils.safeIsDirectory(jspDirectory))
374         {
375             oldJSPDir = new File(jspDirectory.getPath() + "_old");
376             
377             if(oldJSPDir.exists())
378             {
379                 // what if there is a regular file with the name we need? Let's check...
380

381                 if(FileUtils.safeIsDirectory(oldJSPDir))
382                     FileUtils.whack(oldJSPDir);
383                 else
384                     oldJSPDir.delete();
385             }
386             
387             if(!jspDirectory.renameTo(oldJSPDir)) {
388                 String JavaDoc msg = localStrings.getString(
389                         "enterprise.deployment.backend.directory_rename_error",
390                         jspDirectory.getPath(),
391                         oldJSPDir.getPath() );
392                 throw new IASDeploymentException( msg );
393             }
394         }
395                 if(FileUtils.safeIsDirectory(xmlDirectory))
396                 {
397                         oldXMLDir = new File(xmlDirectory.getPath() + "_old");
398
399                         if(oldXMLDir.exists())
400                         {
401                                 // what if there is a regular file with the name we need? Let's check...
402

403                                 if(FileUtils.safeIsDirectory(oldXMLDir))
404                                         FileUtils.whack(oldXMLDir);
405                                 else
406                                         oldXMLDir.delete();
407                         }
408
409                         if( ! FileUtils.renameFile(xmlDirectory, oldXMLDir)) {
410                                 String JavaDoc msg = localStrings.getString(
411                                                 "enterprise.deployment.backend.directory_rename_error",
412                                                 xmlDirectory.getPath(),
413                                                 oldXMLDir.getPath() );
414                                 throw new IASDeploymentException( msg );
415                         }
416                 }
417
418                 if (FileUtils.safeIsDirectory(jwsDirectory)) {
419                     if (jwsDirectory.list().length > 0) {
420                         // warning if the directory exist and has some contents
421
DeploymentStatus jwsStatus = new DeploymentStatus(
422                             request.getCurrentDeploymentStatus());
423                         jwsStatus.setStageStatus(DeploymentStatus.WARNING);
424                         jwsStatus.setStageStatusMessage(localStrings.getString(
425                             "enterprise.deployment.backend.jws_redeploy",
426                             jwsDirectory.getPath()));
427
428                     }
429                     oldJWSDir = new File(jwsDirectory.getPath() + "_old");
430
431                     if(oldJWSDir.exists())
432                     {
433                         // what if there is a regular file with the name we need? // Let's check...
434

435                         if(FileUtils.safeIsDirectory(oldJWSDir)) {
436                             FileUtils.whack(oldJWSDir);
437                         }
438                         else {
439                             oldJWSDir.delete();
440                         }
441                     }
442
443                     if( ! FileUtils.renameFile(jwsDirectory, oldJWSDir)) {
444                         String JavaDoc msg = localStrings.getString(
445                             "enterprise.deployment.backend.directory_rename_error",
446                             jwsDirectory.getPath(),
447                             oldJWSDir.getPath() );
448                             throw new IASDeploymentException( msg );
449                     }
450                 }
451     }
452
453     ///////////////////////////////////////////////////////////////////////////
454

455     
456     private String JavaDoc failureMessage = "\n*********************\n****Redeployment Failed -- rolled back redeployment";
457     private String JavaDoc successMessage = "\n*********************\n****Redeployment Successful for ";
458     private File oldAppDir = null;
459     private File oldStubsDir = null;
460     private File oldJSPDir = null;
461     private File oldXMLDir = null;
462     private File oldJWSDir = null;
463     private File originalAppDir = null;
464     //private String oldRegisteredLocation = null;
465
private boolean appdirWasRenamed = false;
466     private boolean appWasUnregistered = false;
467     private static StringManager localStrings =
468         StringManager.getManager( AppReDeployer.class );
469 }
470
Popular Tags