1 /*******************************************************************************
2 * Copyright (c) 2008, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 219530 [jar application] add Jar-in-Jar ClassLoader option
11 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 262746 [jar exporter] Create a builder for jar-in-jar-loader.zip
12 *******************************************************************************/
14 package org.eclipse.jdt.internal.ui.jarpackagerfat;
16 import java.io.ByteArrayInputStream;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.HashSet;
24 import java.util.jar.JarEntry;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27 import java.util.zip.ZipInputStream;
29 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.core.runtime.MultiStatus;
35 import org.eclipse.core.runtime.Status;
37 import org.eclipse.jdt.ui.JavaUI;
38 import org.eclipse.jdt.ui.jarpackager.IManifestProvider;
39 import org.eclipse.jdt.ui.jarpackager.JarPackageData;
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil;
45 * A jar builder which copies the referenced libraries into the generated jar and adds a special
46 * class loader which allows to load the classes from the referenced libraries.
50 public class FatJarRsrcUrlBuilder extends FatJarBuilder {
52 public static final String BUILDER_ID= "org.eclipse.jdt.ui.fat_jar_rsrc_url_builder"; //$NON-NLS-1$
53 public static final String JAR_RSRC_LOADER_ZIP= "jar-in-jar-loader.zip"; //$NON-NLS-1$
55 private Set<String> jarNames;
56 private JarPackageData fJarPackage;
61 public String getId() {
66 * we do not need to merge any manifests here.
70 public boolean isMergeManifests() {
75 * we do not need to remove signers here.
79 public boolean isRemoveSigners() {
87 public String getManifestClasspath() {
88 return "."; //$NON-NLS-1$
94 public IManifestProvider getManifestProvider() {
95 return new FatJarRsrcUrlManifestProvider(this);
102 public void open(JarPackageData jarPackage, Shell displayShell, MultiStatus status) throws CoreException {
103 super.open(jarPackage, displayShell, status);
104 fJarPackage= jarPackage;
105 jarNames= new HashSet<String>();
107 writeRsrcUrlClasses();
108 } catch (IOException e) {
109 throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, e.getMessage(), e));
116 public void writeArchive(ZipFile jarFile, IProgressMonitor progressMonitor) {
117 File jarPathFile= new File(jarFile.getName());
118 String jarName = jarPathFile.getName();
119 while (jarNames.contains(jarName)) {
120 jarName= FatJarPackagerUtil.nextNumberedFileName(jarName);
122 jarNames.add(jarName);
123 JarEntry newEntry = new JarEntry(jarName);
124 newEntry.setMethod(ZipEntry.STORED);
125 byte[] readBuffer= new byte[4096];
127 if (!fJarPackage.isCompressed())
128 JarPackagerUtil.calculateCrcAndSize(newEntry, new FileInputStream(jarPathFile), readBuffer);
129 getJarWriter().addZipEntryStream(newEntry, new FileInputStream(jarPathFile), jarName);
130 } catch (FileNotFoundException e) {
131 throw new RuntimeException(e);
132 } catch (IOException e) {
133 throw new RuntimeException(e);
137 public void writeRsrcUrlClasses() throws IOException {
138 InputStream is= JavaPlugin.getDefault().getBundle().getEntry(JAR_RSRC_LOADER_ZIP).openStream();
139 ZipInputStream zis= new ZipInputStream(is);
140 ZipEntry zipEntry= zis.getNextEntry();
141 while (zipEntry != null) {
142 if (!zipEntry.isDirectory()) {
143 String entryName= zipEntry.getName();
144 byte[] content= FatJarPackagerUtil.readInputStream(zis);
145 getJarWriter().addZipEntryStream(zipEntry, new ByteArrayInputStream(content), entryName);
147 zipEntry= zis.getNextEntry();