1 23 24 package com.sun.enterprise.config.serverbeans.validation.tests; 25 26 import java.net.UnknownHostException ; 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import java.util.StringTokenizer ; 30 import java.util.Vector ; 31 import java.util.logging.Level ; 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 ; 46 import java.util.Arrays ; 47 import java.util.Iterator ; 48 49 50 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); 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 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 final VirtualServer vs = (VirtualServer) cce.getClassObject(); 84 final Config config = (Config) vs.parent().parent(); 85 String name = cce.getName(); 86 String value = (String ) cce.getObject(); 87 validateAttribute(name,value,config, result); 88 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 getReferers(final VirtualServer vs) throws ConfigException { 108 final Set listeners = getPeerListeners(vs); 109 if (listeners.isEmpty()) { return Collections.EMPTY_SET; } 110 111 final Set result = new HashSet (); 112 for (final Iterator 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 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 result = new HashSet (); 126 result.addAll(Arrays.asList(hs.getHttpListener())); 127 return result; 128 } 129 130 131 132 public void validateAttribute(String name, String value, Config config, Result result) { 133 134 if(value== null || value.equals("")) 135 return; 136 137 if(name.equals(ServerTags.HOSTS)) { 138 Vector address = tokens(value); 139 for(int i=0;i<address.size();i++) { 140 try { 141 StaticTest.checkIPAddress((String ) address.get(i)); 142 } catch(UnknownHostException 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 []{name,(String )address.get(i)})); 146 } 147 } 148 } 149 } 150 151 public Vector tokens(String value) { 152 StringTokenizer token = new StringTokenizer (value,","); 153 Vector test = new Vector (); 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 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 []{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 |