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 EjbCmpView extends EjbEntityView { 44 private static final Logger log = Log.open(EjbCmpView.class); 45 private static final L10N L = new L10N(EjbCmpView.class); 46 47 private EjbEntityBean _entityBean; 48 49 52 public EjbCmpView(EjbEntityBean bean, JClass apiClass, String prefix) 53 throws ConfigException 54 { 55 super(bean, apiClass, prefix); 56 57 _entityBean = bean; 58 } 59 60 63 protected EjbMethod introspectEJBMethod(JMethod method) 64 throws ConfigException 65 { 66 String methodName = method.getName(); 67 JClass []paramTypes = method.getParameterTypes(); 68 69 if (methodName.startsWith("ejbSelect") && method.isAbstract()) { 70 _entityBean.addStubMethod(method); 71 72 return null; 73 } 74 75 return super.introspectEJBMethod(method); 76 } 77 78 81 protected EjbMethod createBusinessMethod(JMethod apiMethod, 82 JMethod implMethod) 83 throws ConfigException 84 { 85 String methodName = implMethod.getName(); 86 JClass []paramTypes = implMethod.getParameterTypes(); 87 88 if (methodName.startsWith("get") && 89 methodName.length() > 3 && 90 paramTypes.length == 0) { 91 String fieldName = toFieldName(methodName.substring(3)); 92 93 CmpField field = _entityBean.getCmpField(fieldName); 94 95 if (field != null) { 96 validateCmpMethod(implMethod); 97 98 if (isLocal() && apiMethod.getExceptionTypes().length != 0) { 99 throw new ConfigException(L.l("{0}: '{1}' must not throw {2}. Container managed fields and relations must not throw exceptions.", 100 apiMethod.getDeclaringClass().getName(), 101 EjbBean.getFullMethodName(apiMethod), 102 apiMethod.getExceptionTypes()[0].getName())); 103 } 104 105 if (field.isId()) 106 return new CmpIdGetter(this, apiMethod, implMethod); 107 else 108 return new CmpGetter(this, apiMethod, implMethod); 109 } 110 111 CmrRelation rel = _entityBean.getRelation(fieldName); 112 113 if (rel != null) { 114 validateCmpMethod(implMethod); 115 116 rel.setHasGetter(true); 117 118 return rel.createGetter(this, apiMethod, implMethod); 119 } 120 121 if (! implMethod.isAbstract()) { 122 validateCmpMethod(implMethod); 123 124 126 return new EjbMethod(this, apiMethod, implMethod); 127 } 128 } 129 else if (methodName.startsWith("get") && 130 methodName.length() > 3 && 131 paramTypes.length == 1) { 132 String fieldName = toFieldName(methodName.substring(3)); 133 134 CmrRelation rel = _entityBean.getRelation(fieldName); 135 136 if (rel instanceof CmrMap) { 137 CmrMap map = (CmrMap) rel; 138 139 validateCmpMethod(implMethod); 140 141 rel.setHasGetter(true); 142 143 return new CmpGetter(this, apiMethod, implMethod); 145 } 146 } 147 else if (methodName.startsWith("set") && 148 methodName.length() > 3 && 149 paramTypes.length == 1) { 150 String fieldName = toFieldName(methodName.substring(3)); 151 152 CmpField field = _entityBean.getCmpField(fieldName); 153 154 if (field != null) { 155 validateCmpMethod(implMethod); 156 157 if (isLocal() && apiMethod.getExceptionTypes().length != 0) { 158 throw new ConfigException(L.l("{0}: '{1}' must not throw {2}. Container managed fields and relations must not throw exceptions.", 159 _entityBean.getEJBClass().getName(), 160 EjbBean.getFullMethodName(apiMethod), 161 apiMethod.getExceptionTypes()[0].getName())); 162 } 163 164 return new EjbMethod(this, apiMethod, implMethod); 165 } 166 167 CmrRelation rel = _entityBean.getRelation(fieldName); 168 169 if (rel instanceof CmrOneToMany) { 170 validateCmpMethod(implMethod); 171 172 return new CmpCollectionSetter(this, apiMethod, implMethod); 173 } 175 else if (rel instanceof CmrManyToOne) { 176 validateCmpMethod(implMethod); 177 178 CmrManyToOne manyToOne = (CmrManyToOne) rel; 179 180 return new EjbManyToOneSetMethod(this, apiMethod, implMethod, manyToOne); 181 } 182 else if (rel instanceof CmrManyToMany) { 183 validateCmpMethod(implMethod); 184 185 return new CmpCollectionSetter(this, apiMethod, implMethod); 186 } 188 189 if (! implMethod.isAbstract()) { 190 validateCmpMethod(implMethod); 191 192 return new EjbMethod(this, apiMethod, implMethod); 193 } 194 else 195 throw new ConfigException(L.l("{0}: abstract setter {1}.", 196 implMethod.getDeclaringClass().getName(), 197 getFullMethodName(implMethod))); 198 } 199 200 201 return super.createBusinessMethod(apiMethod, implMethod); 202 } 203 204 protected String toFieldName(String name) 205 { 206 if (name.length() == 0) 207 return ""; 208 else if (name.length() == 1) 209 return String.valueOf(Character.toLowerCase(name.charAt(0))); 210 else if (Character.isUpperCase(name.charAt(1))) 211 return name; 212 else 213 return Character.toLowerCase(name.charAt(0)) + name.substring(1); 214 } 215 216 219 protected void validateCmpMethod(JMethod implMethod) 220 throws ConfigException 221 { 222 if (! implMethod.isPublic()) { 223 throw error(L.l("{0}: `{1}' must be public. CMP method implementations must be public.", 224 implMethod.getDeclaringClass().getName(), 225 getFullMethodName(implMethod))); 226 } 227 228 if (implMethod.isStatic()) { 229 throw error(L.l("{0}: `{1}' must not be static. CMP method implementations must not be static.", 230 implMethod.getDeclaringClass().getName(), 231 getFullMethodName(implMethod))); 232 } 233 } 234 } 235 | Popular Tags |