KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > FileValidator


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.admin.servermgmt;
25
26 import java.io.File JavaDoc;
27
28 import com.sun.enterprise.util.io.FileUtils;
29 import com.sun.enterprise.util.i18n.StringManager;
30
31 /**
32  * This class performs the file related validations such as
33  * <ul>
34  * existence of the file
35  * read, write & execute permissions,
36  * whether the file is a directory or a file
37  * </ul>
38  */

39 public class FileValidator extends Validator
40 {
41     /**
42      * The valid constraint set.
43      */

44     public static String JavaDoc validConstraints = "drwx";
45
46     /**
47      * i18n strings manager object
48      */

49     private static final StringManager strMgr =
50         StringManager.getManager(FileValidator.class);
51
52     /**
53      * The current constraint set.
54      */

55     private String JavaDoc constraints = "r";
56
57     /**
58      * Constructs a new FileValidator object.
59      * @param name The name of the entity that will be validated. This name is
60      * used in the error message.
61      * @param constraints The constaint set that will be checked for any given
62      * file during validation.
63      */

64     public FileValidator(String JavaDoc name, String JavaDoc constraints)
65     {
66         super(name, java.lang.String JavaDoc.class);
67
68         if (isValidConstraints(constraints))
69         {
70             this.constraints = constraints;
71         }
72     }
73
74     /**
75      * @return Returns the current constraint set.
76      */

77     public String JavaDoc getConstraints()
78     {
79         return constraints;
80     }
81
82     /**
83      * Sets the current constraint set to the given set if it is a valid
84      * constriant set.
85      */

86     public String JavaDoc setConstraints(String JavaDoc constraints)
87     {
88         if (isValidConstraints(constraints))
89         {
90             this.constraints = constraints;
91         }
92         return this.constraints;
93     }
94
95     /**
96      * Validates the given File.
97      * @param str Must be the absolute path of the File that will be validated.
98      * @throws InvalidConfigException
99      */

100     public void validate(Object JavaDoc str) throws InvalidConfigException
101     {
102         super.validate(str);
103         new StringValidator(getName()).validate(str);
104         File JavaDoc f = new File JavaDoc((String JavaDoc)str);
105         validateConstraints(f);
106     }
107
108     /**
109      * Validates the given File against the current constraint set.
110      */

111     void validateConstraints(File JavaDoc file) throws InvalidConfigException
112     {
113         final File JavaDoc f = FileUtils.safeGetCanonicalFile(file);
114         final String JavaDoc constriants = getConstraints();
115         char[] ca = constriants.toCharArray();
116         for (int i = 0; i < ca.length; i++)
117         {
118             switch (ca[i])
119             {
120                 case 'r' :
121                     if (!f.canRead())
122                     {
123                         throw new InvalidConfigException(
124                             strMgr.getString("fileValidator.no_read",
125                                              f.getAbsolutePath()));
126                     }
127                     break;
128                 case 'w' :
129                     if (!f.canWrite())
130                     {
131                         throw new InvalidConfigException(
132                             strMgr.getString("fileValidator.no_write",
133                                              f.getAbsolutePath()));
134                     }
135                     break;
136                 case 'd' :
137                     if (!f.isDirectory())
138                     {
139                         throw new InvalidConfigException(
140                             strMgr.getString("fileValidator.not_a_dir",
141                                              f.getAbsolutePath()));
142                     }
143                     break;
144                 case 'x' :
145                     //do what
146
break;
147                 default :
148                     break;
149             }
150         }
151     }
152
153     /**
154      * Checks if the given constraint set is a subset of valid constraint set.
155      * @param constraints
156      * @return Returns true if the given constraint set is subset or equal to
157      * the valid constraint set - "drwx".
158      */

159     boolean isValidConstraints(String JavaDoc constraints)
160     {
161         if (constraints == null) { return false; }
162         final int length = constraints.length();
163         if ((length == 0) || (length > 4)) { return false; }
164         boolean isValid = true;
165         for (int i = 0; i < length; i++)
166         {
167             char ch = constraints.charAt(i);
168             switch (ch)
169             {
170                 case 'r' :
171                 case 'w' :
172                 case 'x' :
173                 case 'd' :
174                     continue;
175                 default :
176                     isValid = false;
177                     break;
178             }
179         }
180         return isValid;
181     }
182 }
183
Popular Tags