1 5 package xdoclet.modules.ejb.env; 6 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 import java.util.Properties ; 12 13 import org.apache.commons.logging.Log; 14 import xjavadoc.XClass; 15 import xjavadoc.XTag; 16 17 import xdoclet.XDocletException; 18 import xdoclet.modules.ejb.EjbTagsHandler; 19 import xdoclet.modules.ejb.XDocletModulesEjbMessages; 20 import xdoclet.modules.ejb.home.HomeTagsHandler; 21 import xdoclet.modules.ejb.intf.InterfaceTagsHandler; 22 import xdoclet.util.LogUtil; 23 import xdoclet.util.Translator; 24 25 34 public class EnvEjbRefTagsHandler extends EnvTagsHandler 35 { 36 37 44 private String referringClassId; 45 private Map already = new HashMap (); 46 private XClass refedEJBClass; 47 48 57 public String ejbRefId() throws XDocletException 58 { 59 return referringClassId + '_' + EjbTagsHandler.getEjbIdFor(refedEJBClass); 60 } 61 62 71 public void forAllEjbRefs(String template, Properties attributes) throws XDocletException 72 { 73 already.clear(); 74 75 if (attributes.getProperty("tagName") == null) { 76 attributes.setProperty("tagName", "ejb.ejb-ref"); 77 } 78 79 forTags(template, attributes, true, true, true); 80 81 already.clear(); 82 } 83 84 93 public void forAllEjbRefMembers(String template, Properties attributes) throws XDocletException 94 { 95 already.clear(); 96 97 if (attributes.getProperty("tagName") == null) { 98 attributes.setProperty("tagName", "ejb.ejb-ref"); 99 } 100 101 forTags(template, attributes, false, true, true); 102 103 already.clear(); 104 } 105 106 115 public void forAllEjbRefMethods(String template, Properties attributes) throws XDocletException 116 { 117 already.clear(); 118 119 if (attributes.getProperty("tagName") == null) { 120 attributes.setProperty("tagName", "ejb.ejb-ref"); 121 } 122 123 forTags(template, attributes, false, true, false); 124 125 already.clear(); 126 } 127 128 135 public String ejbRefJndiName() throws XDocletException 136 { 137 String ejbRefJndiName = null; 138 139 String jndiNameParameter = currentTag.getAttributeValue("jndi-name"); 140 141 if (jndiNameParameter != null) { 142 ejbRefJndiName = jndiNameParameter; 143 } 144 else { 145 String refed_ejb_name = currentTag.getAttributeValue("ejb-name"); 146 147 if (refed_ejb_name == null) { 148 throw new XDocletException("No ejb-name attribute found in ejb-ref specified in bean " + getCurrentClass()); 149 } 150 151 XClass refed_clazz = findEjb(refed_ejb_name); 152 String ejb_type = EjbTagsHandler.isLocalEjb(refed_clazz) ? "local" : "remote"; 153 154 ejbRefJndiName = HomeTagsHandler.getJndiNameOfTypeFor(ejb_type, refed_clazz); 155 156 } 157 158 return ejbRefJndiName; 159 } 160 161 168 public void ifLocalEjbRef(String template) throws XDocletException 169 { 170 if (isLocalEjbRef(currentTag)) { 171 generate(template); 172 } 173 } 174 175 182 public void ifRemoteEjbRef(String template) throws XDocletException 183 { 184 if (isRemoteEjbRef(currentTag)) { 185 generate(template); 186 } 187 } 188 189 public String name(Properties attributes) throws XDocletException 190 { 191 if (currentMember == null) { 192 return EjbTagsHandler.ejbRefName(currentTag, refedEJBClass); 193 } 194 else { 195 attributes.setProperty("paramName", "ref-name"); 196 return super.name(attributes); 197 } 198 } 199 200 207 public String homeInterface() throws XDocletException 208 { 209 String intf; 210 211 if ("ejb.ejb-external-ref".equals(currentTag.getName())) { 212 intf = currentTag.getAttributeValue("home"); 213 if (intf == null) { 214 mandatoryParamNotFound(currentTag.getDoc(), "home", "ejb.ejb-external-ref"); 215 } 216 } 217 else { 218 String type = isRemoteEjbRef(currentTag) ? "remote" : "local"; 219 220 intf = HomeTagsHandler.getHomeInterface(type, getCurrentClass()); 221 } 222 return intf; 223 } 224 225 232 public String componentInterface() throws XDocletException 233 { 234 String intf; 235 236 if ("ejb.ejb-external-ref".equals(currentTag.getName())) { 237 intf = currentTag.getAttributeValue("business"); 238 if (intf == null) { 239 intf = currentTag.getAttributeValue("remote"); 240 } 241 if (intf == null) { 242 mandatoryParamNotFound(currentTag.getDoc(), "business", "ejb.ejb-external-ref"); 243 } 244 } 245 else { 246 String type = isRemoteEjbRef(currentTag) ? "remote" : "local"; 247 248 intf = InterfaceTagsHandler.getComponentInterface(type, getCurrentClass()); 249 } 250 return intf; 251 } 252 253 260 protected boolean isLocalEjbRef(XTag ejbRefTag) throws XDocletException 261 { 262 String viewTypeParameter = ejbRefTag.getAttributeValue("view-type"); 263 264 if (viewTypeParameter == null) { 265 return EjbTagsHandler.isLocalEjb(refedEJBClass) && !EjbTagsHandler.isRemoteEjb(refedEJBClass); 266 278 } 279 else { 280 return "local".equals(viewTypeParameter); 281 } 282 } 283 284 291 protected boolean isRemoteEjbRef(XTag ejbRefTag) throws XDocletException 292 { 293 return !isLocalEjbRef(ejbRefTag); 294 } 295 296 300 protected void doGenerate(String template) throws XDocletException 301 { 302 Log log = LogUtil.getLog(EnvEjbRefTagsHandler.class, "doGenerate"); 303 304 storeReferringClassId(); 305 306 String ejbNameAttribute = currentTag.getAttributeValue("ejb-name"); 307 308 if ("ejb.ejb-ref".equals(currentTag.getName())) { 309 if (ejbNameAttribute == null || ejbNameAttribute.length() < 1) { 310 mandatoryParamNotFound(currentTag.getDoc(), "ejb-name", "ejb.ejb-ref"); 311 } 312 refedEJBClass = findEjb(ejbNameAttribute); 313 } 314 315 String refName = name(new Properties ()); 316 317 if (!already.containsKey(refName)) { 318 already.put(refName, currentTag); 319 if (refedEJBClass != null) { 320 pushCurrentClass(refedEJBClass); 321 } 322 generate(template); 323 if (refedEJBClass != null) { 324 popCurrentClass(); 325 } 326 } 327 else { 328 XTag previousTag = (XTag) already.get(refName); 329 330 String prevEjbName = previousTag.getAttributeValue("ejb-name"); 331 332 if (prevEjbName == null) 333 prevEjbName = ""; 334 335 String prevJndiName = previousTag.getAttributeValue("jndi-name"); 336 337 if (prevJndiName == null) 338 prevJndiName = ""; 339 340 if (!(prevEjbName.equals(currentTag.getAttributeValue("ejb-name")) || 341 prevJndiName.equals(currentTag.getAttributeValue("jndi-name")))) { 342 log.error("Duplicate @ejb.ejb-ref found with different parameters!"); 343 log.error("Previous tag: @ejb.ejb-ref ref-name=\"" + 344 previousTag.getAttributeValue("ref-name") + "\" ejb-name=\"" + 345 previousTag.getAttributeValue("ejb-name") + "\" view-type=\"" + 346 previousTag.getAttributeValue("view-type") + "\""); 347 log.error("Current tag: @ejb.ejb-ref ref-name=\"" + 348 currentTag.getAttributeValue("ref-name") + "\" ejb-name=\"" + 349 currentTag.getAttributeValue("ejb-name") + "\" view-type=\"" + 350 currentTag.getAttributeValue("view-type") + "\""); 351 throw new XDocletException("Duplicate @ejb.ejb-ref with different parameters"); 352 } 353 else { 354 log.warn("Duplicated @ejb.ejb-ref found, ref-name=\"" + refName + "\""); 355 } 356 } 357 358 referringClassId = null; 359 360 } 361 362 368 protected void storeReferringClassId() throws XDocletException 369 { 370 referringClassId = EjbTagsHandler.getEjbIdFor(getCurrentClass()); 371 } 372 373 380 protected XClass findEjb(String ejbName) throws XDocletException 381 { 382 Collection classes = getXJavaDoc().getSourceClasses(); 383 384 for (Iterator i = classes.iterator(); i.hasNext(); ) { 385 XClass clazz = (XClass) i.next(); 386 387 if (EjbTagsHandler.isEjb(clazz) && ejbName.equals(EjbTagsHandler.getEjbNameFor(clazz))) { 388 return clazz; 389 } 390 } 391 392 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.NOT_DEFINED, new String []{ejbName})); 393 } 394 } 395 | Popular Tags |