You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/02/02 15:24:08 UTC

[GitHub] [camel] davsclaus commented on a change in pull request #4998: CAMEL-15560: generic route loader

davsclaus commented on a change in pull request #4998:
URL: https://github.com/apache/camel/pull/4998#discussion_r568681988



##########
File path: core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesBuilderLoader.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.impl.engine;
+
+import java.util.Optional;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
+
+public class DefaultRoutesBuilderLoader implements RoutesBuilderLoader, CamelContextAware {
+    private CamelContext camelContext;
+
+    public DefaultRoutesBuilderLoader() {
+    }
+
+    public DefaultRoutesBuilderLoader(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public RoutesBuilder loadRoutesBuilder(Resource resource) throws Exception {
+        final String location = resource.getLocation();
+        final String language = getLanguage(location);
+
+        if (ObjectHelper.isEmpty(language)) {
+            throw new IllegalArgumentException("Unable to determine language for resource: " + location);
+        }
+
+        return getLoader(language).loadRoutesBuilder(resource);
+    }
+
+    private String getLanguage(String location) {
+        String answer = StringHelper.afterLast(location, ":", location);
+        answer = StringHelper.afterLast(answer, ".", answer);
+
+        return answer;
+    }
+
+    private RoutesBuilderLoader getLoader(String language) {
+        RoutesBuilderLoader answer = getCamelContext().getRegistry().lookupByNameAndType(language, RoutesBuilderLoader.class);
+
+        if (answer == null) {
+            final ExtendedCamelContext ecc = getCamelContext(ExtendedCamelContext.class);
+            final FactoryFinder finder = ecc.getFactoryFinder(RoutesBuilderLoader.LOADERS_FACTORY_PATH);

Review comment:
       Use bootstrap factory finder as we would load routes during bootstrapping/starting up camel

##########
File path: core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesBuilderLoader.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.impl.engine;
+
+import java.util.Optional;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
+
+public class DefaultRoutesBuilderLoader implements RoutesBuilderLoader, CamelContextAware {
+    private CamelContext camelContext;
+
+    public DefaultRoutesBuilderLoader() {
+    }
+
+    public DefaultRoutesBuilderLoader(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public RoutesBuilder loadRoutesBuilder(Resource resource) throws Exception {
+        final String location = resource.getLocation();
+        final String language = getLanguage(location);
+
+        if (ObjectHelper.isEmpty(language)) {
+            throw new IllegalArgumentException("Unable to determine language for resource: " + location);
+        }
+
+        return getLoader(language).loadRoutesBuilder(resource);
+    }
+
+    private String getLanguage(String location) {
+        String answer = StringHelper.afterLast(location, ":", location);
+        answer = StringHelper.afterLast(answer, ".", answer);

Review comment:
       I assume the language is the file extension of the location?

##########
File path: core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.xml.jaxb;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.RouteTemplatesDefinition;
+import org.apache.camel.model.RoutesDefinition;
+import org.apache.camel.model.rest.RestsDefinition;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+
+import static org.apache.camel.xml.jaxb.JaxbHelper.loadRestsDefinition;
+import static org.apache.camel.xml.jaxb.JaxbHelper.loadRouteTemplatesDefinition;
+import static org.apache.camel.xml.jaxb.JaxbHelper.loadRoutesDefinition;
+
+public class JaxbXmlRoutesBuilderLoader implements RoutesBuilderLoader, CamelContextAware {

Review comment:
       We should use the `@JdkService` annotation as then the maven tooling generated all the needed files for META-INF and the license files and so on, in the src/generated folder.
   
   See for the how we use
   @JdkService(ModelToXMLDumper.FACTORY)

##########
File path: core/camel-xml-io/src/main/java/org/apache/camel/xml/in/XmlRoutesBuilderLoader.java
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.xml.in;
+
+import java.io.InputStream;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.RouteTemplatesDefinition;
+import org.apache.camel.model.RoutesDefinition;
+import org.apache.camel.model.rest.RestsDefinition;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+
+public class XmlRoutesBuilderLoader implements RoutesBuilderLoader, CamelContextAware {
+    public static final String NAMESPACE = "http://camel.apache.org/schema/spring";
+
+    private CamelContext camelContext;
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public RoutesBuilder loadRoutesBuilder(Resource resource) throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                try (InputStream is = resource.getInputStream()) {

Review comment:
       Whoa that is some clever hack




----------------------------------------------------------------
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.

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