servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r960740 - in /servicemix/smx4/nmr/trunk/document/src: main/java/org/apache/servicemix/document/factory/ main/java/org/apache/servicemix/document/impl/ main/java/org/apache/servicemix/document/impl/blueprint/ main/java/org/apache/servicemix/...
Date Tue, 06 Jul 2010 01:05:26 GMT
Author: gnodet
Date: Tue Jul  6 01:05:25 2010
New Revision: 960740

URL: http://svn.apache.org/viewvc?rev=960740&view=rev
Log:
SMX4NMR-205: Provide a clean custom namespace for the document registry

Added:
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/BlueprintDocumentFactory.java
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/NamespaceHandler.java
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/NamespaceHandler.java
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/SpringDocumentFactory.java
    servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.handlers
    servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.schemas
    servicemix/smx4/nmr/trunk/document/src/main/resources/org/
    servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/
    servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/servicemix/
    servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/servicemix/document/
    servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/servicemix/document/document.xsd
Removed:
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/factory/
Modified:
    servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/Activator.java
    servicemix/smx4/nmr/trunk/document/src/test/resources/test.xml

Modified: servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/Activator.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/Activator.java?rev=960740&r1=960739&r2=960740&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/Activator.java
(original)
+++ servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/Activator.java
Tue Jul  6 01:05:25 2010
@@ -35,6 +35,11 @@ public class Activator implements Bundle
                             new String[] { DocumentRepository.class.getName(), URLStreamHandlerService.class.getName()
},
                             new DocumentRepositoryImpl(),
                             properties);
