KickJava   Java API By Example, From Geeks To Geeks.

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


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.logging.Level JavaDoc;
27
28 import com.sun.enterprise.config.ConfigContextEvent;
29 import com.sun.enterprise.config.ConfigException;
30 import com.sun.enterprise.config.serverbeans.JmsHost;
31 import com.sun.enterprise.config.serverbeans.JmsService;
32 import com.sun.enterprise.config.serverbeans.ServerTags;
33 import com.sun.enterprise.config.serverbeans.validation.GenericValidator;
34 import com.sun.enterprise.config.serverbeans.validation.Result;
35 import com.sun.enterprise.config.serverbeans.validation.ValidationDescriptor;
36
37 /**
38    Custom Test for JMS Host Test which calls the Generic Validation before performing custom tests
39
40    @author Sreenivas Munnangi
41    @version 1.0
42 */

43
44 public class JmsHostTest extends GenericValidator {
45     
46     private static final char [] illegalChars = {'*', ',', ':'};
47
48     public JmsHostTest (ValidationDescriptor desc) {
49         super(desc);
50     }
51
52     public Result validate(ConfigContextEvent cce) {
53
54         Result result = super.validate(cce);
55         try {
56             if (cce.getChoice().equals(StaticTest.DELETE)){
57                 if (isReferencedByParent(cce)){
58                     result.failed(
59                         smh.getLocalString(
60                             getClass().getName() + ".cannotDeleteReferencedJmsHost",
61                             "Jms host (name={0}) can not be removed. It is referenced by its parent jms-service",
62                             new Object JavaDoc[]{getJmsHost(cce).getName()}));
63                 }
64             } else {
65                 if (cce.getName().equals(ServerTags.ADMIN_USER_NAME)) {
66                     validateAdminUserName((String JavaDoc) cce.getObject(), result);
67                 } else if (cce.getName().equals(ServerTags.ADMIN_PASSWORD)) {
68                     validateAdminUserPassword((String JavaDoc) cce.getObject(), result);
69                 }
70             }
71         } catch(Exception JavaDoc e) {
72             _logger.log(Level.FINE, "domainxmlverifier.exception", e);
73         }
74         return result;
75     }
76
77     private final boolean isReferencedByParent(final ConfigContextEvent cce) throws ConfigException {
78         final JmsHost host = getJmsHost(cce);
79         final JmsService parent = (JmsService) host.parent();
80         return parent.getDefaultJmsHost() != null && parent.getDefaultJmsHost().equals(host.getName());
81     }
82
83     private void validateAdminUserName(String JavaDoc name, Result result) {
84
85         if ((name == null) || (name.equals("")) || (name.length() < 1)) {
86             result.failed(smh.getLocalString(getClass().getName() +
87                                              ".blankOrNullString",
88                                              "Attribute {0} : cannot be null or blank",
89                                              new Object JavaDoc[] {ServerTags.ADMIN_USER_NAME}));
90             return;
91         }
92
93         if (hasIllegalChar(name.toCharArray(), illegalChars)) {
94             result.failed(smh.getLocalString(getClass().getName() +
95                                              ".stringHasIllegalChars",
96                                              "Attribute {0} cannot have any of illegal characters {1}",
97                                              new Object JavaDoc[] {ServerTags.ADMIN_USER_NAME, new String JavaDoc(illegalChars)}));
98         }
99     }
100
101     private void validateAdminUserPassword(String JavaDoc name, Result result) {
102
103         if ((name == null) || (name.equals("")) || (name.length() < 1)) {
104             result.failed(smh.getLocalString(getClass().getName() +
105                                              ".blankOrNullString",
106                                              "Attribute {0} : cannot be null or blank",
107                                              new Object JavaDoc[] {ServerTags.ADMIN_PASSWORD}));
108             return;
109         }
110
111         if (hasIllegalChar(name.toCharArray(), illegalChars)) {
112             result.failed(smh.getLocalString(getClass().getName() +
113                                              ".stringHasIllegalChars",
114                                              "Attribute {0} cannot have any of illegal characters {1}",
115                                              new Object JavaDoc[] {ServerTags.ADMIN_PASSWORD, new String JavaDoc(illegalChars)}));
116         }
117     }
118
119     private boolean hasIllegalChar (char[] inputCharArr, char[] illegalCharArr) {
120
121         int inputCharArrLen = inputCharArr.length;
122         int illegalCharArrLen = illegalCharArr.length;
123
124         for (int i=0; i<illegalCharArrLen; i++) {
125             for (int j=0; j<inputCharArrLen; j++) {
126                 if (inputCharArr[j] == illegalCharArr[i]) {
127                     return true;
128                 }
129             }
130         }
131
132         return false;
133     }
134
135     private final JmsHost getJmsHost(final ConfigContextEvent cce) throws ConfigException{
136         return (JmsHost) cce.getValidationTarget();
137     }
138     
139 }
140
Popular Tags