1 19 20 package org.netbeans.modules.j2ee.ejbcore; 21 22 import java.io.File ; 23 import java.util.Set ; 24 import java.util.TreeSet ; 25 import org.netbeans.api.java.project.JavaProjectConstants; 26 import org.netbeans.api.project.FileOwnerQuery; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.api.project.ProjectUtils; 29 import org.netbeans.api.project.SourceGroup; 30 import org.netbeans.api.project.Sources; 31 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb; 32 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 33 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 37 42 public class EjbGenerationUtil { 43 44 private static final String [] EJB_NAME_CONTEXTS = new String [] { 45 EnterpriseBeans.SESSION, 46 EnterpriseBeans.ENTITY, 47 EnterpriseBeans.MESSAGE_DRIVEN 48 }; 49 50 public static String getFullClassName(String pkg, String className) { 51 return (pkg==null||pkg.length()==0)?className:pkg+"."+className; } 53 54 public static String getBaseName(String fullClassName) { 55 return fullClassName.substring(fullClassName.lastIndexOf('.')+1); } 57 58 public static String [] getPackages(Project project) { 59 Sources sources = ProjectUtils.getSources(project); 60 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 61 Set <String > pkgs = new TreeSet <String >(); 62 for (int i = 0; i < groups.length; i++) { 63 findPackages(groups[i].getRootFolder(),"", pkgs); 64 } 65 return pkgs.toArray(new String [pkgs.size()]); 66 } 67 68 private static void findPackages (FileObject root, String curPkg, Set <String > pkgs) { 69 for (FileObject kid : root.getChildren()) { 70 String name = curPkg + (curPkg.length() != 0 ? "." : "") + kid.getName(); 71 pkgs.add (name); 72 findPackages (kid, name, pkgs); 73 } 74 } 75 76 public static boolean isEjbNameInDD(String ejbName, EjbJar ejbJar) { 77 EnterpriseBeans beans = ejbJar.getEnterpriseBeans(); 78 Object ejb = null; 79 if (beans != null) { 80 for (int i = 0; i < EJB_NAME_CONTEXTS.length; i++) { 81 ejb = beans.findBeanByName(EJB_NAME_CONTEXTS[i], Ejb.EJB_NAME, ejbName); 82 if (ejb != null) { 83 break; 84 } 85 } 86 } 87 return beans != null && ejb != null; 88 } 89 90 public static FileObject getPackageFileObject(SourceGroup location, String pkgName, Project project) { 91 String relativePkgName = pkgName.replace('.', '/'); 92 FileObject fileObject = null; 93 fileObject = location.getRootFolder().getFileObject(relativePkgName); 94 if (fileObject != null) { 95 return fileObject; 96 } else { 97 File rootFile = FileUtil.toFile(location.getRootFolder()); 98 File pkg = new File (rootFile,relativePkgName); 99 pkg.mkdirs(); 100 fileObject = location.getRootFolder().getFileObject(relativePkgName); 101 } 102 return fileObject; 103 } 104 105 public static String getSelectedPackageName(FileObject targetFolder) { 106 Project project = FileOwnerQuery.getOwner(targetFolder); 107 Sources sources = ProjectUtils.getSources(project); 108 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 109 String packageName = null; 110 for (int i = 0; i < groups.length && packageName == null; i++) { 111 packageName = FileUtil.getRelativePath(groups [i].getRootFolder(), targetFolder); 112 } 113 if (packageName != null) { 114 packageName = packageName.replaceAll("/", "."); 115 } 116 return packageName+""; 117 } 118 119 } 120 | Popular Tags |