You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2023/02/06 10:03:52 UTC
[camel] 02/02: Ensure the resource is read only once, the stream closed and add missing calls
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 898d95035604a0df4a76b7f69b379c3403867398
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Fri Feb 3 13:38:40 2023 +0100
Ensure the resource is read only once, the stream closed and add missing calls
---
.../org/apache/camel/support/CachedResource.java | 65 ++++++++++++++++++++++
.../camel/dsl/xml/io/XmlRoutesBuilderLoader.java | 57 +++++++++----------
2 files changed, 91 insertions(+), 31 deletions(-)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/CachedResource.java b/core/camel-support/src/main/java/org/apache/camel/support/CachedResource.java
new file mode 100644
index 00000000000..6104132a99f
--- /dev/null
+++ b/core/camel-support/src/main/java/org/apache/camel/support/CachedResource.java
@@ -0,0 +1,65 @@
+/*
+ * 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.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.camel.spi.Resource;
+
+/**
+ * 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 = delegate.getInputStream()) {
+ data = is.readAllBytes();
+ }
+ }
+ return new ByteArrayInputStream(data);
+ }
+}
diff --git a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
index 397844b78e3..a8ffe061bbf 100644
--- a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
+++ b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.dsl.xml.io;
+import java.util.List;
+
import org.apache.camel.CamelContextAware;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.builder.RouteBuilder;
@@ -28,6 +30,7 @@ import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.annotations.RoutesLoader;
+import org.apache.camel.support.CachedResource;
import org.apache.camel.xml.in.ModelParser;
@ManagedResource(description = "Managed XML RoutesBuilderLoader")
@@ -35,51 +38,43 @@ import org.apache.camel.xml.in.ModelParser;
public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport {
public static final String EXTENSION = "xml";
public static final String NAMESPACE = "http://camel.apache.org/schema/spring";
+ private static final List<String> NAMESPACES = List.of("", NAMESPACE);
public XmlRoutesBuilderLoader() {
super(EXTENSION);
}
@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 : NAMESPACES) {
+ new ModelParser(resource, ns)
+ .parseRouteTemplatesDefinition()
+ .ifPresent(this::setRouteTemplateCollection);
+ new ModelParser(resource, ns)
+ .parseTemplatedRoutesDefinition()
+ .ifPresent(this::setTemplatedRouteCollection);
+ new ModelParser(resource, ns)
+ .parseRestsDefinition()
+ .ifPresent(this::setRestCollection);
+ new ModelParser(resource, ns)
+ .parseRoutesDefinition()
+ .ifPresent(this::addRoutes);
+ }
}
@Override
public void configuration() throws Exception {
- new ModelParser(resource, NAMESPACE)
- .parseRouteConfigurationsDefinition()
- .ifPresent(this::addConfigurations);
- new ModelParser(resource)
- .parseRouteConfigurationsDefinition()
- .ifPresent(this::addConfigurations);
+ for (String ns : NAMESPACES) {
+ new ModelParser(resource, ns)
+ .parseRouteConfigurationsDefinition()
+ .ifPresent(this::addConfigurations);
+ }
}
private void addRoutes(RoutesDefinition routes) {