+        try {
+            org.apache.servicemix.document.impl.blueprint.NamespaceHandler.register(context);
+        } catch (Throwable t) {
+            // Ignore
+        }
     }
 
     public void stop(BundleContext context) throws Exception {

Added: servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/BlueprintDocumentFactory.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/BlueprintDocumentFactory.java?rev=960740&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/BlueprintDocumentFactory.java
(added)
+++ servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/BlueprintDocumentFactory.java
Tue Jul  6 01:05:25 2010
@@ -0,0 +1,223 @@
+/*
+ * 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 org.apache.servicemix.document.impl.blueprint;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.aries.blueprint.ComponentDefinitionRegistry;
+import org.apache.aries.blueprint.ComponentDefinitionRegistryProcessor;
+import org.apache.aries.blueprint.mutable.*;
+import org.apache.servicemix.document.DocumentRepository;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.blueprint.container.ComponentDefinitionException;
+import org.osgi.service.blueprint.reflect.*;
+
+public class BlueprintDocumentFactory implements ComponentDefinitionRegistryProcessor {
+
+    private BundleContext bundleContext;
+    private String document;
+    private String documentId;
+    private String beanName;
+    private DocumentRepository repository;
+
+    public void setDocument(String document) {
+        this.document = document;
+    }
+
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+
+    public void setBeanName(String beanName) {
+        this.beanName = beanName;
+    }
+
+    public void setRepository(DocumentRepository repository) {
+        this.repository = repository;
+    }
+
+    public void afterPropertiesSet() throws Exception {
+        if (document == null) {
+            throw new IllegalStateException("document must be set");
+        }
+        if (repository != null) {
+            this.documentId = repository.register(document.getBytes());
+        } else if (bundleContext != null) {
+            ServiceReference ref = bundleContext.getServiceReference(DocumentRepository.class.getName());
+            if (ref == null) {
+                throw new IllegalStateException("Can not get a reference to the DocumentRepository");
+            }
+            try {
+                DocumentRepository rep = (DocumentRepository) bundleContext.getService(ref);
+                this.documentId = rep.register(document.getBytes());
+            } finally {
+                if (ref != null) {
+                    bundleContext.ungetService(ref);
+                }
+            }
+        } else {
+            throw new IllegalStateException("repoitory or bundleContext must be set");
+        }
+    }
+
+    public void process(ComponentDefinitionRegistry registry) throws ComponentDefinitionException
{
+        for (String name : registry.getComponentDefinitionNames()) {
+            processMetadata(registry.getComponentDefinition(name));
+        }
+    }
+
+    protected Metadata processMetadata(Metadata metadata) {
+        if (metadata instanceof BeanMetadata) {
+            return processBeanMetadata((BeanMetadata) metadata);
+        } else if (metadata instanceof ReferenceListMetadata) {
+            return processRefCollectionMetadata((ReferenceListMetadata) metadata);
+        } else if (metadata instanceof ReferenceMetadata) {
+            return processReferenceMetadata((ReferenceMetadata) metadata);
+        } else if (metadata instanceof ServiceMetadata) {
+            return processServiceMetadata((ServiceMetadata) metadata);
+        } else if (metadata instanceof CollectionMetadata) {
+            return processCollectionMetadata((CollectionMetadata) metadata);
+        } else if (metadata instanceof MapMetadata) {
+            return processMapMetadata((MapMetadata) metadata);
+        } else if (metadata instanceof PropsMetadata) {
+            return processPropsMetadata((PropsMetadata) metadata);
+        } else if (metadata instanceof ValueMetadata) {
+            return processValueMetadata((ValueMetadata) metadata);
+        } else {
+            return metadata;
+        }
+    }
+
+    protected Metadata processBeanMetadata(BeanMetadata component) {
+        for (BeanArgument arg :  component.getArguments()) {
+            ((MutableBeanArgument) arg).setValue(processMetadata(arg.getValue()));
+        }
+        for (BeanProperty prop : component.getProperties()) {
+            ((MutableBeanProperty) prop).setValue(processMetadata(prop.getValue()));
+        }
+        ((MutableBeanMetadata) component).setFactoryComponent((Target) processMetadata(component.getFactoryComponent()));
+        return component;
+    }
+
+    protected Metadata processServiceMetadata(ServiceMetadata component) {
+        ((MutableServiceMetadata) component).setServiceComponent((Target) processMetadata(component.getServiceComponent()));
+        List<MapEntry> entries = new ArrayList<MapEntry>(component.getServiceProperties());
+        for (MapEntry entry : entries) {
+            ((MutableServiceMetadata) component).removeServiceProperty(entry);
+        }
+        for (MapEntry entry : processMapEntries(entries)) {
+            ((MutableServiceMetadata) component).addServiceProperty(entry);
+        }
+        for (RegistrationListener listener : component.getRegistrationListeners()) {
+            ((MutableRegistrationListener) listener).setListenerComponent((Target) processMetadata(listener.getListenerComponent()));
+        }
+        return component;
+    }
+
+    protected Metadata processReferenceMetadata(ReferenceMetadata component) {
+        for (ReferenceListener listener : component.getReferenceListeners()) {
+            ((MutableReferenceListener) listener).setListenerComponent((Target) processMetadata(listener.getListenerComponent()));
+        }
+        return component;
+    }
+
+    protected Metadata processRefCollectionMetadata(ReferenceListMetadata component) {
+        for (ReferenceListener listener : component.getReferenceListeners()) {
+            ((MutableReferenceListener) listener).setListenerComponent((Target) processMetadata(listener.getListenerComponent()));
+        }
+        return component;
+    }
+
+    protected Metadata processPropsMetadata(PropsMetadata metadata) {
+        List<MapEntry> entries = new ArrayList<MapEntry>(metadata.getEntries());
+        for (MapEntry entry : entries) {
+            ((MutablePropsMetadata) metadata).removeEntry(entry);
+        }
+        for (MapEntry entry : processMapEntries(entries)) {
+            ((MutablePropsMetadata) metadata).addEntry(entry);
+        }
+        return metadata;
+    }
+
+    protected Metadata processMapMetadata(MapMetadata metadata) {
+        List<MapEntry> entries = new ArrayList<MapEntry>(metadata.getEntries());
+        for (MapEntry entry : entries) {
+            ((MutableMapMetadata) metadata).removeEntry(entry);
+        }
+        for (MapEntry entry : processMapEntries(entries)) {
+            ((MutableMapMetadata) metadata).addEntry(entry);
+        }
+        return metadata;
+    }
+
+    protected List<MapEntry> processMapEntries(List<MapEntry> entries) {
+        for (MapEntry entry : entries) {
+            ((MutableMapEntry) entry).setKey((NonNullMetadata) processMetadata(entry.getKey()));
+            ((MutableMapEntry) entry).setValue(processMetadata(entry.getValue()));
+        }
+        return entries;
+    }
+
+    protected Metadata processCollectionMetadata(CollectionMetadata metadata) {
+        List<Metadata> values = new ArrayList<Metadata>(metadata.getValues());
+        for (Metadata value : values) {
+            ((MutableCollectionMetadata) metadata).removeValue(value);
+        }
+        for (Metadata value : values) {
+            ((MutableCollectionMetadata) metadata).addValue(processMetadata(value));
+        }
+        return metadata;
+    }
+
+    protected Metadata processValueMetadata(ValueMetadata metadata) {
+        return new LateBindingValueMetadata(metadata);
+    }
+
+    public class LateBindingValueMetadata implements ValueMetadata {
+
+        private final ValueMetadata metadata;
+        private boolean retrieved;
+        private String retrievedValue;
+
+        public LateBindingValueMetadata(ValueMetadata metadata) {
+            this.metadata = metadata;
+        }
+
+        public String getStringValue() {
+            if (!retrieved) {
+                retrieved = true;
+                retrievedValue = processString(metadata.getStringValue());
+            }
+            return retrievedValue;
+        }
+
+        public String getType() {
+            return metadata.getType();
+        }
+
+    }
+
+    protected String processString(String str) {
+        if (str.equals("document-name:" + beanName)) {
+            str = documentId;
+        }
+        return str;
+    }
+
+}

Added: servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/NamespaceHandler.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/NamespaceHandler.java?rev=960740&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/NamespaceHandler.java
(added)
+++ servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/blueprint/NamespaceHandler.java
Tue Jul  6 01:05:25 2010
@@ -0,0 +1,121 @@
+/*
+ * 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 org.apache.servicemix.document.impl.blueprint;
+
+import java.net.URL;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.aries.blueprint.ParserContext;
+import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
+import org.apache.aries.blueprint.mutable.MutableRefMetadata;
+import org.apache.aries.blueprint.mutable.MutableValueMetadata;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.blueprint.container.ComponentDefinitionException;
+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 DOCUMENT = "document";
+    public static final String ID = "id";
+    public static final String REPOSITORY = "repository";
+
+    public static ServiceRegistration register(BundleContext context) {
+        Properties props = new Properties();
+        props.put("osgi.service.blueprint.namespace", "http://servicemix.apache.org/schema/document");
+        return context.registerService(
+                new String[] { org.apache.aries.blueprint.NamespaceHandler.class.getName()
},
+                new NamespaceHandler(),
+                props
+        );
+    }
+
+    public URL getSchemaLocation(String s) {
+        return getClass().getResource("/org/apache/servicemix/document/document.xsd");
+    }
+
+    public Set<Class> getManagedClasses() {
+        return new HashSet<Class>(Arrays.asList(
+                        BlueprintDocumentFactory.class
+        ));
+    }
+
+    public Metadata parse(Element element, ParserContext context) {
+        String name = element.getLocalName() != null ? element.getLocalName() : element.getNodeName();
+        if (DOCUMENT.equals(name)) {
+            return parseDocument(element, context);
+        }
+        throw new ComponentDefinitionException("Bad xml syntax: unknown element '" + name
+ "'");
+    }
+
+    public ComponentMetadata decorate(Node node, ComponentMetadata componentMetadata, ParserContext
parserContext) {
+        throw new ComponentDefinitionException("Unsupported node: " + node.getNodeName());
+    }
+
+    private Metadata parseDocument(Element element, ParserContext context) {
+        MutableBeanMetadata bean = context.createMetadata(MutableBeanMetadata.class);
+        bean.setRuntimeClass(BlueprintDocumentFactory.class);
+        String id = element.getAttribute(ID);
+        bean.setId(id);
+        bean.addProperty("beanName", createValue(context, id));
+        bean.addProperty("bundleContext", createRef(context, "blueprintBundleContext"));
+        if (element.hasAttribute(REPOSITORY)) {
+            bean.addProperty(REPOSITORY, createRef(context, element.getAttribute(REPOSITORY)));
+        }
+        bean.addProperty(DOCUMENT, createValue(context, getTextValue(element)));
+        bean.setInitMethod("afterPropertiesSet");
+        bean.setProcessor(true);
+        return bean;
+    }
+
+    private ValueMetadata createValue(ParserContext context, String value) {
+        MutableValueMetadata v = context.createMetadata(MutableValueMetadata.class);
+        v.setStringValue(value);
+        return v;
+    }
+
+    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();
+    }
+
+}

Added: servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/NamespaceHandler.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/NamespaceHandler.java?rev=960740&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/NamespaceHandler.java
(added)
+++ servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/NamespaceHandler.java
Tue Jul  6 01:05:25 2010
@@ -0,0 +1,66 @@
+/*
+ * 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 org.apache.servicemix.document.impl.spring;
+
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
+import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
+import org.springframework.beans.factory.xml.ParserContext;
+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 extends NamespaceHandlerSupport {
+
+    public static final String DOCUMENT = "document";
+    public static final String REPOSITORY = "repository";
+
+    public void init() {
+        registerBeanDefinitionParser(DOCUMENT, new DocumentParser());
+    }
+
+    public static class DocumentParser extends AbstractBeanDefinitionParser {
+
+        @Override
+        protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
{
+            BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(SpringDocumentFactory.class);
+            if (element.hasAttribute(REPOSITORY)) {
+                bdb.addPropertyReference(REPOSITORY, element.getAttribute(REPOSITORY));
+            }
+            bdb.addPropertyValue(DOCUMENT, getTextValue(element));
+            return bdb.getBeanDefinition();
+        }
+
+    }
+
+    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();
+    }
+
+}

Added: servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/SpringDocumentFactory.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/SpringDocumentFactory.java?rev=960740&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/SpringDocumentFactory.java
(added)
+++ servicemix/smx4/nmr/trunk/document/src/main/java/org/apache/servicemix/document/impl/spring/SpringDocumentFactory.java
Tue Jul  6 01:05:25 2010
@@ -0,0 +1,132 @@
+/*
+ * 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 org.apache.servicemix.document.impl.spring;
+
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanDefinitionStoreException;
+import org.springframework.beans.factory.BeanNameAware;
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.config.BeanDefinitionVisitor;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.BeansException;
+import org.springframework.osgi.context.BundleContextAware;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.apache.servicemix.document.DocumentRepository;
+
+/**
+ * A spring document factory allowing documents to be registered in the DocumentRegistry
+ * and also post processes the document-name:xxx url to transform it into a
+ * document: url handled by the repository.
+ */
+public class SpringDocumentFactory implements FactoryBean, BundleContextAware, InitializingBean,
BeanFactoryPostProcessor, BeanNameAware, BeanFactoryAware {
+
+    private BundleContext bundleContext;
+    private String document;
+    private String documentId;
+    private BeanFactory beanFactory;
+    private String beanName;
+    private DocumentRepository repository;
+
+    public Object getObject() throws Exception {
+        return documentId;
+    }
+
+    public Class getObjectType() {
+        return String.class;
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+
+    public void setDocument(String document) {
+        this.document = document;
+    }
+
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+
+    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
+        this.beanFactory = beanFactory;
+    }
+
+    public void setBeanName(String beanName) {
+        this.beanName = beanName;
+    }
+
+    public void setRepository(DocumentRepository repository) {
+        this.repository = repository;
+    }
+
+    public void afterPropertiesSet() throws Exception {
+        if (document == null) {
+            throw new IllegalStateException("document must be set");
+        }
+        if (repository != null) {
+            this.documentId = repository.register(document.getBytes());
+        } else if (bundleContext != null) {
+            ServiceReference ref = bundleContext.getServiceReference(DocumentRepository.class.getName());
+            if (ref == null) {
+                throw new IllegalStateException("Can not get a reference to the DocumentRepository");
+            }
+            try {
+                DocumentRepository rep = (DocumentRepository) bundleContext.getService(ref);
+                this.documentId = rep.register(document.getBytes());
+            } finally {
+                if (ref != null) {
+                    bundleContext.ungetService(ref);
+                }
+            }
+        } else {
+            throw new IllegalStateException("repoitory or bundleContext must be set");
+        }
+    }
+
+    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactoryToProcess)
throws BeansException {
+        BeanDefinitionVisitor visitor = new BundleExtUrlBeanDefinitionVisitor();
+        String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
+        for (int i = 0; i < beanNames.length; i++) {
+            // Check that we're not parsing our own bean definition,
+            // to avoid failing on unresolvable placeholders in properties file locations.
+            if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory)))
{
+                BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]);
+                try {
+                    visitor.visitBeanDefinition(bd);
+                } catch (BeanDefinitionStoreException ex) {
+                    throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i],
ex.getMessage());
+                }
+            }
+        }
+    }
+
+    private class BundleExtUrlBeanDefinitionVisitor extends BeanDefinitionVisitor {
+
+        protected String resolveStringValue(String string) {
+            if (string.equals("document-name:" + beanName)) {
+                string = documentId;
+            }
+            return string;
+        }
+    }
+
+}

