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.servicemix.client;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.ComponentContext;
021    import javax.naming.InitialContext;
022    
023    import org.apache.servicemix.jbi.container.JBIContainer;
024    import org.springframework.beans.factory.FactoryBean;
025    import org.springframework.beans.factory.InitializingBean;
026    
027    /**
028     * A factory bean which creates a ServiceMixClient.
029     * It first try to use the configured ClientFactory, the ComponentContext
030     * then JBIContainer and at last, it tries to locate the ClientFactory
031     * in JNDI under the provided name.
032     * 
033     * @author <a href="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
034     * @org.apache.xbean.XBean element="client"
035     */
036    public class ClientFactoryBean implements FactoryBean, InitializingBean {
037    
038        private String name = ClientFactory.DEFAULT_JNDI_NAME;
039        private JBIContainer container;
040        private ClientFactory factory;
041        private ComponentContext context;
042        
043        public ClientFactoryBean() {
044        }
045    
046        /**
047         * @return the context
048         */
049        public ComponentContext getContext() {
050            return context;
051        }
052    
053        /**
054         * @param context the context to set
055         */
056        public void setContext(ComponentContext context) {
057            this.context = context;
058        }
059    
060        /**
061         * @return the container
062         */
063        public JBIContainer getContainer() {
064            return container;
065        }
066    
067        /**
068         * @param container the container to set
069         */
070        public void setContainer(JBIContainer container) {
071            this.container = container;
072        }
073    
074        /**
075         * @return the factory
076         */
077        public ClientFactory getFactory() {
078            return factory;
079        }
080    
081        /**
082         * @param factory the factory to set
083         */
084        public void setFactory(ClientFactory factory) {
085            this.factory = factory;
086        }
087    
088        /**
089         * @return the name
090         */
091        public String getName() {
092            return name;
093        }
094    
095        /**
096         * @param name the name to set
097         */
098        public void setName(String name) {
099            this.name = name;
100        }
101    
102        public Object getObject() throws Exception {
103            return factory.createClient();
104        }
105    
106        public Class getObjectType() {
107            return ServiceMixClient.class;
108        }
109    
110        public boolean isSingleton() {
111            return false;
112        }
113        
114        public void afterPropertiesSet() throws Exception {
115            if (factory == null) {
116                if (context != null) {
117                    factory = new ClientFactory() {
118                        public ServiceMixClient createClient() throws JBIException {
119                            return new ServiceMixClientFacade(context);
120                        }
121                    };
122                } else if (container != null) {
123                    factory = container.getClientFactory();
124                } else {
125                    factory = (ClientFactory) new InitialContext().lookup(name);
126                }
127            }
128        }
129        
130    }