1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38
39
40
41
42
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
57
58 public String getDescription() {
59 return "Check that the value of this binding is present and of the correct type.";
60 }
61
62
63
64
65
66 public void initializeFromAttributes(Attributes attributes) {
67 super.initializeFromAttributes(attributes);
68 this.setJndiName(attributes.getValue(PARAMETER_NAME));
69
70 this.setValueClassName(attributes.getValue(PARAMETER_CLASS));
71
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
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
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
100
101
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
138
139 public String getInitalContextFactory() {
140 return initialContextFactory;
141 }
142
143
144
145 public String getNamingServerURL() {
146 return namingServerURL;
147 }
148
149
150
151 public String getValueClassName() {
152 return valueClassName;
153 }
154
155
156
157 public void setInitalContextFactory(String string) {
158 initialContextFactory = string;
159 }
160
161
162
163 public void setNamingServerURL(String string) {
164 namingServerURL = string;
165 }
166
167
168
169 public void setValueClassName(String class1) {
170 valueClassName = class1;
171 }
172
173
174
175 public String getJndiName() {
176 return jndiName;
177 }
178
179
180
181 public void setJndiName(String string) {
182 jndiName = string;
183 }
184 }