1 package org.apache.ojb.jboss; 2 3 17 18 import javax.naming.Context ; 19 import javax.naming.InitialContext ; 20 import javax.naming.Name ; 21 import javax.naming.NameNotFoundException ; 22 import javax.naming.NamingException ; 23 import java.io.Serializable ; 24 25 import org.apache.ojb.odmg.ODMGJ2EEFactory; 26 import org.apache.ojb.odmg.OJB; 27 import org.jboss.system.ServiceMBeanSupport; 28 import org.odmg.Implementation; 29 30 public class ODMGFactory extends ServiceMBeanSupport 31 implements ODMGJ2EEFactory, Serializable , ODMGFactoryMBean 32 { 33 private static final String JAVA_NAMESPACE = "java:/"; 34 35 private String jndiName; 36 37 public ODMGFactory() 38 { 39 System.out.println("ODMGFactory called"); 40 } 41 42 protected void startService() throws Exception 43 { 44 try 45 { 46 bind(new InitialContext (), JAVA_NAMESPACE + jndiName, this); 47 } 48 catch (Exception e) 49 { 50 System.out.println("Binding ODMG to JNDI failed"); 51 e.printStackTrace(); 52 throw e; 53 } 54 System.out.println("** OJB-ODMG MBean integration"); 55 System.out.println("** ODMGFactory: " + this.getClass().getName()); 56 System.out.println("** Use ODMGFactory via lookup:"); 57 System.out.println("** ODMGFactory factory = (ODMGFactory) ctx.lookup(" + JAVA_NAMESPACE + jndiName + ")"); 58 System.out.println("** Implementation odmg = factory.getInstance();"); 59 } 60 61 public void stopService() 62 { 63 try 64 { 65 (new InitialContext ()).unbind(JAVA_NAMESPACE + jndiName); 66 } 67 catch (NamingException namingexception) 68 { 69 namingexception.printStackTrace(); 70 } 71 } 72 73 public Implementation getInstance() 74 { 75 return OJB.getInstance(); 76 } 77 78 public void setJndiName(String jndiName) 79 { 80 this.jndiName = jndiName; 81 } 82 83 public String getJndiName() 84 { 85 return jndiName; 86 } 87 88 private void bind(Context ctx, String name, Object val) throws NamingException 89 { 90 Name n; 91 for (n = ctx.getNameParser("").parse(name); n.size() > 1; n = n.getSuffix(1)) 92 { 93 String ctxName = n.get(0); 94 try 95 { 96 ctx = (Context ) ctx.lookup(ctxName); 97 } 98 catch (NameNotFoundException namenotfoundexception) 99 { 100 ctx = ctx.createSubcontext(ctxName); 101 } 102 } 103 ctx.bind(n.get(0), val); 104 } 105 } 106 | Popular Tags |