KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.UnknownHostException JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.logging.Level JavaDoc;
32
33 import com.sun.enterprise.config.ConfigContext;
34 import com.sun.enterprise.config.ConfigBean;
35 import com.sun.enterprise.config.ConfigContextEvent;
36 import com.sun.enterprise.config.ConfigException;
37 import com.sun.enterprise.config.serverbeans.Config;
38 import com.sun.enterprise.config.serverbeans.HttpListener;
39 import com.sun.enterprise.config.serverbeans.HttpService;
40 import com.sun.enterprise.config.serverbeans.ServerTags;
41 import com.sun.enterprise.config.serverbeans.VirtualServer;
42 import com.sun.enterprise.config.serverbeans.validation.GenericValidator;
43 import com.sun.enterprise.config.serverbeans.validation.Result;
44 import com.sun.enterprise.config.serverbeans.validation.ValidationDescriptor;
45 import java.util.Collections JavaDoc;
46 import java.util.Arrays JavaDoc;
47 import java.util.Iterator JavaDoc;
48
49
50 /**
51    Custom Test for Virtual Server Test which calls the Generic Validation before performing custom tests
52
53    @author Srinivas Krishnan
54    @version 2.0
55 */

56
57 public class VirtualServerTest extends GenericValidator {
58     
59     
60     public VirtualServerTest(ValidationDescriptor desc) {
61         super(desc);
62     }
63     
64     public Result validate(ConfigContextEvent cce) {
65         Result result = super.validate(cce); // Before doing custom validation do basic validation
66
try{
67             if(cce.getChoice().equals(StaticTest.ADD) || cce.getChoice().equals(StaticTest.VALIDATE)) {
68                 ConfigContext context = cce.getConfigContext();
69                 VirtualServer virtual = (VirtualServer) cce.getValidationTarget();
70            
71                     // For an ADD operation the virtual server is being
72
// added to an http-service, which is the
73
// configcontextevents class object. Therefore the config is
74
// the parent of the class object:
75
final Config config = (Config) ((HttpService) cce.getClassObject()).parent();
76                 validateAttribute(ServerTags.HTTP_LISTENERS,virtual.getHttpListeners(),config, result);
77                 validateAttribute(ServerTags.HOSTS,virtual.getHosts(),config, result);
78             } else if(cce.getChoice().equals("UPDATE")) {
79                     // For an UPDATE operation the class object is the
80
// virtual server whose attributes are being
81
// updated. Therefore the config is the parent of the
82
// parent of this object:
83
final VirtualServer vs = (VirtualServer) cce.getClassObject();
84                 final Config config = (Config) vs.parent().parent();
85                 String JavaDoc name = cce.getName();
86                 String JavaDoc value = (String JavaDoc) cce.getObject();
87                 validateAttribute(name,value,config, result);
88                     // IF the state is being turned off then this can only
89
// occur if there's no http-listener which has this
90
// virtual server as a default and which is enabled.
91
if (name.equals(ServerTags.STATE) && !value.equals("on")){
92                     checkAllRelatedHttpListenersAreDisabled(vs, result);
93                 }
94             }
95         }
96         
97         catch (final ConfigException ce){
98             _logger.log(Level.WARNING, "domainxmlverifier.exception", ce);
99         }
100
101
102         return result;
103     }
104     
105         
106
107     private Set JavaDoc getReferers(final VirtualServer vs) throws ConfigException {
108         final Set JavaDoc listeners = getPeerListeners(vs);
109         if (listeners.isEmpty()) { return Collections.EMPTY_SET; }
110
111         final Set JavaDoc result = new HashSet JavaDoc();
112         for (final Iterator JavaDoc it = listeners.iterator(); it.hasNext(); ){
113             final HttpListener l = (HttpListener) it.next();
114             if (l.getDefaultVirtualServer().equals(vs.getId())){
115                 result.add(l);
116             }
117         }
118         return result;
119     }
120
121     private Set JavaDoc getPeerListeners(final VirtualServer vs) throws ConfigException {
122         final HttpService hs = (HttpService) vs.parent();
123         if (null == hs) { return Collections.EMPTY_SET; }
124
125         final Set JavaDoc result = new HashSet JavaDoc();
126         result.addAll(Arrays.asList(hs.getHttpListener()));
127         return result;
128     }
129         
130         
131     
132     public void validateAttribute(String JavaDoc name, String JavaDoc value, Config config, Result result) {
133         
134         if(value== null || value.equals(""))
135             return;
136                   
137         if(name.equals(ServerTags.HOSTS)) {
138             Vector JavaDoc address = tokens(value);
139             for(int i=0;i<address.size();i++) {
140                 try {
141                     StaticTest.checkIPAddress((String JavaDoc) address.get(i));
142                 } catch(UnknownHostException JavaDoc u) {
143                     result.failed(smh.getLocalString(getClass().getName() + ".invalidHostsAddress",
144                           "Invalid value for virtual server's ''{0}'' attribute. ''{1}'' is incorrect network address. ",
145                           new Object JavaDoc[]{name,(String JavaDoc)address.get(i)}));
146                 }
147             }
148         }
149     }
150    
151     public Vector JavaDoc tokens(String JavaDoc value) {
152         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(value,",");
153         Vector JavaDoc test = new Vector JavaDoc();
154         while(token.hasMoreTokens())
155             test.add(token.nextToken());
156         return test;
157     }
158
159     private final void checkAllRelatedHttpListenersAreDisabled(final VirtualServer vs, final Result result) throws ConfigException {
160         for (final Iterator JavaDoc it = getReferers(vs).iterator(); it.hasNext(); ){
161             final HttpListener l = (HttpListener) it.next();
162             if (l.isEnabled()){
163                 result.failed(smh.getLocalString(getClass().getName()+".listenerEnabled",
164                                                 "Cannot disable the virtual server \"{0}\" because this is the default virtual server for the http listener \"{1}\".",
165                                                 new Object JavaDoc[]{vs.getId(), l.getId()}));
166             }
167         }
168     }
169
170     private final VirtualServer getVirtualServer(final ConfigContextEvent cce) throws ConfigException {
171         return (VirtualServer) cce.getValidationTarget();
172     }
173     
174 }
175
176
Popular Tags