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 org.apache.servicemix.common.ServiceUnit;
020    import org.apache.xbean.kernel.Kernel;
021    import org.apache.xbean.kernel.ServiceFactory;
022    import org.apache.xbean.kernel.ServiceName;
023    import org.apache.xbean.kernel.ServiceNotFoundException;
024    
025    import javax.jbi.JBIException;
026    
027    public class XBeanServiceUnit extends ServiceUnit {
028    
029        private Kernel kernel;
030        private ServiceName configuration;
031        private ClassLoader classLoader;
032    
033        /**
034         * @return Returns the kernel.
035         */
036        public Kernel getKernel() {
037            return kernel;
038        }
039    
040        /**
041         * @param kernel The kernel to set.
042         */
043        public void setKernel(Kernel kernel) {
044            this.kernel = kernel;
045        }
046    
047        public ServiceName getConfiguration() {
048            return configuration;
049        }
050    
051        public void setConfiguration(ServiceName configuration) {
052            this.configuration = configuration;
053        }
054        
055        /* (non-Javadoc)
056         * @see org.apache.servicemix.common.ServiceUnit#shutDown()
057         */
058        public void shutDown() throws JBIException {
059            super.shutDown();
060            classLoader = null;
061            if (kernel != null) {
062                kernel.destroy();
063            }
064        }
065        
066        public ClassLoader getConfigurationClassLoader() {
067            if (classLoader == null && kernel != null && configuration != null) {
068                try {
069                    ServiceFactory sf = kernel.getServiceFactory(configuration);
070                    classLoader = sf.getClassLoader();
071                } catch (ServiceNotFoundException e) {
072                    // This should never happen
073                }
074            }
075            ClassLoader cl = classLoader;
076            if (cl == null) {
077                cl = Thread.currentThread().getContextClassLoader();
078            }
079            if (cl == null) {
080                cl = getClass().getClassLoader();
081            }
082            return cl;
083        }
084        
085    }