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.components.util;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.ComponentContext;
021    import javax.jbi.component.ComponentLifeCycle;
022    import javax.management.ObjectName;
023    import javax.xml.namespace.QName;
024    
025    import org.springframework.beans.factory.DisposableBean;
026    
027    /**
028     * Adapts a POJO to a {@link ComponentLifeCycle} without performing any activation
029     *
030     * @version $Revision: 564374 $
031     */
032    public class PojoLifecycleAdaptor implements ComponentLifeCycle {
033    
034        private Object pojo;
035        private QName service;
036        private String endpoint;
037        private ComponentContext context;
038        private ObjectName extensionMBeanName;
039    
040        public PojoLifecycleAdaptor(Object pojo, QName service, String endpoint) {
041            this.pojo = pojo;
042            this.service = service;
043            this.endpoint = endpoint;
044        }
045    
046        public ObjectName getExtensionMBeanName() {
047            return extensionMBeanName;
048        }
049    
050        public void init(ComponentContext ctx) throws JBIException {
051            this.context = ctx;
052            if (service != null && endpoint != null) {
053                ctx.activateEndpoint(service, endpoint);
054            }
055        }
056    
057    
058        public void shutDown() throws JBIException {
059            if (pojo instanceof DisposableBean) {
060                DisposableBean disposableBean = (DisposableBean) pojo;
061                try {
062                    disposableBean.destroy();
063                } catch (Exception e) {
064                    throw new JBIException(e);
065                }
066            }
067        }
068    
069        public void start() throws JBIException {
070        }
071    
072        public void stop() throws JBIException {
073        }
074    
075        // Properties
076        //-------------------------------------------------------------------------
077        public Object getPojo() {
078            return pojo;
079        }
080    
081        public void setExtensionMBeanName(ObjectName extensionMBeanName) {
082            this.extensionMBeanName = extensionMBeanName;
083        }
084    
085        public ComponentContext getContext() {
086            return context;
087        }
088    }