]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
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 | |
7 | * | |
8 | * Contributors: | |
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 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.jdt.internal.ui.jarpackagerfat; | |
15 | ||
16 | import java.io.ByteArrayInputStream; | |
17 | import java.io.File; | |
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; | |
23 | import java.util.Set; | |
24 | import java.util.jar.JarEntry; | |
25 | import java.util.zip.ZipEntry; | |
26 | import java.util.zip.ZipFile; | |
27 | import java.util.zip.ZipInputStream; | |
28 | ||
29 | import org.eclipse.swt.widgets.Shell; | |
30 | ||
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; | |
36 | ||
37 | import org.eclipse.jdt.ui.JavaUI; | |
38 | import org.eclipse.jdt.ui.jarpackager.IManifestProvider; | |
39 | import org.eclipse.jdt.ui.jarpackager.JarPackageData; | |
40 | ||
41 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
42 | import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil; | |
43 | ||
44 | /** | |
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. | |
47 | * | |
48 | * @since 3.5 | |
49 | */ | |
50 | public class FatJarRsrcUrlBuilder extends FatJarBuilder { | |
51 | ||
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$ | |
54 | ||
55 | private Set<String> jarNames; | |
56 | private JarPackageData fJarPackage; | |
57 | ||
58 | /** | |
59 | * {@inheritDoc} | |
60 | */ | |
61 | public String getId() { | |
62 | return BUILDER_ID; | |
63 | } | |
64 | ||
65 | /** | |
66 | * we do not need to merge any manifests here. | |
67 | * @return false | |
68 | */ | |
69 | @Override | |
70 | public boolean isMergeManifests() { | |
71 | return false; | |
72 | } | |
73 | ||
74 | /** | |
75 | * we do not need to remove signers here. | |
76 | * @return false | |
77 | */ | |
78 | @Override | |
79 | public boolean isRemoveSigners() { | |
80 | return false; | |
81 | } | |
82 | ||
83 | /** | |
84 | * {@inheritDoc} | |
85 | */ | |
86 | @Override | |
87 | public String getManifestClasspath() { | |
88 | return "."; //$NON-NLS-1$ | |
89 | } | |
90 | ||
91 | /** | |
92 | * {@inheritDoc} | |
93 | */ | |
94 | public IManifestProvider getManifestProvider() { | |
95 | return new FatJarRsrcUrlManifestProvider(this); | |
96 | } | |
97 | ||
98 | /** | |
99 | * {@inheritDoc} | |
100 | */ | |
101 | @Override | |
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>(); | |
106 | try { | |
107 | writeRsrcUrlClasses(); | |
108 | } catch (IOException e) { | |
109 | throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, e.getMessage(), e)); | |
110 | } | |
111 | } | |
112 | ||
113 | /** | |
114 | * {@inheritDoc} | |
115 | */ | |
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); | |
121 | } | |
122 | jarNames.add(jarName); | |
123 | JarEntry newEntry = new JarEntry(jarName); | |
124 | newEntry.setMethod(ZipEntry.STORED); | |
125 | byte[] readBuffer= new byte[4096]; | |
126 | try { | |
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); | |
134 | } | |
135 | } | |
136 | ||
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); | |
146 | } | |
147 | zipEntry= zis.getNextEntry(); | |
148 | } | |
149 | } | |
150 | } |