KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > collect > AppInfoCollector


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 package com.sun.enterprise.diagnostics.collect;
24
25 import com.sun.logging.LogDomains;
26 import com.sun.enterprise.diagnostics.DiagnosticException;
27 import com.sun.enterprise.diagnostics.ServiceConfig;
28 import com.sun.enterprise.diagnostics.CLIOptions;
29 import com.sun.enterprise.diagnostics.Constants;
30 import com.sun.enterprise.diagnostics.Data;
31 import com.sun.enterprise.diagnostics.util.FileUtils;
32 import com.sun.enterprise.diagnostics.util.DDFilter;
33
34 import java.io.IOException JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FilenameFilter JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39 /**
40  * Responsible for collecting applications deployment descriptors and generated
41  * files.
42  * @author Manisha Umbarje
43  */

44
45 public class AppInfoCollector implements Collector {
46     
47     private String JavaDoc destFolder;
48     private String JavaDoc repositoryFolder;
49     private ServiceConfig config;
50     private static Logger JavaDoc logger =
51     LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
52     /**
53      * Creates new instance of AppInfoCollector
54      * @param repositoryFolder central/cache repository root
55      * @param destFolder destination folder in which files are collected
56      */

57     public AppInfoCollector(String JavaDoc repositoryFolder, String JavaDoc destFolder) {
58         this.destFolder = destFolder;
59         this.repositoryFolder = repositoryFolder;
60         this.config = config;
61     }
62    
63     /**
64      * Captures applications deployment descriptors and generated files
65      * @throws DiagnosticException
66      */

67     public Data capture() throws DiagnosticException {
68         WritableDataImpl dataImpl = new WritableDataImpl(DataType.APPL_INFO);
69         dataImpl.addChild(captureAppRelatedInfo(Constants.GENERATED_DIR));
70         dataImpl.addChild(
71                 captureAppRelatedInfo(Constants.APPLICATIONS_DIR,
72                 new DDFilter()));
73         return dataImpl;
74     }//capture
75

76     /**
77      * Captures files
78      * @param relativePath directory to be copied relative to central/cache
79      * repository
80      * @throw DiagnosticException
81      */

82     private Data captureAppRelatedInfo(String JavaDoc relativePath)
83     throws DiagnosticException {
84         try {
85             String JavaDoc sourceFolder = repositoryFolder + File.separator + relativePath;
86             String JavaDoc destFileObj = destFolder + File.separator + relativePath;
87             FileUtils.copyDir(sourceFolder, destFileObj, true);
88             return new FileData(destFileObj, DataType.APPL_INFO);
89         } catch(IOException JavaDoc ioe) {
90             logger.log(Level.WARNING, "diagnostic-service.copy_failed" ,
91                     new Object JavaDoc[]{relativePath, ioe.getMessage()});
92         }
93         return null;
94     }
95     
96     /**
97      * Capture filtered files
98      * @param relativePath directory to be copied relative to central/cache
99      * repository
100      * @param filter file name filter
101      */

102     private Data captureAppRelatedInfo(String JavaDoc relativePath,
103             FilenameFilter JavaDoc filter) throws DiagnosticException {
104         WritableDataImpl dataImpl = new WritableDataImpl(relativePath);
105         String JavaDoc sourceFolderName = repositoryFolder + File.separator + relativePath;
106    
107         File JavaDoc sourceFolder = new File JavaDoc(sourceFolderName);
108         String JavaDoc[] filteredChildren = null;
109         String JavaDoc[] children = null;
110
111         // Assumes that there is no further sub directory , if children
112
// satisfying the filter are found
113
if (filter != null) {
114             filteredChildren = sourceFolder.list(filter);
115          }
116          // Visit subfolders to search files with matching the filter
117

118         if (filteredChildren != null) {
119             if(filteredChildren.length == 0) {
120                 children = sourceFolder.list();
121                 for (int i = 0 ; i < children.length; i++) {
122                     String JavaDoc childName = relativePath + File.separator + children[i];
123                     String JavaDoc absoluteChildName = repositoryFolder + File.separator + childName;
124                     File JavaDoc child = new File JavaDoc(absoluteChildName);
125                     if (child.isDirectory())
126                         dataImpl.addChild(captureAppRelatedInfo(childName, filter));
127                 }
128             } else {
129                 for (int i = 0 ; i < filteredChildren.length; i++) {
130                     String JavaDoc childName = relativePath + File.separator +
131                             filteredChildren[i];
132                     String JavaDoc absoluteChildName = repositoryFolder +
133                             File.separator + childName;
134                     File JavaDoc child = new File JavaDoc(absoluteChildName);
135                     try {
136                         String JavaDoc dest = destFolder + File.separator + childName;
137                         FileUtils.copyFile(absoluteChildName, dest);
138                         dataImpl.addChild(new FileData(dest, DataType.APPL_INFO));
139                     }catch(IOException JavaDoc ioe) {
140                         logger.log(Level.WARNING, "diagnostic-service.copy_failed" ,
141                             new Object JavaDoc[]{absoluteChildName, ioe.getMessage()});
142                     }
143                 }// for
144
}//else
145
}//if
146
return dataImpl;
147     }//createFileDataObjects
148
}
149
Popular Tags