Added: servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.handlers
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.handlers?rev=960740&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.handlers (added)
+++ servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.handlers Tue Jul
 6 01:05:25 2010
@@ -0,0 +1,24 @@
+##
+## 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.
+##
+
+##
+## $Rev: 697807 $ $Date: 2008-09-22 20:12:04 +0700 (Mon, 22 Sep 2008) $
+##
+
+http\://servicemix.apache.org/schema/document=org.apache.servicemix.document.impl.spring.NamespaceHandler
\ No newline at end of file

Added: servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.schemas
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.schemas?rev=960740&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.schemas (added)
+++ servicemix/smx4/nmr/trunk/document/src/main/resources/META-INF/spring.schemas Tue Jul
 6 01:05:25 2010
@@ -0,0 +1,24 @@
+##
+## 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.
+##
+
+##
+## $Rev: 697807 $ $Date: 2008-09-22 20:12:04 +0700 (Mon, 22 Sep 2008) $
+##
+
+http\://servicemix.apache.org/schema/document/document.xsd=org/apache/servicemix/document/document.xsd

Added: servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/servicemix/document/document.xsd
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/servicemix/document/document.xsd?rev=960740&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/servicemix/document/document.xsd
(added)
+++ servicemix/smx4/nmr/trunk/document/src/main/resources/org/apache/servicemix/document/document.xsd
Tue Jul  6 01:05:25 2010
@@ -0,0 +1,36 @@
+<?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.
+-->
+
+<!-- $Rev: 699828 $ $Date: 2008-09-28 16:35:27 +0200 (Sun, 28 Sep 2008) $ -->
+
+<xsd:schema xmlns="http://servicemix.apache.org/schema/document"
+        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+        targetNamespace="http://servicemix.apache.org/schema/document"
+        elementFormDefault="qualified"
+        attributeFormDefault="unqualified">
+
+    <xsd:element name="document">
+        <xsd:complexType mixed="true">
+            <xsd:attribute name="id" type="xsd:ID" use="required"/>
+            <xsd:attribute name="repository" type="xsd:string" use="optional"/>
+        </xsd:complexType>
+    </xsd:element>
+
+</xsd:schema>
\ No newline at end of file

