KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DirectoryScanner.java
26  *
27  *
28  * Created on February 19, 2003, 10:17 AM
29  */

30
31 package com.sun.enterprise.deployment.autodeploy;
32
33 import java.io.File JavaDoc;
34 import java.io.FileFilter JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import com.sun.enterprise.util.i18n.StringManager;
40
41 /**
42  * Implementation of Directory scanner for autodeployment </br>
43  * Providing functionality for scanning the input source directory </br>
44  * and return the list of deployable components for autodeployment.</br>
45  * Provide the list of deployable modules/application, depending upon the "type" entry </br>
46  * passed to getAllDeployableEntity(File autodeployDir, String type).
47  *
48  *@author vikas
49  */

50 public class AutoDeployDirectoryScanner implements DirectoryScanner{
51     
52     
53     private static final Logger JavaDoc sLogger=AutoDeployControllerImpl.sLogger;
54     private static StringManager localStrings =
55                     StringManager.getManager( AutoDeployDirectoryScanner.class );
56     
57     public AutoDeployDirectoryScanner() {
58     }
59     
60      public void deployedEntity(File JavaDoc autodeployDir, File JavaDoc deployedEntity) {
61          try {
62          AutoDeployedFilesManager adfm = AutoDeployedFilesManager.loadStatus(autodeployDir);
63          adfm.setDeployedFileInfo(deployedEntity);
64          adfm.writeStatus();
65          } catch (Exception JavaDoc e) {
66              printException(e);
67              // Do nothing
68
}
69
70      }
71      
72      public void undeployedEntity(File JavaDoc autodeployDir, File JavaDoc undeployedEntity) {
73          try {
74          AutoDeployedFilesManager adfm = AutoDeployedFilesManager.loadStatus(autodeployDir);
75          adfm.deleteDeployedFileInfo(undeployedEntity);
76          adfm.writeStatus();
77          } catch (Exception JavaDoc e) {
78              printException(e);
79              // Do nothing
80
}
81      }
82      
83     /**
84      * return true if any new deployable entity is present in autodeployDir
85      * @param autodeployDir
86      * @return
87      */

88     public boolean hasNewDeployableEntity(File JavaDoc autodeployDir) {
89         boolean newFilesExist=false;
90             try {
91                 AutoDeployedFilesManager adfm = AutoDeployedFilesManager.loadStatus(autodeployDir);
92                 if(adfm.getFilesForDeployment(getListOfFiles(autodeployDir)).length > 0) {
93                     //atleast one new file is there
94
newFilesExist=true;
95                 }
96             } catch (Exception JavaDoc e) {
97                 printException(e);
98                 return false;
99             }
100         
101         return newFilesExist;
102         
103     }
104     // this should never be called from system dir autodeploy code...
105
public File JavaDoc[] getAllFilesForUndeployment(File JavaDoc autodeployDir) {
106
107         try {
108             AutoDeployedFilesManager adfm = AutoDeployedFilesManager.loadStatus(autodeployDir);
109             return adfm.getFilesForUndeployment(getListOfFiles(autodeployDir, true));
110             } catch (Exception JavaDoc e) {
111                 printException(e);
112                 return new File JavaDoc[0];
113             }
114     }
115          
116     /**
117      * Get the list of all deployable files
118      * @param autodeployDir
119      * @return */

120     public File JavaDoc[] getAllDeployableModules(File JavaDoc autodeployDir, boolean includeSubDir) {
121         
122         AutoDeployedFilesManager adfm = null;
123         try {
124         adfm = AutoDeployedFilesManager.loadStatus(autodeployDir);
125         } catch (Exception JavaDoc e) {
126             printException(e);
127             return new File JavaDoc[0];
128         }
129         
130         return adfm.getFilesForDeployment(getListOfFiles(autodeployDir, includeSubDir));
131     }
132
133     protected void printException(Exception JavaDoc e) {
134         sLogger.log(Level.SEVERE, e.getMessage(), e);
135         e.printStackTrace();
136     }
137     
138     protected File JavaDoc[] getListOfFiles(File JavaDoc dir) {
139         return getListOfFiles(dir, false);
140     }
141   
142     protected File JavaDoc[] getListOfFiles(File JavaDoc dir, boolean includeSubDir) {
143         return getListOfFilesAsSet(dir, includeSubDir).toArray(new File JavaDoc[0]);
144     }
145             
146             
147     static Set JavaDoc<File JavaDoc> getListOfFilesAsSet(File JavaDoc dir, boolean includeSubDir) {
148         Set JavaDoc<File JavaDoc> result = new HashSet JavaDoc<File JavaDoc>();
149         File JavaDoc[] dirFiles = dir.listFiles();
150         for (File JavaDoc dirFile : dirFiles) {
151             if (dirFile.isDirectory()) {
152                 if (includeSubDir && !(dirFile.getName().equals(".autodeploystatus"))) {
153                     result.addAll(getListOfFilesAsSet(dirFile, true));
154                 }
155             } else {
156                 String JavaDoc name = dirFile.getName();
157                 String JavaDoc fileType = name.substring(name.lastIndexOf(".") + 1);
158                 if(fileType != null && !fileType.equals("") &&
159                         (fileType.equals(AutoDeployConstants.EAR_EXTENSION) ||
160                         fileType.equals(AutoDeployConstants.WAR_EXTENSION) ||
161                         fileType.equals(AutoDeployConstants.JAR_EXTENSION) ||
162                         fileType.equals(AutoDeployConstants.RAR_EXTENSION) ||
163                         fileType.equals("class"))) {
164                 
165                     result.add(dirFile);
166                 }
167                 
168             }
169         }
170         return result;
171     }
172     
173 }
174
Popular Tags