KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.parsers.*;
27 import org.xml.sax.*;
28 import java.io.ByteArrayInputStream JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31
32 import com.sun.enterprise.util.i18n.StringManager;
33
34 /**
35  * Validates the repository name. A repository name must be a
36  * - valid file name,
37  * - valid xml CDATA value &
38  * - valid javax.management.ObjectName property value.
39  */

40 public class RepositoryNameValidator extends StringValidator
41 {
42     private static final String JavaDoc VALID_CHAR =
43         "[^\\,\\/ \\&\\;\\`\\'\\\\\"\\|\\*\\!\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\:\\%]*";
44
45     private static final String JavaDoc IAS_NAME = "com.sun.appserv:name=";
46
47     private static final String JavaDoc XML_1 =
48         "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <xml>";
49
50     private static final String JavaDoc XML_2 = "</xml>";
51
52     /**
53      * i18n strings manager object
54      */

55     private static final StringManager strMgr =
56         StringManager.getManager(RepositoryNameValidator.class);
57
58     /**
59      * Constructs new RepositoryNameValidator object.
60      * @param name
61      */

62     public RepositoryNameValidator(String JavaDoc name)
63     {
64         super(name);
65     }
66
67     /**
68      * Validates the given value for the given entry. This method first invokes
69      * its superclass's validate method and then performs additional validations.
70      * @throws InvalidConfigException
71      */

72     public void validate(Object JavaDoc str) throws InvalidConfigException
73     {
74         super.validate(str);
75         checkValidName((String JavaDoc)str);
76         checkValidXmlToken((String JavaDoc)str);
77         checkValidObjectNameToken((String JavaDoc)str);
78     }
79
80     public void checkValidName(String JavaDoc name) throws InvalidConfigException
81     {
82         if (!name.matches(VALID_CHAR))
83         {
84             throw new InvalidConfigException(
85                 strMgr.getString("validator.invalid_value", getName(), name));
86         }
87     }
88
89     /**
90      * Implementation copied from
91      * com.sun.enterprise.admin.verifier.tests.StaticTest
92      */

93     public void checkValidXmlToken(String JavaDoc name) throws InvalidConfigException
94     {
95         try
96         {
97             //Construct a valid xml string
98
String JavaDoc xml = XML_1 + name + XML_2;
99             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(xml.getBytes());
100             InputSource is = new InputSource(bais);
101             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
102             dbf.setValidating(false);
103             DocumentBuilder db = dbf.newDocumentBuilder();
104             db.parse(is);
105         }
106         catch (Exception JavaDoc e)
107         {
108             throw new InvalidConfigException(
109                 strMgr.getString("validator.invalid_value", getName(), name));
110         }
111     }
112
113     public void checkValidObjectNameToken(String JavaDoc name)
114         throws InvalidConfigException
115     {
116         try
117         {
118             new ObjectName JavaDoc(IAS_NAME + name);
119         }
120         catch (Exception JavaDoc e)
121         {
122             throw new InvalidConfigException(
123                 strMgr.getString("validator.invalid_value", getName(), name));
124         }
125     }
126 }
127
Popular Tags