clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [2/2] git commit: CLEREZZA-886: Parser and Serialized indicate thee supported format with a service property
Date Sun, 02 Mar 2014 13:29:08 GMT
CLEREZZA-886: Parser and Serialized indicate thee supported format with a service property

Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/cf4ea9c4
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/cf4ea9c4
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/cf4ea9c4

Branch: refs/heads/master
Commit: cf4ea9c4cfb62e6e4f2f3540fe8f5f32fd9b0d63
Parents: 659b780
Author: retobg <reto@apache.org>
Authored: Sun Mar 2 14:28:24 2014 +0100
Committer: retobg <reto@apache.org>
Committed: Sun Mar 2 14:28:24 2014 +0100

----------------------------------------------------------------------
 .../rdf/core/serializedform/Parser.java         | 114 +++++++++++++++----
 .../rdf/core/serializedform/Serializer.java     |  91 ++++++++++++---
 .../core/serializedform/SupportedFormat.java    |   4 +
 3 files changed, 174 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/cf4ea9c4/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java
----------------------------------------------------------------------
diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java
b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java
index 62096d7..474902b 100644
--- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java
+++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java
@@ -18,44 +18,57 @@
  */
 package org.apache.clerezza.rdf.core.serializedform;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.ServiceLoader;
 import java.util.Set;
+import java.util.logging.Level;
 
 import org.apache.clerezza.rdf.core.Graph;
 import org.apache.clerezza.rdf.core.MGraph;
 import org.apache.clerezza.rdf.core.UriRef;
 import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
 
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.osgi.service.component.annotations.ReferencePolicy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
- * This singleton class provides a method <code>parse</code> to transform 
- * serialized RDF forms into {@link Graph}s.
- * 
+ * This singleton class provides a method
+ * <code>parse</code> to transform serialized RDF forms into {@link Graph}s.
+ *
  * Functionality is delegated to registered {@link ParsingProvider}s. Such
- * <code>ParsingProvider</code>s can be registered and unregistered, later 
- * registered <code>ParsingProvider</code>s shadow previously registered
- * providers for the same format.
+ * <code>ParsingProvider</code>s can be registered and unregistered, later
+ * registered
+ * <code>ParsingProvider</code>s shadow previously registered providers for the
+ * same format.
+ *
+ * Note on synchronization:
+ * <code>ParsingProvider</code>s must be able to handle concurrent requests.
  *
- * Note on synchronization: <code>ParsingProvider</code>s must be able to handle
- * concurrent requests.
- * 
  * @author reto
