karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1164155 - in /karaf/trunk/jaas/jasypt: ./ src/main/java/org/apache/karaf/jaas/jasypt/handler/ src/main/java/org/apache/karaf/jaas/jasypt/impl/ src/main/resources/OSGI-INF/blueprint/ src/main/resources/org/ src/main/resources/org/apache/ sr...
Date Thu, 01 Sep 2011 16:43:16 GMT
Author: gnodet
Date: Thu Sep  1 16:43:15 2011
New Revision: 1164155

URL: http://svn.apache.org/viewvc?rev=1164155&view=rev
Log:
[KARAF-845] Add a jasypt based property placeholder to support encrypted values

Added:
    karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/
    karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholder.java
    karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/NamespaceHandler.java
    karaf/trunk/jaas/jasypt/src/main/resources/org/
    karaf/trunk/jaas/jasypt/src/main/resources/org/apache/
    karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/
    karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/
    karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/jasypt/
    karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/jasypt/handler/
    karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/jasypt/handler/karaf-jasypt-1.0.0.xsd
    karaf/trunk/jaas/jasypt/src/test/java/org/apache/karaf/jaas/jasypt/handler/
    karaf/trunk/jaas/jasypt/src/test/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholderTest.java
    karaf/trunk/jaas/jasypt/src/test/resources/
    karaf/trunk/jaas/jasypt/src/test/resources/log4j.properties
    karaf/trunk/jaas/jasypt/src/test/resources/org/
    karaf/trunk/jaas/jasypt/src/test/resources/org/apache/
    karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/
    karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/
    karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/jasypt/
    karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/jasypt/handler/
    karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/jasypt/handler/test.xml
Modified:
    karaf/trunk/jaas/jasypt/pom.xml
    karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/impl/JasyptEncryption.java
    karaf/trunk/jaas/jasypt/src/main/resources/OSGI-INF/blueprint/karaf-jaas-jasypt.xml

Modified: karaf/trunk/jaas/jasypt/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/pom.xml?rev=1164155&r1=1164154&r2=1164155&view=diff
==============================================================================
--- karaf/trunk/jaas/jasypt/pom.xml (original)
+++ karaf/trunk/jaas/jasypt/pom.xml Thu Sep  1 16:43:15 2011
@@ -47,14 +47,58 @@
             <groupId>org.apache.servicemix.bundles</groupId>
             <artifactId>org.apache.servicemix.bundles.jasypt</artifactId>
         </dependency>
-        
+
+        <dependency>
+            <groupId>org.apache.aries.blueprint</groupId>
+            <artifactId>org.apache.aries.blueprint</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
-            <groupId>com.ibm.icu</groupId>
-            <artifactId>icu4j</artifactId>
-            <version>3.4.4</version>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
             <scope>test</scope>
         </dependency>
-    
+
+        <dependency>
+            <groupId>com.googlecode.pojosr</groupId>
+            <artifactId>de.kalpatec.pojosr.framework</artifactId>
+            <version>0.1.4</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.ops4j.pax.swissbox</groupId>
+            <artifactId>pax-swissbox-tinybundles</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries.proxy</groupId>
+            <artifactId>org.apache.aries.proxy</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-log4j12</artifactId>
+            <version>${slf4j.version}</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>jcl-over-slf4j</artifactId>
+            <version>${slf4j.version}</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
     <build>

Added: karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholder.java
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholder.java?rev=1164155&view=auto
==============================================================================
--- karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholder.java
(added)
+++ karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholder.java
Thu Sep  1 16:43:15 2011
@@ -0,0 +1,41 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.jasypt.handler;
+
+import org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder;
+import org.jasypt.encryption.StringEncryptor;
+
+public class EncryptablePropertyPlaceholder extends AbstractPropertyPlaceholder {
+
+    private StringEncryptor encryptor;
+
+    public StringEncryptor getEncryptor() {
+        return encryptor;
+    }
+
+    public void setEncryptor(StringEncryptor encryptor) {
+        this.encryptor = encryptor;
+    }
+
+    public void init() {
+
+    }
+
+    @Override
+    protected String getProperty(String val) {
+        return encryptor.decrypt(val);
+    }
+
+}

