From 81f448ebbdb0650572b9fea86c543a29a566ada3 Mon Sep 17 00:00:00 2001 From: nash Date: Thu, 4 Nov 2010 14:33:30 +0000 Subject: Tag for 1.6.1-RC1 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1031003 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/test/java/impl/osgi/OSGiTestBundles.java | 111 +++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 sca-java-1.x/tags/1.6.1-RC1/itest/validation/src/test/java/impl/osgi/OSGiTestBundles.java (limited to 'sca-java-1.x/tags/1.6.1-RC1/itest/validation/src/test/java/impl/osgi/OSGiTestBundles.java') diff --git a/sca-java-1.x/tags/1.6.1-RC1/itest/validation/src/test/java/impl/osgi/OSGiTestBundles.java b/sca-java-1.x/tags/1.6.1-RC1/itest/validation/src/test/java/impl/osgi/OSGiTestBundles.java new file mode 100644 index 0000000000..398c028dd8 --- /dev/null +++ b/sca-java-1.x/tags/1.6.1-RC1/itest/validation/src/test/java/impl/osgi/OSGiTestBundles.java @@ -0,0 +1,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(); + + + } +} -- cgit v1.2.3