You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/09/26 11:32:58 UTC
svn commit: r699244 - in /servicemix/smx4/kernel/trunk/spring/src:
main/java/org/apache/servicemix/kernel/spring/SpringTransformer.java
test/java/org/apache/servicemix/kernel/spring/SpringDeploymentListenerTest.java
test/resources/ test/resources/test.xml
Author: gnodet
Date: Fri Sep 26 02:32:57 2008
New Revision: 699244
URL: http://svn.apache.org/viewvc?rev=699244&view=rev
Log:
SMX4KNL-74: When using the spring deployer, allow the customization of the OSGi manifest entries
Added:
servicemix/smx4/kernel/trunk/spring/src/test/resources/
servicemix/smx4/kernel/trunk/spring/src/test/resources/test.xml
Modified:
servicemix/smx4/kernel/trunk/spring/src/main/java/org/apache/servicemix/kernel/spring/SpringTransformer.java
servicemix/smx4/kernel/trunk/spring/src/test/java/org/apache/servicemix/kernel/spring/SpringDeploymentListenerTest.java
Modified: servicemix/smx4/kernel/trunk/spring/src/main/java/org/apache/servicemix/kernel/spring/SpringTransformer.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/spring/src/main/java/org/apache/servicemix/kernel/spring/SpringTransformer.java?rev=699244&r1=699243&r2=699244&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/spring/src/main/java/org/apache/servicemix/kernel/spring/SpringTransformer.java (original)
+++ servicemix/smx4/kernel/trunk/spring/src/main/java/org/apache/servicemix/kernel/spring/SpringTransformer.java Fri Sep 26 02:32:57 2008
@@ -26,6 +26,8 @@
import java.net.URL;
import java.util.Set;
import java.util.TreeSet;
+import java.util.Properties;
+import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
@@ -39,38 +41,59 @@
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Element;
public class SpringTransformer {
static Transformer transformer;
static DocumentBuilderFactory dbf;
+ static TransformerFactory tf;
public static void transform(URL url, OutputStream os) throws Exception {
+ // Build dom document
Document doc = parse(url);
+ // Heuristicly retrieve name and version
String name = url.getPath();
int idx = name.lastIndexOf('/');
if (idx >= 0) {
name = name.substring(idx + 1);
}
String[] str = extractNameVersionType(name);
-
+ // Create manifest
Manifest m = new Manifest();
m.getMainAttributes().putValue("Manifest-Version", "2");
m.getMainAttributes().putValue("Bundle-SymbolicName", str[0]);
m.getMainAttributes().putValue("Bundle-Version", str[1]);
m.getMainAttributes().putValue("Spring-Context", "*;publish-context:=false;create-asynchronously:=true");
- InputStream is = url.openStream();
- String importPkgs = getImportPackages(analyze(is));
- is.close();
+ String importPkgs = getImportPackages(analyze(new DOMSource(doc)));
if (importPkgs != null && importPkgs.length() > 0) {
m.getMainAttributes().putValue("Import-Package", importPkgs);
}
m.getMainAttributes().putValue("DynamicImport-Package", "*");
+ // Extract manifest entries from the DOM
+ NodeList l = doc.getElementsByTagName("manifest");
+ if (l != null) {
+ for (int i = 0; i < l.getLength(); i++) {
+ Element e = (Element) l.item(i);
+ String text = e.getTextContent();
+ Properties props = new Properties();
+ props.load(new ByteArrayInputStream(text.trim().getBytes()));
+ Enumeration en = props.propertyNames();
+ while (en.hasMoreElements()) {
+ String k = (String) en.nextElement();
+ String v = props.getProperty(k);
+ m.getMainAttributes().putValue(k, v);
+ }
+ e.getParentNode().removeChild(e);
+ }
+ }
JarOutputStream out = new JarOutputStream(os);
ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
@@ -84,9 +107,11 @@
out.closeEntry();
e = new ZipEntry("META-INF/spring/" + name);
out.putNextEntry(e);
- is = url.openStream();
- copyInputStream(is, out);
- is.close();
+ // Copy the new DOM
+ if (tf == null) {
+ tf = TransformerFactory.newInstance();
+ }
+ tf.newTransformer().transform(new DOMSource(doc), new StreamResult(out));
out.closeEntry();
out.close();
}
@@ -152,19 +177,20 @@
}
}
- public static Set<String> analyze(InputStream in) throws Exception {
+ public static Set<String> analyze(Source source) throws Exception {
if (transformer == null) {
- TransformerFactory tf = TransformerFactory.newInstance();
- Source source = new StreamSource(SpringTransformer.class.getResourceAsStream("extract.xsl"));
- transformer = tf.newTransformer(source);
+ if (tf == null) {
+ tf = TransformerFactory.newInstance();
+ }
+ Source s = new StreamSource(SpringTransformer.class.getResourceAsStream("extract.xsl"));
+ transformer = tf.newTransformer(s);
}
Set<String> refers = new TreeSet<String>();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Result r = new StreamResult(bout);
- Source s = new StreamSource(in);
- transformer.transform(s, r);
+ transformer.transform(source, r);
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
bout.close();
Modified: servicemix/smx4/kernel/trunk/spring/src/test/java/org/apache/servicemix/kernel/spring/SpringDeploymentListenerTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/spring/src/test/java/org/apache/servicemix/kernel/spring/SpringDeploymentListenerTest.java?rev=699244&r1=699243&r2=699244&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/spring/src/test/java/org/apache/servicemix/kernel/spring/SpringDeploymentListenerTest.java (original)
+++ servicemix/smx4/kernel/trunk/spring/src/test/java/org/apache/servicemix/kernel/spring/SpringDeploymentListenerTest.java Fri Sep 26 02:32:57 2008
@@ -19,8 +19,14 @@
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.InputStream;
import java.util.Iterator;
import java.util.Set;
+import java.util.jar.JarInputStream;
+
+import javax.xml.transform.dom.DOMSource;
import junit.framework.TestCase;
@@ -29,7 +35,7 @@
public void testPackagesExtraction() throws Exception {
SpringDeploymentListener l = new SpringDeploymentListener();
File f = new File(getClass().getClassLoader().getResource("META-INF/spring/spring-deployer.xml").toURI());
- Set<String> pkgs = SpringTransformer.analyze(new FileInputStream(f));
+ Set<String> pkgs = SpringTransformer.analyze(new DOMSource(SpringTransformer.parse(f.toURL())));
assertNotNull(pkgs);
assertEquals(2, pkgs.size());
Iterator<String> it = pkgs.iterator();
@@ -37,6 +43,21 @@
assertEquals("org.osgi.service.url", it.next());
}
+ public void testCustomManifest() throws Exception {
+ File f = File.createTempFile("smx", ".jar");
+ try {
+ OutputStream os = new FileOutputStream(f);
+ SpringTransformer.transform(getClass().getClassLoader().getResource("test.xml"), os);
+ os.close();
+ InputStream is = new FileInputStream(f);
+ JarInputStream jar = new JarInputStream(is);
+ jar.getManifest().write(System.err);
+ is.close();
+ } finally {
+ f.delete();
+ }
+ }
+
public void testVersions() {
assertVersion("org.apache.servicemix.bundles.ant-1.7.0-1.0-m3-SNAPSHOT.jar",
"org.apache.servicemix.bundles.ant-1.7.0", "1.0.0.m3-SNAPSHOT", "jar");
Added: servicemix/smx4/kernel/trunk/spring/src/test/resources/test.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/spring/src/test/resources/test.xml?rev=699244&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/spring/src/test/resources/test.xml (added)
+++ servicemix/smx4/kernel/trunk/spring/src/test/resources/test.xml Fri Sep 26 02:32:57 2008
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:osgi="http://www.springframework.org/schema/osgi"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/util
+ http://www.springframework.org/schema/util/spring-util.xsd
+ http://www.springframework.org/schema/osgi
+ http://www.springframework.org/schema/osgi/spring-osgi.xsd">
+
+ <manifest>
+ RequireBundle=org.osgi
+ Header=value
+ </manifest>
+
+ <bean id="springDeploymentListener" class="org.apache.servicemix.kernel.spring.SpringDeploymentListener">
+
+ </bean>
+
+ <osgi:service ref="springDeploymentListener">
+ <osgi:interfaces>
+ <value>org.apache.servicemix.kernel.filemonitor.DeploymentListener</value>
+ </osgi:interfaces>
+ </osgi:service>
+
+ <bean id="springHandler" class="org.apache.servicemix.kernel.spring.SpringURLHandler" />
+
+ <osgi:service ref="springHandler" interface="org.osgi.service.url.URLStreamHandlerService">
+ <osgi:service-properties>
+ <entry key="url.handler.protocol" value="spring"/>
+ </osgi:service-properties>
+ </osgi:service>
+
+</beans>
\ No newline at end of file