KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > JvmOptionsHelper


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  * $Id: JvmOptionsHelper.java,v 1.2 2005/12/25 03:53:09 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.util;
29
30 import java.util.*;
31
32 import com.sun.enterprise.util.i18n.StringManager;
33
34 import com.sun.enterprise.admin.util.QuotedStringTokenizer;
35
36 /**
37  * A helper class to facilitate the add/delete/get jvm options.
38  */

39 final public class JvmOptionsHelper
40 {
41     /**
42      * First in the chain of responsibility
43      */

44     private final JvmOptionsElement head;
45
46     /*
47      * Constructs a new JvmOptionsHelper object. Stores the options as a chain of
48      * JvmOptionElements.
49      * @param options
50      * @throws InvalidJvmOptionException If any option is invalid. For example,
51      * an option that does not start with '-'.
52      * @throws IllegalArgumentException If the options param is null.
53      */

54     public JvmOptionsHelper(String JavaDoc[] options) throws InvalidJvmOptionException
55     {
56        if (null == options) {
57             throw new IllegalArgumentException JavaDoc();
58         }
59         if(options.length == 0) {
60             head = new JvmOptionsElement("");
61         } else {
62             head = new JvmOptionsElement(options[0]);
63         }
64         JvmOptionsElement current = head;
65         for (int i = 1; i < options.length; i++) {
66             JvmOptionsElement next = new JvmOptionsElement(options[i]);
67             current.setNext(next);
68             current = next;
69         }
70     }
71
72     /**
73      * Adds the options to its current set. Omits options that already exist.
74      * Note :- This method depends on the exact String comparision of the
75      * options. Hence an option "a=b c=d" will be added even if individual
76      * options already exist.
77      * @param options
78      * @return Returns an array of options that <bold>could not</bold> be added.
79      * The array will be atleast of 0 length. An array of length > 0 indicates
80      * that some options haven't been added successfully.
81      * @throws InvalidJvmOptionException If any option is invalid. For example,
82      * an option that does not start with '-'.
83      * @throws IllegalArgumentException If options param is null.
84      */

85     public String JavaDoc[] addJvmOptions(String JavaDoc[] options) throws InvalidJvmOptionException
86     {
87        if (null == options)
88        {
89            throw new IllegalArgumentException JavaDoc();
90        }
91        final Set alreadyExist = new HashSet();
92        JvmOptionsElement last = last();
93        for (int i = 0; i < options.length; i++)
94        {
95            if (!head.hasOption(options[i]))
96            {
97                JvmOptionsElement x = new JvmOptionsElement(options[i]);
98                last.setNext(x);
99                last = x;
100            }
101            else
102            {
103                alreadyExist.add(options[i]);
104            }
105        }
106        return toStringArray(alreadyExist);
107     }
108
109     /**
110      * Returns the last JvmOptionsElement in the chain of responsibility.
111      */

112     public JvmOptionsElement last()
113     {
114         JvmOptionsElement current = head;
115         while (current.hasNext())
116         {
117             current = current.next();
118         }
119         return current;
120     }
121
122     /**
123      * Deletes the options from its current set.
124      * @param options
125      * @return Returns an array of options that <bold>could not</bold> be
126      * deleted. The array will be atleast of 0 length. An array of
127      * length > 0 indicates that some options haven't been deleted successfully.
128      * @throws IllegalArgumentException If options param is null.
129      */

130     public String JavaDoc[] deleteJvmOptions(String JavaDoc[] options)
131     {
132        if (null == options)
133        {
134            throw new IllegalArgumentException JavaDoc();
135        }
136        //The following tokenizes the options.
137
// options = tokenize(options);
138
final Set donotExist = new HashSet();
139        for (int i = 0; i < options.length; i++)
140        {
141            if (!head.deleteJvmOption(options[i]))
142            {
143                donotExist.add(options[i]);
144            }
145        }
146        return toStringArray(donotExist);
147     }
148
149     /**
150      * Returns the current set of Jvm options.
151      */

152     public String JavaDoc[] getJvmOptionsAsStoredInXml()
153     {
154         Set s = new LinkedHashSet();
155         JvmOptionsElement current = head;
156         while (!JvmOptionsElement.isLast(current))
157         {
158             String JavaDoc options = current.getJvmOptionsAsStoredInXml();
159             if ((options != null) && (options.length() > 0))
160             {
161                 s.add(options);
162             }
163             current = current.next();
164         }
165         return toStringArray(s);
166     }
167
168     /**
169      * Returns the current set of Jvm options.
170      */

171     public String JavaDoc[] getJvmOptions()
172     {
173         Set s = new LinkedHashSet();
174         JvmOptionsElement current = head;
175         while (!JvmOptionsElement.isLast(current))
176         {
177             ArrayList options = current.getJvmOptions();
178             if ((options != null) && (options.size() > 0))
179             {
180                 s.addAll(options);
181             }
182             current = current.next();
183         }
184         return toStringArray(s);
185     }
186
187     public static String JavaDoc[] toStringArray(Collection c)
188     {
189         final String JavaDoc[] s = new String JavaDoc[c.size()];
190         final Iterator it = c.iterator();
191         int i = 0;
192         while (it.hasNext())
193         {
194             s[i] = (String JavaDoc)it.next();
195             i++;
196         }
197         return s;
198     }
199
200     private static String JavaDoc[] tokenize(String JavaDoc[] options)
201     {
202         Set s = new LinkedHashSet();
203         //4923404
204
for (int i = 0; i < options.length; i++)
205         {
206             QuotedStringTokenizer strTok = new QuotedStringTokenizer(
207                                                 options[i], " \t");
208             while (strTok.hasMoreTokens())
209             {
210                 s.add(strTok.nextToken());
211             }
212         }
213         //4923404
214
return toStringArray(s);
215     }
216 }
217
218 /**
219  * Represents individual handlers in the chain of responsibility. Executes the
220  * methods such as hasNext(), deleteJvmOption(), hasOption() on its options set
221  * and then invokes the next in the chain.
222  */

