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.common.xbean;
018    
019    import javax.jbi.JBIException;
020    
021    import org.apache.servicemix.common.ServiceUnit;
022    import org.apache.servicemix.common.DefaultServiceUnit;
023    import org.springframework.context.support.AbstractXmlApplicationContext;
024    
025    public class XBeanServiceUnit extends DefaultServiceUnit {
026    
027        private ClassLoader classLoader;
028        private AbstractXmlApplicationContext applicationContext;
029    
030        public AbstractXmlApplicationContext getApplicationContext() {
031            return applicationContext;
032        }
033    
034        public void setApplicationContext(AbstractXmlApplicationContext applicationContext) {
035            this.applicationContext = applicationContext;
036        }
037    
038        /* (non-Javadoc)
039         * @see org.apache.servicemix.common.ServiceUnit#shutDown()
040         */
041        public void shutDown() throws Exception {
042            super.shutDown();
043            classLoader = null;
044            try {
045                if (applicationContext != null) {
046                    applicationContext.destroy();
047                }
048                applicationContext = null;
049            } catch (Exception e) {
050                throw new JBIException("Unable to close application context", e);
051            }
052        }
053        
054        public ClassLoader getConfigurationClassLoader() {
055            if (classLoader == null && applicationContext != null) {
056                classLoader = applicationContext.getClassLoader();
057            }
058            ClassLoader cl = classLoader;
059            if (cl == null) {
060                cl = Thread.currentThread().getContextClassLoader();
061            }
062            if (cl == null) {
063                cl = getClass().getClassLoader();
064            }
065            return cl;
066        }
067        
068    }