001/*
002 * (C) Copyright 2006-2007 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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme;
023
024import java.net.URLStreamHandler;
025import java.net.URLStreamHandlerFactory;
026import java.util.Collections;
027import java.util.Map;
028
029import org.nuxeo.common.utils.URLStreamHandlerFactoryInstaller;
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.theme.perspectives.PerspectiveManager;
032import org.nuxeo.theme.protocol.nxtheme.Handler;
033import org.nuxeo.theme.relations.RelationStorage;
034import org.nuxeo.theme.resources.ResourceManager;
035import org.nuxeo.theme.services.ThemeService;
036import org.nuxeo.theme.themes.ThemeManager;
037import org.nuxeo.theme.types.TypeRegistry;
038import org.nuxeo.theme.uids.UidManager;
039import org.nuxeo.theme.vocabularies.VocabularyManager;
040
041public final class Manager {
042
043    private static final String PROTOCOL_HANDLER_PKG = "org.nuxeo.theme.protocol";
044
045    private Manager() {
046    }
047
048    public static ThemeService getThemeService() {
049        return (ThemeService) Framework.getRuntime().getComponent(ThemeService.ID);
050    }
051
052    private static Map<String, Registrable> getRegistries() {
053        // avoid error when clearing registries at shutdown
054        ThemeService service = getThemeService();
055        if (service != null) {
056            return service.getRegistries();
057        } else {
058            return Collections.emptyMap();
059        }
060    }
061
062    public static Registrable getRegistry(final String name) {
063        return getRegistries().get(name);
064    }
065
066    public static RelationStorage getRelationStorage() {
067        return (RelationStorage) getRegistry("relations");
068    }
069
070    public static UidManager getUidManager() {
071        return (UidManager) getRegistry("uids");
072    }
073
074    public static ThemeManager getThemeManager() {
075        return (ThemeManager) getRegistry("themes");
076    }
077
078    public static TypeRegistry getTypeRegistry() {
079        return (TypeRegistry) getRegistry("types");
080    }
081
082    public static ResourceManager getResourceManager() {
083        return (ResourceManager) getRegistry("resources");
084    }
085
086    public static PerspectiveManager getPerspectiveManager() {
087        return (PerspectiveManager) getRegistry("perspectives");
088    }
089
090    public static VocabularyManager getVocabularyManager() {
091        return (VocabularyManager) getRegistry("vocabularies");
092    }
093
094    protected static URLStreamHandlerFactory shf;
095
096    public static void initializeProtocols() {
097        shf = new URLStreamHandlerFactory() {
098            @Override
099            public URLStreamHandler createURLStreamHandler(String protocol) {
100                if ("nxtheme".equals(protocol)) {
101                    return new Handler();
102                }
103                return null;
104            }
105        };
106        URLStreamHandlerFactoryInstaller.installURLStreamHandlerFactory(shf);
107    }
108
109    public static void resetProtocols() {
110        URLStreamHandlerFactoryInstaller.uninstallURLStreamHandlerFactory(shf);
111        shf = null;
112    }
113}