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.xbean.kernel;
018    
019    import java.util.Collections;
020    import java.util.Set;
021    
022    /**
023     * A basic service factory that always creates the supplied object.
024     *
025     * @author Dain Sundstrom
026     * @version $Id$
027     * @since 2.0
028     */
029    public class StaticServiceFactory extends AbstractServiceFactory {
030        private final Object service;
031        private final ClassLoader classLoader;
032    
033        /**
034         * Creates a non-restartable service factory which will simply returns the specified service from the createService
035         * method.
036         *
037         * @param service the static to which this factory "creates"
038         * @throws NullPointerException if service is null
039         */
040        public StaticServiceFactory(Object service) throws NullPointerException {
041            if (service == null) throw new NullPointerException("service is null");
042            this.service = service;
043            ClassLoader cl = service.getClass().getClassLoader();
044            if (cl == null) {
045                cl = ClassLoader.getSystemClassLoader();
046            }
047            this.classLoader = cl;
048        }
049    
050        /**
051         * Creates a non-restartable service factory which will simply returns the specified service from the createService
052         * method.
053         *
054         * @param service the static to which this factory "creates"
055         * @param classLoader the classLoader used to load the service class
056         * @throws NullPointerException if service or classLoader is null
057         */
058        public StaticServiceFactory(Object service, ClassLoader classLoader) throws NullPointerException {
059            if (service == null) throw new NullPointerException("service is null");
060            if (classLoader == null) throw new NullPointerException("classLoader is null");
061            this.service = service;
062            this.classLoader = classLoader;
063        }
064    
065        public Class[] getTypes() {
066            return new Class[]{service.getClass()};
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public boolean isRestartable() {
073            return false;
074        }
075    
076        /**
077         * {@inheritDoc}
078         */
079        public Set getOwnedServices() {
080            return Collections.EMPTY_SET;
081        }
082    
083        /**
084         * Returns the static service instance.
085         *
086         * @param serviceContext ignored
087         * @return the static service instance
088         */
089        public Object createService(ServiceContext serviceContext) {
090            return service;
091        }
092    
093        /**
094         * This method is a noop.
095         *
096         * @param serviceContext ignored
097         */
098        public void destroyService(ServiceContext serviceContext) {
099        }
100    
101        /**
102         * {@inheritDoc}
103         */
104        public ClassLoader getClassLoader() {
105            return classLoader;
106        }
107        
108    }