KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.util.LocalStringManagerImpl;
28 import com.sun.enterprise.config.serverbeans.validation.StringManagerHelper;
29 import com.sun.enterprise.config.serverbeans.validation.Result;
30
31 import com.sun.enterprise.admin.util.QuotedStringTokenizer;
32
33 public class JvmOptionsTest {
34
35     private static LocalStringManagerImpl smh =
36         StringManagerHelper.getLocalStringsManager();
37
38     private static final JvmOptionsTest instance = new JvmOptionsTest();
39
40     private JvmOptionsTest() {
41     }
42
43     public static void validateJvmOptions(String JavaDoc[] jvmOptions, Result result) {
44         try {
45             instance.checkForNullOptions(jvmOptions);
46             Set optionsSet = instance.tokenizeJvmOptions(jvmOptions);
47             instance.checkBeginWithDash(optionsSet);
48             instance.checkQuotes(optionsSet);
49         }
50         catch (Exception JavaDoc e) {
51             result.failed(e.getMessage());
52         }
53     }
54
55     private void checkForNullOptions(String JavaDoc[] options)
56         throws InvalidJvmOptionsException
57     {
58         if (null == options) {
59             throw new InvalidJvmOptionsException(getNullJvmOptionMsg());
60         }
61         for (int i = 0; i < options.length; i++) {
62             if (null == options[i]) {
63                 throw new InvalidJvmOptionsException(getNullJvmOptionMsg());
64             }
65         }
66     }
67
68     private void checkBeginWithDash(Set options)
69         throws InvalidJvmOptionsException
70     {
71         List invalidOptions = new ArrayList();
72         Iterator it = options.iterator();
73         while (it.hasNext()) {
74             String JavaDoc option = it.next().toString();
75             if (!option.startsWith("-") &&
76                 !(option.startsWith("\"-") && option.endsWith("\"")) )
77             {
78                 invalidOptions.add(option);
79             }
80         }
81         if (invalidOptions.size() > 0) {
82             throw new InvalidJvmOptionsException(
83                 getInvalidJvmOptionMsg(invalidOptions.toString()));
84         }
85     }
86
87     private void checkQuotes(Set options) throws InvalidJvmOptionsException
88     {
89         List invalidOptions = new ArrayList();
90         final Iterator it = options.iterator();
91         while (it.hasNext()) {
92             String JavaDoc option = it.next().toString();
93             if (!checkQuotes(option)) {
94                 invalidOptions.add(option);
95             }
96         }
97         if (invalidOptions.size() > 0) {
98             throw new InvalidJvmOptionsException(
99                 getIncorrectQuotesMsg(invalidOptions.toString()));
100         }
101     }
102
103     private boolean checkQuotes(String JavaDoc option)
104     {
105         int length = option.length();
106         int numQuotes = 0;
107         int index = 0;
108
109         while (index < length &&
110                 (index = option.indexOf('\"', index)) != -1)
111         {
112             numQuotes++;
113             index++;
114         }
115         return ((numQuotes % 2) == 0);
116     }
117
118     private Set tokenizeJvmOptions(String JavaDoc[] options) {
119         final Set optionsSet = new LinkedHashSet();
120         final String JavaDoc DELIM = " \t";
121         for (int i = 0; i < options.length; i++) {
122             QuotedStringTokenizer strTok = new QuotedStringTokenizer(
123                                                 options[i], DELIM);
124             while (strTok.hasMoreTokens()) {
125                 optionsSet.add(strTok.nextToken());
126             }
127         }
128         return Collections.unmodifiableSet(optionsSet);
129     }
130
131     private String JavaDoc getNullJvmOptionMsg() {
132         return smh.getLocalString(getClass().getName() + ".nullJvmOption",
133             "Null Jvm option", new Object JavaDoc[0]);
134     }
135
136     private String JavaDoc getInvalidJvmOptionMsg(String JavaDoc invalidOptions) {
137         return smh.getLocalString(getClass().getName() + ".invalidJvmOption",
138             "{0} - Invalid Jvm option. Option must begin with -",
139             new Object JavaDoc[] {invalidOptions});
140     }
141
142     private String JavaDoc getIncorrectQuotesMsg(String JavaDoc invalidOptions) {
143         return smh.getLocalString(getClass().getName() + ".incorrectQuotesInJvmOption",
144             "{0} - Invalid Jvm option. Check quotes",
145             new Object JavaDoc[] {invalidOptions});
146     }
147
148     private static final class InvalidJvmOptionsException extends Exception JavaDoc {
149         InvalidJvmOptionsException(String JavaDoc msg) {
150             super(msg);
151         }
152     }
153 }
154
155
Popular Tags