Added: karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/NamespaceHandler.java
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/NamespaceHandler.java?rev=1164155&view=auto
==============================================================================
--- karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/NamespaceHandler.java
(added)
+++ karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/handler/NamespaceHandler.java
Thu Sep  1 16:43:15 2011
@@ -0,0 +1,173 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.jasypt.handler;
+
+import java.net.URL;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.aries.blueprint.ParserContext;
+import org.apache.aries.blueprint.ext.PlaceholdersUtils;
+import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
+import org.apache.aries.blueprint.mutable.MutableCollectionMetadata;
+import org.apache.aries.blueprint.mutable.MutableRefMetadata;
+import org.apache.aries.blueprint.mutable.MutableValueMetadata;
+import org.osgi.service.blueprint.container.ComponentDefinitionException;
+import org.osgi.service.blueprint.reflect.BeanMetadata;
+import org.osgi.service.blueprint.reflect.CollectionMetadata;
+import org.osgi.service.blueprint.reflect.ComponentMetadata;
+import org.osgi.service.blueprint.reflect.Metadata;
+import org.osgi.service.blueprint.reflect.RefMetadata;
+import org.osgi.service.blueprint.reflect.ValueMetadata;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Element;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public class NamespaceHandler implements org.apache.aries.blueprint.NamespaceHandler {
+
+    public static final String ID_ATTRIBUTE = "id";
+    public static final String PLACEHOLDER_PREFIX_ATTRIBUTE = "placeholder-prefix";
+    public static final String PLACEHOLDER_SUFFIX_ATTRIBUTE = "placeholder-suffix";
+    public static final String PROPERTY_PLACEHOLDER_ELEMENT = "property-placeholder";
+    public static final String ENCRYPTOR_REF_ATTRIBUTE = "encryptor-ref";
+    public static final String ENCRYPTOR_ELEMENT = "encryptor";
+    public static final String JASYPT_NAMESPACE_1_0 = "http://karaf.apache.org/xmlns/jasypt/v1.0.0";
+
+    private int idCounter;
+
+    public URL getSchemaLocation(String s) {
+        return getClass().getResource("/org/apache/karaf/jaas/jasypt/handler/karaf-jasypt-1.0.0.xsd");
+    }
+
+    public Set<Class> getManagedClasses() {
+        return new HashSet<Class>(Arrays.asList(
+                EncryptablePropertyPlaceholder.class
+        ));
+    }
+
+    public Metadata parse(Element element, ParserContext context) {
+        String name = element.getLocalName() != null ? element.getLocalName() : element.getNodeName();
+        if (PROPERTY_PLACEHOLDER_ELEMENT.equals(name)) {
+            return parsePropertyPlaceholder(element, context);
+        }
+        throw new ComponentDefinitionException("Bad xml syntax: unknown element '" + name
+ "'");
+    }
+
+    public ComponentMetadata decorate(Node node, ComponentMetadata componentMetadata, ParserContext
parserContext) {
+        throw new ComponentDefinitionException("Bad xml syntax: node decoration is not supported");
+    }
+
+    public ComponentMetadata parsePropertyPlaceholder(Element element, ParserContext context)
{
+        MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
+        metadata.setProcessor(true);
+        metadata.setId(getId(context, element));
+        metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
+        metadata.setRuntimeClass(EncryptablePropertyPlaceholder.class);
+        metadata.setInitMethod("init");
+        String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE)
+                                    ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE)
+                                    : "ENC(";
+        metadata.addProperty("placeholderPrefix", createValue(context, prefix));
+        String suffix = element.hasAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE)
+                                    ? element.getAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE)
+                                    : ")";
+        metadata.addProperty("placeholderSuffix", createValue(context, suffix));
+        String encryptorRef = element.hasAttribute("encryptor-ref")
+                                    ? element.getAttribute("encryptor-ref")
+                                    : null;
+        if (encryptorRef != null) {
+            metadata.addProperty("encryptor", createRef(context, encryptorRef));
+        }
+        NodeList nl = element.getChildNodes();
+        for (int i = 0; i < nl.getLength(); i++) {
+            Node node = nl.item(i);
+            if (node instanceof Element) {
+                Element e = (Element) node;
+                if (JASYPT_NAMESPACE_1_0.equals(e.getNamespaceURI())) {
+                    String name = e.getLocalName() != null ? e.getLocalName() : e.getNodeName();
+                    if (ENCRYPTOR_ELEMENT.equals(name)) {
+                        if (encryptorRef != null) {
+                            throw new ComponentDefinitionException("Only one of " + ENCRYPTOR_REF_ATTRIBUTE
+ " attribute or " + ENCRYPTOR_ELEMENT + " element is allowed");
+                        }
+                        BeanMetadata encryptor = context.parseElement(BeanMetadata.class,
metadata, e);
+                        metadata.addProperty("encryptor", encryptor);
+                    }
+                }
+            }
+        }
+        PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
+        return metadata;
+    }
+
+    public String getId(ParserContext context, Element element) {
+        if (element.hasAttribute(ID_ATTRIBUTE)) {
+            return element.getAttribute(ID_ATTRIBUTE);
+        } else {
+            return generateId(context);
+        }
+    }
+
+    private String generateId(ParserContext context) {
+        String id;
+        do {
+            id = ".jaas-" + ++idCounter;
+        } while (context.getComponentDefinitionRegistry().containsComponentDefinition(id));
+        return id;
+    }
+
+    private static ValueMetadata createValue(ParserContext context, String value) {
+        return createValue(context, value, null);
+    }
+
+    private static ValueMetadata createValue(ParserContext context, String value, String
type) {
+        MutableValueMetadata m = context.createMetadata(MutableValueMetadata.class);
+        m.setStringValue(value);
+        m.setType(type);
+        return m;
+    }
+
+    private static CollectionMetadata createList(ParserContext context, List<String>
list) {
+        MutableCollectionMetadata m = context.createMetadata(MutableCollectionMetadata.class);
+        m.setCollectionClass(List.class);
+        m.setValueType(String.class.getName());
+        for (String v : list) {
+            m.addValue(createValue(context, v, String.class.getName()));
+        }
+        return m;
+    }
+
+    private RefMetadata createRef(ParserContext context, String value) {
+        MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
+        r.setComponentId(value);
+        return r;
+    }
+
+    private static String getTextValue(Element element) {
+        StringBuffer value = new StringBuffer();
+        NodeList nl = element.getChildNodes();
+        for (int i = 0; i < nl.getLength(); i++) {
+            Node item = nl.item(i);
+            if ((item instanceof CharacterData && !(item instanceof Comment)) ||
item instanceof EntityReference) {
+                value.append(item.getNodeValue());
+            }
+        }
+        return value.toString();
+    }
+}

