1 package org.apache.ojb.ejb; 2 3 17 18 import javax.naming.Context ; 19 import javax.naming.InitialContext ; 20 import javax.naming.NamingException ; 21 import java.util.Properties ; 22 23 32 public class ContextHelper 33 { 34 private static InitialContext ctx; 35 36 public synchronized static Context getContext() 37 { 38 if(ctx == null) 39 { 40 Properties prop = System.getProperties(); 41 if (prop.getProperty(Context.INITIAL_CONTEXT_FACTORY) == null) 44 { 45 System.out.println("System property " + Context.INITIAL_CONTEXT_FACTORY 46 + " is not set. Try to use default setting for JNDI lookup"); 47 ctx = jbossJNDI(); 48 if(ctx == null) ctx = jonasJNDI(); 49 if(ctx == null) throw new RuntimeException ("Can't initialize naming context"); 50 } 51 else 52 { 53 try 54 { 55 ctx = new InitialContext (prop); 56 } 57 catch (NamingException e) 58 { 59 e.printStackTrace(); 60 } 61 } 62 try 63 { 64 System.out.println("Used JNDI Context: " + ctx.getEnvironment()); 65 } 78 catch(NamingException e) 79 { 80 e.printStackTrace(); 81 } 82 } 83 84 return ctx; 85 } 86 87 private static InitialContext jbossJNDI() 88 { 89 InitialContext ctx = null; 90 try 91 { 92 System.out.println("Try to use JBoss naming service"); 93 Properties prop = jbossNamingProperties(); 94 ctx = new InitialContext (prop); 95 } 96 catch(NamingException e) 97 { 98 System.err.println("JBoss JNDI lookup failed: " + e.getMessage()); 99 } 100 return ctx; 101 } 102 103 private static InitialContext jonasJNDI() 104 { 105 InitialContext ctx = null; 106 try 107 { 108 System.out.println("Try to use JOnAS naming service"); 109 Properties prop = jonasNamingProperties(); 110 ctx = new InitialContext (prop); 111 } 112 catch(NamingException e) 113 { 114 System.err.println("JonAS JNDI lookup failed: " + e.getMessage()); 115 e.printStackTrace(); 116 } 117 return ctx; 118 } 119 120 private static Properties jbossNamingProperties() 121 { 122 Properties prop = new Properties (); 123 prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 124 prop.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099"); 125 prop.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); 126 return prop; 127 } 128 129 private static Properties jonasNamingProperties() 130 { 131 Properties prop = new Properties (); 132 prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory"); 133 return prop; 136 } 137 } 138 | Popular Tags |