You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2006/09/07 21:57:37 UTC

svn commit: r441195 - in /incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera: Abdera.java util/AbderaConfiguration.java util/Constants.java util/ServiceUtil.java

Author: jmsnell
Date: Thu Sep  7 12:57:36 2006
New Revision: 441195

URL: http://svn.apache.org/viewvc?view=rev&rev=441195
Log:
Improvements to the thread safety of AbderaConfiguration

Make the creation of the ParserFactory, WriterFactory and Writer components consistent
with the Parser, Factory, etc.

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/Abdera.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/Abdera.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/Abdera.java?view=diff&rev=441195&r1=441194&r2=441195
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/Abdera.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/Abdera.java Thu Sep  7 12:57:36 2006
@@ -138,33 +138,21 @@
    * Return a new instance of org.apache.abdera.parser.ParserFactory
    */
   private ParserFactory newParserFactory() {
-    return
-      (ParserFactory) ServiceUtil.newInstance(
-      "org.apache.abdera.parser.ParserFactory",
-      "org.apache.abdera.parser.stax.FOMParserFactory", 
-      this);
+    return ServiceUtil.newParserFactoryInstance(this);
   }
     
   /**
    * Return a new instance of org.apache.abdera.writer.WriterFactory
    */
   private WriterFactory newWriterFactory() {
-    return
-      (WriterFactory) ServiceUtil.newInstance(
-        "org.apache.abdera.writer.WriterFactory",
-        "org.apache.abdera.parser.stax.FOMWriterFactory", 
-        this);
+    return ServiceUtil.newWriterFactoryInstance(this);
   }
     
   /**
    * Return a new instance of org.apache.abdera.writer.Writer
    */
   private Writer newWriter() {
-    return 
-      (Writer) ServiceUtil.newInstance(
-      "org.apache.abdera.writer.Writer",
-      "org.apache.abdera.parser.stax.FOMWriter", 
-      this);
+    return ServiceUtil.newWriterInstance(this);
   }
   
   // Static convenience methods //

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java?view=diff&rev=441195&r1=441194&r2=441195
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java Thu Sep  7 12:57:36 2006
@@ -17,6 +17,7 @@
 */
 package org.apache.abdera.util;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -30,66 +31,78 @@
 public final class AbderaConfiguration 
   implements Constants, Cloneable {
   
-  private static AbderaConfiguration instance = null;
-  
   public static synchronized AbderaConfiguration getDefault() {
-    if (instance == null) {
-      try {
-        ResourceBundle bundle = ResourceBundle.getBundle("abdera");
-        instance = new AbderaConfiguration(bundle);
-      } catch (Exception e) {
-        instance = new AbderaConfiguration();
-      }
-    } 
-    return instance;
+    AbderaConfiguration instance = null;
+    try {
+      ResourceBundle bundle = ResourceBundle.getBundle("abdera");
+      instance = new AbderaConfiguration(bundle);
+    } catch (Exception e) {
+      instance = new AbderaConfiguration();
+    }
+    return instance; 
   }
   
   private static ResourceBundle getBundle(
     ClassLoader loader, 
     Locale locale) {
+      ResourceBundle bundle = null;
       try {
-        ResourceBundle bundle = 
+        bundle = 
           ResourceBundle.getBundle(
             "abdera", 
             locale, 
             loader);
-        return bundle;
       } catch (Exception e) {
-        // Nothing
+        // Do nothing
       }
-      return null;
+      return bundle;
   }
   
-  private ResourceBundle bundle = null;
-  private List<ExtensionFactory> factories = null;
-  private Map<String,NamedWriter> writers = null;
-  private Map<String,NamedParser> parsers = null;
-  private String xpath = null;
-  private String parser = null;
-  private String factory = null;
+  private final ResourceBundle bundle;
+  private final String xpath;
+  private final String parser;
+  private final String factory;
+  private final String parserFactory;
+  private final String writerFactory;
+  private final String writer;
+  private final List<ExtensionFactory> factories;
+  private final Map<String,NamedWriter> writers;
+  private final Map<String,NamedParser> parsers;
   
-  public AbderaConfiguration() {}
+  public AbderaConfiguration() {
+    this(null);
+  }
   
   protected AbderaConfiguration(ResourceBundle bundle) {
-    this.bundle = bundle;
-  }  
-  
-  private synchronized ResourceBundle getBundle() {
-    if (bundle == null) {
-      bundle = AbderaConfiguration.getBundle(
+    this.bundle = (bundle != null) ? bundle : 
+      AbderaConfiguration.getBundle(
         ServiceUtil.getClassLoader(), 
         Locale.getDefault());
-    } 
+    xpath = getConfigurationOption(CONFIG_XPATH, DEFAULT_XPATH);
+    parser = getConfigurationOption(CONFIG_PARSER, DEFAULT_PARSER);
+    factory = getConfigurationOption(CONFIG_FACTORY, DEFAULT_FACTORY);
+    parserFactory = getConfigurationOption(CONFIG_PARSERFACTORY, DEFAULT_PARSERFACTORY);
+    writerFactory = getConfigurationOption(CONFIG_WRITERFACTORY, DEFAULT_WRITERFACTORY);
+    writer = getConfigurationOption(CONFIG_WRITER, DEFAULT_WRITER);
+    factories = ServiceUtil.loadExtensionFactories();
+    writers = initNamedWriters();
+    parsers = initNamedParsers();
+  }  
+  
+  private ResourceBundle getBundle() {
     return bundle;
   }
 
   public String getConfigurationOption(String id) {
     String option = System.getProperty(id);
-    try {
-      ResourceBundle bundle = getBundle();
-      if (option == null && bundle != null)
-        option = bundle.getString(id);
-    } catch (Exception e) {}
+    if (option == null) {
+      try {
+        ResourceBundle bundle = getBundle();
+        if (bundle != null) option = bundle.getString(id);
+      } catch (Exception e) {
+        // Do Nothing
+      }
+    }
     return option;
   }
   
@@ -99,64 +112,76 @@
   }
   
   public String getDefaultXPath() {
-    return (xpath == null) ? 
-      getConfigurationOption(CONFIG_XPATH, DEFAULT_XPATH) : xpath;
+    return xpath;
   }
   
   public String getDefaultParser() {
-    return (parser == null) ? 
-      getConfigurationOption(CONFIG_PARSER, DEFAULT_PARSER) : parser;
+    return parser;
   }
   
   public String getDefaultFactory() {
-    return (factory == null) ? 
-      getConfigurationOption(CONFIG_FACTORY, DEFAULT_FACTORY) : factory;
+    return factory;
+  }
+  
+  public String getDefaultParserFactory() {
+    return parserFactory; 
+  }
+  
+  public String getDefaultWriterFactory() {
+    return writerFactory;
+  }
+  
+  public String getDefaultWriter() {
+    return writer;
   }
   
-  public synchronized void addExtensionFactory(ExtensionFactory factory) {
+  public void addExtensionFactory(ExtensionFactory factory) {
     List<ExtensionFactory> factories = getExtensionFactories();
     if (!factories.contains(factory))
       factories.add(factory);
   }
   
-  public synchronized List<ExtensionFactory> getExtensionFactories() {
-    if (factories == null) factories = ServiceUtil.loadExtensionFactories();
+  public List<ExtensionFactory> getExtensionFactories() {
     return factories;
   }
   
-  public synchronized void addNamedWriter(NamedWriter writer) {
+  public void addNamedWriter(NamedWriter writer) {
     Map<String,NamedWriter> writers = getNamedWriters();
     writers.put(writer.getName(), writer);
   }
   
-  public synchronized Map<String,NamedWriter> getNamedWriters() {
-    if (writers == null) {
-      List<NamedWriter> _writers = 
-        ServiceUtil._loadimpls(
-          "META-INF/services/org.apache.abdera.writer.NamedWriter");
-      writers = new HashMap<String,NamedWriter>();
-      for (NamedWriter writer : _writers) {
-        writers.put(writer.getName().toLowerCase(), writer);
-      }
+  private Map<String,NamedWriter> initNamedWriters() {
+    Map<String,NamedWriter> writers = null;
+    List<NamedWriter> _writers = 
+      ServiceUtil._loadimpls(NAMED_WRITER);
+    writers = Collections.synchronizedMap(new HashMap<String,NamedWriter>());
+    for (NamedWriter writer : _writers) {
+      writers.put(writer.getName().toLowerCase(), writer);
     }
     return writers;
   }
   
-  public synchronized void addNamedParser(NamedParser parser) {
+  public Map<String,NamedWriter> getNamedWriters() {
+    return writers;
+  }
+  
+  public void addNamedParser(NamedParser parser) {
     Map<String,NamedParser> parsers = getNamedParsers();
     parsers.put(parser.getName(), parser);
   }
   
-  public synchronized Map<String,NamedParser> getNamedParsers() {
-    if (parsers == null) {
-      List<NamedParser> _parsers = 
-        ServiceUtil._loadimpls(
-          "META-INF/services/org.apache.abdera.parser.NamedParser");
-      parsers = new HashMap<String,NamedParser>();
-      for (NamedParser parser : _parsers) {
-        parsers.put(parser.getName().toLowerCase(), parser);
-      }
+  private Map<String,NamedParser> initNamedParsers() {
+    Map<String,NamedParser> parsers = null;
+    List<NamedParser> _parsers = 
+      ServiceUtil._loadimpls(NAMED_PARSER);
+    parsers = Collections.synchronizedMap(new HashMap<String,NamedParser>());
+    for (NamedParser parser : _parsers) {
+      parsers.put(parser.getName().toLowerCase(), parser);
     }
+    return parsers;
+  }
+  
+  public Map<String,NamedParser> getNamedParsers() {
     return parsers;
   }
   

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java?view=diff&rev=441195&r1=441194&r2=441195
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java Thu Sep  7 12:57:36 2006
@@ -24,9 +24,17 @@
   public static final String CONFIG_PARSER        = "org.apache.abdera.parser.Parser";
   public static final String CONFIG_FACTORY       = "org.apache.abdera.factory.Factory";
   public static final String CONFIG_XPATH         = "org.apache.abdera.xpath.XPath";
+  public static final String CONFIG_PARSERFACTORY = "org.apache.abdera.parser.ParserFactory";
+  public static final String CONFIG_WRITERFACTORY = "org.apache.abdera.writer.WriterFactory";
+  public static final String CONFIG_WRITER        = "org.apache.abdera.writer.Writer";
   public static final String DEFAULT_PARSER       = "org.apache.abdera.parser.stax.FOMParser";
   public static final String DEFAULT_FACTORY      = "org.apache.abdera.parser.stax.FOMFactory";
   public static final String DEFAULT_XPATH        = "org.apache.abdera.parser.stax.FOMXPath";
+  public static final String DEFAULT_PARSERFACTORY= "org.apache.abdera.parser.stax.FOMParserFactory";
+  public static final String DEFAULT_WRITERFACTORY= "org.apache.abdera.parser.stax.FOMWriterFactory";
+  public static final String DEFAULT_WRITER       = "org.apache.abdera.parser.stax.FOMWriter";
+  public static final String NAMED_WRITER         = "META-INF/services/org.apache.abdera.writer.NamedWriter";
+  public static final String NAMED_PARSER         = "META-INF/services/org.apache.abdera.parser.NamedParser";
   public static final String PREFIX               = "";
   public static final String APP_PREFIX           = "";
   public static final String CONTROL_PREFIX       = "";

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java?view=diff&rev=441195&r1=441194&r2=441195
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java Thu Sep  7 12:57:36 2006
@@ -24,6 +24,7 @@
 import java.lang.reflect.Constructor;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
 
@@ -31,6 +32,9 @@
 import org.apache.abdera.factory.ExtensionFactory;
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.parser.Parser;
+import org.apache.abdera.parser.ParserFactory;
+import org.apache.abdera.writer.Writer;
+import org.apache.abdera.writer.WriterFactory;
 import org.apache.abdera.xpath.XPath;
 
 public final class ServiceUtil 
@@ -80,6 +84,27 @@
       abdera);
   }
   
+  public static ParserFactory newParserFactoryInstance(Abdera abdera) {
+    return (ParserFactory) newInstance(
+      CONFIG_PARSERFACTORY,
+      abdera.getConfiguration().getDefaultParserFactory(),
+      abdera);
+  }
+  
+  public static WriterFactory newWriterFactoryInstance(Abdera abdera) {
+    return (WriterFactory) newInstance(
+      CONFIG_WRITERFACTORY,
+      abdera.getConfiguration().getDefaultWriterFactory(),
+      abdera) ;
+  }
+  
+  public static Writer newWriterInstance(Abdera abdera) {
+    return (Writer) newInstance(
+      CONFIG_WRITER,
+      abdera.getConfiguration().getDefaultWriter(),
+      abdera);
+  }
+  
   /**
    * Get the context class loader for this thread
    */
@@ -204,7 +229,7 @@
   
   @SuppressWarnings("unchecked")
   protected static <T>List<T> _loadimpls(String sid) {
-    List<T> impls = new ArrayList<T>();
+    List<T> impls = Collections.synchronizedList(new ArrayList<T>());
     ClassLoader loader = getClassLoader();
     try {
       Enumeration<URL> e = locateResources(loader,sid);