KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > SystemAppScanner


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 package com.sun.enterprise.server;
25
26 import java.io.File JavaDoc;
27 import java.io.FileFilter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
31 import java.util.jar.Attributes JavaDoc;
32
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import com.sun.logging.LogDomains;
36
37 import com.sun.enterprise.server.ManagerFactory;
38 import com.sun.enterprise.server.ApplicationManager;
39 import com.sun.enterprise.server.StandAloneConnectorModulesManager;
40 import com.sun.enterprise.server.StandAloneEJBModulesManager;
41 import com.sun.enterprise.deployment.autodeploy.AutoDeployConstants;
42 import com.sun.enterprise.deployment.autodeploy.AutoDeployDirectoryScanner;
43
44 /**
45  * SystemAppScanner takes the system directory and scans the archives based on
46  * Application-Type specified in the manifest file of archive.
47  * TargetFileFilter filters the archives based on the targetType and
48  * Application-Type
49  * @author Sandhya E
50  */

51
52 public class SystemAppScanner extends AutoDeployDirectoryScanner {
53
54     private String JavaDoc targetType;
55     
56     public SystemAppScanner(String JavaDoc targetType){
57         this.targetType = targetType;
58     }
59     
60     protected File JavaDoc[] getListOfFiles(File JavaDoc dir, boolean includeSubDir) {
61         return dir.listFiles(new TargetFileFilter(targetType));
62     }
63     
64 }
65
66 class TargetFileFilter implements FileFilter JavaDoc{
67     private static final Logger JavaDoc sLogger= LogDomains.getLogger(LogDomains.CORE_LOGGER);
68     
69     String JavaDoc targetType = null;
70     
71     TargetFileFilter(String JavaDoc targetType){
72         this.targetType = targetType;
73     }
74     
75      
76     
77     /**
78      * Accept method implementation of FileFilter, specific for app/modules,/br>
79      *its not a directory/br>
80      *write/read permission is provided </br>
81      *valid name </br>
82      *valid type(ear/jar/war/rar etc) </br>
83      *
84      */

85     public boolean accept(File JavaDoc f){
86         boolean flag=false;
87         String JavaDoc name= f.getName();
88         String JavaDoc fileType = null;
89         int lastIndex=-1;
90         
91         if(name !=null && name.length() >0){
92             lastIndex = name.lastIndexOf('.');
93             try{
94                 if (lastIndex >= 0)
95                     fileType = name.substring(lastIndex + 1);
96                 if(!f.isDirectory() && f.canRead())
97                 {
98                     name=name.substring(0,lastIndex); //valid name
99
if(name !=null && name.length() >0)
100                        flag=true;
101                 }
102             }catch(SecurityException JavaDoc se){
103                 sLogger.log(Level.WARNING, "SecurityException occured while accessing :" +f.getName());
104                 //ignore the file, and lets just not accept its as deployable component file
105
}catch(Exception JavaDoc e){
106                 sLogger.log(Level.WARNING, "Exception occured while accessing :" +f.getName());
107                 //ignore the file, and lets just not accept its as deployable component file
108
}
109         }
110         if(flag && isApplicableToTarget(f) && !(isRegistered(name,fileType))) {
111             sLogger.log(Level.FINE,"Selecting file ["+ f.getAbsolutePath()+"] for autodeployment");
112             return true;
113         }else
114             return false;
115         
116     }
117
118     private boolean isApplicableToTarget(File JavaDoc f) {
119             JarFile JavaDoc j = null;
120         try{
121             j = new JarFile JavaDoc(f);
122             Manifest JavaDoc m = j.getManifest();
123             Attributes JavaDoc a = m.getMainAttributes();
124             String JavaDoc appType = a.getValue(Constants.APPLICATION_TYPE);
125             if(appType == null) appType = Constants.USER;
126             if(targetType.equals(Constants.TARGET_TYPE_ADMIN)){
127                 if(appType.equals(Constants.SYSTEM_ADMIN) || appType.equals(Constants.SYSTEM_ALL))
128                     return true;
129                 else
130                     return false;
131             }else if(targetType.equals(Constants.TARGET_TYPE_INSTANCE)){
132                 if(appType.equals(Constants.SYSTEM_INSTANCE) || appType.equals(Constants.SYSTEM_ALL))
133                     return true;
134                 else
135                     return false;
136             }
137             return false;
138         }catch(Exception JavaDoc e) {
139             sLogger.log(Level.WARNING, "Exception occured while accessing :" +f.getName());
140             return false;
141         } finally {
142                     if (j != null) {
143                         try {
144                             j.close();
145                         } catch (IOException JavaDoc ioe) {
146                             sLogger.log(Level.WARNING, "Error closing jar file after checking for autodeploy", ioe);
147                         }
148                     }
149                 }
150     }
151
152         /**
153          * Checks if an app is registered. Takes the registration
154          * name and type of the archive
155          * Type of the archive is ear/war/jar/rar
156          * @return true if app is registered
157          **/

158         private boolean isRegistered(String JavaDoc name, String JavaDoc type)
159         {
160             try{
161             
162                 if(type.equals(AutoDeployConstants.EAR_EXTENSION)) {
163                 
164                     ApplicationManager amgr =
165                                 ManagerFactory.getApplicationManager();
166                     return amgr.isRegistered(name);
167                     
168                 }else if(type.equals(AutoDeployConstants.JAR_EXTENSION)) {
169                 
170                     StandAloneEJBModulesManager emgr =
171                                 ManagerFactory.getSAEJBModulesManager();
172                     return emgr.isRegistered(name);
173                     
174                 }else if(type.equals(AutoDeployConstants.RAR_EXTENSION)) {
175                 
176                     StandAloneConnectorModulesManager cmgr =
177                                 ManagerFactory.getSAConnectorModulesManager();
178                     return cmgr.isRegistered(name);
179                     
180                 }else if(type.equals(AutoDeployConstants.WAR_EXTENSION)) {
181                 }
182                 
183             }catch(Exception JavaDoc e){
184                 sLogger.log(Level.FINE, "error_occured_in_isreg",e);
185             }
186             return false;
187         }
188
189 }
190         
191
Popular Tags