1 25 26 package org.objectweb.easybeans.deployment.helper; 27 28 import static javax.ejb.TransactionManagementType.CONTAINER ; 29 30 import java.util.List ; 31 32 import javax.naming.Context ; 33 import javax.naming.NamingException ; 34 35 import org.objectweb.easybeans.api.Factory; 36 import org.objectweb.easybeans.container.EasyBeansEJBContext; 37 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata; 38 import org.objectweb.easybeans.deployment.xml.struct.EJB3; 39 import org.objectweb.easybeans.deployment.xml.struct.EnterpriseBeans; 40 import org.objectweb.easybeans.deployment.xml.struct.Session; 41 import org.objectweb.easybeans.deployment.xml.struct.common.EnvEntry; 42 import org.objectweb.easybeans.log.JLog; 43 import org.objectweb.easybeans.log.JLogFactory; 44 import org.objectweb.easybeans.naming.NamingManager; 45 46 50 public final class JavaContextHelper { 51 52 55 private static JLog logger = JLogFactory.getLog(JavaContextHelper.class); 56 57 60 private JavaContextHelper() { 61 62 } 63 64 71 public static Context build(final ClassAnnotationMetadata bean, final Factory easyBeansFactory) 72 throws JavaContextHelperException { 73 Context javaCtx = null; 74 try { 75 javaCtx = NamingManager.getInstance().createEnvironmentContext(bean.getClassName()); 76 } catch (NamingException e) { 77 throw new IllegalStateException ("Cannot build a new environment", e); 78 } 79 80 String beanClassName = bean.getClassName().replace("/", "."); 81 82 Context compCtx; 84 try { 85 compCtx = (Context ) javaCtx.lookup("comp"); 86 } catch (NamingException e) { 87 throw new JavaContextHelperException("Cannot create subcontext", e); 88 } 89 90 91 if (easyBeansFactory.getBeanInfo().getTransactionManagementType() == CONTAINER) { 94 logger.debug("Bean is container managed so remove availability of java:comp/UserTransaction object"); 95 try { 96 compCtx.unbind("UserTransaction"); 97 } catch (NamingException e) { 98 throw new IllegalStateException ("Cannot remove java:comp/UserTransaction object", e); 99 } 100 } 101 102 103 try { 105 compCtx.bind("EJBContext", new EasyBeansEJBContext(easyBeansFactory)); 106 } catch (NamingException e) { 107 throw new JavaContextHelperException("Cannot bind EJBContext", e); 108 } 109 110 111 Context envCtx; 112 try { 113 envCtx = compCtx.createSubcontext("env"); 114 } catch (NamingException e) { 115 throw new JavaContextHelperException("Cannot create subcontext", e); 116 } 117 118 EJB3 ejb3 = bean.getEjbJarAnnotationMetadata().getEjb3(); 120 121 String beanName = bean.getJCommonBean().getName(); 122 if (ejb3 != null) { 123 EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans(); 125 if (enterpriseBeans != null) { 126 List <Session> sessionList = enterpriseBeans.getSessionList(); 127 for (Session session : sessionList) { 128 if (!beanClassName.equals(session.getEjbClass()) 130 && (beanName != null && !beanName.equals(session.getEjbName()))) { 131 continue; 132 } 133 134 List <EnvEntry> envEntryList = session.getEnvEntryList(); 136 for (EnvEntry envEntry : envEntryList) { 137 String name = envEntry.getEnvEntryName(); 138 Object value = getEnvEntryValue(envEntry); 139 if (value != null) { 141 try { 142 envCtx.rebind(name, value); 143 } catch (NamingException e) { 144 throw new JavaContextHelperException("Cannot bind element '" + name + "'.", e); 145 } 146 } 147 } 148 149 } 150 151 } 152 153 } 154 155 return javaCtx; 156 } 157 158 163 private static Object getEnvEntryValue(final EnvEntry envEntry) { 164 final String type = envEntry.getEnvEntryType(); 165 final String value = envEntry.getEnvEntryValue(); 166 167 Object returnedValue = null; 168 169 if (type.equals(Boolean .class.getName())) { 170 if ("true".equalsIgnoreCase(value)) { 171 returnedValue = Boolean.TRUE; 172 } else if ("false".equalsIgnoreCase(value)) { 173 returnedValue = Boolean.FALSE; 174 } 175 } else if (type.equals(String .class.getName())) { 176 returnedValue = value; 177 } else if (type.equals(Integer .class.getName())) { 178 if (value != null) { 179 returnedValue = new Integer (value); 180 } 181 } else if (type.equals(Character .class.getName())) { 182 if (value != null) { 183 if (value.length() != 1) { 184 throw new IllegalStateException ("The value '" + value 185 + "' is not a valid value for env-entry of type java.lang.Character."); 186 } 187 returnedValue = new Character (value.charAt(0)); 188 } 189 } else if (type.equals(Double .class.getName())) { 190 if (value != null) { 191 returnedValue = new Double (value); 192 } 193 } else if (type.equals(Byte .class.getName())) { 194 if (value != null) { 195 returnedValue = new Byte (value); 196 } 197 } else if (type.equals(Short .class.getName())) { 198 if (value != null) { 199 returnedValue = new Short (value); 200 } 201 } else if (type.equals(Long .class.getName())) { 202 if (value != null) { 203 returnedValue = new Long (value); 204 } 205 } else if (type.equals(Float .class.getName())) { 206 if (value != null) { 207 returnedValue = new Float (value); 208 } 209 } else { 210 throw new IllegalStateException (type + " is not a valid type for env-entry."); 211 } 212 return returnedValue; 213 } 214 } 215 | Popular Tags |