KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > DomainXmlVerifier


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;
25
26 // config imports
27
import com.sun.enterprise.config.ConfigFactory;
28 import com.sun.enterprise.config.ConfigContext;
29 import com.sun.enterprise.config.ConfigException;
30 import com.sun.enterprise.config.ConfigBean;
31 import com.sun.enterprise.config.ConfigContextEvent;
32
33
34 import com.sun.enterprise.config.serverbeans.Config;
35 import com.sun.enterprise.config.serverbeans.Domain;
36 import com.sun.enterprise.config.serverbeans.Resources;
37 import com.sun.enterprise.config.serverbeans.Applications;
38 import com.sun.enterprise.config.serverbeans.Configs;
39 import com.sun.enterprise.config.serverbeans.HttpService;
40 import com.sun.enterprise.config.serverbeans.IiopService;
41 import com.sun.enterprise.config.serverbeans.JmsService;
42 import com.sun.enterprise.config.serverbeans.Servers;
43 import com.sun.enterprise.config.serverbeans.Applications;
44 import com.sun.enterprise.config.serverbeans.AdminService;
45 import com.sun.enterprise.config.serverbeans.SecurityService;
46 //import com.sun.enterprise.config.serverbeans.validation.tests.StaticTest;
47

48 import java.net.URL JavaDoc;
49
50 // Logging
51
import com.sun.enterprise.util.LocalStringManagerImpl;
52 import java.util.logging.Logger JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import com.sun.logging.LogDomains;
55
56 import javax.xml.transform.Transformer JavaDoc;
57 import javax.xml.transform.TransformerConfigurationException JavaDoc;
58 import javax.xml.transform.TransformerException JavaDoc;
59 import javax.xml.transform.TransformerFactory JavaDoc;
60 import javax.xml.transform.stream.StreamResult JavaDoc;
61 import javax.xml.transform.stream.StreamSource JavaDoc;
62  
63 //import com.sun.enterprise.config.serverbeans.VirtualServerClass;
64
import com.sun.enterprise.config.serverbeans.ConnectorConnectionPool;
65
66 import java.util.Vector JavaDoc;
67 import java.io.*;
68
69 /**
70     Class which invokes the config validator from Command Line
71  
72     @author Srinivas Krishnan
73     @version 2.0
74 */

