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: package org.netbeans.modules.visualweb.complib;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.net.URL;
047: import java.net.URLClassLoader;
048: import java.util.List;
049: import java.util.jar.JarFile;
050: import java.util.jar.Manifest;
051:
052: import org.netbeans.modules.visualweb.complib.Complib.InitialPaletteFolder;
053: import org.netbeans.modules.visualweb.complib.api.ComplibException;
054:
055: /**
056: * Represents an unexpanded complib package file.
057: *
058: * @author Edwin Goei
059: */
060: public class ComplibPackage {
061:
062: /**
063: * ClassLoader used to load l10n resources from a *.complib file
064: *
065: * @author Edwin Goei
066: */
067: private static class ResourceClassLoader extends URLClassLoader {
068:
069: public ResourceClassLoader(URL packageFileUrl) {
070: super (new URL[0], ResourceClassLoader.class
071: .getClassLoader());
072: addURL(packageFileUrl);
073: }
074: }
075:
076: /**
077: * ClassLoader used to load resources referenced in package file metadata such as the manifest
078: * and initial-palette config file. These resources can be accessed before or after a package is
079: * expanded.
080: */
081: private ResourceClassLoader resourceClassLoader;
082:
083: private ComplibManifest complibManifest;
084:
085: /** Points to jar file */
086: private File packageFile;
087:
088: /**
089: * This constructor partially validates the complib package file. A successful return indicates
090: * a valid package file.
091: *
092: * @param packageFile
093: * @throws ComplibException
094: * if complib file is not valid
095: * @throws IOException
096: */
097: public ComplibPackage(File packageFile) throws ComplibException,
098: IOException {
099: File freedJarFile = IdeUtil.freeJarFile(packageFile);
100:
101: JarFile jarFile = new JarFile(freedJarFile);
102: Manifest manifest = jarFile.getManifest();
103: if (manifest == null) {
104: // Construct an appropriate error message
105: String message = freedJarFile.equals(packageFile) ? ""
106: : "' copied from '" + packageFile; // NOI18N
107: throw new ComplibException("File '" + freedJarFile
108: + message + "' must contain a complib manifest."); // NOI18N
109: }
110:
111: // Init resource ClassLoader to get localized complib package resources
112: URL packageFileUrl = freedJarFile.toURI().toURL();
113: resourceClassLoader = new ResourceClassLoader(packageFileUrl);
114:
115: complibManifest = ComplibManifest.getInstance(manifest,
116: resourceClassLoader);
117: jarFile.close();
118:
119: this .packageFile = freedJarFile;
120: }
121:
122: public ComplibManifest getManifest() {
123: return complibManifest;
124: }
125:
126: /**
127: * Method does not throw a checked exception. Used by UI.
128: *
129: * @return
130: */
131: public List<InitialPaletteFolder> getInitialPaletteFolders() {
132: return complibManifest.getInitialPalette();
133: }
134:
135: /**
136: * Returns the localized Title if it has been localized
137: *
138: * @return
139: */
140: public String getTitle() {
141: return complibManifest.getTitle();
142: }
143:
144: public Complib.Identifier getIdentifer() {
145: return complibManifest.getIdentifier();
146: }
147:
148: File getPackageFile() {
149: return packageFile;
150: }
151: }
|