KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.util.logging.Level JavaDoc;
29
30 import com.sun.enterprise.config.ConfigContextEvent;
31 import com.sun.enterprise.config.ConfigException;
32 import com.sun.enterprise.config.serverbeans.AdminObjectResource;
33 import com.sun.enterprise.config.serverbeans.AppclientModule;
34 import com.sun.enterprise.config.serverbeans.ApplicationRef;
35 import com.sun.enterprise.config.serverbeans.Applications;
36 import com.sun.enterprise.config.serverbeans.Config;
37 import com.sun.enterprise.config.serverbeans.Configs;
38 import com.sun.enterprise.config.serverbeans.ConnectorConnectionPool;
39 import com.sun.enterprise.config.serverbeans.ConnectorModule;
40 import com.sun.enterprise.config.serverbeans.ConnectorResource;
41 import com.sun.enterprise.config.serverbeans.CustomResource;
42 import com.sun.enterprise.config.serverbeans.Domain;
43 import com.sun.enterprise.config.serverbeans.EjbModule;
44 import com.sun.enterprise.config.serverbeans.ExternalJndiResource;
45 import com.sun.enterprise.config.serverbeans.J2eeApplication;
46 import com.sun.enterprise.config.serverbeans.JdbcConnectionPool;
47 import com.sun.enterprise.config.serverbeans.JdbcResource;
48 import com.sun.enterprise.config.serverbeans.LifecycleModule;
49 import com.sun.enterprise.config.serverbeans.MailResource;
50 import com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource;
51 import com.sun.enterprise.config.serverbeans.ResourceAdapterConfig;
52 import com.sun.enterprise.config.serverbeans.ResourceRef;
53 import com.sun.enterprise.config.serverbeans.Resources;
54 import com.sun.enterprise.config.serverbeans.Server;
55 import com.sun.enterprise.config.serverbeans.ServerTags;
56 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
57 import com.sun.enterprise.config.serverbeans.Servers;
58 import com.sun.enterprise.config.serverbeans.WebModule;
59 import com.sun.enterprise.config.serverbeans.validation.GenericValidator;
60 import com.sun.enterprise.config.serverbeans.validation.Result;
61 import com.sun.enterprise.config.serverbeans.validation.ValidationDescriptor;
62
63 /**
64    Custom Test for Server Element which calls the Generic Validation before performing custom tests
65
66    @author Srinivas Krishnan
67    @version 2.0
68 */