75
76 public class DomainXmlVerifier {
77     
78     public ConfigContext configContext;
79     public DomainMgr domainMgr;
80     public String JavaDoc domainFile;
81     public boolean error;
82     public boolean debug = false;
83     public static int count = 0;
84     PrintStream _out;
85
86     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER);
87     static {
88         StringManagerHelper.setLocalStringsManager(DomainXmlVerifier.class);
89     }
90     static LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager();
91     public DomainXmlVerifier(String JavaDoc file) throws Exception JavaDoc {
92         this(file, System.out);
93     }
94     public DomainXmlVerifier(String JavaDoc file, PrintStream out) throws Exception JavaDoc {
95         _out = out;
96         domainFile = file;
97         configContext = ConfigFactory.createConfigContext(file);
98         configContext.refresh();
99         domainMgr = new DomainMgr(configContext, true);
100         error = false;
101     }
102    
103     public DomainMgr getDomainMgr() {
104         return domainMgr;
105     }
106     
107     public ConfigContext getConfigContext() {
108         return configContext;
109     }
110     
111     public void setDebug(boolean flag) {
112         debug = flag;
113     }
114     
115     public void output(Result result) {
116         String JavaDoc element = result.getAssertion();
117         String JavaDoc key = result.getTestName();
118         if(!result.getErrorDetails().toString().equals("[]")) {
119             error = true;
120             out("Element : " + element);
121             if(key != null)
122                 out("Key : " + key);
123             Vector JavaDoc error = result.getErrorDetails();
124             for(int i=0;i<error.size();i++) {
125                 if(i==0)
126                     out("Error : " + error.get(i));
127                 else if(i>0)
128                     out(" " + error.get(i));
129             }
130             out("");
131             count++; // Number of test to be printed
132
}
133     }
134     
135     public void check(ConfigBean configBean) {
136         ConfigContextEvent cce = new ConfigContextEvent(getConfigContext(), ConfigContextEvent.PRE_ADD_CHANGE,configBean.toString(),configBean,"VALIDATE");
137         cce.setClassObject(configBean.parent());
138         Result result = domainMgr.check(cce);
139         if(result != null)
140             output(result);
141     }
142     
143     public void checkUnique(ConfigBean configBean) {
144
145         try {
146             
147             Domain domain = (Domain)configContext.getRootConfigBean();
148             
149             // Resources
150
Resources resource = domain.getResources();
151 // checkDuplicate("admin-object-resource", resource.getAdminObjectResource(), "jndi-name");
152
// checkDuplicate("resource-adapter-config", resource.getResourceAdapterConfig(), "resource-adapter-name");
153
checkDuplicate("connector-connection-pool", resource.getConnectorConnectionPool(), "name");
154             ConnectorConnectionPool[] connpool = resource.getConnectorConnectionPool();
155             for(int i=0;i<connpool.length;i++)
156                 checkDuplicate("security-map", connpool[i].getSecurityMap(), "name");
157
158 // checkDuplicate("connector-resource", resource.getConnectorResource(), "jndi-name");
159
// checkDuplicate("custom-resource", resource.getCustomResource(), "jndi-name");
160
// checkDuplicate("external-jndi-resource", resource.getExternalJndiResource(), "jndi-name");
161
// checkDuplicate("jdbc-connection-pool", resource.getJdbcConnectionPool(), "name");
162
// checkDuplicate("jdbc-resource", resource.getJdbcResource(), "jndi-name");
163
// checkDuplicate("mail-resource", resource.getMailResource(), "jndi-name");
164
// checkDuplicate("persistence-manger-factory-resource", resource.getPersistenceManagerFactoryResource(), "jndi-name");
165

166             //Applications
167
Applications applications = domain.getApplications();
168 // checkDuplicate("appclient-module",applications.getAppclientModule(),"name");
169
// checkDuplicate("connector-module",applications.getConnectorModule(),"name");
170
// checkDuplicate("ejb-module", applications.getEjbModule(),"name");
171
// checkDuplicate("j2ee-application", applications.getJ2eeApplication(),"name");
172
// checkDuplicate("lifecycle-module", applications.getLifecycleModule(),"name");
173
// checkDuplicate("web-module", applications.getWebModule(),"name");
174

175             // Configs
176
Configs configs = domain.getConfigs();
177             checkDuplicate("config", configs.getConfig(), "name");
178             Config[] config = configs.getConfig();
179             for(int i=0;i<config.length;i++) {
180                 
181                 HttpService httpservice = config[i].getHttpService();
182                 //checkDuplicate("acl",httpservice.getAcl(), "name");
183
checkDuplicate("http-listener",httpservice.getHttpListener(), "id");
184                 //checkDuplicate("mime",httpservice.getMime(), "name");
185
checkDuplicate("virtual-server", httpservice.getVirtualServer(), "id");
186                 
187                 IiopService iiopservice = config[i].getIiopService();
188                 checkDuplicate("iiop-listener",iiopservice.getIiopListener(), "id");
189                 AdminService adminservice = config[i].getAdminService();
190                 checkDuplicate("jmx-connector",adminservice.getJmxConnector(), "name");
191                 
192                 JmsService jmsservice = config[i].getJmsService();
193                 checkDuplicate("jms-host",jmsservice.getJmsHost(), "name");
194                 
195                 SecurityService securityservice = config[i].getSecurityService();
196                 checkDuplicate("audit-module", securityservice.getAuditModule(), "name");
197                 checkDuplicate("auth-realm", securityservice.getAuthRealm(), "name");
198                 checkDuplicate("jacc-provider", securityservice.getJaccProvider(), "name");
199             }
200
201             Servers servers = domain.getServers();
202             checkDuplicate("server", servers.getServer(), "name");
203             
204         } catch(Exception JavaDoc e) {
205             e.printStackTrace();
206         }
207
208     }
209     
210     public void checkDuplicate(String JavaDoc elementName, ConfigBean[] cb, String JavaDoc attr) {
211             Result result = new Result();
212             result.setAssertion(elementName);
213             for(int i=0;i<cb.length-1;i++) {
214                         String JavaDoc name=cb[i].getAttributeValue(attr);
215                         for(int j=i+1;j<cb.length;j++) {
216                             if(name.equals(cb[j].getAttributeValue(attr))) {
217                                 result.failed("Duplicate Element : " + cb[j] + "(" + attr + "=" + name + ")");
218                                 output(result);
219                             }
220                         }
221             }
222     }
223     
224     
225     public void preOrder(ConfigBean configBean) {
226             if(configBean != null) {
227                 check(configBean);
228                 ConfigBean configBean1[] = configBean.getAllChildBeans();
229                 if(configBean1 != null) {
230                     for(int j=0;j<configBean1.length;j++) {
231                            if(configBean1[j] != null)
232                                 preOrder(configBean1[j]);
233                     }
234                 }
235             }
236     }
237     
238     public void out(String JavaDoc out) {
239         if(count < 50)
240             _out.println(out);
241         else if(count >= 50 && debug)
242             _out.println(out);
243     }
244     
245     public boolean validate() {
246         try {
247             checkUnique(configContext.getRootConfigBean());
248             preOrder(configContext.getRootConfigBean());
249             if(!error)
250                 _out.println("All Tests Passed, domain.xml is valid");
251         } catch(Exception JavaDoc e) {
252             e.printStackTrace();
253         }
254         return error;
255     }
256     
257     public boolean invokeConfigValidator() {
258         boolean failed = false;
259         try {
260             failed = validate();
261         } catch(Exception JavaDoc e) {
262             e.printStackTrace();
263         }
264         return failed;
265     }
266     
267     public static void main(String JavaDoc args[]) throws ConfigException {
268         
269         // if no args supplied print the usage
270
int i=0;
271         if(args.length < 1) {
272             System.out.println("Usage : verify-domain-xml [--verbose] domain_xml_file");
273             return;
274         }
275         
276         boolean isDebug = false;
277         // if verbose set, set debug to true
278
if(args[i].startsWith("--verbose")) {
279             if(args.length == 1) {
280                 System.out.println("Usage : verify-domain-xml [--verbose] domain_xml_file");
281                 return;
282             }
283             isDebug = true;
284             i++;
285         }
286         
287         // check if domain.xml path supplied exists
288
File f = new File(args[i]);
289         if(!f.exists()) {
290             //TODO i18n
291
System.out.println("Error Config file: " + args[i] + " is not found");
292             return;
293         }
294         String JavaDoc file = args[i];
295         boolean fileCheck = false;
296         boolean classCheck = false;
297         
298         try {
299             DomainXmlVerifier validator = new DomainXmlVerifier(file);
300             if(isDebug)
301                 validator.debug = true;
302             // do not check for file and class path
303
// for(int j=0;j<args.length;j++) {
304
// if(args[j].startsWith("--check-classpath"))
305
// classCheck = true;
306
// if(args[j].startsWith("--check-filepath"))
307
// fileCheck = true;
308
// }
309
// StaticTest.fileCheck = fileCheck;
310
// StaticTest.classPathCheck = classCheck;
311
validator.invokeConfigValidator();
312         } catch (ConfigException ce) {
313             Throwable JavaDoc cause = ce.getCause();
314             while(cause!=null && !(cause instanceof org.xml.sax.SAXParseException JavaDoc))
315                 cause = cause.getCause();
316             if(cause!=null)
317                 System.out.println("XML: "+cause.getMessage());
318             else
319                 ce.printStackTrace();
320         } catch (Exception JavaDoc e) {
321             e.printStackTrace();
322         }
323     }
324 }
325
Popular Tags