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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.compapp.javaee.annotation.handler;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.net.JarURLConnection;
033: import java.net.URL;
034: import java.net.URLConnection;
035: import java.util.jar.JarEntry;
036: import java.util.jar.JarInputStream;
037: import java.util.logging.Logger;
038: import org.netbeans.modules.classfile.ClassFile;
039: import org.netbeans.modules.classfile.ClassName;
040: import org.netbeans.modules.compapp.javaee.util.ProjectUtil;
041:
042: /**
043: *
044: * @author gpatil
045: */
046: public class JarInJarClassFileLoader implements ClassFileLoader {
047: private URL jarInJarUrl;
048: private String rootClassDir;
049: private Logger logger = Logger.getLogger(JarClassFileLoader.class
050: .getName());
051:
052: /**
053: * j - URL to the WEB/EJB archive inside an 'ear'.
054: * root - Root of class files, inside a EJB/WEB archive in an 'ear'.
055: **/
056: public JarInJarClassFileLoader(URL j, String root) {
057: this .jarInJarUrl = j;
058: this .rootClassDir = root;
059: }
060:
061: private String getClassPath(ClassName cn) {
062: String ret = cn.getExternalName(true).replaceAll("\\.", "\\/");//NOI18N
063: ret = ret + ".class"; //NOI18N
064: return ret;
065: }
066:
067: private String getClassPath(String internalClassName) {
068: return getClassPath(ClassName.getClassName(internalClassName));
069: }
070:
071: private ClassFile loadClassFileFor(String entryName)
072: throws IOException {
073: ClassFile ret = null;
074: JarURLConnection jConn = null;
075: JarInputStream jis = null;
076: InputStream is = null;
077: URLConnection uConn = this .jarInJarUrl.openConnection();
078: try {
079: if (uConn instanceof JarURLConnection) {
080: jConn = (JarURLConnection) uConn;
081: is = jConn.getInputStream();
082: jis = new JarInputStream(is);
083: JarEntry je = jis.getNextJarEntry();
084: while ((je != null)
085: && (!entryName.equals(je.getName()))) {
086: je = jis.getNextJarEntry();
087: }
088: if (je != null) {
089: ret = new ClassFile(jis);
090: }
091: }
092: } finally {
093: ProjectUtil.close(jis);
094: ProjectUtil.close(is);
095: }
096: return ret;
097: }
098:
099: public ClassFile getClassFileFromInternalName(
100: String internalClassName) {
101: ClassFile cf = null;
102: internalClassName = getClassPath(internalClassName);
103: try {
104: cf = loadClassFileFor(rootClassDir + internalClassName);
105: } catch (IOException ex) {
106: logger.warning("Exception while reading class file:"
107: + internalClassName + ":" + ex.getMessage());
108: }
109:
110: return cf;
111: }
112:
113: public ClassFile getClassFile(ClassName className) {
114: ClassFile cf = null;
115: try {
116: cf = loadClassFileFor(rootClassDir
117: + getClassPath(className));
118: } catch (IOException ex) {
119: logger.warning("Exception while reading class file:"
120: + className + ":" + ex.getMessage());
121: }
122:
123: return cf;
124: }
125:
126: public ClassFile getClassFileUsingJarEntry(JarEntry je) {
127: ClassFile cf = null;
128: try {
129: cf = loadClassFileFor(je.getName());
130: } catch (IOException ex) {
131: logger.warning("Exception while reading class file:"
132: + je.getName() + ":" + ex.getMessage());
133: }
134: return cf;
135: }
136: }
|