You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "essobedo (via GitHub)" <gi...@apache.org> on 2023/02/03 18:23:46 UTC

[GitHub] [camel] essobedo commented on a diff in pull request #9272: Remove deprecated XmlRoutesDefinitionLoader

essobedo commented on code in PR #9272:
URL: https://github.com/apache/camel/pull/9272#discussion_r1096099726


##########
dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java:
##########
@@ -41,45 +44,36 @@ public XmlRoutesBuilderLoader() {
     }
 
     @Override
-    public RouteBuilder doLoadRouteBuilder(Resource resource) throws Exception {
+    public RouteBuilder doLoadRouteBuilder(Resource input) throws Exception {
         return new RouteConfigurationBuilder() {
+            final Resource resource = new CachedResource(input);
+
             @Override
             public void configure() throws Exception {
                 // we use configure to load the routes (with namespace and without namespace)
-                new ModelParser(resource, NAMESPACE)
-                        .parseRouteTemplatesDefinition()
-                        .ifPresent(this::setRouteTemplateCollection);
-                new ModelParser(resource, NAMESPACE)
-                        .parseTemplatedRoutesDefinition()
-                        .ifPresent(this::setTemplatedRouteCollection);
-                new ModelParser(resource, NAMESPACE)
-                        .parseRestsDefinition()
-                        .ifPresent(this::setRestCollection);
-                new ModelParser(resource, NAMESPACE)
-                        .parseRoutesDefinition()
-                        .ifPresent(this::addRoutes);
-                new ModelParser(resource)
-                        .parseRouteTemplatesDefinition()
-                        .ifPresent(this::setRouteTemplateCollection);
-                new ModelParser(resource)
-                        .parseTemplatedRoutesDefinition()
-                        .ifPresent(this::setTemplatedRouteCollection);
-                new ModelParser(resource)
-                        .parseRestsDefinition()
-                        .ifPresent(this::setRestCollection);
-                new ModelParser(resource)
-                        .parseRoutesDefinition()
-                        .ifPresent(this::addRoutes);
+                for (String ns : List.of("", NAMESPACE)) {

Review Comment:
   The list could be a static final field to avoid creating it at each call



##########
core/camel-support/src/main/java/org/apache/camel/support/CachedResource.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.camel.support;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.camel.spi.Resource;
+import org.apache.camel.util.IOHelper;
+
+/**
+ * A resource which will cache the content of the input stream on first read.
+ */
+public class CachedResource extends ResourceSupport {
+
+    private final Resource delegate;
+    private byte[] data;
+
+    public CachedResource(Resource delegate) {
+        super("cached:" + delegate.getScheme(), delegate.getLocation());
+        this.delegate = delegate;
+    }
+
+    @Override
+    public boolean exists() {
+        return delegate.exists();
+    }
+
+    @Override
+    public URI getURI() {
+        return delegate.getURI();
+    }
+
+    @Override
+    public URL getURL() throws MalformedURLException {
+        return delegate.getURL();
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        if (data == null) {
+            try (InputStream is = IOHelper.buffered(delegate.getInputStream())) {
+                ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                IOHelper.copyAndCloseInput(is, bos);
+                data = bos.toByteArray();
+            }

Review Comment:
   Any reason not to simply call `readAllBytes()` on the `InputStream`?



##########
dsl/camel-xml-jaxb-dsl-test/definition/src/test/java/org/apache/camel/dsl/xml/jaxb/definition/LoadRestFromXmlTest.java:
##########
@@ -54,10 +52,9 @@ public void testLoadRestsDefinitionFromXml() throws Exception {
         foo.assertIsSatisfied();
 
         // load rest from XML and add them to the existing camel context
-        InputStream is = getClass().getResourceAsStream("barRest.xml");
         ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
-        RestsDefinition rests = (RestsDefinition) ecc.getXMLRoutesDefinitionLoader().loadRestsDefinition(ecc, is);
-        context.addRestDefinitions(rests.getRests(), true);
+        Resource resource = ecc.getResourceLoader().resolveResource("org/apache/camel/dsl/xml/jaxb/definition/barRest.xml");
+        ecc.getRoutesLoader().loadRoutes(resource);

Review Comment:
   To avoid duplicating the same code, we could have a helper class that proposes a method allowing us to load the routes from a file of a given name located in a given package. This helper class could be added to the test classes of camel-core (the test classes of camel-core are packaged as a jar) to be reusable in different modules.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org