Modified: karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/impl/JasyptEncryption.java
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/impl/JasyptEncryption.java?rev=1164155&r1=1164154&r2=1164155&view=diff
==============================================================================
--- karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/impl/JasyptEncryption.java
(original)
+++ karaf/trunk/jaas/jasypt/src/main/java/org/apache/karaf/jaas/jasypt/impl/JasyptEncryption.java
Thu Sep  1 16:43:15 2011
@@ -2,20 +2,6 @@
  *  Licensed 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.
- *  under the License.
- */
-/*
- *  Licensed 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
  * 

Modified: karaf/trunk/jaas/jasypt/src/main/resources/OSGI-INF/blueprint/karaf-jaas-jasypt.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/main/resources/OSGI-INF/blueprint/karaf-jaas-jasypt.xml?rev=1164155&r1=1164154&r2=1164155&view=diff
==============================================================================
--- karaf/trunk/jaas/jasypt/src/main/resources/OSGI-INF/blueprint/karaf-jaas-jasypt.xml (original)
+++ karaf/trunk/jaas/jasypt/src/main/resources/OSGI-INF/blueprint/karaf-jaas-jasypt.xml Thu
Sep  1 16:43:15 2011
@@ -26,4 +26,15 @@
         <bean class="org.apache.karaf.jaas.jasypt.impl.JasyptEncryptionService"/>
     </service>
 
+    <service interface="org.apache.aries.blueprint.NamespaceHandler">
+        <service-properties>
+            <entry key="osgi.service.blueprint.namespace">
+                <array>
+                    <value>http://karaf.apache.org/xmlns/jasypt/v1.0.0</value>
+                </array>
+            </entry>
+        </service-properties>
+        <bean class="org.apache.karaf.jaas.jasypt.handler.NamespaceHandler"/>
+    </service>
+
 </blueprint>
\ No newline at end of file

Added: karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/jasypt/handler/karaf-jasypt-1.0.0.xsd
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/jasypt/handler/karaf-jasypt-1.0.0.xsd?rev=1164155&view=auto
==============================================================================
--- karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/jasypt/handler/karaf-jasypt-1.0.0.xsd
(added)
+++ karaf/trunk/jaas/jasypt/src/main/resources/org/apache/karaf/jaas/jasypt/handler/karaf-jasypt-1.0.0.xsd
Thu Sep  1 16:43:15 2011
@@ -0,0 +1,43 @@
+<?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.
+
+-->
+<xs:schema elementFormDefault='qualified'
+           targetNamespace='http://karaf.apache.org/xmlns/jasypt/v1.0.0'
+           xmlns:xs='http://www.w3.org/2001/XMLSchema'
+           xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:tns='http://karaf.apache.org/xmlns/jasypt/v1.0.0'>
+
+    <xs:import namespace="http://www.osgi.org/xmlns/blueprint/v1.0.0"/>
+
+    <xs:element name="property-placeholder">
+        <xs:complexType>
+            <xs:complexContent>
+                <xs:extension base="bp:Tcomponent">
+                    <xs:sequence>
+                        <xs:element maxOccurs="1" minOccurs="0" name="encryptor" type="bp:Tbean"/>
+                    </xs:sequence>
+                    <xs:attribute name="placeholder-prefix" type="xs:string" use="optional"
default="ENC("/>
+                    <xs:attribute name="placeholder-suffix" type="xs:string" use="optional"
default=")"/>
+                    <xs:attribute name="encryptor-ref" type="bp:Tidref" use="optional"/>
+                </xs:extension>
+            </xs:complexContent>
+        </xs:complexType>
+    </xs:element>
+
+</xs:schema>

Added: karaf/trunk/jaas/jasypt/src/test/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholderTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/test/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholderTest.java?rev=1164155&view=auto
==============================================================================
--- karaf/trunk/jaas/jasypt/src/test/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholderTest.java
(added)
+++ karaf/trunk/jaas/jasypt/src/test/java/org/apache/karaf/jaas/jasypt/handler/EncryptablePropertyPlaceholderTest.java
Thu Sep  1 16:43:15 2011
@@ -0,0 +1,228 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.jasypt.handler;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.JarInputStream;
+
+import de.kalpatec.pojosr.framework.PojoServiceRegistryFactoryImpl;
+import de.kalpatec.pojosr.framework.launch.BundleDescriptor;
+import de.kalpatec.pojosr.framework.launch.ClasspathScanner;
+import de.kalpatec.pojosr.framework.launch.PojoServiceRegistry;
+import de.kalpatec.pojosr.framework.launch.PojoServiceRegistryFactory;
+import junit.framework.TestCase;
+import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
+import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.ops4j.pax.swissbox.tinybundles.core.TinyBundle;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+
+import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.newBundle;
+
+public class EncryptablePropertyPlaceholderTest extends TestCase {
+
+    public static final long DEFAULT_TIMEOUT = 30000;
+
+    private BundleContext bundleContext;
+
+    @Before
+    public void setUp() throws Exception {
+
+        StandardPBEStringEncryptor enc = new StandardPBEStringEncryptor();
+        EnvironmentStringPBEConfig env = new EnvironmentStringPBEConfig();
+        env.setAlgorithm("PBEWithMD5AndDES");
+        env.setPassword("password");
+        enc.setConfig(env);
+        String val = enc.encrypt("bar");
+        System.setProperty("foo", val);
+
+        System.setProperty("org.osgi.framework.storage", "target/osgi/" + System.currentTimeMillis());
+        System.setProperty("karaf.name", "root");
+
+        List<BundleDescriptor> bundles = new ClasspathScanner().scanForBundles("(Bundle-SymbolicName=*)");
+        bundles.add(getBundleDescriptor(
+                "target/jasypt.jar",
+                newBundle().add("OSGI-INF/blueprint/karaf-jaas-jasypt.xml", getClass().getResource("/OSGI-INF/blueprint/karaf-jaas-jasypt.xml"))
+                           .set("Manifest-Version", "2")
+                           .set("Bundle-ManifestVersion", "2")
+                           .set("Bundle-SymbolicName", "jasypt")
+                           .set("Bundle-Version", "0.0.0")));
+        bundles.add(getBundleDescriptor(
+                "target/test.jar",
+                newBundle().add("OSGI-INF/blueprint/test.xml", getClass().getResource("test.xml"))
+                           .set("Manifest-Version", "2")
+                           .set("Bundle-ManifestVersion", "2")
+                           .set("Bundle-SymbolicName", "test")
+                           .set("Bundle-Version", "0.0.0")));
+
+        Map config = new HashMap();
+        config.put(PojoServiceRegistryFactory.BUNDLE_DESCRIPTORS, bundles);
+        PojoServiceRegistry reg = new PojoServiceRegistryFactoryImpl().newPojoServiceRegistry(config);
+        bundleContext = reg.getBundleContext();
+    }
+
+    private BundleDescriptor getBundleDescriptor(String path, TinyBundle bundle) throws Exception
{
+        File file = new File(path);
+        FileOutputStream fos = new FileOutputStream(file);
+        copy(bundle.build(), fos);
+        fos.close();
+        JarInputStream jis = new JarInputStream(new FileInputStream(file));
+        Map<String, String> headers = new HashMap<String, String>();
+        for (Map.Entry entry : jis.getManifest().getMainAttributes().entrySet()) {
+            headers.put(entry.getKey().toString(), entry.getValue().toString());
+        }
+        return new BundleDescriptor(
+                getClass().getClassLoader(),
+                new URL("jar:" + file.toURI().toString() + "!/"),
+                headers);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        bundleContext.getBundle().stop();
+    }
+
+    @Test
+    public void testPlaceholder() throws Exception {
+
+        for (Bundle bundle: bundleContext.getBundles()) {
+            System.out.println(bundle.getSymbolicName() + " / " + bundle.getVersion());
+        }
+
+        String encoded = getOsgiService(String.class, "(encoded=*)");
+        assertEquals("bar", encoded);
+    }
+
+
+    protected <T> T getOsgiService(Class<T> type, long timeout) {
+        return getOsgiService(type, null, timeout);
+    }
+
+    protected <T> T getOsgiService(Class<T> type) {
+        return getOsgiService(type, null, DEFAULT_TIMEOUT);
+    }
+
+    protected <T> T getOsgiService(Class<T> type, String filter) {
+        return getOsgiService(type, filter, DEFAULT_TIMEOUT);
+    }
+
+    protected <T> T getOsgiService(Class<T> type, String filter, long timeout)
{
+        ServiceTracker tracker = null;
+        try {
+            String flt;
+            if (filter != null) {
+                if (filter.startsWith("(")) {
+                    flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")"
+ filter + ")";
+                } else {
+                    flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")("
+ filter + "))";
+                }
+            } else {
+                flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
+            }
+            Filter osgiFilter = FrameworkUtil.createFilter(flt);
+            tracker = new ServiceTracker(bundleContext, osgiFilter, null);
+            tracker.open(true);
+            // Note that the tracker is not closed to keep the reference
+            // This is buggy, as the service reference may change i think
+            Object svc = type.cast(tracker.waitForService(timeout));
+            if (svc == null) {
+                Dictionary dic = bundleContext.getBundle().getHeaders();
+                System.err.println("Test bundle headers: " + explode(dic));
+
+                for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null,
null))) {
+                    System.err.println("ServiceReference: " + ref);
+                }
+
+                for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null,
flt))) {
+                    System.err.println("Filtered ServiceReference: " + ref);
+                }
+
+                throw new RuntimeException("Gave up waiting for service " + flt);
+            }
+            return type.cast(svc);
+        } catch (InvalidSyntaxException e) {
+            throw new IllegalArgumentException("Invalid filter", e);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /*
+     * Explode the dictionary into a ,-delimited list of key=value pairs
+     */
+    private static String explode(Dictionary dictionary) {
+        Enumeration keys = dictionary.keys();
+        StringBuffer result = new StringBuffer();
+        while (keys.hasMoreElements()) {
+            Object key = keys.nextElement();
+            result.append(String.format("%s=%s", key, dictionary.get(key)));
+            if (keys.hasMoreElements()) {
+                result.append(", ");
+            }
+        }
+        return result.toString();
+    }
+
+    /*
+     * Provides an iterable collection of references, even if the original array is null
+     */
+    private static final Collection<ServiceReference> asCollection(ServiceReference[]
references) {
+        List<ServiceReference> result = new LinkedList<ServiceReference>();
+        if (references != null) {
+            for (ServiceReference reference : references) {
+                result.add(reference);
+            }
+        }
+        return result;
+    }
+
+    public static long copy(final InputStream input, final OutputStream output) throws IOException
{
+        return copy(input, output, 8024);
+    }
+
+    public static long copy(final InputStream input, final OutputStream output, int buffersize)
throws IOException {
+        final byte[] buffer = new byte[buffersize];
+        int n;
+        long count=0;
+        while (-1 != (n = input.read(buffer))) {
+            output.write(buffer, 0, n);
+            count += n;
+        }
+        return count;
+    }
+
+}

