KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.util.i18n.StringManager;
27
28 /**
29  * Base class for all domain config validators. Validates the non-null ness
30  * of a domain config entry and its type.
31  */

32 public class Validator
33 {
34     /**
35      * i18n strings manager object
36      */

37     private static final StringManager strMgr =
38         StringManager.getManager(Validator.class);
39
40     /**
41      * The accepted type of an entry.
42      */

43     private final Class JavaDoc type;
44
45     /**
46      * The name of an entry that is used in case of validation error.
47      */

48     private final String JavaDoc name;
49
50     /**
51      * Constructs new Validator object.
52      * @param name Name of an entry that is used in case of validation errors.
53      * If the name is null "" is used instead.
54      * @param type
55      */

56     public Validator(String JavaDoc name, Class JavaDoc type)
57     {
58         this.name = (name != null) ? name : "";
59         this.type = (type != null) ? type : java.lang.Object JavaDoc.class;
60     }
61
62     /**
63      * Returns the name of the entry.
64      */

65     public String JavaDoc getName()
66     {
67         return name;
68     }
69
70     /**
71      * Checks the validity of the given value for the entry. This method does
72      * basic checks such as null ness & type.
73      * @param obj
74      * @Throws InvalidConfigException
75      */

76     public void validate(Object JavaDoc obj) throws InvalidConfigException
77     {
78         if (obj == null)
79         {
80             throw new InvalidConfigException(
81                 strMgr.getString("validator.invalid_value", getName(), null));
82         }
83         Class JavaDoc c = obj.getClass();
84         if (!type.isAssignableFrom(c))
85         {
86             throw new InvalidConfigException(
87                 strMgr.getString("validator.invalid_type",
88                     getName(), type.getName(), c.getName()));
89         }
90     }
91 }
92
Popular Tags