KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > tests > StaticTest


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  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31
32 package com.sun.enterprise.config.serverbeans.validation.tests;
33 import java.util.regex.Pattern JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.MalformedObjectNameException JavaDoc;
36
37 import com.sun.enterprise.config.serverbeans.validation.Result;
38 import com.sun.enterprise.config.ConfigContext;
39
40 import javax.xml.parsers.*;
41 import org.xml.sax.*;
42 import java.io.ByteArrayInputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.File JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46 import java.util.Vector JavaDoc;
47
48 // Logging
49
import java.util.logging.Logger JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import com.sun.logging.LogDomains;
52 import java.lang.StringBuffer JavaDoc;
53
54 import com.sun.enterprise.config.serverbeans.Config;
55 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
56 import java.net.UnknownHostException JavaDoc;
57 import java.net.InetAddress JavaDoc;
58
59
60
61
62
63 /* Utility functions
64  * Author : srini@sun.com
65  **/

66
67 public class StaticTest {
68     
69     /* Adding various constants to this file. This should remove the hard coded values that
70      * we are using in various tests.
71      */

72     
73     // Represents the ADD configbean request
74
public static final String JavaDoc ADD = "ADD";
75     // Represents the DELETE configbean request
76
public static final String JavaDoc DELETE = "DELETE";
77     // Represents the UPDATE configbean request
78
public static final String JavaDoc UPDATE = "UPDATE";
79     // Represents the SET configbean request
80
public static final String JavaDoc SET = "SET";
81     // Represents the VALIDATE configbean request
82
public static final String JavaDoc VALIDATE = "VALIDATE";
83     
84     // flag to check for file existence
85
public static boolean fileCheck = false;
86     // flag to check for classpath existence
87
public static boolean classPathCheck = false;
88     // flag to check for javaHome existence
89
public static boolean javaHomeCheck = false;
90     
91     public StaticTest() {
92     }
93     
94
95         /**
96          * Check the IP address syntax of the given address. If its
97          * invalid throw UnknownHostException. Ignore the address if
98          * its a token
99          */

100     public static void checkIPAddress(final String JavaDoc addr) throws UnknownHostException JavaDoc{
101         if (valueContainsTokenExpression (addr) || validSymbolicAddress(addr)){
102             return;
103         } else {
104             InetAddress.getByName(addr);
105         }
106     }
107
108         /**
109            Indicate if the given address is a symbolic
110            address. i.e. is "any", "inaddr_any" or "localhost", in any case.
111          */

112     private static boolean validSymbolicAddress(final String JavaDoc address){
113         return address.equalsIgnoreCase("ANY") ||
114         address.equalsIgnoreCase("INADDR_ANY") ||
115         address.equalsIgnoreCase("localhost");
116     }
117     
118         
119     // Method to get the reference to config
120
public static Config getConfig(ConfigContext context) {
121         Config mConfig=null;
122         try {
123             mConfig = ServerBeansFactory.getConfigBean(context);
124         } catch(Exception JavaDoc e) {
125         }
126         return mConfig;
127     }
128     
129     public static boolean isOptionsValid(String JavaDoc options) {
130         boolean flag = true;
131         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(options," ");
132         while(token.hasMoreTokens()) {
133               if(!token.nextToken().startsWith("-")) {
134                     flag = false;
135                     break;
136               }
137         }
138         return flag;
139     }
140     
141     public static boolean isClassPathValid(String JavaDoc path) {
142         boolean flag = true;
143         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(path, File.pathSeparator);
144         while(token.hasMoreTokens()) {
145             File JavaDoc f = new File JavaDoc(token.nextToken());
146             // User requires to validate the file path check for existence, else blank check
147
if(classPathCheck) {
148                 if(!f.exists()) {
149                     flag = false;
150                     break;
151                 }
152             }
153         }
154         return flag;
155     }
156     
157     /**
158      * Verifies that the given path is a valid java-home path.
159      *
160      * @param path The java-home path to be validated.
161      * @return boolean true if valid false if invalid
162      */

163     public static boolean isJavaHomeValid(String JavaDoc path) {
164         boolean flag = true;
165         if(javaHomeCheck) {
166             String JavaDoc jdkPath = path + File.separator + "bin" + File.separatorChar;
167             if(System.getProperty("os.name").startsWith("Win")) {
168                 jdkPath = jdkPath + "java.exe";
169             } else {
170                 jdkPath = jdkPath + "java";
171             }
172             StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(path, File.pathSeparator);
173             while(token.hasMoreTokens()) {
174                 File JavaDoc f = new File JavaDoc(token.nextToken());
175                 // User requires to validate the file path check for existence, else blank check
176
if(!f.exists()) {
177                     flag = false;
178                     break;
179                 }
180             }
181         }
182         return flag;
183     }
184     
185
186     public static Vector JavaDoc tokens(String JavaDoc value) {
187         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(value,",");
188         Vector JavaDoc test = new Vector JavaDoc();
189         while(token.hasMoreTokens())
190                   test.add(token.nextToken().trim());
191         return test;
192     }
193
194     public static boolean isIdInList(String JavaDoc list, String JavaDoc poolId)
195     {
196         return null != poolId && tokens(list+"").contains(poolId);
197     }
198     
199
200     
201     static void setJavaHomeCheck(boolean check) {
202         javaHomeCheck = true;
203     }
204     
205     public static boolean valueContainsTokenExpression(final Object JavaDoc value){
206         return null != value && value instanceof String JavaDoc && token_pattern.matcher((String JavaDoc) value).lookingAt();
207     }
208     
209     private static Pattern JavaDoc token_pattern = Pattern.compile("\\$\\{[^}]*}");
210
211         /**
212            Name of config reserved for DAS server
213         */

214     public static final String JavaDoc DAS_CONFIG_NAME = "server-config";
215
216         /**
217            Name of config used as a template
218          */

219     public static final String JavaDoc CONFIG_TEMPLATE_NAME = "default-config";
220     
221
222 }
223
Popular Tags