69
70 public class ServerTest extends GenericValidator {
71     
72     public ServerTest(ValidationDescriptor desc) {
73         super(desc);
74     }
75
76     void validateAdd(final ConfigContextEvent cce, final Result result) throws ConfigException {
77         checkNameNotAgent(cce, result);
78         checkConfigRefValidity(cce, result);
79     }
80
81     void validateUpdate(final ConfigContextEvent cce, final Result result) throws ConfigException {
82         validateAttributeChange(cce, result);
83     }
84
85
86     protected final String JavaDoc getName(final ConfigContextEvent cce) throws ConfigException {
87         return getServer(cce).getName();
88
89     }
90     protected final boolean isReferenced(final ConfigContextEvent cce) throws ConfigException {
91         return ServerBeansFactory.isReferenced(getServer(cce), cce.getConfigContext());
92     }
93     
94     protected final Set JavaDoc getReferers(final ConfigContextEvent cce) throws ConfigException{
95         return ServerBeansFactory.getReferers(getServer(cce), cce.getConfigContext());
96     }
97
98     private void checkNameNotAgent(final ConfigContextEvent cce, final Result result) throws ConfigException {
99         final Server s = getServer(cce);
100         if ("agent".equals(s.getName())){
101             result.failed(smh.getLocalString(getClass().getName() + ".illegalServerName",
102                                              "Illegal Server Name {0}", new Object JavaDoc[]{s.getName()}));
103         }
104     }
105
106    private Server getServer(final ConfigContextEvent cce) throws ConfigException {
107        return (Server) cce.getValidationTarget();
108    }
109     private void preventChangeToNodeAgentRef(final ConfigContextEvent cce, final Result result){
110         if (cce.getName().equals(ServerTags.NODE_AGENT_REF)){
111             result.failed(smh.getLocalString(getClass().getName()+".noChangeToNodeAgentRef",
112                                              "Cannot change a node agent ref"));
113         }
114     }
115     
116     
117
118     public Result validate(ConfigContextEvent cce) {
119         Result result = super.validate(cce); // Before doing custom validation do basic validation
120
boolean flag = false;
121         String JavaDoc choice = cce.getChoice();
122         try {
123             if (choice.equals(StaticTest.SET)){
124                 validateSet(cce, result);
125             } else if (choice.equals(StaticTest.UPDATE)){
126                 validateUpdate(cce, result);
127             } else if (choice.equals(StaticTest.ADD)){
128                 validateAdd(cce, result);
129             } else if(choice.equals(StaticTest.VALIDATE)) {
130                 Domain domain = (Domain)cce.getConfigContext().getRootConfigBean();
131                 Servers servers = domain.getServers();
132                 Server[] server = servers.getServer();
133                     String JavaDoc svrName = server[0].getName();
134                     if(!server[0].getName().equals("server"))
135                         result.failed(smh.getLocalString(getClass().getName() + ".invalidserverName",
136                                                          "Invalid Server Name {0}: Required 'server'", new Object JavaDoc[]{svrName}));
137             }
138             
139             
140             } catch(Exception JavaDoc e) {
141                 _logger.log(Level.FINE, "domainxmlverifier.exception", e);
142             }
143             return result;
144     }
145
146     private void validateSet(final ConfigContextEvent cce, final Result result) throws ConfigException {
147         validateAttributeChange(cce, result);
148     }
149
150     private void validateAttributeChange(final ConfigContextEvent cce, final Result result) throws ConfigException {
151         checkNameNotAgent(cce, result);
152         preventChangeToNodeAgentRef(cce, result);
153         if (this.isReferenced(cce)){
154             preventChangeToConfigRef(cce, result);
155         } else {
156             preventInvalidConfigRef(cce, result);
157         }
158     }
159
160     private final void preventInvalidConfigRef(final ConfigContextEvent cce, final Result result) throws ConfigException {
161         if (cce.getName().equals(ServerTags.CONFIG_REF)){
162             checkConfigRefValidity((String JavaDoc) cce.getObject(), result);
163         }
164     }
165     
166     private final void checkConfigRefValidity(final ConfigContextEvent cce, final Result result) throws ConfigException {
167         checkConfigRefValidity(getServer(cce).getConfigRef(), result);
168     }
169     
170             
171     private void checkConfigRefValidity(final String JavaDoc config_ref, final Result result){
172         if (config_ref.equals(StaticTest.DAS_CONFIG_NAME)){
173             result.failed(smh.getLocalString(getClass().getName()+".cannotHaveDASasConfig",
174                                              "The configuration of the Domain Administration Server (named {0}) cannot be referenced by a server",
175                                              new Object JavaDoc[]{StaticTest.DAS_CONFIG_NAME}));
176         } else if (config_ref.equals(StaticTest.CONFIG_TEMPLATE_NAME)){
177             result.failed(smh.getLocalString(getClass().getName()+".cannotHaveTemplateConfig",
178                                              "The default configuration template (named {0}) cannot be referenced by a server",
179                                              new Object JavaDoc[]{StaticTest.CONFIG_TEMPLATE_NAME}));
180
181         }
182     }
183         
184
185     private void preventChangeToConfigRef(final ConfigContextEvent cce, final Result result){
186         if (cce.getName().equals(ServerTags.CONFIG_REF)){
187             result.failed(smh.getLocalString(getClass().getName()+".noChangeToConfigRefInCluster",
188                                              "Cannot change a config-ref when the instance is part of a cluster"));
189         }
190     }
191     
192
193 }
194
Popular Tags