View Javadoc

1   /*
2       Copyright 2006 Ernest Micklei @ PhilemonWorks.com
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15     
16  */
17  package com.philemonworks.selfdiagnose.check;
18  
19  import java.util.Hashtable;
20  
21  import javax.naming.Context;
22  import javax.naming.InitialContext;
23  import javax.naming.NamingException;
24  
25  import org.xml.sax.Attributes;
26  
27  import com.philemonworks.selfdiagnose.DiagnoseException;
28  import com.philemonworks.selfdiagnose.DiagnoseUtil;
29  import com.philemonworks.selfdiagnose.DiagnosticTask;
30  import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
31  import com.philemonworks.selfdiagnose.ExecutionContext;
32  /**
33   * 
34   * CheckJNDIBinding is a task to check whether the naming server has a JNDI binding for a given name.
35   * <p/>
36   * <pre>
37   &lt;checkjndibinding name="jdbc/mydb"/&gt;
38   &lt;checkjndibinding name="jdbc/mydb" class="mypackage.DataSource"/&gt;
39   &lt;checkjndibinding name="jdbc/mydb" class="mypackage.DataSource" url="localhost:900" factory="someFactory"/&gt;
40   * </pre>
41   * Stores the value found into the (optional) specified variable. 
42   * @author E.M.Micklei
43   */
44  public class CheckJNDIBinding extends DiagnosticTask {	
45      private static final long serialVersionUID = 4773822648065380753L;
46      private static final String PARAMETER_FACTORY = "factory";
47  	private static final String PARAMETER_URL = "url";
48  	private static final String PARAMETER_CLASS = "class";
49  	private static final String PARAMETER_NAME = "name";
50  	private String initialContextFactory;
51  	private String namingServerURL;
52  	private String jndiName;
53  	private String valueClassName;
54  	private Class valueClass;
55  	/**
56  	 * Return the description of this task.
57  	 */
58  	public String getDescription() {
59  		return "Check that the value of this binding is present and of the correct type.";
60  	}		
61  	/*
62  	 * (non-Javadoc)
63  	 * 
64  	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#initializeFromAttributes(Attributes)
65  	 */
66  	public void initializeFromAttributes(Attributes attributes) {
67  		super.initializeFromAttributes(attributes);
68  		this.setJndiName(attributes.getValue(PARAMETER_NAME));
69  		// optional
70  		this.setValueClassName(attributes.getValue(PARAMETER_CLASS));
71  		// optional as pair
72  		this.setNamingServerURL(attributes.getValue(PARAMETER_URL));
73  	    this.setInitalContextFactory(attributes.getValue(PARAMETER_FACTORY));
74  	}		
75  	public void setUp(ExecutionContext ctx) throws DiagnoseException {
76  		super.setUp(ctx);
77  		DiagnoseUtil.verifyNonEmptyString(PARAMETER_NAME, jndiName, CheckJNDIBinding.class);
78  		// if specified then both must be present
79  		if (this.hasContextFactorySpecified()) {
80  		    DiagnoseUtil.verifyNonEmptyString(PARAMETER_URL, namingServerURL, CheckJNDIBinding.class);
81  		    DiagnoseUtil.verifyNonEmptyString(PARAMETER_FACTORY, initialContextFactory, CheckJNDIBinding.class);
82  		}    		
83  		if (this.hasValueClassSpecified()){
84  			// resolve to valueClass
85  			try {
86  				valueClass = DiagnoseUtil.classForName(valueClassName);
87  			} catch (ClassNotFoundException e) {
88  				throw new DiagnoseException("No such class ["+valueClassName+"]",e);
89  			}
90  		}
91  	}
92  	public boolean hasContextFactorySpecified(){
93  	    return namingServerURL != null || initialContextFactory != null; 
94  	}
95  	public boolean hasValueClassSpecified(){
96  	    return valueClassName != null;
97  	}
98  	/*
99  	 * (non-Javadoc)
100 	 * 
101 	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#run()
102 	 */
103 	public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
104 	    Context initialContext = null;
105 	    Object value = null;
106 		try {
107 		    if (this.hasContextFactorySpecified()){
108 		        Hashtable env = new Hashtable();
109 		        env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
110 		        env.put(Context.PROVIDER_URL, namingServerURL);
111 		        initialContext = new InitialContext(env);
112 		    } else {
113 		        initialContext = new InitialContext();
114 		    }
115 			value = initialContext.lookup(jndiName);
116 		} catch (NamingException e1) {
117 			throw new DiagnoseException("No such binding ["+jndiName+"]",e1);
118 		}
119 		if (value == null) {
120 			result.setFailedMessage("Value for binding [" + jndiName + "] is null, expected was [" + valueClassName + "]");
121 			return;
122 		}
123 		ctx.setValue(this.getVariableName(), value);
124 		if (this.hasValueClassSpecified() && !(valueClass.isAssignableFrom(value.getClass()))) {
125 		        throw new DiagnoseException("Value for binding [" + jndiName + "] is of type [" + value.getClass().getName() + "], expected was (or implementor of)["
126 					+ valueClassName + "] ");
127 		}
128 		if (this.hasValueClassSpecified()) {
129 			String msg = "JNDI binding [" + jndiName + "] of type [" + valueClassName + "] is available in naming server";
130 			if (namingServerURL != null)
131 				msg += "[" + namingServerURL + "]";
132 			result.setPassedMessage(msg);
133 		} else
134 		    result.setPassedMessage("JNDI binding [" + jndiName + "] is available");
135 	}
136 	/**
137 	 * @return String initalContextFactory
138 	 */
139 	public String getInitalContextFactory() {
140 		return initialContextFactory;
141 	}
142 	/**
143 	 * @return String namingServerURL
144 	 */
145 	public String getNamingServerURL() {
146 		return namingServerURL;
147 	}
148 	/**
149 	 * @return String
150 	 */
151 	public String getValueClassName() {
152 		return valueClassName;
153 	}
154 	/**
155 	 * @param string
156 	 */
157 	public void setInitalContextFactory(String string) {
158 		initialContextFactory = string;
159 	}
160 	/**
161 	 * @param string
162 	 */
163 	public void setNamingServerURL(String string) {
164 		namingServerURL = string;
165 	}
166 	/**
167 	 * @param class1
168 	 */
169 	public void setValueClassName(String class1) {
170 		valueClassName = class1;
171 	}
172 	/**
173 	 * @return String jndiName
174 	 */
175 	public String getJndiName() {
176 		return jndiName;
177 	}
178 	/**
179 	 * @param string
180 	 */
181 	public void setJndiName(String string) {
182 		jndiName = string;
183 	}
184 }