001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.api;
023
024import java.util.Hashtable;
025import java.util.Map;
026
027/**
028 * A service provider.
029 * <p>
030 * A service provider is used by the framework to be able to change the local services are found
031 * <p>
032 * For example, you may want to use a simple service provider for testing purpose to avoid loading the nuxeo runtime
033 * framework to register services.
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class DefaultServiceProvider implements ServiceProvider {
038
039    private static ServiceProvider provider;
040
041    public static void setProvider(ServiceProvider provider) {
042        DefaultServiceProvider.provider = provider;
043    }
044
045    public static ServiceProvider getProvider() {
046        return provider;
047    }
048
049    protected final Map<Class<?>, ServiceRef> registry = new Hashtable<>();
050
051    @Override
052    @SuppressWarnings("unchecked")
053    public <T> T getService(Class<T> serviceClass) {
054        ServiceRef ref = registry.get(serviceClass);
055        if (ref != null) {
056            return (T) ref.getService();
057        }
058        return null;
059    }
060
061    public <T> void registerService(Class<T> serviceClass, Class<?> implClass) {
062        registry.put(serviceClass, new ServiceRef(implClass));
063    }
064
065    public <T> void registerService(Class<T> serviceClass, Object impl) {
066        registry.put(serviceClass, new ServiceRef(impl));
067    }
068
069    public static class ServiceRef {
070        final Class<?> type;
071
072        Object service;
073
074        public ServiceRef(Object service) {
075            this.service = service;
076            type = service.getClass();
077        }
078
079        public ServiceRef(Class<?> type) {
080            service = null;
081            this.type = type;
082        }
083
084        public Class<?> getType() {
085            return type;
086        }
087
088        public Object getService() {
089            if (service == null) {
090                try {
091                    service = type.getDeclaredConstructor().newInstance();
092                } catch (ReflectiveOperationException e) {
093                    throw new RuntimeException(e);
094                }
095            }
096            return service;
097        }
098    }
099
100}