knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kris...@apache.org
Subject [knox] branch master updated: KNOX-1710 - Reuse JAXBContext since they are thread safe
Date Tue, 08 Jan 2019 21:47:10 GMT
This is an automated email from the ASF dual-hosted git repository.

krisden pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new b85fec2  KNOX-1710 - Reuse JAXBContext since they are thread safe
b85fec2 is described below

commit b85fec2804f986882193f646d4f61b7fd09a1bff
Author: Kevin Risden <krisden@apache.org>
AuthorDate: Fri Dec 21 18:35:28 2018 -0500

    KNOX-1710 - Reuse JAXBContext since they are thread safe
    
    Signed-off-by: Kevin Risden <krisden@apache.org>
---
 .../impl/xml/XmlUrlRewriteRulesImporter.java       | 17 +++++++-------
 .../knox/gateway/deploy/DeploymentFactory.java     | 18 +++++++++++----
 .../impl/ApplicationDeploymentContributor.java     | 12 ++++++++--
 .../topology/impl/DefaultTopologyService.java      | 27 +++++++++++++---------
 .../simple/ProviderConfigurationParser.java        | 13 +++++++++--
 .../gateway/util/ServiceDefinitionsLoader.java     | 16 +++++++++----
 6 files changed, 71 insertions(+), 32 deletions(-)

diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java
index 0d2b68b..411eeaf 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java
@@ -30,8 +30,8 @@ import java.io.Reader;
 import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
 
 public class XmlUrlRewriteRulesImporter implements UrlRewriteRulesImporter {
-
-  private static DigesterLoader loader = newLoader( new XmlRewriteRulesDigester() );
+  private static final DigesterLoader loader = newLoader( new XmlRewriteRulesDigester() );
+  private static final Digester digester = loader.newDigester( new ExtendedBaseRules() );
 
   @Override
   public String getFormat() {
@@ -40,12 +40,13 @@ public class XmlUrlRewriteRulesImporter implements UrlRewriteRulesImporter
{
 
   @Override
   public UrlRewriteRulesDescriptor load( Reader reader ) throws IOException {
-    Digester digester = loader.newDigester( new ExtendedBaseRules() );
-    digester.setValidating( false );
-    try {
-      return digester.parse( reader );
-    } catch( SAXException e ) {
-      throw new IOException( e );
+    synchronized (digester) {
+      digester.setValidating(false);
+      try {
+        return digester.parse(reader);
+      } catch (SAXException e) {
+        throw new IOException(e);
+      }
     }
   }
 }
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
b/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
index 1c2a6c2..7caf054 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
@@ -65,6 +65,19 @@ import java.util.Set;
 import java.util.TreeMap;
 
 public abstract class DeploymentFactory {
+  private static final JAXBContext jaxbContext = getJAXBContext();
+
+  private static JAXBContext getJAXBContext() {
+    Map<String,String> properties = new HashMap<>(2);
+    properties.put( "eclipselink-oxm-xml", "org/apache/knox/gateway/topology/topology_binding-xml.xml");
+    properties.put( "eclipselink.media-type", "application/xml" );
+
+    try {
+      return JAXBContext.newInstance(Topology.class.getPackage().getName(), Topology.class.getClassLoader(),
properties);
+    } catch (JAXBException e) {
+      throw new IllegalStateException(e);
+    }
+  }
 
   private static final String SERVLET_NAME_SUFFIX = "-knox-gateway-servlet";
   private static final String FILTER_NAME_SUFFIX = "-knox-gateway-filter";
@@ -203,11 +216,6 @@ public abstract class DeploymentFactory {
     StringWriter writer = new StringWriter();
     String xml;
     try {
-      Map<String,Object> properties = new HashMap<>(2);
-      properties.put( "eclipselink-oxm-xml",
-          "org/apache/knox/gateway/topology/topology_binding-xml.xml");
-      properties.put( "eclipselink.media-type", "application/xml" );
-      JAXBContext jaxbContext = JAXBContext.newInstance( Topology.class.getPackage().getName(),
Topology.class.getClassLoader() , properties );
       Marshaller marshaller = jaxbContext.createMarshaller();
       marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
       marshaller.marshal( topology, writer );
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/deploy/impl/ApplicationDeploymentContributor.java
b/gateway-server/src/main/java/org/apache/knox/gateway/deploy/impl/ApplicationDeploymentContributor.java
index 4496fb5..b4d065d 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/deploy/impl/ApplicationDeploymentContributor.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/deploy/impl/ApplicationDeploymentContributor.java
@@ -56,6 +56,15 @@ import java.util.Locale;
 import java.util.Map;
 
 public class ApplicationDeploymentContributor extends ServiceDeploymentContributorBase {
+  private static final JAXBContext jaxbContext = getJAXBContext();
+
+  private static JAXBContext getJAXBContext() {
+    try {
+      return JAXBContext.newInstance( ServiceDefinition.class );
+    } catch (JAXBException e) {
+      throw new IllegalStateException(e);
+    }
+  }
 
   private static final String SERVICE_DEFINITION_FILE_NAME = "service.xml";
   private static final String REWRITE_RULES_FILE_NAME = "rewrite.xml";
@@ -83,8 +92,7 @@ public class ApplicationDeploymentContributor extends ServiceDeploymentContribut
       routes.add( route );
       definition.setRoutes( routes );
     } else {
-      JAXBContext context = JAXBContext.newInstance( ServiceDefinition.class );
-      Unmarshaller unmarshaller = context.createUnmarshaller();
+      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
       try(InputStream inputStream = Files.newInputStream(file.toPath()) ) {
           definition = (ServiceDefinition) unmarshaller.unmarshal( inputStream );
       }
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/DefaultTopologyService.java
b/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/DefaultTopologyService.java
index deef8aa..d92ed38 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/DefaultTopologyService.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/DefaultTopologyService.java
@@ -15,10 +15,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.knox.gateway.services.topology.impl;
 
-
 import org.apache.commons.digester3.Digester;
 import org.apache.commons.digester3.binder.DigesterLoader;
 import org.apache.commons.io.FileUtils;
@@ -86,11 +84,25 @@ import java.util.Set;
 
 import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
 
-
 public class DefaultTopologyService
     extends FileAlterationListenerAdaptor
     implements TopologyService, TopologyMonitor, TopologyProvider, FileFilter, FileAlterationListener
{
 
+  private static final JAXBContext jaxbContext = getJAXBContext();
+
+  private static JAXBContext getJAXBContext() {
+    String pkgName = Topology.class.getPackage().getName();
+    String bindingFile = pkgName.replace(".", "/") + "/topology_binding-xml.xml";
+
+    Map<String, Object> properties = new HashMap<>(1);
+    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindingFile);
+    try {
+      return JAXBContext.newInstance(pkgName, Topology.class.getClassLoader(), properties);
+    } catch (JAXBException e) {
+      throw new IllegalStateException(e);
+    }
+  }
+
   private static Auditor auditor = AuditServiceFactory.getAuditService().getAuditor(
     AuditConstants.DEFAULT_AUDITOR_NAME, AuditConstants.KNOX_SERVICE_NAME,
     AuditConstants.KNOX_COMPONENT_NAME);
@@ -295,14 +307,7 @@ public class DefaultTopologyService
 
     try {
       File temp = new File(topologiesDirectory.getAbsolutePath() + "/" + t.getName() + ".xml.temp");
-      Package topologyPkg = Topology.class.getPackage();
-      String pkgName = topologyPkg.getName();
-      String bindingFile = pkgName.replace(".", "/") + "/topology_binding-xml.xml";
-
-      Map<String, Object> properties = new HashMap<>(1);
-      properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindingFile);
-      JAXBContext jc = JAXBContext.newInstance(pkgName, Topology.class.getClassLoader(),
properties);
-      Marshaller mr = jc.createMarshaller();
+      Marshaller mr = jaxbContext.createMarshaller();
 
       mr.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
       mr.marshal(t, temp);
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/ProviderConfigurationParser.java
b/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/ProviderConfigurationParser.java
index 45d10c2..26ce59b 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/ProviderConfigurationParser.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/ProviderConfigurationParser.java
@@ -22,6 +22,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import org.apache.commons.io.FilenameUtils;
 
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -40,6 +41,16 @@ import java.util.Map;
 
 public class ProviderConfigurationParser {
 
+  private static final JAXBContext jaxbContext = getJAXBContext();
+
+  private static JAXBContext getJAXBContext() {
+    try {
+      return JAXBContext.newInstance(XMLProviderConfiguration.class);
+    } catch (JAXBException e) {
+      throw new IllegalStateException(e);
+    }
+  }
+
   private static final String EXT_XML  = "xml";
   private static final String EXT_JSON = "json";
   private static final String EXT_YML  = "yml";
@@ -92,8 +103,6 @@ public class ProviderConfigurationParser {
 
   static ProviderConfiguration parseXML(InputStream in) throws Exception {
     XMLProviderConfiguration providerConfig;
-
-    JAXBContext jaxbContext = JAXBContext.newInstance(XMLProviderConfiguration.class);
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
     providerConfig = (XMLProviderConfiguration) jaxbUnmarshaller.unmarshal(in);
 
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
b/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
index fad219b..a4a2ca4 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
@@ -44,6 +44,16 @@ import java.util.HashSet;
 import java.util.Set;
 
 public class ServiceDefinitionsLoader {
+  private static final JAXBContext jaxbContext = getJAXBContext();
+
+  private static JAXBContext getJAXBContext() {
+    try {
+      return JAXBContext.newInstance(ServiceDefinition.class);
+    } catch (JAXBException e) {
+      throw new IllegalStateException(e);
+    }
+  }
+
   private static final GatewayMessages log = MessagesFactory.get(GatewayMessages.class);
 
   private static final String SERVICE_FILE_NAME = "service";
@@ -54,8 +64,7 @@ public class ServiceDefinitionsLoader {
     Set<ServiceDeploymentContributor> contributors = new HashSet<>();
     if ( servicesDir.exists() && servicesDir.isDirectory() ) {
       try {
-        JAXBContext context = JAXBContext.newInstance(ServiceDefinition.class);
-        Unmarshaller unmarshaller = context.createUnmarshaller();
+        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
 
         for ( File file : getFileList(servicesDir) ) {
           try (InputStream inputStream = Files.newInputStream(file.toPath())) {
@@ -80,8 +89,7 @@ public class ServiceDefinitionsLoader {
   public static Set<ServiceDefinition> getServiceDefinitions(File servicesDir) {
     Set<ServiceDefinition> definitions = new HashSet<>();
     try {
-      JAXBContext context = JAXBContext.newInstance(ServiceDefinition.class);
-      Unmarshaller unmarshaller = context.createUnmarshaller();
+      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
 
       for (File f : getFileList(servicesDir)){
         ServiceDefinition definition = (ServiceDefinition) unmarshaller.unmarshal(f);


Mime
View raw message