You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2015/02/01 16:01:22 UTC

syncope git commit: Fastert DOM (de)serialization + ResourceWithFallbackLoader for user and role XML routes

Repository: syncope
Updated Branches:
  refs/heads/2_0_X dae87ef89 -> 2b7dd5d2c


Fastert DOM (de)serialization + ResourceWithFallbackLoader for user and role XML routes


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

Branch: refs/heads/2_0_X
Commit: 2b7dd5d2cd587454a98c9639bf412ab6fe5a3deb
Parents: dae87ef
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Sun Feb 1 16:00:51 2015 +0100
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Sun Feb 1 16:00:51 2015 +0100

----------------------------------------------------------------------
 .../server/logic/init/CamelRouteLoader.java     | 80 ++++++++++++--------
 .../provisioning/camel/SyncopeCamelContext.java | 48 ++++--------
 .../src/main/resources/provisioning.properties  |  1 +
 .../main/resources/provisioningCamelContext.xml | 34 +++++++++
 4 files changed, 98 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/2b7dd5d2/syncope620/ext/camel/logic/src/main/java/org/apache/syncope/server/logic/init/CamelRouteLoader.java
----------------------------------------------------------------------
diff --git a/syncope620/ext/camel/logic/src/main/java/org/apache/syncope/server/logic/init/CamelRouteLoader.java b/syncope620/ext/camel/logic/src/main/java/org/apache/syncope/server/logic/init/CamelRouteLoader.java
index 865b4b1..34181ae 100644
--- a/syncope620/ext/camel/logic/src/main/java/org/apache/syncope/server/logic/init/CamelRouteLoader.java
+++ b/syncope620/ext/camel/logic/src/main/java/org/apache/syncope/server/logic/init/CamelRouteLoader.java
@@ -22,29 +22,30 @@ import java.io.StringWriter;
 import java.util.List;
 import java.util.Map;
 import javax.sql.DataSource;
-import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
 import org.apache.syncope.common.lib.types.SubjectType;
+import org.apache.syncope.server.misc.spring.ResourceWithFallbackLoader;
 import org.apache.syncope.server.persistence.api.SyncopeLoader;
 import org.apache.syncope.server.persistence.api.entity.CamelEntityFactory;
 import org.apache.syncope.server.persistence.api.entity.CamelRoute;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.Resource;
 import org.springframework.dao.DataAccessException;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