- * 
+ *
  */
 @Component(service = Parser.class)
 public class Parser {
 
+    private ConfigurationAdmin configurationAdmin;
     /**
      * The list of providers in the order of registration
      */
@@ -68,26 +81,29 @@ public class Parser {
      * The singleton instance
      */
     private volatile static Parser instance;
+    private boolean active;
 
+    private static final Logger log = LoggerFactory.getLogger(Parser.class);
     /**
      * the constructor sets the singleton instance to allow instantiation
      * by OSGi-DS. This constructor should not be called except by OSGi-DS,
      * otherwise the static <code>getInstance</code> method should be used.
      */
     public Parser() {
+        log.info("constructing Parser");
         Parser.instance = this;
     }
 
     /**
      * A constructor for tests, which doesn't set the singleton instance
-     * 
+     *
      * @param dummy an ignored argument to distinguish this from the other constructor
      */
     Parser(Object dummy) {
     }
 
     /**
-     * This returns the singleton instance, if an instance has been previously 
+     * This returns the singleton instance, if an instance has been previously
      * created (e.g. by OSGi declarative services) this instance is returned,
      * otherwise a new instance is created and providers are injected using 
      * the service provider interface (META-INF/services/)
@@ -112,6 +128,36 @@ public class Parser {
 
     }
 
+    @Activate
+    protected void activate(final ComponentContext componentContext) {
+        active = true;
+        refreshProviderMap();
+        //changing the congiguration before this finshed activating causes a new instance
to be created
+        /*(new Thread() {
+            @Override
+            public void run() {
+                try {
+                    Thread.sleep(100);
+                } catch (InterruptedException ex) {
+                    return;
+                }
+                refreshProviderMap();
+            }
+            
+            
+        }).start();*/
+    }
+
+    @Deactivate
+    protected void deactivate(final ComponentContext componentContext) {
+        active = false;
+    }
+    
+    @Modified
+    void modified(ComponentContext ctx) {
+        log.debug("modified");
+    }
+
     /**
      * Parses a serialized Graph from an InputStream. This delegates the
      * processing to the provider registered for the specified format, if
@@ -190,9 +236,10 @@ public class Parser {
         }
         provider.parse(target, serializedGraph, formatIdentifier, baseUri);
     }
-    
+
     /**
      * Get a set of supported formats
+     *
      * @return a set if stings identifying formats (usually the MIME-type)
      */
     public Set<String> getSupportedFormats() {
@@ -204,7 +251,7 @@ public class Parser {
      *
      * @param provider the provider to be registered
      */
-    @Reference(policy = ReferencePolicy.DYNAMIC, 
+    @Reference(policy = ReferencePolicy.DYNAMIC,
             cardinality = ReferenceCardinality.MULTIPLE)
     public void bindParsingProvider(ParsingProvider provider) {
         providerList.add(provider);
@@ -223,19 +270,35 @@ public class Parser {
 
     /**
      * Update providerMap with the providers in the providerList
-     * 
+     *
      */
     private void refreshProviderMap() {
-        final Map<String, ParsingProvider> newProviderMap = new HashMap<String,
ParsingProvider>();
-        for (ParsingProvider provider : providerList) {
-            String[] formatIdentifiers = getFormatIdentifiers(provider);
-            for (String formatIdentifier : formatIdentifiers) {
-                newProviderMap.put(formatIdentifier, provider);
+        if (active) {
+            try {
+                final Map<String, ParsingProvider> newProviderMap = new HashMap<String,
ParsingProvider>();
+                for (ParsingProvider provider : providerList) {
+                    String[] formatIdentifiers = getFormatIdentifiers(provider);
+                    for (String formatIdentifier : formatIdentifiers) {
+                        newProviderMap.put(formatIdentifier, provider);
+                    }
+                }
+                providerMap = newProviderMap;
+                Dictionary<String, Object> newConfig = configurationAdmin.getConfiguration(getClass().getName()).getProperties();
+                if (newConfig == null) {
+                    newConfig = new Hashtable<String, Object>();
+                }
+                Set<String> supportedFormats = getSupportedFormats();
+                String[] supportedFromatsArray = supportedFormats.toArray(new String[supportedFormats.size()]);
+                newConfig.put(SupportedFormat.supportedFormat, supportedFromatsArray);
+                configurationAdmin.getConfiguration(getClass().getName()).update(newConfig);
+            } catch (IOException ex) {
+                throw new RuntimeException(ex);
             }
         }
-        providerMap = newProviderMap;
     }
 
+    
+
     /**
      * Extract format identifiers for a parsing provider
      *
@@ -248,4 +311,13 @@ public class Parser {
         String[] formatIdentifiers = supportedFormatAnnotation.value();
         return formatIdentifiers;
     }
+
+    @Reference
+    protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
+        this.configurationAdmin = configurationAdmin;
+    }
+
+    protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
+        this.configurationAdmin = null;
+    }
 }

http://git-wip-us.apache.org/repos/asf/clerezza/blob/cf4ea9c4/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Serializer.java
----------------------------------------------------------------------
diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Serializer.java
b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Serializer.java
index 1a4c8a9..05bf549 100644
--- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Serializer.java
+++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Serializer.java
@@ -18,22 +18,33 @@
  */
 package org.apache.clerezza.rdf.core.serializedform;
 
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.ServiceLoader;
+import java.util.Set;
 
 import org.apache.clerezza.rdf.core.TripleCollection;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
 
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.osgi.service.component.annotations.ReferencePolicy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This singleton class provides a method <code>serialize</code> to transform
a
@@ -52,6 +63,8 @@ import org.osgi.service.component.annotations.ReferencePolicy;
  */
 @Component(service = Serializer.class)
 public class Serializer {
+    
+    private ConfigurationAdmin configurationAdmin;
 
     /**
      * The list of providers in the order of registration
@@ -67,6 +80,10 @@ public class Serializer {
      * The singleton instance
      */
     private volatile static Serializer instance;
+    
+    private static final Logger log = LoggerFactory.getLogger(Serializer.class);
+    
+    private boolean active;
 
     /**
      * the constructor sets the singleton instance to allow instantiation
@@ -113,6 +130,22 @@ public class Serializer {
         return instance;
 
     }
+ 
+    @Activate
+    protected void activate(final ComponentContext componentContext) {
+        active = true;
+        refreshProviderMap();
+    }
+
+    @Deactivate
+    protected void deactivate(final ComponentContext componentContext) {
+        active = false;
+    }
+    
+    @Modified
+    void modified(ComponentContext ctx) {
+        log.debug("modified");
+    }
 
     /**
      * Serializes a Graph into an OutputStream. This delegates the
@@ -145,6 +178,15 @@ public class Serializer {
     }
 
     /**
+     * Get a set of supported formats
+     *
+     * @return a set if stings identifying formats (usually the MIME-type)
+     */
+    public Set<String> getSupportedFormats() {
+        return Collections.unmodifiableSet(providerMap.keySet());
+    }
+    
+    /**
      * Registers a Serializing provider
      * 
      * @param provider
@@ -171,23 +213,35 @@ public class Serializer {
     }
 
     private void refreshProviderMap() {
-        final Map<String, SerializingProvider> newProviderMap = new HashMap<String,
SerializingProvider>();
-        //we want more generic providers first so they get overridden by more specific ones
-        Collections.sort(providerList, new Comparator<SerializingProvider>() {
-
-            @Override
-            public int compare(SerializingProvider s1, SerializingProvider s2) {
-                return getFormatIdentifiers(s2).length - getFormatIdentifiers(s1).length;
+        if (active) {
+            final Map<String, SerializingProvider> newProviderMap = new HashMap<String,
SerializingProvider>();
+            //we want more generic providers first so they get overridden by more specific
ones
+            Collections.sort(providerList, new Comparator<SerializingProvider>() {
+                @Override
+                public int compare(SerializingProvider s1, SerializingProvider s2) {
+                    return getFormatIdentifiers(s2).length - getFormatIdentifiers(s1).length;
+                }
+            });
+            for (SerializingProvider provider : providerList) {
+                String[] formatIdentifiers = getFormatIdentifiers(provider);
+                for (String formatIdentifier : formatIdentifiers) {
+                    newProviderMap.put(formatIdentifier, provider);
+                }
             }
-
-        });
-        for (SerializingProvider provider : providerList) {
-            String[] formatIdentifiers = getFormatIdentifiers(provider);
-            for (String formatIdentifier : formatIdentifiers) {
-                newProviderMap.put(formatIdentifier, provider);
+            providerMap = newProviderMap;
+            try {
+                Dictionary<String, Object> newConfig = configurationAdmin.getConfiguration(getClass().getName()).getProperties();
+                if (newConfig == null) {
+                    newConfig = new Hashtable<String, Object>();
+                }
+                Set<String> supportedFormats = getSupportedFormats();
+                String[] supportedFromatsArray = supportedFormats.toArray(new String[supportedFormats.size()]);
+                newConfig.put(SupportedFormat.supportedFormat, supportedFromatsArray);
+                configurationAdmin.getConfiguration(getClass().getName()).update(newConfig);
+            } catch (IOException ex) {
+                throw new RuntimeException(ex);
             }
         }
-        providerMap = newProviderMap;
     }
 
     private String[] getFormatIdentifiers(
@@ -199,4 +253,13 @@ public class Serializer {
         String[] formatIdentifiers = supportedFormatAnnotation.value();
         return formatIdentifiers;
     }
+    
+    @Reference
+    protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
+        this.configurationAdmin = configurationAdmin;
+    }
+
+    protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
+        this.configurationAdmin = null;
+    }
 }

http://git-wip-us.apache.org/repos/asf/clerezza/blob/cf4ea9c4/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/SupportedFormat.java
----------------------------------------------------------------------
diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/SupportedFormat.java
b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/SupportedFormat.java
index b867c48..dc4a69f 100644
--- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/SupportedFormat.java
+++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/SupportedFormat.java
@@ -33,6 +33,10 @@ import java.lang.annotation.Target;
 @Retention(RetentionPolicy.RUNTIME)
 public @interface SupportedFormat {
 
+    /**
+     * used as a key for the OSGi service property
+     */
+    public static final String supportedFormat = "supportedFormat";
     public static final String RDF_XML = "application/rdf+xml";
     public static final String TURTLE = "text/turtle";
     public static final String X_TURTLE = "application/x-turtle";


Mime
View raw message