KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > autodeploy > AutoDeployedFilesManager


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24
25 /*
26  * AutoDeployedFilesManager.java
27  *
28  * Created on September 4, 2003
29  *
30  */

31
32 package com.sun.enterprise.deployment.autodeploy;
33
34 import java.io.*;
35 import java.util.*;
36 import java.util.logging.Logger JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import com.sun.enterprise.server.Constants;
39
40 /**
41  * Contains the status of list of files that have been autodeployed.
42  *
43  * @author Mahesh Rangamani
44 */

45
46 public class AutoDeployedFilesManager {
47     
48     private static final Logger JavaDoc sLogger=AutoDeployControllerImpl.sLogger;
49     protected static final String JavaDoc STATUS_DIR_NAME = ".autodeploystatus";
50     protected String JavaDoc statDir = null;
51
52
53     public AutoDeployedFilesManager() {
54     }
55     
56     protected AutoDeployedFilesManager(String JavaDoc s) {
57         statDir = s;
58     }
59     
60     /**
61      * Create an instance from the persisted file in the specified directory.
62      * @param statusDir Directory in which the status file is to read.
63     */

64     public static AutoDeployedFilesManager loadStatus(File statusDir) throws Exception JavaDoc {
65         return loadStatus(statusDir.getAbsolutePath());
66     }
67     
68     public static AutoDeployedFilesManager loadStatus(String JavaDoc autoDeploymentDir) throws Exception JavaDoc {
69
70         String JavaDoc statusDir = autoDeploymentDir + File.separator + STATUS_DIR_NAME;
71         String JavaDoc sysAppDirPath = System.getProperty(Constants.INSTALL_ROOT) +
72             File.separator + Constants.LIB + File.separator +
73             Constants.LIB_INSTALL + File.separator +
74             Constants.LIB_INSTALL_APPLICATIONS;
75
76         File fileObj = new File(statusDir);
77    
78         // if the .autodeploystatus directory does not exist
79
// and this is not for auto deploying system applications
80
// create the .autodeploystatus directory
81
if (!fileObj.exists() && !autoDeploymentDir.equals(sysAppDirPath)) {
82             sLogger.log(Level.INFO, "autoDeployment status dir missing, creating a new one");
83             fileObj.mkdirs();
84         }
85         AutoDeployedFilesManager adfm = new AutoDeployedFilesManager(statusDir);
86         return adfm;
87         
88     }
89     
90     public void writeStatus() throws Exception JavaDoc {
91            // Nothing to do
92
}
93        
94     /**
95      * Update the status of the file as deployed.
96      *
97      */

98     public void setDeployedFileInfo(File f) throws Exception JavaDoc {
99       try {
100         File statusFile = getStatusFile(f);
101         statusFile.createNewFile();
102         statusFile.setLastModified(f.lastModified());
103       } catch (Exception JavaDoc e) { throw e; }
104     }
105    
106     /*
107      * Delete status info for file
108      */

109     public void deleteDeployedFileInfo(File f) throws Exception JavaDoc {
110       try {
111         File statusFile = getStatusFile(f);
112         statusFile.delete();
113       } catch (Exception JavaDoc e) { throw e;}
114     }
115     
116     // calculate the status file path.
117
private File getStatusFile(File f) {
118         
119         File outDir = new File(this.statDir);
120         File autoDeployDir = outDir.getParentFile();
121         File dir = f.getParentFile();
122         while (!dir.getAbsolutePath().equals(autoDeployDir.getAbsolutePath())) {
123             outDir = new File(outDir, dir.getName());
124             dir = dir.getParentFile();
125         }
126         outDir.mkdirs();
127         return new File(outDir, f.getName());
128     }
129    
130     /**
131       * Compare the list of files with the current status info
132       * and determine the files that need to be deployed
133       *
134       */

135     public File[] getFilesForDeployment(File[] latestFiles) {
136
137         if (latestFiles == null) return new File[0];
138         File statusDir = new File(statDir);
139
140         ArrayList<File> arrList = new ArrayList<File>();
141         for (File deployDirFile : latestFiles) {
142             File statusFile = getStatusFile(deployDirFile);
143             if (!statusFile.exists() || deployDirFile.lastModified() != statusFile.lastModified()) {
144                 arrList.add(deployDirFile);
145             }
146         }
147         return arrList.toArray(new File[0]);
148         
149     }
150     
151     /**
152       * Compare the list of files with the current status info
153       * and determine the apps that need to be undeployed.
154       *
155       */

156     public File[] getFilesForUndeployment(File[] latestFiles) {
157         
158         File statusDir = new File(statDir);
159         Set<File> statusFiles =
160                 AutoDeployDirectoryScanner.getListOfFilesAsSet(statusDir, true);
161         
162         // The file might have been deleted. In that case, return null.
163
if (statusFiles == null || statusFiles.isEmpty()){
164             return null;
165         }
166         
167         // now let's get the latestFiles status files names to remove them
168
// from the list of status files, any remaining ones will need to
169
// be undeployed
170
for (File deployDirFile : latestFiles) {
171             statusFiles.remove(getStatusFile(deployDirFile));
172         }
173         ArrayList<File> appNames = new ArrayList<File>();
174         File autodeployDir = statusDir.getParentFile();
175         for(File statusDirFile : statusFiles) {
176             
177             // calculate the original file as it was copied in the autodeploy
178
// directory
179
File filePath = statusDir.getParentFile();
180             File f = statusDirFile.getParentFile();
181             while (!f.equals(statusDir)) {
182                 filePath = new File(filePath, f.getName());
183                 f = f.getParentFile();
184             }
185             filePath = new File(filePath, statusDirFile.getName());
186             appNames.add(filePath);
187         }
188         return appNames.toArray(new File[0]);
189     }
190 }
191
192
193
Popular Tags