1 29 30 package com.caucho.ejb.cfg; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.bytecode.JMethod; 34 import com.caucho.config.ConfigException; 35 import com.caucho.log.Log; 36 import com.caucho.util.L10N; 37 38 import java.util.logging.Logger ; 39 40 43 public class EjbEntityHomeView extends EjbHomeView { 44 private static final Logger log = Log.open(EjbEntityHomeView.class); 45 private static final L10N L = new L10N(EjbEntityHomeView.class); 46 47 private boolean _hasFindByPrimaryKey; 48 49 52 public EjbEntityHomeView(EjbBean bean, JClass apiClass, String prefix) 53 throws ConfigException 54 { 55 super(bean, apiClass, prefix); 56 } 57 58 protected boolean isCMP() 59 { 60 return ((EjbEntityBean) getBean()).isCMP(); 61 } 62 63 66 protected void introspect() 67 throws ConfigException 68 { 69 super.introspect(); 70 71 if (! _hasFindByPrimaryKey) 72 throw new ConfigException(L.l("{0}: expected 'findByPrimaryKey'. All entity homes must define findByPrimaryKey.", 73 getApiClass().getName())); 74 } 75 76 79 protected EjbMethod introspectEJBMethod(JMethod method) 80 throws ConfigException 81 { 82 String methodName = method.getName(); 83 JClass []paramTypes = method.getParameterTypes(); 84 85 if (methodName.startsWith("ejbCreate")) { 86 String apiMethodName = "c" + methodName.substring(4); 87 88 JMethod apiMethod = EjbBean.getMethod(getApiClass(), 89 apiMethodName, 90 paramTypes); 91 92 if (apiMethod == null) { 93 103 log.config(errorMissingMethod(getApiClass(), apiMethodName, method).getMessage()); 105 return null; 106 } 107 108 String postMethodName = "ejbPost" + methodName.substring(3); 109 110 JMethod postCreateMethod = EjbBean.getMethod(getImplClass(), 111 postMethodName, 112 method.getParameterTypes()); 113 114 return new EjbEntityCreateMethod(this, apiMethod, 115 method, postCreateMethod); 116 } 117 else if (methodName.startsWith("ejbFind")) { 118 125 126 if (methodName.equals("ejbFindByPrimaryKey")) { 127 _hasFindByPrimaryKey = true; 128 129 JClass primKeyClass = ((EjbEntityBean) getBean()).getPrimKeyClass(); 130 131 if (paramTypes.length != 1 || ! paramTypes[0].equals(primKeyClass)) 132 throw error(L.l("{0}: '{1}' expected as only argument of {2}. ejbFindByPrimaryKey must take the primary key as its only argument.", 133 method.getDeclaringClass().getName(), 134 primKeyClass.getName(), 135 methodName)); 136 } 137 138 String apiMethodName = "f" + methodName.substring(4); 139 140 JMethod apiMethod = EjbBean.getMethod(getApiClass(), 141 apiMethodName, 142 paramTypes); 143 144 if (apiMethod != null) 145 return new EjbEntityFindMethod(this, apiMethod, method); 146 } 147 else if (methodName.startsWith("ejbHome")) { 148 String apiMethodName = (Character.toLowerCase(methodName.charAt(7)) + 150 methodName.substring(8)); 151 152 JMethod apiMethod = EjbBean.getMethod(getApiClass(), 153 apiMethodName, 154 paramTypes); 155 156 if (apiMethod == null) { 157 log.config(errorMissingMethod(getApiClass(), apiMethodName, method).getMessage()); 158 return null; 159 } 160 161 validateImplMethod(method); 162 163 return new EjbMethod(this, apiMethod, method); 164 } 165 166 return null; 167 } 168 169 172 protected EjbMethod introspectApiMethod(JMethod apiMethod) 173 throws ConfigException 174 { 175 String methodName = apiMethod.getName(); 176 JClass []paramTypes = apiMethod.getParameterTypes(); 177 178 if (methodName.equals("findByPrimaryKey")) { 179 _hasFindByPrimaryKey = true; 180 181 JClass primKeyClass = ((EjbEntityBean) getBean()).getPrimKeyClass(); 182 183 if (paramTypes.length != 1 || ! paramTypes[0].equals(primKeyClass)) 184 throw error(L.l("{0}: `{1}' expected as only argument of {2}. findByPrimaryKey must take the primary key as its only argument.", 185 apiMethod.getDeclaringClass().getName(), 186 primKeyClass.getName(), 187 methodName)); 188 189 190 return new EjbEntityFindMethod(this, apiMethod); 191 } 192 else if (methodName.startsWith("find")) { 193 if (isCMP()) { 194 EjbMethodPattern pattern = findMethodPattern(apiMethod, 195 getPrefix()); 196 197 if (pattern == null) 198 throw error(L.l("{0}: '{1}' expects an ejb-ql query. All find methods need queries defined in the EJB deployment descriptor.", 199 apiMethod.getDeclaringClass().getName(), 200 getFullMethodName(apiMethod))); 201 202 String query = pattern.getQuery(); 203 204 EjbAmberFindMethod findMethod; 205 findMethod = new EjbAmberFindMethod(this, apiMethod, query, 206 pattern.getQueryLocation()); 207 208 findMethod.setQueryLoadsBean(pattern.getQueryLoadsBean()); 209 210 return findMethod; 211 } 212 213 String name = methodName; 214 name = Character.toUpperCase(name.charAt(0)) + name.substring(1); 215 216 throw errorMissingMethod(getImplClass(), "ejb" + name, apiMethod); 217 } 218 else if (methodName.startsWith("create")) { 219 String name = apiMethod.getName(); 220 221 name = Character.toUpperCase(name.charAt(0)) + name.substring(1); 222 223 throw errorMissingMethod(getImplClass(), "ejb" + name, apiMethod); 224 } 225 else if (methodName.startsWith("ejb")) { 226 throw new ConfigException(L.l("{0}: '{1}' forbidden. ejbXXX methods are reserved by the EJB specification.", 227 apiMethod.getDeclaringClass().getName(), 228 getFullMethodName(apiMethod))); 229 } 230 else if (methodName.startsWith("remove")) { 231 throw new ConfigException(L.l("{0}: '{1}' forbidden. removeXXX methods are reserved by the EJB specification.", 232 apiMethod.getDeclaringClass().getName(), 233 getFullMethodName(apiMethod))); 234 } 235 else { 236 String name = apiMethod.getName(); 237 238 name = Character.toUpperCase(name.charAt(0)) + name.substring(1); 239 240 throw errorMissingMethod(getImplClass(), "ejbHome" + name, apiMethod); 241 } 242 } 243 } 244 | Popular Tags |