KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.serverbeans.validation.tests;
25
26 import java.util.*;
27 import java.util.logging.Level JavaDoc;
28 import java.io.File JavaDoc;
29
30 import com.sun.enterprise.config.serverbeans.validation.GenericValidator;
31 import com.sun.enterprise.config.serverbeans.validation.ValidationDescriptor;
32 import com.sun.enterprise.config.serverbeans.validation.Result;
33 import com.sun.enterprise.config.serverbeans.ServerTags;
34 import com.sun.enterprise.config.serverbeans.JavaConfig;
35 import com.sun.enterprise.config.serverbeans.validation.AttrClassName;
36 import com.sun.enterprise.config.serverbeans.validation.tests.StaticTest;
37
38 import com.sun.enterprise.config.ConfigBean;
39 import com.sun.enterprise.config.ConfigContextEvent;
40 import com.sun.enterprise.config.ConfigException;
41
42 /**
43     Custom Test for Java Config Test which calls the Generic Validation before performing custom tests
44
45     @author Srinivas Krishnan
46     @version 2.0
47 */

48
49 public class JavaConfigTest extends GenericValidator {
50     
51     public JavaConfigTest(ValidationDescriptor desc) {
52         super(desc);
53     }
54     
55     public Result validate(ConfigContextEvent cce) {
56         Result result = super.validate(cce); // Before doing custom validation do basic validation
57

58         if(cce.getChoice().equals(StaticTest.VALIDATE)) {
59             StaticTest.setJavaHomeCheck(true);
60             JavaConfig javaConfig = (JavaConfig) cce.getObject();
61             if(javaConfig.getJavaHome().indexOf("${")<0)
62                validateAttribute(ServerTags.JAVA_HOME, javaConfig.getJavaHome(), result);
63             validateAttribute(ServerTags.DEBUG_OPTIONS, javaConfig.getDebugOptions(), result);
64             validateAttribute(ServerTags.RMIC_OPTIONS, javaConfig.getRmicOptions(), result);
65             validateAttribute(ServerTags.JAVAC_OPTIONS, javaConfig.getJavacOptions(), result);
66             
67             validateAttribute(ServerTags.CLASSPATH_PREFIX, javaConfig.getClasspathPrefix(), result);
68             validateAttribute(ServerTags.CLASSPATH_SUFFIX, javaConfig.getClasspathSuffix(), result);
69             validateAttribute(ServerTags.NATIVE_LIBRARY_PATH_PREFIX, javaConfig.getNativeLibraryPathPrefix(), result);
70             validateAttribute(ServerTags.NATIVE_LIBRARY_PATH_SUFFIX, javaConfig.getNativeLibraryPathSuffix(), result);
71             validateAttribute(ServerTags.BYTECODE_PREPROCESSORS, javaConfig.getBytecodePreprocessors(), result);
72             JvmOptionsTest.validateJvmOptions(javaConfig.getJvmOptions(), result);
73         }
74         
75         if(cce.getChoice().equals(StaticTest.UPDATE)) {
76             validateAttribute(cce.getName(), (String JavaDoc) cce.getObject(), result);
77         }
78         else if(cce.getChoice().equals(StaticTest.SET)) {
79             final String JavaDoc name = cce.getName();
80            /*
81             * JvmOptions is an element in domain xml. Hence for JvmOptions we
82             * need to use camelized name even thouth it is treated as an
83             * attribute.
84             */

85             if (name.equals("JvmOptions")) {
86                 JvmOptionsTest.validateJvmOptions((String JavaDoc[])cce.getObject(), result);
87             }
88         }
89         else {
90             //Do nothing?
91
}
92         return result;
93     }
94     
95     public void validateAttribute(String JavaDoc name, String JavaDoc value, Result result) {
96         
97         if(value == null || value.equals(""))
98             return;
99         if(name.equals(ServerTags.DEBUG_OPTIONS)) {
100             if(!StaticTest.isOptionsValid(value))
101                 result.failed(smh.getLocalString(getClass().getName() + ".invalidDebugOption",
102                     "{0} : Invalid Java Debug options should start with -", new Object JavaDoc[]{value}));
103                    
104             validateRunJDWP(value, result);
105         }
106         if(name.equals(ServerTags.RMIC_OPTIONS)) {
107             if(!StaticTest.isOptionsValid(value))
108                 result.failed(smh.getLocalString(getClass().getName() + ".invalidRmicOption",
109                     "{0} : Invalid RMIC options should start with -", new Object JavaDoc[]{value}));
110         }
111         if(name.equals(ServerTags.JAVAC_OPTIONS)) {
112             if(!StaticTest.isOptionsValid(value))
113                 result.failed(smh.getLocalString(getClass().getName() + ".invalidJavacOptions",
114                    "{0} : Invalid javac options should start with -", new Object JavaDoc[]{value}));
115         }
116         
117         if(name.equals(ServerTags.CLASSPATH_PREFIX) || name.equals(ServerTags.CLASSPATH_SUFFIX) ||
118                 name.equals(ServerTags.NATIVE_LIBRARY_PATH_PREFIX) ||
119                          name.equals(ServerTags.NATIVE_LIBRARY_PATH_SUFFIX)) {
120             if(!StaticTest.isClassPathValid(value))
121                 result.failed(smh.getLocalString(getClass().getName() + ".invalidClasspath",
122                    "{0} Classpath contains invalid path : Check the path", new Object JavaDoc[]{name}));
123         }
124         if(name.equals(ServerTags.JAVA_HOME)) {
125             if(!StaticTest.isJavaHomeValid(value))
126                 result.failed(smh.getLocalString(getClass().getName() + ".invalidJavaHome",
127                    "Warning : (java-home={0}), JDK does not exists in java home", new Object JavaDoc[]{value}));
128         }
129         if(name.equals(ServerTags.BYTECODE_PREPROCESSORS))
130         {
131             StringTokenizer tokens = new StringTokenizer(value,".");
132             while(tokens.hasMoreTokens())
133             {
134                String JavaDoc token = tokens.nextToken().trim();
135                if(!AttrClassName.isValidClassName(token))
136                {
137                     result.failed(smh.getLocalString(getClass().getName() + ".invalidClassName",
138                    "Attribute (bytecode-preprocessors={0}), {1} Invalid Class Name", new Object JavaDoc[]{value,token}));
139                }
140             }
141         }
142     }
143     
144     public void validateRunJDWP(String JavaDoc value, Result result) {
145         
146            // code to validate the debug option -Xrunjdwp:transport=dt_socket,address=<value>,server=<value>,suspend=<value>
147
String JavaDoc runjdwp = value.substring(value.indexOf("jdwp:")+5);
148             int index=0;
149             try {
150                 if(runjdwp != null)
151                     index = runjdwp.indexOf("-X");
152             } catch(Exception JavaDoc e) {
153             }
154             String JavaDoc debugStr=null;
155             if(index > 0)
156                 debugStr = runjdwp.substring(0,index);
157             else
158                 debugStr = runjdwp;
159             if(debugStr != null) {
160                 String JavaDoc[] tokens = debugStr.split(",");
161                 for(int i=0;i<tokens.length;i++) {
162                     if(tokens[i].indexOf("=") < 0) {
163                             result.failed(smh.getLocalString(getClass().getName() + ".invalidJDWPDebugOption",
164                                   "{0} : Invalid -Xrunjdwp option, please check syntax", new Object JavaDoc[]{debugStr}));
165                             break;
166                     }
167                 }
168             }
169     }
170 }
171
Popular Tags