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     */
017    package org.apache.camel.blueprint;
018    
019    import java.util.Set;
020    import javax.xml.bind.annotation.XmlAccessType;
021    import javax.xml.bind.annotation.XmlAccessorType;
022    import javax.xml.bind.annotation.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.core.xml.AbstractCamelProducerTemplateFactoryBean;
027    import org.osgi.service.blueprint.container.BlueprintContainer;
028    
029    /**
030     * A factory for creating a new {@link org.apache.camel.ProducerTemplate}
031     * instance with a minimum of XML
032     *
033     * @version 
034     */
035    @XmlRootElement(name = "template")
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public class CamelProducerTemplateFactoryBean extends AbstractCamelProducerTemplateFactoryBean {
038    
039        @XmlTransient
040        private BlueprintContainer blueprintContainer;
041    
042        public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
043            this.blueprintContainer = blueprintContainer;
044        }
045    
046        @Override
047        protected CamelContext getCamelContextWithId(String camelContextId) {
048            if (blueprintContainer != null) {
049                return (CamelContext) blueprintContainer.getComponentInstance(camelContextId);
050            }
051            return null;
052        }
053    
054        @Override
055        protected CamelContext discoverDefaultCamelContext() {
056            if (blueprintContainer != null) {
057                Set<String> ids = BlueprintCamelContextLookupHelper.lookupBlueprintCamelContext(blueprintContainer);
058                if (ids.size() == 1) {
059                    // there is only 1 id for a BlueprintCamelContext so fallback and use this
060                    return getCamelContextWithId(ids.iterator().next());
061                }
062            }
063            return null;
064        }
065    
066    }