1 19 20 package org.netbeans.modules.j2ee.ejbcore; 21 22 import java.util.ArrayList ; 23 import org.netbeans.api.java.source.ElementHandle; 24 import org.netbeans.api.java.source.JavaSource; 25 import org.netbeans.api.project.ant.AntArtifact; 26 import org.netbeans.api.project.ui.OpenProjects; 27 import org.netbeans.modules.j2ee.api.ejbjar.EjbReference; 28 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer; 29 import org.netbeans.modules.j2ee.common.queries.api.InjectionTargetQuery; 30 import org.netbeans.modules.j2ee.common.source.AbstractTask; 31 import org.netbeans.modules.j2ee.common.method.MethodModel; 32 import org.netbeans.modules.j2ee.common.method.MethodModelSupport; 33 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef; 34 import org.netbeans.modules.j2ee.dd.api.common.EjbRef; 35 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 36 import org.openide.DialogDisplayer; 37 import org.openide.NotifyDescriptor; 38 import org.openide.WizardDescriptor; 39 import javax.swing.*; 40 import java.io.File ; 41 import java.io.IOException ; 42 import java.util.Arrays ; 43 import java.util.List ; 44 import java.util.Set ; 45 import javax.lang.model.element.Element; 46 import javax.lang.model.element.ExecutableElement; 47 import javax.lang.model.element.Modifier; 48 import javax.lang.model.element.TypeElement; 49 import org.netbeans.api.java.classpath.ClassPath; 50 import org.netbeans.api.java.project.classpath.ProjectClassPathModifier; 51 import org.netbeans.api.java.source.CompilationController; 52 import org.netbeans.api.java.source.WorkingCopy; 53 import org.netbeans.api.project.FileOwnerQuery; 54 import org.netbeans.api.project.Project; 55 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider; 56 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 57 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EjbMethodController; 58 import org.openide.filesystems.FileObject; 59 60 public class Utils { 61 62 private static final String WIZARD_PANEL_CONTENT_DATA = "WizardPanel_contentData"; private static final String WIZARD_PANEL_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; 65 public static String toClasspathString(File [] classpathEntries) { 66 if (classpathEntries == null) { 67 return ""; 68 } 69 StringBuffer classpath = new StringBuffer (); 70 for (int i = 0; i < classpathEntries.length; i++) { 71 classpath.append(classpathEntries[i].getAbsolutePath()); 72 if (i + 1 < classpathEntries.length) { 73 classpath.append(':'); 74 } 75 } 76 return classpath.toString(); 77 } 78 79 public static void notifyError(Exception exception) { 80 NotifyDescriptor ndd = new NotifyDescriptor.Message(exception.getMessage(), NotifyDescriptor.ERROR_MESSAGE); 81 DialogDisplayer.getDefault().notify(ndd); 82 } 83 84 public static void mergeSteps(WizardDescriptor wizard, WizardDescriptor.Panel[] panels, String [] steps) { 85 Object prop = wizard.getProperty(WIZARD_PANEL_CONTENT_DATA); 86 String [] beforeSteps; 87 int offset; 88 if (prop instanceof String []) { 89 beforeSteps = (String []) prop; 90 offset = beforeSteps.length; 91 if (offset > 0 && ("...".equals(beforeSteps[offset - 1]))) { offset--; 93 } 94 } else { 95 beforeSteps = null; 96 offset = 0; 97 } 98 String [] resultSteps = new String [ (offset) + panels.length]; 99 System.arraycopy(beforeSteps, 0, resultSteps, 0, offset); 100 setSteps(panels, steps, resultSteps, offset); 101 } 102 103 private static void setSteps(WizardDescriptor.Panel[] panels, String [] steps, String [] resultSteps, int offset) { 104 int numberOfSteps = steps == null ? 0 : steps.length; 105 for (int i = 0; i < panels.length; i++) { 106 final JComponent component = (JComponent) panels[i].getComponent(); 107 String step = i < numberOfSteps ? steps[i] : null; 108 if (step == null) { 109 step = component.getName(); 110 } 111 component.putClientProperty(WIZARD_PANEL_CONTENT_DATA, resultSteps); 112 component.putClientProperty(WIZARD_PANEL_CONTENT_SELECTED_INDEX, Integer.valueOf(i)); 113 component.getAccessibleContext().setAccessibleDescription(step); 114 resultSteps[i + offset] = step; 115 } 116 } 117 118 public static void setSteps(WizardDescriptor.Panel[] panels, String [] steps) { 119 setSteps(panels, steps, steps, 0); 120 } 121 122 public static boolean areInSameJ2EEApp(Project project1, Project project2) { 123 Project[] openProjects = OpenProjects.getDefault().getOpenProjects(); 124 for (int i = 0; i < openProjects.length; i++) { 125 Project project = openProjects[i]; 126 Object j2eeAppProvider = project.getLookup().lookup(J2eeAppProvider.class); 127 if (j2eeAppProvider != null) { J2eeAppProvider j2eeApp = (J2eeAppProvider)j2eeAppProvider; 129 J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders(); 130 if ((j2eeModules != null) && (j2eeModules.length > 0)) { J2eeModuleProvider affectedPrjProvider1 = 132 (J2eeModuleProvider)project1.getLookup().lookup(J2eeModuleProvider.class); 133 J2eeModuleProvider affectedPrjProvider2 = 134 (J2eeModuleProvider)project2.getLookup().lookup(J2eeModuleProvider.class); 135 if (affectedPrjProvider1 != null && affectedPrjProvider2 != null) { 136 List childModules = Arrays.asList(j2eeModules); 137 if (childModules.contains(affectedPrjProvider1) && 138 childModules.contains(affectedPrjProvider2)) { 139 return true; 140 } 141 } 142 } 143 } 144 } 145 return false; 146 } 147 148 150 152 public static boolean canExposeInLocal(FileObject ejbClassFO, final ElementHandle<ExecutableElement> methodHandle) throws IOException { 153 JavaSource javaSource = JavaSource.forFileObject(ejbClassFO); 154 final String [] ejbClassName = new String [1]; 155 final MethodModel[] methodModel = new MethodModel[1]; 156 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 157 public void run(WorkingCopy workingCopy) throws IOException { 158 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 159 ExecutableElement executableElement = methodHandle.resolve(workingCopy); 160 Set <Modifier> modifiers = executableElement.getModifiers(); 161 boolean signatureOk = modifiers.contains(Modifier.PUBLIC) && !modifiers.contains(Modifier.STATIC); 162 if (signatureOk) { 163 Element enclosingElement = executableElement.getEnclosingElement(); 164 ejbClassName[0] = ((TypeElement) enclosingElement).getQualifiedName().toString(); 165 methodModel[0] = MethodModelSupport.createMethodModel(workingCopy, executableElement); 166 } 167 } 168 }); 169 if (methodModel[0] != null) { 170 EjbMethodController ejbMethodController = EjbMethodController.createFromClass(ejbClassFO, ejbClassName[0]); 171 return ejbMethodController != null && ejbMethodController.hasLocal() && !ejbMethodController.hasMethodInInterface(methodModel[0], ejbMethodController.getMethodTypeFromImpl(methodModel[0]), true); 172 } 173 return false; 174 } 175 176 public static void exposeInLocal(FileObject ejbClassFO, final ElementHandle<ExecutableElement> methodHandle) throws IOException { 177 JavaSource javaSource = JavaSource.forFileObject(ejbClassFO); 178 final String [] ejbClassName = new String [1]; 179 final MethodModel[] methodModel = new MethodModel[1]; 180 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 181 public void run(WorkingCopy workingCopy) throws IOException { 182 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 183 ExecutableElement method = methodHandle.resolve(workingCopy); 184 Element enclosingElement = method.getEnclosingElement(); 185 ejbClassName[0] = ((TypeElement) enclosingElement).getQualifiedName().toString(); 186 methodModel[0] = MethodModelSupport.createMethodModel(workingCopy, method); 187 } 188 }); 189 EjbMethodController ejbMethodController = EjbMethodController.createFromClass(ejbClassFO, ejbClassName[0]); 190 ejbMethodController.createAndAddInterface(methodModel[0], true); 191 } 192 193 public static boolean canExposeInRemote(FileObject ejbClassFO, final ElementHandle<ExecutableElement> methodHandle) throws IOException { 194 JavaSource javaSource = JavaSource.forFileObject(ejbClassFO); 195 final String [] ejbClassName = new String [1]; 196 final MethodModel[] methodModel = new MethodModel[1]; 197 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 198 public void run(WorkingCopy workingCopy) throws IOException { 199 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 200 ExecutableElement executableElement = methodHandle.resolve(workingCopy); 201 Set <Modifier> modifiers = executableElement.getModifiers(); 202 boolean signatureOk = modifiers.contains(Modifier.PUBLIC) && !modifiers.contains(Modifier.STATIC); 203 if (signatureOk) { 204 Element enclosingElement = executableElement.getEnclosingElement(); 205 ejbClassName[0] = ((TypeElement) enclosingElement).getQualifiedName().toString(); 206 methodModel[0] = MethodModelSupport.createMethodModel(workingCopy, executableElement); 207 } 208 } 209 }); 210 if (methodModel[0] != null) { 211 EjbMethodController ejbMethodController = EjbMethodController.createFromClass(ejbClassFO, ejbClassName[0]); 212 return ejbMethodController != null && ejbMethodController.hasRemote() && !ejbMethodController.hasMethodInInterface(methodModel[0], ejbMethodController.getMethodTypeFromImpl(methodModel[0]), true); 213 } 214 return false; 215 } 216 217 public static void exposeInRemote(FileObject ejbClassFO, final ElementHandle<ExecutableElement> methodHandle) throws IOException { 218 JavaSource javaSource = JavaSource.forFileObject(ejbClassFO); 219 final String [] ejbClassName = new String [1]; 220 final MethodModel[] methodModel = new MethodModel[1]; 221 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 222 public void run(WorkingCopy workingCopy) throws IOException { 223 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 224 ExecutableElement method = methodHandle.resolve(workingCopy); 225 Element enclosingElement = method.getEnclosingElement(); 226 ejbClassName[0] = ((TypeElement) enclosingElement).getQualifiedName().toString(); 227 methodModel[0] = MethodModelSupport.createMethodModel(workingCopy, method); 228 } 229 }); 230 EjbMethodController ejbMethodController = EjbMethodController.createFromClass(ejbClassFO, ejbClassName[0]); 231 ejbMethodController.createAndAddInterface(methodModel[0], false); 232 } 233 234 public static void addReference(FileObject fileObject, String className, EjbReference ref, String serviceLocator, boolean remote, 235 boolean throwExceptions, String ejbRefName, Project nodeProject) throws IOException { 236 Project enterpriseProject = FileOwnerQuery.getOwner(fileObject); 238 EnterpriseReferenceContainer erc = (EnterpriseReferenceContainer) 239 enterpriseProject.getLookup().lookup(EnterpriseReferenceContainer.class); 240 241 boolean enterpriseProjectIsJavaEE5 = isJavaEE5orHigher(enterpriseProject); 242 boolean nodeProjectIsJavaEE5 = isJavaEE5orHigher(nodeProject); 243 244 if (remote) { 245 EjbRef ejbRef = ref.createRef(); 246 if (ejbRefName != null) { 247 ejbRef.setEjbRefName(ejbRefName); 248 } 249 if (enterpriseProjectIsJavaEE5 && InjectionTargetQuery.isInjectionTarget(fileObject, className)) { 250 addProjectToClassPath(enterpriseProject, ref); 251 } else if (nodeProjectIsJavaEE5 == enterpriseProjectIsJavaEE5){ erc.addEjbReference(ejbRef, fileObject, className, ref.getClientJarTarget()); 253 } 254 if (serviceLocator == null) { 255 ref.generateReferenceCode(fileObject, ejbRef, throwExceptions); 258 } else { 259 ref.generateServiceLocatorLookup(fileObject, ejbRef, serviceLocator, throwExceptions); 262 } 263 } else { 264 EjbLocalRef ejbLocalRef = ref.createLocalRef(); 265 if (ejbRefName != null) { 266 ejbLocalRef.setEjbRefName(ejbRefName); 267 } 268 if (enterpriseProjectIsJavaEE5 && InjectionTargetQuery.isInjectionTarget(fileObject, className)) { 269 addProjectToClassPath(enterpriseProject, ref); 270 } else if (nodeProjectIsJavaEE5 == enterpriseProjectIsJavaEE5){ erc.addEjbLocalReference(ejbLocalRef, fileObject, className, ref.getClientJarTarget()); 272 } 273 if (serviceLocator == null) { 274 ref.generateReferenceCode(fileObject, ejbLocalRef, throwExceptions); 277 } else { 278 ref.generateServiceLocatorLookup(fileObject, ejbLocalRef, serviceLocator, throwExceptions); 281 } 282 } 283 if (serviceLocator != null) { 284 erc.setServiceLocatorName(serviceLocator); 285 } 286 } 287 288 private static void addProjectToClassPath(final Project enterpriseProject, final EjbReference ref) throws IOException { 289 AntArtifact target = ref.getClientJarTarget(); 290 boolean differentProject = target != null && !enterpriseProject.equals(target.getProject()); 291 if (differentProject) { 292 ProjectClassPathModifier cpModifier = enterpriseProject.getLookup().lookup(ProjectClassPathModifier.class); 293 if (cpModifier != null) { 294 cpModifier.addAntArtifacts( 295 new AntArtifact[] {target}, 296 target.getArtifactLocations(), 297 enterpriseProject.getProjectDirectory(), 298 ClassPath.COMPILE 299 ); 300 } 301 } 302 } 303 304 308 public static Project [] getCallableEjbProjects(Project enterpriseProject) { 309 Project[] allProjects = OpenProjects.getDefault().getOpenProjects(); 310 311 boolean isCallerFreeform = enterpriseProject.getClass().getName().equals("org.netbeans.modules.ant.freeform.FreeformProject"); 314 315 List <Project> filteredResults = new ArrayList <Project>(allProjects.length); 316 for (int i = 0; i < allProjects.length; i++) { 317 boolean isEJBModule = false; 318 J2eeModuleProvider j2eeModuleProvider = allProjects[i].getLookup().lookup(J2eeModuleProvider.class); 319 if (j2eeModuleProvider != null && j2eeModuleProvider.getJ2eeModule().getModuleType().equals(J2eeModule.EJB)) { 320 isEJBModule = true; 321 } 322 if ((isEJBModule && !isCallerFreeform) || 323 (isCallerFreeform && enterpriseProject.equals(allProjects[i]))) { 324 filteredResults.add(allProjects[i]); 325 } 326 } 327 return filteredResults.toArray(new Project[filteredResults.size()]); 328 } 329 330 public static boolean isJavaEE5orHigher(Project project) { 333 if (project == null) { 334 return false; 335 } 336 J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 337 if (j2eeModuleProvider != null) { 338 J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule(); 339 if (j2eeModule != null) { 340 Object type = j2eeModule.getModuleType(); 341 double version = Double.parseDouble(j2eeModule.getModuleVersion()); 342 if (J2eeModule.EJB.equals(type) && (version > 2.1)) { 343 return true; 344 } 345 if (J2eeModule.WAR.equals(type) && (version > 2.4)) { 346 return true; 347 } 348 if (J2eeModule.CLIENT.equals(type) && (version > 1.4)) { 349 return true; 350 } 351 } 352 } 353 return false; 354 } 355 356 public static boolean isAppClient(Project project) { 357 J2eeModuleProvider module = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 358 return (module != null) ? module.getJ2eeModule().getModuleType().equals(J2eeModule.CLIENT) : false; 359 } 360 361 364 public static boolean isTargetJavaSE(FileObject fileObject, final String className) throws IOException { 365 Project owner = FileOwnerQuery.getOwner(fileObject); 366 if (owner.getLookup().lookup(J2eeModuleProvider.class) == null){ 367 return true; 368 } 369 JavaSource javaSource = JavaSource.forFileObject(fileObject); 370 final boolean[] result = new boolean[] { false }; 371 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 372 public void run(CompilationController controller) throws IOException { 373 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 374 TypeElement typeElement = controller.getElements().getTypeElement(className); 375 TypeElement junitTestCase = controller.getElements().getTypeElement("junit.framework.TestCase"); 376 if (junitTestCase != null) { 377 result[0] = controller.getTypes().isSubtype(typeElement.asType(), junitTestCase.asType()); 378 } 379 } 380 }, true); 381 return result[0]; 382 } 383 384 399 413 public static String jndiNameToCamelCase(String jndiName, boolean lowerCaseFirstChar, String prefixToStrip){ 414 415 String strippedJndiName = jndiName; 416 if (prefixToStrip != null && jndiName.startsWith(prefixToStrip)){ 417 strippedJndiName = jndiName.substring(jndiName.indexOf(prefixToStrip) + prefixToStrip.length()); 418 } 419 420 StringBuilder result = new StringBuilder (); 421 422 for (String token : strippedJndiName.split("/")){ 423 if (token.length() == 0){ 424 continue; 425 } 426 char firstChar = token.charAt(0); 427 if (lowerCaseFirstChar && result.length() == 0 && !isAllUpperCase(token)){ 428 firstChar = Character.toLowerCase(firstChar); 429 } else { 430 firstChar = Character.toUpperCase(firstChar); 431 } 432 result.append(firstChar); 433 result.append(token.substring(1)); 434 } 435 436 return result.toString(); 437 } 438 439 443 private static boolean isAllUpperCase(String str){ 444 if (str.length() <= 1){ 445 return false; 446 } 447 for (char c : str.toCharArray()) { 448 if (Character.isLowerCase(c)){ 449 return false; 450 } 451 } 452 return true; 453 } 454 455 479 } 480 | Popular Tags |