Added: karaf/trunk/jaas/jasypt/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/test/resources/log4j.properties?rev=1164155&view=auto
==============================================================================
--- karaf/trunk/jaas/jasypt/src/test/resources/log4j.properties (added)
+++ karaf/trunk/jaas/jasypt/src/test/resources/log4j.properties Thu Sep  1 16:43:15 2011
@@ -0,0 +1,34 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+#
+# The logging properties used during tests..
+#
+log4j.rootLogger=DEBUG, console, file
+
+# Console will only display warnnings
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+log4j.appender.console.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1}
| %-32.32C %4L | %m%n
+#log4j.appender.console.threshold=WARN
+
+# File appender will contain all info messages
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d | %-5p | %m | %c | %t%n
+log4j.appender.file.file=target/test.log
+log4j.appender.file.append=true

Added: karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/jasypt/handler/test.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/jasypt/handler/test.xml?rev=1164155&view=auto
==============================================================================
--- karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/jasypt/handler/test.xml
(added)
+++ karaf/trunk/jaas/jasypt/src/test/resources/org/apache/karaf/jaas/jasypt/handler/test.xml
Thu Sep  1 16:43:15 2011
@@ -0,0 +1,28 @@
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+        xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
+        xmlns:enc="http://karaf.apache.org/xmlns/jasypt/v1.0.0">
+
+
+    <ext:property-placeholder />
+
+    <enc:property-placeholder>
+        <enc:encryptor class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
+            <property name="config">
+                <bean class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
+                    <property name="algorithm" value="PBEWithMD5AndDES" />
+                    <property name="password" value="password" />
+                </bean>
+            </property>
+        </enc:encryptor>
+    </enc:property-placeholder>
+
+    <service auto-export="all-classes">
+        <service-properties>
+            <entry key="encoded" value="ENC(${foo})" />
+        </service-properties>
+        <bean class="java.lang.String">
+            <argument value="ENC(${foo})" />
+        </bean>
+    </service>
+
+</blueprint>
\ No newline at end of file



Mime
View raw message