001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026
027import javax.xml.bind.JAXBContext;
028import javax.xml.bind.JAXBException;
029import javax.xml.bind.Marshaller;
030import javax.xml.bind.Unmarshaller;
031
032import org.apache.camel.model.language.NamespaceAwareExpression;
033import org.apache.camel.util.CamelContextHelper;
034import org.apache.camel.util.ObjectHelper;
035
036/**
037 * Helper for {@link RouteContextRefDefinition}.
038 */
039public final class RouteContextRefDefinitionHelper {
040
041    private static JAXBContext jaxbContext;
042
043    private RouteContextRefDefinitionHelper() {
044    }
045
046    /**
047     * Lookup the routes from the {@link RouteContextRefDefinition}.
048     * <p/>
049     * This implementation must be used to lookup the routes as it performs a deep clone of the routes
050     * as a {@link RouteContextRefDefinition} can be re-used with multiple {@link ModelCamelContext} and each
051     * context should have their own instances of the routes. This is to ensure no side-effects and sharing
052     * of instances between the contexts. For example such as property placeholders may be context specific
053     * so the routes should not use placeholders from another {@link ModelCamelContext}.
054     *
055     * @param camelContext the CamelContext
056     * @param ref          the id of the {@link RouteContextRefDefinition} to lookup and get the routes.
057     * @return the routes.
058     */
059    @SuppressWarnings("unchecked")
060    public static synchronized List<RouteDefinition> lookupRoutes(ModelCamelContext camelContext, String ref) {
061        ObjectHelper.notNull(camelContext, "camelContext");
062        ObjectHelper.notNull(ref, "ref");
063
064        List<RouteDefinition> answer = CamelContextHelper.lookup(camelContext, ref, List.class);
065        if (answer == null) {
066            throw new IllegalArgumentException("Cannot find RouteContext with id " + ref);
067        }
068
069        // must clone the route definitions as they can be reused with multiple CamelContexts
070        // and they would need their own instances of the definitions to not have side effects among
071        // the CamelContext - for example property placeholder resolutions etc.
072        List<RouteDefinition> clones = new ArrayList<>(answer.size());
073        try {
074            JAXBContext jaxb = getOrCreateJAXBContext(camelContext);
075            for (RouteDefinition def : answer) {
076                RouteDefinition clone = cloneRouteDefinition(jaxb, def);
077                if (clone != null) {
078                    clones.add(clone);
079                }
080            }
081        } catch (Exception e) {
082            throw ObjectHelper.wrapRuntimeCamelException(e);
083        }
084
085        return clones;
086    }
087
088    private static synchronized JAXBContext getOrCreateJAXBContext(final ModelCamelContext camelContext) throws JAXBException {
089        if (jaxbContext == null) {
090            jaxbContext = camelContext.getModelJAXBContextFactory().newJAXBContext();
091        }
092        return jaxbContext;
093    }
094
095    private static RouteDefinition cloneRouteDefinition(JAXBContext jaxbContext, RouteDefinition def) throws JAXBException {
096        Marshaller marshal = jaxbContext.createMarshaller();
097        ByteArrayOutputStream bos = new ByteArrayOutputStream();
098        marshal.marshal(def, bos);
099
100        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
101        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
102        Object clone = unmarshaller.unmarshal(bis);
103
104        if (clone instanceof RouteDefinition) {
105            RouteDefinition def2 = (RouteDefinition) clone;
106
107            // need to clone the namespaces also as they are not JAXB marshalled (as they are transient)
108            Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(def.getOutputs(), ExpressionNode.class);
109            Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(def2.getOutputs(), ExpressionNode.class);
110            while (it.hasNext() && it2.hasNext()) {
111                ExpressionNode node = it.next();
112                ExpressionNode node2 = it2.next();
113
114                NamespaceAwareExpression name = null;
115                NamespaceAwareExpression name2 = null;
116                if (node.getExpression() instanceof NamespaceAwareExpression) {
117                    name = (NamespaceAwareExpression) node.getExpression();
118                }
119                if (node2.getExpression() instanceof NamespaceAwareExpression) {
120                    name2 = (NamespaceAwareExpression) node2.getExpression();
121                }
122
123                if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) {
124                    Map<String, String> map = new HashMap<>();
125                    map.putAll(name.getNamespaces());
126                    name2.setNamespaces(map);
127                }
128            }
129
130            return def2;
131        }
132
133        return null;
134    }
135
136}