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.jbi.framework;
018    
019    import java.io.Serializable;
020    
021    import javax.jbi.JBIException;
022    import javax.management.JMException;
023    import javax.management.MBeanOperationInfo;
024    import javax.naming.NamingException;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.servicemix.client.DefaultServiceMixClient;
029    import org.apache.servicemix.client.ServiceMixClient;
030    import org.apache.servicemix.jbi.management.BaseSystemService;
031    import org.apache.servicemix.jbi.management.OperationInfoHelper;
032    
033    /**
034     * @author <a href="mailto:gnodet [at] apache.org">Guillaume Nodet</a>
035     */
036    public class ClientFactory extends BaseSystemService implements ClientFactoryMBean, Serializable {
037    
038        private static final Log LOG = LogFactory.getLog(ClientFactory.class);
039        
040        private String jndiName = DEFAULT_JNDI_NAME;
041    
042        private boolean isFactoryJNDIregistered;
043    
044        public ClientFactory() {
045        }
046        
047        /**
048         * @return the jndiName
049         */
050        public String getJndiName() {
051            return jndiName;
052        }
053    
054        /**
055         * @param jndiName the jndiName to set
056         */
057        public void setJndiName(String jndiName) {
058            this.jndiName = jndiName;
059        }
060    
061        public ServiceMixClient createClient() throws JBIException {
062            return new DefaultServiceMixClient(getContainer());
063        }
064    
065        protected Class getServiceMBean() {
066            return ClientFactoryMBean.class;
067        }
068    
069        public String getDescription() {
070            return "Client Factory Service";
071        }
072    
073        public MBeanOperationInfo[] getOperationInfos() throws JMException {
074            OperationInfoHelper helper = new OperationInfoHelper();
075            helper.addOperation(getObjectToManage(), "createClient", 0, "create a new client");
076            return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos());
077        }
078    
079        /**
080         * Start the item.
081         * 
082         * @exception javax.jbi.JBIException if the item fails to start.
083         */
084        public void start() throws javax.jbi.JBIException {
085            try {
086                getContainer().getNamingContext().bind(jndiName, this);
087                isFactoryJNDIregistered = true;
088            } catch (NamingException e) {
089                LOG.warn("Cound not start ClientFactory: " + e);
090                if (LOG.isDebugEnabled()) {
091                    LOG.debug("Could not start ClientFactory", e);
092                }
093                isFactoryJNDIregistered = false;
094            }
095            super.start();
096        }
097    
098        /**
099         * Stop the item. This suspends current messaging activities.
100         * 
101         * @exception javax.jbi.JBIException if the item fails to stop.
102         */
103        public void stop() throws javax.jbi.JBIException {
104            super.stop();
105            try {
106                if (isFactoryJNDIregistered) {
107                    getContainer().getNamingContext().unbind(jndiName);
108                }
109            } catch (NamingException e) {
110                LOG.warn("Cound not stop ClientFactory: " + e);
111                if (LOG.isDebugEnabled()) {
112                    LOG.debug("Could not stop ClientFactory", e);
113                }
114            }
115        }
116    
117    }