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.util.blueprint;
018    
019    import java.util.Set;
020    import javax.xml.bind.annotation.XmlRootElement;
021    import javax.xml.bind.annotation.XmlTransient;
022    import javax.xml.bind.annotation.XmlType;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.blueprint.BlueprintCamelContextLookupHelper;
026    import org.apache.camel.core.xml.util.jsse.AbstractSSLContextParametersFactoryBean;
027    import org.osgi.service.blueprint.container.BlueprintContainer;
028    
029    @XmlRootElement(name = "sslContextParameters")
030    @XmlType(propOrder = {})
031    public class SSLContextParametersFactoryBean extends AbstractSSLContextParametersFactoryBean {
032        
033        private KeyManagersParametersFactoryBean keyManagers;
034        
035        private TrustManagersParametersFactoryBean trustManagers;
036            
037        private SecureRandomParametersFactoryBean secureRandom;
038        
039        private SSLContextClientParametersFactoryBean clientParameters;
040        
041        private SSLContextServerParametersFactoryBean serverParameters;
042        
043        @XmlTransient
044        private BlueprintContainer blueprintContainer;
045    
046        @Override
047        public KeyManagersParametersFactoryBean getKeyManagers() {
048            return keyManagers;
049        }
050        
051        public void setKeyManagers(KeyManagersParametersFactoryBean keyManagers) {
052            this.keyManagers = keyManagers;
053        }
054    
055        @Override
056        public TrustManagersParametersFactoryBean getTrustManagers() {
057            return trustManagers;
058        }
059        
060        public void setTrustManagers(TrustManagersParametersFactoryBean trustManagers) {
061            this.trustManagers = trustManagers;
062        }
063    
064        @Override
065        public SecureRandomParametersFactoryBean getSecureRandom() {
066            return secureRandom;
067        }
068    
069        public void setSecureRandom(SecureRandomParametersFactoryBean secureRandom) {
070            this.secureRandom = secureRandom;
071        }
072    
073        @Override
074        public SSLContextClientParametersFactoryBean getClientParameters() {
075            return clientParameters;
076        }
077    
078        public void setClientParameters(SSLContextClientParametersFactoryBean clientParameters) {
079            this.clientParameters = clientParameters;
080        }
081    
082        @Override
083        public SSLContextServerParametersFactoryBean getServerParameters() {
084            return serverParameters;
085        }
086        
087        public void setServerParameters(SSLContextServerParametersFactoryBean serverParameters) {
088            this.serverParameters = serverParameters;
089        }
090        
091        public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
092            this.blueprintContainer = blueprintContainer;
093        }
094        
095        @Override
096        protected CamelContext getCamelContextWithId(String camelContextId) {
097            if (blueprintContainer != null) {
098                return (CamelContext) blueprintContainer.getComponentInstance(camelContextId);
099            }
100            return null;
101        }
102    
103        @Override
104        protected CamelContext discoverDefaultCamelContext() {
105            if (blueprintContainer != null) {
106                Set<String> ids = BlueprintCamelContextLookupHelper.lookupBlueprintCamelContext(blueprintContainer);
107                if (ids.size() == 1) {
108                    // there is only 1 id for a BlueprintCamelContext so fallback and use this
109                    return getCamelContextWithId(ids.iterator().next());
110                }
111            }
112            return null;
113        }
114    }