223 class JvmOptionsElement
224 {
225     private static final StringManager strMgr =
226         StringManager.getManager(JvmOptionsElement.class);
227
228     /**
229      * Used to indicate the last element in the chain.
230      */

231     private static final JvmOptionsElement DEFAULT =
232         new JvmOptionsElement() {
233             boolean hasOption(String JavaDoc option) { return false; }
234             boolean deleteJvmOption(String JavaDoc option) { return false; }
235             String JavaDoc getJvmOptionsAsStoredInXml() { return ""; }
236             ArrayList getJvmOptions() { return new ArrayList(); }
237             boolean hasNext() { return false; }
238             void setNext() { throw new UnsupportedOperationException JavaDoc(); }
239         };
240
241     private final Set jvmOptions = new LinkedHashSet();
242
243     private JvmOptionsElement next;
244
245     static boolean isLast(JvmOptionsElement e)
246     {
247         return (e == DEFAULT);
248     }
249
250     /**
251      * private default ctor. To be used only within the scope of this class.
252      */

253     private JvmOptionsElement()
254     {
255     }
256
257     /**
258      * Constructs a new JvmOptionsElement object.
259      * @param options Tokenizes the options and stores them as a Set.
260      * Spaces are used as delimiter.
261      * @throws InvalidJvmOptionException If any option is invalid. For example,
262      * an option that does not start with '-'.
263      * @throws IllegalArgumentException If options is null.
264      */

265     JvmOptionsElement(String JavaDoc options) throws InvalidJvmOptionException
266     {
267         if (null == options)
268         {
269             throw new IllegalArgumentException JavaDoc();
270         }
271         //4923404
272
QuotedStringTokenizer strTok = new QuotedStringTokenizer(options, " \t");
273         //4923404
274
while (strTok.hasMoreTokens())
275         {
276             String JavaDoc option = strTok.nextToken();
277             checkValidOption(option);
278             jvmOptions.add(option);
279         }
280         next = DEFAULT;
281     }
282
283     /**
284      * Sets the next element.
285      * @throws IllegalArgumentException If element is null.
286      */

287     void setNext(JvmOptionsElement element)
288     {
289         if (null == element)
290         {
291             throw new IllegalArgumentException JavaDoc();
292         }
293         this.next = element;
294     }
295
296     boolean hasNext()
297     {
298         return (DEFAULT != next);
299     }
300
301     JvmOptionsElement next() { return next; }
302
303     boolean hasOption(String JavaDoc option)
304     {
305         boolean exists = jvmOptions.contains(option);
306         if (!exists) { exists = next.hasOption(option); }
307         return exists;
308     }
309
310     /**
311      * Deletes the option from its set of jvm options and then invokes the next
312      * in the chain.
313      * @param option
314      * @return Returns true if the option exists in at least one element in the
315      * chain.
316      */

317     boolean deleteJvmOption(String JavaDoc option)
318     {
319         boolean b1 = jvmOptions.remove(option);
320         boolean b2 = next().deleteJvmOption(option);
321         return (b1 || b2);
322     }
323
324     /**
325      */

326     String JavaDoc getJvmOptionsAsStoredInXml()
327     {
328         if (jvmOptions.size() == 0) { return ""; }
329         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
330         final Iterator it = jvmOptions.iterator();
331         while (it.hasNext())
332         {
333             sb.append(it.next());
334             if (it.hasNext()) { sb.append(SEP); }
335         }
336         return sb.toString();
337     }
338
339     /**
340      */

341     ArrayList getJvmOptions()
342     {
343         final ArrayList arr = new ArrayList();
344         if (jvmOptions.size() != 0)
345         {
346             final Iterator it = jvmOptions.iterator();
347             while (it.hasNext())
348             {
349                 String JavaDoc nextOption = (String JavaDoc)it.next();
350                 if(nextOption.length()>2 && nextOption.startsWith("\"") && nextOption.endsWith("\""))
351                 {
352                      nextOption = nextOption.substring(1, nextOption.length()-1);
353                 }
354                 arr.add(nextOption);
355             }
356         }
357         return arr;
358     }
359
360     static final char SEP = ' ';
361
362     public String JavaDoc toString()
363     {
364         return getJvmOptionsAsStoredInXml();
365     }
366
367     public boolean equals(Object JavaDoc o)
368     {
369         if (this == o) { return true; }
370         boolean isEqual = false;
371         if (o instanceof JvmOptionsElement)
372         {
373             JvmOptionsElement that = (JvmOptionsElement)o;
374             return this.jvmOptions.containsAll(that.jvmOptions);
375         }
376         return isEqual;
377     }
378
379     private void checkValidOption(String JavaDoc option)
380         throws InvalidJvmOptionException
381     {
382         if ((null == option) || option.equals(""))
383         {
384             throw new InvalidJvmOptionException(strMgr.getString(
385                 "jvmOptions.invalid_option", option));
386         }
387         if (!option.startsWith("-") &&
388             !(option.startsWith("\"-") && option.endsWith("\"")))
389         {
390             throw new InvalidJvmOptionException(strMgr.getString(
391                 "jvmOptions.no_dash", option));
392         }
393         //4923404
394
checkQuotes(option);
395         //4923404
396
}
397
398     //4923404
399
void checkQuotes(String JavaDoc option) throws InvalidJvmOptionException
400     {
401         int length = option.length();
402         int numQuotes = 0;
403         int index = 0;
404
405         while (index < length &&
406                 (index = option.indexOf('\"', index)) != -1)
407         {
408             numQuotes++;
409             index++;
410         }
411         if ((numQuotes % 2) != 0)
412         {
413             throw new InvalidJvmOptionException(strMgr.getString(
414                 "jvmOptions.incorrect_quotes", option));
415         }
416     }
417     //4923404
418
}
419
Popular Tags