KickJava   Java API By Example, From Geeks To Geeks.

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


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

54
55 public class HttpListenerTest extends GenericValidator {
56     
57     private static final String JavaDoc DELIMITER=",";
58     
59     public HttpListenerTest(ValidationDescriptor desc) {
60         super(desc);
61     }
62     
63     public Result validate(ConfigContextEvent cce) {
64         Result result = super.validate(cce); // Before doing custom validation do basic validation
65
String JavaDoc choice = cce.getChoice();
66         
67         if(choice.equals(StaticTest.ADD) || choice.equals(StaticTest.VALIDATE)) {
68             final HttpListener h = (HttpListener)cce.getObject();
69             String JavaDoc vsId = h.getDefaultVirtualServer();
70             try {
71                 Config config = (Config) ((HttpService) cce.getClassObject()).parent();
72                 if( config!=null ) {
73                     boolean exists = checkVSExists(vsId, config);
74                     if(!exists) {
75                         result.failed(smh.getLocalString(getClass().getName() + ".virtualserverNotFound",
76                                                          "Attribute(default-virtual-server={0}) : Virtual Server not found", new Object JavaDoc[]{vsId}));
77                     } else if (h.isEnabled()){
78                             // When the listener is enabled then the virtual
79
// server must be on.
80
if (! isVirtualServerOn(h, config, result)){
81                             result.failed(smh.getLocalString(getClass().getName() + ".cannotAddVsNotOn",
82                                                              "Cannot add this HttpListener \"{0}\" because it is enabled but its virtual server \"{1}\" has a state other than \"on\" ({2})",
83                                                              new Object JavaDoc[]{h.getId(), vsId, getDefaultVirtualServer(vsId, config).getState()}));
84                         }
85                     }
86                 }
87                 
88             }
89             catch(Exception JavaDoc e){
90                 _logger.log(Level.FINE, "domainxmlverifier.error", e);
91                 
92             }
93         } else if (choice.equals(StaticTest.UPDATE)) {
94             if (cce.getName().equals("enabled") && ConfigBean.toBoolean((String JavaDoc) cce.getObject())){
95                 final HttpListener h = (HttpListener) cce.getClassObject();
96                 final Config c = (Config) ((HttpService) h.parent()).parent();
97                 final VirtualServer vs = getDefaultVirtualServer(h.getDefaultVirtualServer(), c);
98                 if (null != vs && !vs.getState().equals("on")){
99                     result.failed(smh.getLocalString(getClass().getName() + ".cannotUpdateVSNotOn",
100                                                      "Cannot enable this HttpListener \"{0}\" because its virtual server \"{1}\" has a state other than \"on\" ({2})",
101                                                      new Object JavaDoc[]{h.getId(), vs.getId(), vs.getState()}));
102                                                      
103                 }
104             }
105         } else {
106             _logger.log(Level.SEVERE, "domainxmlverifier.unknownchoice", choice);
107         }
108         
109         return result;
110     }
111
112     private final boolean isVirtualServerOn(final HttpListener h, final Config c, final Result result){
113         final VirtualServer vs = getDefaultVirtualServer(h.getDefaultVirtualServer(), c);
114         return (null != vs && vs.getState().equals("on"));
115     }
116     
117         
118         /**
119            Checks whether a virtual server with given id is available in the given
120            server. Current hierarchy is that the http-service has a single
121            virtual-server-class and which has many virtual-servers. An Http lsnr
122            can have any one of these virtual servers as its default-virtual-server.
123            @param vsID String representing the id of vs specified
124            @param server ConfigBean representing the server.xml
125            @return true if and only if the given vsID exists in given Server,
126            false otherwise
127         */

128     private boolean checkVSExists(String JavaDoc vsID, Config config) {
129         return getDefaultVirtualServer(vsID, config) != null;
130     }
131
132         /**
133            Get the default virtual server given an ID and the config
134            in which teh server should be found.
135            @param vsId the id of the virtual server to be found
136            @param config the config to be searched for the virtual
137            server
138            @return the virtual server object, if found; null otherwise.
139          */

140     private final VirtualServer getDefaultVirtualServer(final String JavaDoc vsID, final Config config){
141         final VirtualServer[] virtualServer = config.getHttpService().getVirtualServer();
142         for(int i = 0 ; i < virtualServer.length ; i++) {
143             if(vsID.equals(virtualServer[i].getId())) {
144                 return virtualServer[i];
145             }
146         }
147         return null;
148     }
149     
150         
151 }
152
Popular Tags