001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * EjbLoaderHelper.java
043: *
044: * Created on March 9, 2005, 10:10 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import java.io.File;
050: import java.net.URL;
051: import java.net.URLClassLoader;
052: import java.util.ArrayList;
053: import java.util.Arrays;
054: import java.util.Collections;
055: import java.util.Iterator;
056: import java.util.List;
057:
058: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
059: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
060: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
061: import org.netbeans.modules.visualweb.ejb.datamodel.EjbGroup;
062: import org.netbeans.modules.visualweb.ejb.util.JarExploder;
063:
064: /**
065: *
066: * @author cao
067: */
068: public class EjbLoaderHelper {
069:
070: public EjbLoaderHelper() {
071: }
072:
073: public static URLClassLoader getEjbGroupClassLoader(
074: EjbGroup ejbGroup) {
075:
076: try {
077: // The class path has the client jar files plus Java EE classes which contain the
078: // classes/interfaces for EJBs
079:
080: ArrayList<URL> classPathUrls = new ArrayList<URL>();
081:
082: // Client jars
083: List<String> jarFileNames = ejbGroup.getClientJarFiles();
084: for (String fileName : jarFileNames) {
085: URL url = new File(fileName).toURI().toURL();
086: classPathUrls.add(url);
087: }
088:
089: List<File> containerJars = getJavaEEClasspathEntries();
090: for (File file : containerJars) {
091: URL url = file.toURI().toURL();
092: classPathUrls.add(url);
093: }
094:
095: URL[] classPathArray = classPathUrls
096: .toArray(new URL[classPathUrls.size()]);
097: URLClassLoader classloader = URLClassLoader
098: .newInstance(classPathArray);
099: return classloader;
100: } catch (java.net.MalformedURLException e) {
101: // Log error
102: String logMsg = "Error occurred when trying load classes from "
103: + ejbGroup.getClientJarFiles().toString();
104:
105: e.printStackTrace();
106:
107: return null;
108: }
109: }
110:
111: /**
112: * Looks through all installed servers and looks for one that supports EJBs which typically is a
113: * Java EE container. It then returns a list of classpath entries represented by File-s. A File
114: * is typically a jar file but could also be a directory.
115: *
116: * @return List of classpath entries
117: */
118: public static List<File> getJavaEEClasspathEntries() {
119: for (String serverInstanceID : Deployment.getDefault()
120: .getServerInstanceIDs()) {
121: String displayName = Deployment.getDefault()
122: .getServerInstanceDisplayName(serverInstanceID);
123: J2eePlatform j2eePlatform = Deployment.getDefault()
124: .getJ2eePlatform(serverInstanceID);
125: if (displayName != null
126: && j2eePlatform != null
127: && j2eePlatform.getSupportedModuleTypes().contains(
128: J2eeModule.EJB)) {
129: File[] classpath = j2eePlatform.getClasspathEntries();
130: return Arrays.asList(classpath);
131: }
132: }
133: return Collections.emptyList();
134: }
135:
136: /**
137: * Returns true iff an action should be enabled if that action depends on access to EJB API
138: * classes. In NB6, these classes come from an installed app server that is separate from the
139: * IDE itself.
140: *
141: * @return
142: */
143: public static boolean isEnableAction() {
144: return !getJavaEEClasspathEntries().isEmpty();
145: }
146:
147: public static ArrayList getAllClazz(EjbGroup ejbGroup) {
148:
149: try {
150:
151: ArrayList allClazz = new ArrayList();
152:
153: for (Iterator<String> iter = ejbGroup.getClientJarFiles()
154: .iterator(); iter.hasNext();) {
155: String jarFile = iter.next();
156: allClazz.addAll(JarExploder.getAllClasses(jarFile));
157: }
158:
159: // Remove the invalid ones
160: // URLClassLoader classloader = getEjbGroupClassLoader( ejbGroup );
161: for (Iterator iter = allClazz.iterator(); iter.hasNext();) {
162: String className = (String) iter.next();
163:
164: // No internal classes for now
165: if (className.indexOf('$') != -1) {
166: iter.remove();
167: continue;
168: }
169:
170: /*
171: * try { Class.forName( className, false, classloader ); } catch(
172: * java.lang.ClassNotFoundException e ) { iter.remove(); }
173: */
174: }
175:
176: return allClazz;
177: } catch (Exception e) {
178: e.printStackTrace();
179:
180: return new ArrayList();
181: }
182: }
183: }
|