1 5 package xdoclet.modules.ejb.dd; 6 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Properties ; 11 12 import org.apache.commons.logging.Log; 13 import xjavadoc.XClass; 14 import xjavadoc.XTag; 15 import xdoclet.XDocletException; 16 import xdoclet.modules.ejb.EjbTagsHandler; 17 import xdoclet.modules.ejb.XDocletModulesEjbMessages; 18 import xdoclet.modules.ejb.home.HomeTagsHandler; 19 import xdoclet.util.LogUtil; 20 import xdoclet.util.Translator; 21 import xdoclet.util.TypeConversionUtil; 22 23 33 public class EjbRefTagsHandler extends EjbTagsHandler 34 { 35 42 protected transient String referringClassId; 43 44 53 public String ejbRefId() throws XDocletException 54 { 55 return referringClassId + '_' + EjbTagsHandler.getEjbIdFor(getCurrentClass()); 56 } 57 58 67 public void forAllEjbRefs(String template, Properties attributes) throws XDocletException 68 { 69 Log log = LogUtil.getLog(EjbRefTagsHandler.class, "forAllEjbRefs"); 70 71 boolean superclasses = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), true); 72 73 XClass oldCurClass = getCurrentClass(); 74 75 XClass currentClass = getCurrentClass(); 76 HashMap already = new HashMap (); 77 78 do { 79 Collection tags = currentClass.getDoc().getTags("ejb.ejb-ref"); 80 81 for (Iterator i = tags.iterator(); i.hasNext(); ) { 82 XTag tag = (XTag) i.next(); 83 84 setCurrentClassTag(tag); 85 86 storeReferringClassId(); 87 88 String ejbNameAttribute = tag.getAttributeValue("ejb-name"); 89 90 if (ejbNameAttribute == null || ejbNameAttribute.length() < 1) { 91 mandatoryParamNotFound(tag.getDoc(), "ejb-name", "ejb.ejb-ref"); 92 } 93 94 XClass refedEJBClass = findEjb(ejbNameAttribute); 95 96 setCurrentClass(refedEJBClass); 97 98 String refName = EjbTagsHandler.ejbRefName(); 99 100 if (!already.containsKey(refName)) { 101 already.put(refName, tag); 102 pushCurrentClass(refedEJBClass); 103 generate(template); 104 popCurrentClass(); 105 } 106 else { 107 XTag previousTag = (XTag) already.get(refName); 108 109 if (!previousTag.getAttributeValue("ejb-name").equals(tag.getAttributeValue("ejb-name")) || 110 !previousTag.getAttributeValue("jndi-name").equals(tag.getAttributeValue("jndi-name"))) { 111 log.error("Duplicate @ejb.ejb-ref found with different parameters!"); 112 log.error("Previous tag: @ejb.ejb-ref ref-name=\"" + 113 previousTag.getAttributeValue("ref-name") + "\" ejb-name=\"" + 114 previousTag.getAttributeValue("ejb-name") + "\" view-type=\"" + 115 previousTag.getAttributeValue("view-type") + "\""); 116 log.error("Current tag: @ejb.ejb-ref ref-name=\"" + 117 tag.getAttributeValue("ref-name") + "\" ejb-name=\"" + 118 tag.getAttributeValue("ejb-name") + "\" view-type=\"" + 119 tag.getAttributeValue("view-type") + "\""); 120 throw new XDocletException("Duplicate @ejb.ejb-ref with different parameters"); 121 } 122 else { 123 log.warn("Duplicated @ejb.ejb-ref found, ref-name=\"" + refName + "\""); 124 } 125 } 126 127 setCurrentClassTag(null); 128 referringClassId = null; 129 } 130 if (superclasses == true) { 131 currentClass = currentClass.getSuperclass(); 132 } 133 else { 134 break; 135 } 136 } while (currentClass != null); 137 138 setCurrentClass(oldCurClass); 139 } 140 141 148 public String ejbRefJndiName() throws XDocletException 149 { 150 String ejbRefJndiName = null; 151 152 String jndiNameParameter = getCurrentClassTag().getAttributeValue("jndi-name"); 153 154 if (jndiNameParameter != null) { 155 ejbRefJndiName = jndiNameParameter; 156 } 157 else { 158 String refed_ejb_name = getCurrentClassTag().getAttributeValue("ejb-name"); 159 160 if (refed_ejb_name == null) { 161 throw new XDocletException("No ejb-name attribute found in ejb-ref specified in bean " + getCurrentClass()); 162 } 163 164 XClass refed_clazz = findEjb(refed_ejb_name); 165 String ejb_type = isLocalEjb(refed_clazz) ? "local" : "remote"; 166 167 ejbRefJndiName = HomeTagsHandler.getJndiNameOfTypeFor(ejb_type, refed_clazz); 168 169 } 176 177 return ejbRefJndiName; 178 } 179 180 187 public void ifLocalEjbRef(String template) throws XDocletException 188 { 189 if (isLocalEjbRef(getCurrentClassTag())) { 190 generate(template); 191 } 192 } 193 194 201 public void ifRemoteEjbRef(String template) throws XDocletException 202 { 203 if (isRemoteEjbRef(getCurrentClassTag())) { 204 generate(template); 205 } 206 } 207 208 215 protected boolean isLocalEjbRef(XTag ejbRefTag) throws XDocletException 216 { 217 String viewTypeParameter = ejbRefTag.getAttributeValue("view-type"); 218 219 if (viewTypeParameter == null) { 220 return isLocalEjb(getCurrentClass()) && !isRemoteEjb(getCurrentClass()); 221 } 222 else { 223 return "local".equals(viewTypeParameter); 224 } 225 } 226 227 234 protected boolean isRemoteEjbRef(XTag ejbRefTag) throws XDocletException 235 { 236 return !isLocalEjbRef(ejbRefTag); 237 } 238 239 245 protected void storeReferringClassId() throws XDocletException 246 { 247 referringClassId = EjbTagsHandler.getEjbIdFor(getCurrentClass()); 248 } 249 250 257 protected XClass findEjb(String ejbName) throws XDocletException 258 { 259 Collection classes = getXJavaDoc().getSourceClasses(); 260 261 for (Iterator i = classes.iterator(); i.hasNext(); ) { 262 XClass clazz = (XClass) i.next(); 263 264 if (isEjb(clazz) && ejbName.equals(getEjbNameFor(clazz))) { 265 return clazz; 266 } 267 } 268 269 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.NOT_DEFINED, new String []{ejbName})); 270 } 271 } 272 | Popular Tags |