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    //
018    // This source code implements specifications defined by the Java
019    // Community Process. In order to remain compliant with the specification
020    // DO NOT add / change / or delete method signatures!
021    //
022    package javax.ejb.embeddable;
023    
024    import javax.ejb.EJBException;
025    import javax.ejb.spi.EJBContainerProvider;
026    import java.io.BufferedReader;
027    import java.io.IOException;
028    import java.io.InputStreamReader;
029    import java.net.URL;
030    import java.util.Collections;
031    import java.util.Enumeration;
032    
033    import org.apache.geronimo.osgi.locator.ProviderLocator;
034    
035    public abstract class EJBContainer {
036    
037        public static final String PROVIDER = "javax.ejb.embeddable.provider";
038        public static final String APP_NAME = "javax.ejb.embeddable.appName";
039        public static final String MODULES = "javax.ejb.embeddable.modules";
040    
041        public EJBContainer() {
042        }
043    
044        public abstract void close();
045    
046        public static EJBContainer createEJBContainer() {
047            return createEJBContainer(Collections.EMPTY_MAP);
048        }
049    
050        public static EJBContainer createEJBContainer(java.util.Map<?, ?> properties) {
051            if (properties == null) {
052                properties = Collections.EMPTY_MAP;
053            }
054    
055            Object o = properties.get(PROVIDER);
056    
057            if (o instanceof String) {
058                String providerName = (String) o;
059    
060                Class providerClass;
061    
062                try {
063                    providerClass = ProviderLocator.loadClass(providerName);
064                } catch (Exception e) {
065                    throw new EJBException("Invalid or inaccessible provider class: " + providerName, e);
066                }
067    
068                try {
069                    EJBContainerProvider provider = (EJBContainerProvider) providerClass.newInstance();
070                    return provider.createEJBContainer(properties);
071                } catch (Exception e) {
072                    throw new EJBException("Provider error. Provider: " + providerName, e);
073                }
074            } else {
075                // do the services search processing.
076                try {
077                    ClassLoader loader = Thread.currentThread().getContextClassLoader();
078                    // go check the loader files.
079                    EJBContainerProvider provider = (EJBContainerProvider)ProviderLocator.getService(EJBContainerProvider.class.getName(), EJBContainer.class, loader);
080                    if (provider != null) {
081                        return provider.createEJBContainer(properties);
082                    }
083                    else {
084                        throw new EJBException("Provider error. No provider definition found");
085                    }
086                } catch (Exception e) {
087                    throw new EJBException("Provider error. No provider found", e);
088                }
089            }
090        }
091    
092        public abstract javax.naming.Context getContext();
093    }