1 45 package org.openejb.client; 46 47 import java.util.Enumeration ; 48 import java.util.Hashtable ; 49 import java.util.Properties ; 50 51 import javax.naming.Context ; 52 import javax.naming.InitialContext ; 53 import javax.naming.Name ; 54 import javax.naming.RefAddr ; 55 import javax.naming.Reference ; 56 import javax.naming.spi.ObjectFactory ; 57 58 108 public final class TomcatEjbFactory implements ObjectFactory 109 { 110 private final static String OPENEJB_PREFIX = "openejb."; 111 112 private final static String JAVA_PREFIX = "java."; 113 114 private final static String OPENEJB_EJB_LINK = "openejb.ejb-link"; 115 116 private final static int OPENEJB_PREFIX_LENGTH = OPENEJB_PREFIX.length(); 117 118 public Object getObjectInstance( Object obj, 119 Name name, 120 Context nameCtx, 121 Hashtable environment ) 122 throws Exception 123 { 124 Object beanObj = null; 125 Class ejbRefClass = Class.forName( "org.apache.naming.EjbRef" ); 126 if ( ejbRefClass.isAssignableFrom( obj.getClass() ) ) 127 { 128 RefAddr refAddr = null; 129 String addrType = null; 130 Properties env = new Properties (); 131 String bean = null; 132 133 Reference ref = ( Reference ) obj; 134 135 Enumeration addresses = ref.getAll(); 136 while ( addresses.hasMoreElements() ) 137 { 138 refAddr = ( RefAddr ) addresses.nextElement(); 139 addrType = refAddr.getType(); 140 if ( addrType.startsWith( OPENEJB_PREFIX ) ) 141 { 142 String value = refAddr.getContent().toString(); 143 if ( addrType.equals( OPENEJB_EJB_LINK ) ) 144 { 145 bean = value; 146 continue; 147 } 148 String key = addrType.substring( OPENEJB_PREFIX_LENGTH ); 149 key = JAVA_PREFIX + key; 150 env.put( key, value ); 151 } 152 } 153 154 if ( bean != null ) 155 { 156 beanObj = ( new InitialContext ( env ) ).lookup( bean ); 157 } 158 } 159 return beanObj; 160 } 161 } 162 | Popular Tags |