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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.build.ant.utils;
038:
039: import java.io.File;
040: import java.net.URL;
041: import java.net.URLClassLoader;
042: import java.util.Enumeration;
043: import java.util.jar.JarEntry;
044: import java.util.jar.JarFile;
045:
046: /**
047: * This class verifies the given jar file (first command-line argument) wrt its
048: * validitity, i.e. checks that all classes are loadable. It browses the jar entries
049: * and if it's a class, tries to load it. Mostly it watches for
050: * <code>ClassFormatError</code>s and ignores several expected exceptions.
051: *
052: * It does not provide any means to validate the input data, since it's expected to
053: * be called exclusively from from <code>Package</code>.
054: *
055: * The success/failure is reported via exitcode, 0 means success, 1 - failure.
056: *
057: * @see org.netbeans.installer.infra.build.ant.Package
058: *
059: * @author Kirill Sorokin
060: */
061: public class VerifyFile {
062: /**
063: * The main method.
064: *
065: * @param args command-line arguments
066: */
067: public static void main(String[] args) {
068: File file = new File(args[0]);
069:
070: try {
071: JarFile jar = new JarFile(file);
072: URLClassLoader loader = new URLClassLoader(new URL[] { file
073: .toURI().toURL() });
074:
075: Enumeration<JarEntry> entries = jar.entries();
076: while (entries.hasMoreElements()) {
077: JarEntry entry = entries.nextElement();
078:
079: if (getClassName(entry) != null) {
080: try {
081: System.out.println("loading class "
082: + getClassName(entry)); // NOI18N
083: loader.loadClass(getClassName(entry));
084: } catch (NoClassDefFoundError e) {
085: // do nothing; this is OK - classpath issues
086: } catch (IllegalAccessError e) {
087: // do nothing; this is also somewhat OK, since we do not
088: // define any security policies
089: }
090: }
091: }
092:
093: jar.close();
094:
095: System.exit(0);
096: } catch (Throwable e) {
097: // we need to catch everything here in order to not
098: // allow unexpected exceptions to pass through
099: e.printStackTrace();
100: System.exit(1);
101: }
102: }
103:
104: /**
105: * Converts a jar entry to a class name.
106: *
107: * @param entry <code>JarEntry</code> to process.
108: * @return The classname of the jar entry or <code>null</code> if it cannot be
109: * devised.
110: */
111: private static String getClassName(JarEntry entry) {
112: final String name = entry.getName();
113:
114: if (name.endsWith(".class")) { // NOI18N
115: final String className = name.substring(0,
116: name.length() - 6).replace('/', '.'); // NOMAGI
117: if (className
118: .matches("([a-zA-Z][a-zA-Z0-9_]+\\.)+[a-zA-Z][a-zA-Z0-9_]+")) { // NOI18N
119: return className;
120: } else {
121: return null;
122: }
123: }
124:
125: return null;
126: }
127: }
|