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