KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > EjbGenerationUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore;
21
22 import java.io.File JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.TreeSet JavaDoc;
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 /**
38  * This class contains common functionality for code generation
39  * @author Chris Webster
40  * @author Martin Adamek
41  */

42 public class EjbGenerationUtil {
43
44     private static final String JavaDoc[] EJB_NAME_CONTEXTS = new String JavaDoc[] {
45                 EnterpriseBeans.SESSION,
46                 EnterpriseBeans.ENTITY,
47                 EnterpriseBeans.MESSAGE_DRIVEN
48     };
49     
50     public static String JavaDoc getFullClassName(String JavaDoc pkg, String JavaDoc className) {
51         return (pkg==null||pkg.length()==0)?className:pkg+"."+className; //NOI18N
52
}
53     
54     public static String JavaDoc getBaseName(String JavaDoc fullClassName) {
55         return fullClassName.substring(fullClassName.lastIndexOf('.')+1); //NOI18N
56
}
57     
58     public static String JavaDoc[] getPackages(Project project) {
59         Sources sources = ProjectUtils.getSources(project);
60         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
61         Set JavaDoc<String JavaDoc> pkgs = new TreeSet JavaDoc<String JavaDoc>();
62         for (int i = 0; i < groups.length; i++) {
63             findPackages(groups[i].getRootFolder(),"", pkgs);
64         }
65         return pkgs.toArray(new String JavaDoc[pkgs.size()]);
66     }
67     
68     private static void findPackages (FileObject root, String JavaDoc curPkg, Set JavaDoc<String JavaDoc> pkgs) {
69         for (FileObject kid : root.getChildren()) {
70             String JavaDoc 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 JavaDoc ejbName, EjbJar ejbJar) {
77         EnterpriseBeans beans = ejbJar.getEnterpriseBeans();
78         Object JavaDoc 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 JavaDoc pkgName, Project project) {
91         String JavaDoc relativePkgName = pkgName.replace('.', '/');
92         FileObject fileObject = null;
93         fileObject = location.getRootFolder().getFileObject(relativePkgName);
94         if (fileObject != null) {
95             return fileObject;
96         } else {
97             File JavaDoc rootFile = FileUtil.toFile(location.getRootFolder());
98             File JavaDoc pkg = new File JavaDoc(rootFile,relativePkgName);
99             pkg.mkdirs();
100             fileObject = location.getRootFolder().getFileObject(relativePkgName);
101         }
102         return fileObject;
103     }
104
105     public static String JavaDoc 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 JavaDoc 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