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.blueprint;
018
019import java.util.LinkedHashSet;
020import java.util.Set;
021import javax.xml.bind.JAXBContext;
022import javax.xml.bind.JAXBException;
023
024import org.apache.camel.core.xml.AbstractCamelContextFactoryBean;
025import org.apache.camel.spi.ModelJAXBContextFactory;
026import org.apache.camel.util.blueprint.SSLContextParametersFactoryBean;
027
028public class BlueprintModelJAXBContextFactory implements ModelJAXBContextFactory {
029
030    private final ClassLoader classLoader;
031
032    public BlueprintModelJAXBContextFactory(ClassLoader classLoader) {
033        this.classLoader = classLoader;
034    }
035
036    private String getPackages() {
037        // we nedd to have a class from each different package with jaxb models
038        // and we must use the .class for the classloader to work in OSGi
039        Set<Class<?>> classes = new LinkedHashSet<>();
040        classes.add(CamelContextFactoryBean.class);
041        classes.add(AbstractCamelContextFactoryBean.class);
042        classes.add(SSLContextParametersFactoryBean.class);
043        classes.add(org.apache.camel.ExchangePattern.class);
044        classes.add(org.apache.camel.model.RouteDefinition.class);
045        classes.add(org.apache.camel.model.config.StreamResequencerConfig.class);
046        classes.add(org.apache.camel.model.dataformat.DataFormatsDefinition.class);
047        classes.add(org.apache.camel.model.language.ExpressionDefinition.class);
048        classes.add(org.apache.camel.model.loadbalancer.RoundRobinLoadBalancerDefinition.class);
049        classes.add(org.apache.camel.model.rest.RestDefinition.class);
050        classes.add(org.apache.camel.model.cloud.ServiceCallDefinition.class);
051
052        StringBuilder packages = new StringBuilder();
053        for (Class<?> cl : classes) {
054            if (packages.length() > 0) {
055                packages.append(":");
056            }
057            packages.append(cl.getName().substring(0, cl.getName().lastIndexOf('.')));
058        }
059        return packages.toString();
060    }
061
062    @Override
063    public JAXBContext newJAXBContext() throws JAXBException {
064        return JAXBContext.newInstance(getPackages(), classLoader);
065    }
066}