blob: 398c028dd8997b628a3b5f2331f9caa8678b6b8a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package impl.osgi;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
/**
*
* Utility class to create OSGi bundles
*
* @version $Rev$ $Date$
*/
public class OSGiTestBundles {
public static void createBundle(String jarName,
Class<?> interfaceClass, Class<?> implClass) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
String EOL = System.getProperty("line.separator");
String packageName = interfaceClass.getPackage().getName();
String bundleName = interfaceClass.getName();
String manifestStr = "Manifest-Version: 1.0" + EOL
+ "Bundle-ManifestVersion: 2" + EOL + "Bundle-Name: "
+ bundleName + EOL + "Bundle-SymbolicName: " + bundleName + EOL
+ "Bundle-Version: " + "1.0.0" + EOL
+ "Bundle-Localization: plugin" + EOL;
StringBuilder manifestBuf = new StringBuilder();
manifestBuf.append(manifestStr);
manifestBuf.append("Export-Package: " + packageName + EOL);
manifestBuf.append("Import-Package: org.osgi.framework" + EOL);
manifestBuf.append("Bundle-Activator: " + implClass.getName() + EOL);
ByteArrayInputStream manifestStream = new ByteArrayInputStream(manifestBuf.toString().getBytes());
Manifest manifest = new Manifest();
manifest.read(manifestStream);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
String interfaceClassName = interfaceClass.getName().replaceAll("\\.",
"/")
+ ".class";
URL url = interfaceClass.getClassLoader().getResource(
interfaceClassName);
String path = url.getPath();
ZipEntry ze = new ZipEntry(interfaceClassName);
jarOut.putNextEntry(ze);
FileInputStream file = new FileInputStream(path);
byte[] fileContents = new byte[file.available()];
file.read(fileContents);
jarOut.write(fileContents);
String implClassName = implClass.getName().replaceAll("\\.",
"/")
+ ".class";
url = implClass.getClassLoader().getResource(implClassName);
path = url.getPath();
ze = new ZipEntry(implClassName);
jarOut.putNextEntry(ze);
file = new FileInputStream(path);
fileContents = new byte[file.available()];
file.read(fileContents);
jarOut.write(fileContents);
file.close();
jarOut.close();
out.close();
FileOutputStream fileOut = new FileOutputStream(jarName);
fileOut.write(out.toByteArray());
fileOut.close();
}
}
|