Modified: servicemix/smx4/nmr/trunk/document/src/test/resources/test.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/document/src/test/resources/test.xml?rev=960740&r1=960739&r2=960740&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/document/src/test/resources/test.xml (original)
+++ servicemix/smx4/nmr/trunk/document/src/test/resources/test.xml Tue Jul  6 01:05:25 2010
@@ -21,18 +21,22 @@
        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"
+       xmlns:doc="http://servicemix.apache.org/schema/document"
        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">
+  http://www.springframework.org/schema/osgi/spring-osgi.xsd
+  http://servicemix.apache.org/schema/document
+  http://servicemix.apache.org/schema/document/document.xsd
+  ">
 
     <bean id="documentRepository" class="org.apache.servicemix.document.impl.DocumentRepositoryImpl">
     </bean>
 
-    <bean id="doc" class="org.apache.servicemix.document.factory.SpringDocumentFactory">
+    <bean id="doc" class="org.apache.servicemix.document.impl.spring.SpringDocumentFactory">
         <property name="repository" ref="documentRepository" />
         <property name="document">
             <value>
@@ -41,8 +45,16 @@
         </property>
     </bean>
 
+    <doc:document id="doc2" repository="documentRepository">
+        Hey guy !
+    </doc:document>
+
     <bean id="uri" class="java.net.URI" factory-method="create">
         <constructor-arg value="document-name:doc" />
     </bean>
 
+    <bean id="uri2" class="java.net.URI" factory-method="create">
+        <constructor-arg value="document-name:doc2" />
+    </bean>
+
 </beans>



Mime
View raw message