1 19 20 package org.netbeans.modules.j2ee.spi.ejbjar.support; 21 22 import java.io.IOException ; 23 import org.netbeans.api.project.Project; 24 import org.netbeans.api.project.ProjectManager; 25 import org.netbeans.api.project.ant.AntArtifact; 26 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef; 27 import org.netbeans.modules.j2ee.dd.api.common.EjbRef; 28 import org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef; 29 import org.netbeans.modules.j2ee.dd.api.common.ResourceRef; 30 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException; 31 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer; 32 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 33 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb; 34 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 35 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans; 36 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 37 import org.netbeans.spi.project.support.ant.AntProjectHelper; 38 import org.netbeans.spi.project.support.ant.EditableProperties; 39 import org.openide.ErrorManager; 40 import org.openide.filesystems.FileObject; 41 import org.openide.util.NbBundle; 42 43 44 48 public final class EjbEnterpriseReferenceContainerSupport { 49 50 private EjbEnterpriseReferenceContainerSupport() { 51 } 52 53 public static EnterpriseReferenceContainer createEnterpriseReferenceContainer(Project project, AntProjectHelper helper) { 54 return new ERC(project, helper); 55 } 56 57 private static class ERC implements EnterpriseReferenceContainer { 58 59 private Project ejbProject; 60 private AntProjectHelper antHelper; 61 private static final String SERVICE_LOCATOR_PROPERTY = "project.serviceLocator.class"; 63 private ERC(Project p, AntProjectHelper helper) { 64 ejbProject = p; 65 antHelper = helper; 66 } 67 68 public String addEjbLocalReference(EjbLocalRef localRef, FileObject referencingFile, String referencingClass, AntArtifact target) throws IOException { 69 return addReference(localRef, referencingFile, referencingClass, target); 70 } 71 72 public String addEjbReference(EjbRef ref, FileObject referencingFile, String referencingClass, AntArtifact target) throws IOException { 73 return addReference(ref, referencingFile, referencingClass, target); 74 } 75 76 private String addReference(Object ref, FileObject referencingFile, String referencingClass, AntArtifact target) throws IOException { 77 String refName = null; 78 Ejb model = findEjbForClass(referencingClass); 79 boolean fromSameProject = (target == null || ejbProject.equals(target.getProject())); 82 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJars [] = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(ejbProject); 83 assert ejbJars.length > 0; 84 if (ejbJars[0].getDeploymentDescriptor() != null) { 87 if (model == null) { 88 if (ref instanceof EjbRef) { 89 return ((EjbRef) ref).getEjbRefName(); 90 } else { 91 return ((EjbLocalRef) ref).getEjbRefName(); 92 } 93 } 94 if (ref instanceof EjbRef) { 96 org.netbeans.modules.j2ee.dd.api.common.EjbRef ejbRef = 97 (org.netbeans.modules.j2ee.dd.api.common.EjbRef) ref; 98 refName = getUniqueName(model, Ejb.EJB_REF, 99 ejbRef.EJB_REF_NAME, ejbRef.getEjbRefName()); 100 ejbRef.setEjbRefName(refName); 101 if (fromSameProject) { 102 ejbRef.setEjbLink(stripModuleName(ejbRef.getEjbLink())); 103 } 104 model.addEjbRef(ejbRef); 105 } else { 106 org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ejbRef = 107 (org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef) ref; 108 refName = getUniqueName(model, Ejb.EJB_LOCAL_REF, 109 ejbRef.EJB_REF_NAME, ejbRef.getEjbRefName()); 110 ejbRef.setEjbRefName(refName); 111 if (fromSameProject) { 112 ejbRef.setEjbLink(stripModuleName(ejbRef.getEjbLink())); 113 } 114 model.addEjbLocalRef(ejbRef); 115 } 116 writeDD(); 117 } 118 119 if(!fromSameProject) { 120 try { 121 ProjectClassPathExtender pcpe = ejbProject.getLookup().lookup(ProjectClassPathExtender.class); 122 assert pcpe != null; 123 pcpe.addAntArtifact(target, target.getArtifactLocations()[0]); 124 } catch (IOException ioe) { 125 ErrorManager.getDefault().notify(ioe); 126 } 127 } 128 129 ProjectManager.getDefault().saveProject(ejbProject); 130 return refName; 131 } 132 133 public String getServiceLocatorName() { 134 EditableProperties ep = 135 antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 136 return ep.getProperty(SERVICE_LOCATOR_PROPERTY); 137 } 138 139 public void setServiceLocatorName(String serviceLocator) throws IOException { 140 EditableProperties ep = antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 141 ep.setProperty(SERVICE_LOCATOR_PROPERTY, serviceLocator); 142 antHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 143 ProjectManager.getDefault().saveProject(ejbProject); 144 } 145 146 private String stripModuleName(String ejbLink) { 147 int index = ejbLink.indexOf('#'); 148 return ejbLink.substring(index+1); 149 } 150 151 private Ejb findEjbForClass(String className) throws IOException { 152 EjbJar dd = findDD(); 153 Ejb ejb = null; 154 if (dd != null) { 155 EnterpriseBeans beans = dd.getEnterpriseBeans(); 156 if (beans != null) { 157 ejb = (Ejb) beans.findBeanByName(EnterpriseBeans.SESSION, Ejb.EJB_CLASS, className); 158 if (ejb == null) { 159 ejb = (Ejb) beans.findBeanByName(EnterpriseBeans.ENTITY, Ejb.EJB_CLASS, className); 160 } 161 if (ejb == null) { 162 ejb = (Ejb) beans.findBeanByName(EnterpriseBeans.MESSAGE_DRIVEN, Ejb.EJB_CLASS, className); 163 } 164 } 165 } 166 return ejb; 167 } 168 169 private EjbJar findDD() throws IOException { 170 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJars [] = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(ejbProject); 171 assert ejbJars.length > 0; 172 return DDProvider.getDefault().getMergedDDRoot(ejbJars[0].getMetadataUnit()); 173 } 174 175 private void writeDD() throws IOException { 176 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJars [] = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(ejbProject); 177 assert ejbJars.length > 0; 178 if (isDescriptorMandatory(ejbJars[0].getJ2eePlatformVersion())) { 179 FileObject fo = ejbJars[0].getDeploymentDescriptor(); 180 if (fo != null){ 181 DDProvider.getDefault().getMergedDDRoot(ejbJars[0].getMetadataUnit()).write(fo); 182 } 183 } 184 } 185 186 public String addResourceRef(ResourceRef ref, FileObject referencingFile, String referencingClass) throws IOException { 187 Ejb ejb = findEjbForClass(referencingClass); 188 if (ejb == null) { 189 return ref.getResRefName(); 190 } 191 String resourceRefName = ref.getResRefName(); 192 if (javax.sql.DataSource .class.getName().equals(ref.getResType())) { 193 if (!isJdbcConnectionAlreadyUsed(ejb, ref)) { 194 resourceRefName = getUniqueName(ejb, Ejb.RESOURCE_REF, org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_REF_NAME, ref.getResRefName()); 195 ref.setResRefName(resourceRefName); 196 ejb.addResourceRef((org.netbeans.modules.j2ee.dd.api.common.ResourceRef)ref); 197 writeDD(); 198 } 199 } else { 200 if (!isResourceRefUsed(ejb, ref)) { 201 resourceRefName = getUniqueName(ejb, Ejb.RESOURCE_REF, org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_REF_NAME, ref.getResRefName()); 202 ref.setResRefName(resourceRefName); 203 ejb.addResourceRef((org.netbeans.modules.j2ee.dd.api.common.ResourceRef)ref); 204 writeDD(); 205 } 206 } 207 return resourceRefName; 208 } 209 210 private boolean isJdbcConnectionAlreadyUsed(Ejb ejb, ResourceRef ref) throws IOException { 211 if (javax.sql.DataSource .class.getName().equals(ref.getResType())) { 212 for (ResourceRef existingRef : ejb.getResourceRef()) { 213 String newDefaultDescription = ref.getDefaultDescription(); 214 String existingDefaultDescription = existingRef.getDefaultDescription(); 215 boolean canCompareDefDesc = (newDefaultDescription != null && existingDefaultDescription != null); 216 if (javax.sql.DataSource .class.getName().equals(existingRef.getResType()) && 217 (canCompareDefDesc ? newDefaultDescription.equals(existingDefaultDescription) : true) && 218 ref.getResRefName().equals(existingRef.getResRefName())) { 219 return true; 220 } 221 } 222 } 223 return false; 224 } 225 226 234 private static boolean isResourceRefUsed(Ejb ejb, ResourceRef resRef) { 235 String resRefName = resRef.getResRefName(); 236 String resRefType = resRef.getResType(); 237 for (ResourceRef existingRef : ejb.getResourceRef()) { 238 if (resRefName.equals(existingRef.getResRefName()) && resRefType.equals(existingRef.getResType())) { 239 return true; 240 } 241 } 242 return false; 243 } 244 245 private String getUniqueName(Ejb bean, String beanName, 246 String property, String originalValue) { 247 String proposedValue = originalValue; 248 int index = 1; 249 while (bean.findBeanByName(beanName, property, proposedValue) != null) { 250 proposedValue = originalValue+Integer.toString(index++); 251 } 252 return proposedValue; 253 } 254 255 public ResourceRef createResourceRef(String className) throws IOException { 256 ResourceRef ref = null; 257 Ejb ejb = findEjbForClass(className); 258 if (ejb != null) { 259 ref = ejb.newResourceRef(); 260 } else { 261 try { 262 EjbJar ejbJar = findDD(); 263 if (ejbJar != null) { 264 ref = (ResourceRef) ejbJar.createBean("ResourceRef"); 265 } else { 266 ErrorManager.getDefault().log(ErrorManager.USER, 267 NbBundle.getMessage(EjbEnterpriseReferenceContainerSupport.class, "MSG_MissingMetadata")); 268 } 269 } catch (ClassNotFoundException cnfe) { 270 IOException ioe = new IOException (); 271 ioe.initCause(cnfe); 272 throw ioe; 273 } 274 } 275 return ref; 276 } 277 278 public String addDestinationRef(MessageDestinationRef ref, FileObject referencingFile, String referencingClass) throws IOException { 279 Ejb ejb = findEjbForClass(referencingClass); 280 if (ejb == null) { 281 return ref.getMessageDestinationRefName(); 282 } 283 try { 284 for (MessageDestinationRef mdRef : ejb.getMessageDestinationRef()){ 286 if (mdRef.getMessageDestinationRefName().equals(ref.getMessageDestinationRefName())){ 287 return mdRef.getMessageDestinationRefName(); 288 } 289 } 290 } catch (VersionNotSupportedException ex) { 291 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 292 } 293 294 String destinationRefName = getUniqueName(ejb, Ejb.MESSAGE_DESTINATION_REF, 295 org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef.MESSAGE_DESTINATION_REF_NAME, 296 ref.getMessageDestinationRefName()); 297 ref.setMessageDestinationRefName(destinationRefName); 298 try { 299 ejb.addMessageDestinationRef((org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef)ref); 300 } catch (VersionNotSupportedException vnse) { 301 } 303 writeDD(); 304 return destinationRefName; 305 } 306 307 public MessageDestinationRef createDestinationRef(String className) throws IOException { 308 Ejb ejb = findEjbForClass(className); 309 MessageDestinationRef ref = null; 310 if (ejb != null) { 311 try { 312 ref = ejb.newMessageDestinationRef(); 313 } catch (VersionNotSupportedException vnse) { 314 IOException ioe = new IOException (); 315 ioe.initCause(vnse); 316 throw ioe; 317 } 318 } else { 319 try { 320 ref = (MessageDestinationRef) findDD().createBean("MessageDestinationRef"); 321 } catch (ClassNotFoundException cnfe) { 322 IOException ioe = new IOException (); 323 ioe.initCause(cnfe); 324 throw ioe; 325 } 326 } 327 return ref; 328 } 329 330 } 331 332 private static boolean isDescriptorMandatory(String j2eeVersion) { 333 if ("1.3".equals(j2eeVersion) || "1.4".equals(j2eeVersion)) { 334 return true; 335 } 336 return false; 337 } 338 339 } 340 | Popular Tags |