| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
* SAP AG - consolidation of publishers for PDE formats |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.pde.internal.publishing; |
| 16 |
|
| 17 |
import java.io.BufferedInputStream; |
| 18 |
import java.io.BufferedOutputStream; |
| 19 |
import java.io.File; |
| 20 |
import java.io.FileInputStream; |
| 21 |
import java.io.FileOutputStream; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.InputStream; |
| 24 |
import java.io.OutputStream; |
| 25 |
import java.util.Dictionary; |
| 26 |
import java.util.Enumeration; |
| 27 |
|
| 28 |
import org.eclipse.osgi.service.resolver.BundleDescription; |
| 29 |
import org.eclipse.osgi.util.ManifestElement; |
| 30 |
import org.osgi.framework.BundleException; |
| 31 |
|
| 32 |
public final class Utils { |
| 33 |
|
| 34 |
static public void copy(File source, File destination) throws IOException { |
| 35 |
InputStream in = null; |
| 36 |
OutputStream out = null; |
| 37 |
try { |
| 38 |
in = new BufferedInputStream(new FileInputStream(source)); |
| 39 |
out = new BufferedOutputStream(new FileOutputStream(destination)); |
| 40 |
final byte[] buffer = new byte[8192]; |
| 41 |
while (true) { |
| 42 |
int bytesRead = -1; |
| 43 |
bytesRead = in.read(buffer); |
| 44 |
if (bytesRead == -1) |
| 45 |
break; |
| 46 |
out.write(buffer, 0, bytesRead); |
| 47 |
} |
| 48 |
} finally { |
| 49 |
try { |
| 50 |
if (in != null) |
| 51 |
in.close(); |
| 52 |
} finally { |
| 53 |
if (out != null) |
| 54 |
out.close(); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public static boolean guessUnpack(BundleDescription bundle, String[] classpath) { |
| 60 |
if (bundle == null) |
| 61 |
return true; |
| 62 |
|
| 63 |
@SuppressWarnings("unchecked") |
| 64 |
Dictionary<String, String> properties = (Dictionary<String, String>) bundle.getUserObject(); |
| 65 |
String shape = null; |
| 66 |
if (properties != null && (shape = properties.get(Constants.ECLIPSE_BUNDLE_SHAPE)) != null) { |
| 67 |
return shape.equals("dir"); //$NON-NLS-1$ |
| 68 |
} |
| 69 |
|
| 70 |
// launcher fragments are a special case, they have no bundle-classpath |
| 71 |
if (bundle.getHost() != null && bundle.getName().startsWith(Constants.BUNDLE_EQUINOX_LAUNCHER)) |
| 72 |
return true; |
| 73 |
|
| 74 |
if (new File(bundle.getLocation()).isFile()) |
| 75 |
return false; |
| 76 |
|
| 77 |
if (classpath.length == 0) |
| 78 |
return false; |
| 79 |
|
| 80 |
for (String classpath1 : classpath) { |
| 81 |
if (classpath1.equals(".")) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
public static String[] getBundleClasspath(Dictionary<String, String> manifest) { |
| 89 |
String fullClasspath = getBundleManifestHeader(manifest, Constants.BUNDLE_CLASSPATH); |
| 90 |
String[] result = new String[0]; |
| 91 |
try { |
| 92 |
if (fullClasspath != null) { |
| 93 |
ManifestElement[] classpathEntries; |
| 94 |
classpathEntries = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, fullClasspath); |
| 95 |
result = new String[classpathEntries.length]; |
| 96 |
for (int i = 0; i < classpathEntries.length; i++) { |
| 97 |
result[i] = classpathEntries[i].getValue(); |
| 98 |
} |
| 99 |
} |
| 100 |
} catch (BundleException e) { |
| 101 |
//Ignore |
| 102 |
} |
| 103 |
return result; |
| 104 |
} |
| 105 |
|
| 106 |
public static String getBundleManifestHeader(Dictionary<String, String> manifest, String header) { |
| 107 |
String value = manifest.get(header); |
| 108 |
if (value != null) |
| 109 |
return value; |
| 110 |
|
| 111 |
Enumeration<String> keys = manifest.keys(); |
| 112 |
while (keys.hasMoreElements()) { |
| 113 |
String key = keys.nextElement(); |
| 114 |
if (key.equalsIgnoreCase(header)) |
| 115 |
return manifest.get(key); |
| 116 |
} |
| 117 |
return null; |
| 118 |
} |
| 119 |
} |