-import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.w3c.dom.bootstrap.DOMImplementationRegistry;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSOutput;
+import org.w3c.dom.ls.LSParser;
+import org.w3c.dom.ls.LSSerializer;
 
 @Component
 public class CamelRouteLoader implements SyncopeLoader {
@@ -55,6 +56,12 @@ public class CamelRouteLoader implements SyncopeLoader {
 
     private static final TransformerFactory T_FACTORY = TransformerFactory.newInstance();
 
+    @javax.annotation.Resource(name = "userRoutes")
+    private ResourceWithFallbackLoader userRoutesLoader;
+
+    @javax.annotation.Resource(name = "roleRoutes")
+    private ResourceWithFallbackLoader roleRoutesLoader;
+
     @Autowired
     private DataSource dataSource;
 
@@ -74,8 +81,8 @@ public class CamelRouteLoader implements SyncopeLoader {
     public void load() {
         synchronized (this) {
             if (!loaded) {
-                loadRoutes("/userRoute.xml", SubjectType.USER);
-                loadRoutes("/roleRoute.xml", SubjectType.ROLE);
+                loadRoutes(userRoutesLoader.getResource(), SubjectType.USER);
+                loadRoutes(roleRoutesLoader.getResource(), SubjectType.ROLE);
                 loadEntitlements();
                 loaded = true;
             }
@@ -89,45 +96,52 @@ public class CamelRouteLoader implements SyncopeLoader {
         return !rows.isEmpty();
     }
 
-    private String nodeToString(final Node node) {
-        StringWriter sw = new StringWriter();
+    private String nodeToString(final Node content, final DOMImplementationLS impl) {
+        StringWriter writer = new StringWriter();
         try {
-            Transformer transformer = T_FACTORY.newTransformer();
-            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-            transformer.transform(new DOMSource(node), new StreamResult(sw));
-        } catch (TransformerException te) {
-            LOG.debug("nodeToString Transformer Exception", te);
+            LSSerializer serializer = impl.createLSSerializer();
+            LSOutput lso = impl.createLSOutput();
+            lso.setCharacterStream(writer);
+            serializer.write(content, lso);
+        } catch (Exception e) {
+            LOG.debug("While serializing route node", e);
         }
-        return sw.toString();
+        return writer.toString();
     }
 
-    private void loadRoutes(final String path, final SubjectType subjectType) {
+    private void loadRoutes(final Resource resource, final SubjectType subjectType) {
         if (routesAvailable(subjectType)) {
-            final String query = String.format("INSERT INTO %s(ID, NAME, SUBJECT, ROUTECONTENT) VALUES (?, ?, ?, ?)",
+            String query = String.format("INSERT INTO %s(NAME, SUBJECT, ROUTECONTENT) VALUES (?, ?, ?, ?)",
                     CamelRoute.class.getSimpleName());
+            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
+
             try {
-                final DocumentBuilder dBuilder = DOC_FACTORY.newDocumentBuilder();
-                final Document doc = dBuilder.parse(getClass().getResourceAsStream(path));
-                doc.getDocumentElement().normalize();
-                final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
-                final NodeList listOfRoutes = doc.getElementsByTagName("route");
-                for (int s = 0; s < listOfRoutes.getLength(); s++) {
-                    //getting the route node element
-                    Node routeEl = listOfRoutes.item(s);
+                DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
+                DOMImplementationLS domImpl = (DOMImplementationLS) reg.getDOMImplementation("LS");
+                LSInput lsinput = domImpl.createLSInput();
+                lsinput.setByteStream(resource.getInputStream());
+
+                LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
+
+                NodeList routeNodes = parser.parse(lsinput).getElementsByTagName("route");
+                for (int s = 0; s < routeNodes.getLength(); s++) {
+                    Node routeElement = routeNodes.item(s);
+                    String routeContent = nodeToString(routeNodes.item(s), domImpl);
+
                     //crate an instance of CamelRoute Entity
                     CamelRoute route = entityFactory.newCamelRoute();
                     route.setSubjectType(subjectType);
-                    route.setKey(((Element) routeEl).getAttribute("id"));
-                    route.setContent(nodeToString(listOfRoutes.item(s)));
+                    route.setKey(((Element) routeElement).getAttribute("id"));
+                    route.setContent(routeContent);
 
-                    jdbcTemplate.update(query, new Object[] { size++, ((Element) routeEl).getAttribute("id"),
-                        subjectType.name(), nodeToString(listOfRoutes.item(s)) });
-                    LOG.debug("Route {} successfully registered", ((Element) routeEl).getAttribute("id"));
+                    jdbcTemplate.update(query, new Object[] {
+                        ((Element) routeElement).getAttribute("id"), subjectType.name(), routeContent });
+                    LOG.debug("Route {} successfully loaded", ((Element) routeElement).getAttribute("id"));
                 }
             } catch (DataAccessException e) {
                 LOG.error("While trying to store queries {}", e);
             } catch (Exception e) {
-                LOG.error("Route Registration failed {}", e.getMessage());
+                LOG.error("Route load failed {}", e.getMessage());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/2b7dd5d2/syncope620/ext/camel/provisioning-camel/src/main/java/org/apache/syncope/server/provisioning/camel/SyncopeCamelContext.java
----------------------------------------------------------------------
diff --git a/syncope620/ext/camel/provisioning-camel/src/main/java/org/apache/syncope/server/provisioning/camel/SyncopeCamelContext.java b/syncope620/ext/camel/provisioning-camel/src/main/java/org/apache/syncope/server/provisioning/camel/SyncopeCamelContext.java
index 125b17f..2a7de7b 100644
--- a/syncope620/ext/camel/provisioning-camel/src/main/java/org/apache/syncope/server/provisioning/camel/SyncopeCamelContext.java
+++ b/syncope620/ext/camel/provisioning-camel/src/main/java/org/apache/syncope/server/provisioning/camel/SyncopeCamelContext.java
@@ -25,10 +25,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
 import javax.xml.bind.Unmarshaller;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
 import org.apache.camel.model.Constants;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.spring.SpringCamelContext;
@@ -41,8 +38,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-import org.w3c.dom.Document;
 import org.w3c.dom.Node;
+import org.w3c.dom.bootstrap.DOMImplementationRegistry;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSParser;
 
 @Component
 public class SyncopeCamelContext {
@@ -77,22 +77,25 @@ public class SyncopeCamelContext {
 
     public void loadContext(final CamelRouteDAO routeDAO, final List<CamelRoute> routes) {
         try {
-            DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+            DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
+            DOMImplementationLS domImpl = (DOMImplementationLS) reg.getDOMImplementation("LS");
+            LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
+
             JAXBContext jaxbContext = JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES);
             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
             List<RouteDefinition> routeDefs = new ArrayList<>();
             for (CamelRoute route : routes) {
-                InputStream is = null;
+                InputStream input = null;
                 try {
-                    is = new ByteArrayInputStream(
+                    input = new ByteArrayInputStream(
                             URLDecoder.decode(route.getContent(), SyncopeConstants.DEFAULT_ENCODING).getBytes());
-                    Document doc = dBuilder.parse(is);
-                    doc.getDocumentElement().normalize();
-                    Node routeEl = doc.getElementsByTagName("route").item(0);
-                    JAXBElement<RouteDefinition> obj = unmarshaller.unmarshal(routeEl, RouteDefinition.class);
-                    routeDefs.add(obj.getValue());
+                    LSInput lsinput = domImpl.createLSInput();
+                    lsinput.setByteStream(input);
+
+                    Node routeElement = parser.parse(lsinput).getElementsByTagName("route").item(0);
+                    routeDefs.add(unmarshaller.unmarshal(routeElement, RouteDefinition.class).getValue());
                 } finally {
-                    IOUtils.closeQuietly(is);
+                    IOUtils.closeQuietly(input);
                 }
             }
             camelContext.addRouteDefinitions(routeDefs);
@@ -101,22 +104,6 @@ public class SyncopeCamelContext {
         }
     }
 
-    public void reloadContext() {
-        if (camelContext == null) {
-            getContext();
-        } else {
-            if (!camelContext.getRouteDefinitions().isEmpty()) {
-                try {
-                    camelContext.removeRouteDefinitions(new ArrayList<>(camelContext.getRouteDefinitions()));
-                } catch (Exception e) {
-                    LOG.error("While clearing Camel context {}", e);
-                }
-            }
-
-            loadContext(routeDAO, new ArrayList<>(routeDAO.findAll()));
-        }
-    }
-
     public void reloadContext(final String routeKey) {
         if (camelContext == null) {
             getContext();
@@ -128,7 +115,4 @@ public class SyncopeCamelContext {
         }
     }
 
-    public List<RouteDefinition> getDefinitions() {
-        return camelContext.getRouteDefinitions();
-    }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/2b7dd5d2/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioning.properties
----------------------------------------------------------------------
diff --git a/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioning.properties b/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioning.properties
index b51dbee..b0c8917 100644
--- a/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioning.properties
+++ b/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioning.properties
@@ -14,5 +14,6 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+camel.directory=${conf.directory}
 userProvisioningManager=org.apache.syncope.server.provisioning.camel.CamelUserProvisioningManager
 roleProvisioningManager=org.apache.syncope.server.provisioning.camel.CamelRoleProvisioningManager

http://git-wip-us.apache.org/repos/asf/syncope/blob/2b7dd5d2/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioningCamelContext.xml
----------------------------------------------------------------------
diff --git a/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioningCamelContext.xml b/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioningCamelContext.xml
new file mode 100644
index 0000000..e46b58a
--- /dev/null
+++ b/syncope620/ext/camel/provisioning-camel/src/main/resources/provisioningCamelContext.xml
@@ -0,0 +1,34 @@
+<?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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans   
+                           http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+  <bean id="userRoutes" class="org.apache.syncope.server.misc.spring.ResourceWithFallbackLoader">
+    <property name="primary" value="file:${camel.directory}/userWorkflow.bpmn20.xml"/>
+    <property name="fallback" value="classpath:userRoutes.xml"/>
+  </bean>
+  <bean id="roleRoutes" class="org.apache.syncope.server.misc.spring.ResourceWithFallbackLoader">
+    <property name="primary" value="file:${camel.directory}/userWorkflow.bpmn20.xml"/>
+    <property name="fallback" value="classpath:roleRoutes.xml"/>
+  </bean>
+    
+</beans>