1 7 package org.ejtools.jndi.browser.model; 8 9 import java.lang.reflect.Constructor ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 import java.util.Properties ; 13 import java.util.ResourceBundle ; 14 import java.util.Vector ; 15 16 import javax.naming.Context ; 17 import javax.naming.InitialContext ; 18 import javax.naming.LinkRef ; 19 import javax.naming.NameClassPair ; 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 23 import org.apache.log4j.Logger; 24 import org.ejtools.jndi.browser.model.service.ConnectionMetaData; 25 import org.ejtools.util.service.Profile; 26 import org.ejtools.util.service.ProfileHolder; 27 28 68 public class Server extends JNDIContext implements ProfileHolder 69 { 70 71 protected Profile profile = new Profile(); 72 73 private static Logger logger = Logger.getLogger(Server.class); 74 75 private static Vector proxies = new Vector (); 76 77 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jndi.browser.Resources"); 78 79 80 85 public Server() 86 { 87 super(); 88 this.setName(resources.getString("connection.text.untitled")); 89 } 90 91 92 97 public String getContext() 98 { 99 return this.profile.getProperty(ConnectionMetaData.CONTEXT); 100 } 101 102 103 108 public String getCredentials() 109 { 110 return this.profile.getProperty(ConnectionMetaData.CREDENTIALS); 111 } 112 113 114 119 public String getFactory() 120 { 121 return this.profile.getProperty(ConnectionMetaData.FACTORY); 122 } 123 124 125 130 public String getPackages() 131 { 132 return this.profile.getProperty(ConnectionMetaData.PACKAGES); 133 } 134 135 136 137 142 public String getPrincipal() 143 { 144 return this.profile.getProperty(ConnectionMetaData.PRINCIPAL); 145 } 146 147 148 153 public String getUrl() 154 { 155 return this.profile.getProperty(ConnectionMetaData.URL); 156 } 157 158 159 164 public void refresh() 165 { 166 logger.debug("Cleaning JNDI tree..."); 167 Iterator iterator = iterator(); 168 while (iterator.hasNext()) 169 { 170 remove(iterator.next()); 171 } 172 173 try 174 { 175 Context context = null; 176 if (this.getFactory() != null) 177 { 178 Properties props = new Properties (); 179 props.put(Context.INITIAL_CONTEXT_FACTORY, this.getFactory()); 180 props.put(Context.URL_PKG_PREFIXES, this.getPackages()); 181 props.put(Context.PROVIDER_URL, this.getUrl()); 182 logger.debug("Initial context with " + props); 183 context = new InitialContext (props); 184 } 185 else 186 { 187 context = new InitialContext (); 188 } 189 190 if (!("".equals(this.getContext()))) 191 { 192 logger.debug("Context =<" + this.getContext() + ">"); 193 context = (Context ) context.lookup(this.getContext()); 194 } 195 196 logger.debug("Populating JNDI tree..."); 197 this.list(context, this); 198 } 199 catch (Exception e) 200 { 201 e.printStackTrace(); 202 logger.error("Exception " + e.getMessage()); 203 } 204 } 205 206 207 212 public void setContext(String context) 213 { 214 this.profile.setProperty(ConnectionMetaData.CONTEXT, context); 215 } 216 217 218 219 224 public void setCredentials(String credentials) 225 { 226 this.profile.setProperty(ConnectionMetaData.CREDENTIALS, credentials); 227 } 228 229 230 235 public void setFactory(String factory) 236 { 237 this.profile.setProperty(ConnectionMetaData.FACTORY, factory); 238 } 239 240 241 246 public void setName(String name) 247 { 248 super.setName(name); 249 } 250 251 252 257 public void setPackages(String packages) 258 { 259 this.profile.setProperty(ConnectionMetaData.PACKAGES, packages); 260 } 261 262 263 264 269 public void setPrincipal(String principal) 270 { 271 this.profile.setProperty(ConnectionMetaData.PRINCIPAL, principal); 272 } 273 274 275 280 public void setProfile(Profile profile) 281 { 282 this.profile = profile; 283 } 284 285 286 291 public void setUrl(String url) 292 { 293 this.profile.setProperty(ConnectionMetaData.URL, url); 294 } 295 296 297 304 protected JNDIContext createNode(Context context, String jndiName) 305 { 306 for (int i = 0; i < proxies.size(); i++) 307 { 308 Class clazz = (Class ) proxies.elementAt(i); 309 310 try 311 { 312 Constructor c = clazz.getConstructor(new Class []{javax.naming.Context .class, java.lang.String .class}); 313 JNDIContext node = (JNDIContext) c.newInstance(new Object []{context, jndiName}); 314 if (node != null) 315 { 316 return node; 317 } 318 } 319 catch (Exception e) 320 { 321 } 322 } 323 324 return null; 325 } 326 327 328 334 protected void list(Context ctx, Collection node) 335 { 336 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 337 try 338 { 339 NamingEnumeration ne = ctx.list(""); 340 while (ne.hasMore()) 341 { 342 JNDIContext newNode = null; 343 344 NameClassPair pair = (NameClassPair ) ne.next(); 345 346 String name = pair.getName(); 347 String className = pair.getClassName(); 348 boolean recursive = false; 349 boolean isLinkRef = false; 350 351 Class c = null; 352 try 353 { 354 c = loader.loadClass(className); 355 356 if (Context .class.isAssignableFrom(c)) 357 { 358 recursive = true; 359 } 360 if (LinkRef .class.isAssignableFrom(c)) 361 { 362 isLinkRef = true; 363 } 364 } 365 catch (ClassNotFoundException cnfe) 366 { 367 logger.warn("ClassNotFoundException " + cnfe.getMessage()); 368 } 369 catch (Exception e) 370 { 371 logger.warn("Exception " + e.getMessage()); 372 } 373 374 if (isLinkRef) 375 { 376 try 377 { 378 newNode = new JNDILinkRef(ctx, name); 379 node.add(newNode); 380 } 381 catch (Exception e) 382 { 383 } 384 } 385 386 if (recursive) 387 { 388 newNode = new JNDIContext(); 389 newNode.setName(name); 390 newNode.setClassName(className); 391 392 try 393 { 394 Object value = ctx.lookup(name); 395 396 if (value instanceof Context ) 397 { 398 Context subctx = (Context ) value; 399 list(subctx, newNode); 400 } 401 } 402 catch (Throwable t) 403 { 404 logger.warn("Throwable " + t); 405 } 406 } 407 408 if (newNode == null) 410 { 411 newNode = createNode(ctx, name); 412 } 413 414 if (newNode == null) 416 { 417 newNode = new JNDIEntry(); 418 newNode.setName(name); 419 newNode.setClassName(className); 420 } 421 422 node.add(newNode); 423 } 424 ne.close(); 425 } 426 catch (NamingException ne) 427 { 428 logger.error("error while listing context " + ctx.toString() + ": " + ne.toString(true)); 429 } 430 } 431 432 433 static 434 { 435 proxies.add(org.ejtools.jndi.browser.model.JNDIEnvEntry.class); 436 proxies.add(org.ejtools.jndi.browser.model.ejb.EJBHomeProxy.class); 437 proxies.add(org.ejtools.jndi.browser.model.ejb.EJBLocalHomeProxy.class); 438 proxies.add(org.ejtools.jndi.browser.model.jms.QueueProxy.class); 439 proxies.add(org.ejtools.jndi.browser.model.jms.TopicProxy.class); 440 proxies.add(org.ejtools.jndi.browser.model.jms.ConnectionFactoryProxy.class); 441 proxies.add(org.ejtools.jndi.browser.model.mail.SessionProxy.class); 442 proxies.add(org.ejtools.jndi.browser.model.sql.DataSourceProxy.class); 443 proxies.add(org.ejtools.jndi.browser.model.Proxy.class); 444 } 445 } 446 | Popular Tags |