001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme;
016
017import java.net.URLStreamHandler;
018import java.net.URLStreamHandlerFactory;
019import java.util.Collections;
020import java.util.Map;
021
022import org.nuxeo.common.utils.URLStreamHandlerFactoryInstaller;
023import org.nuxeo.runtime.api.Framework;
024import org.nuxeo.theme.perspectives.PerspectiveManager;
025import org.nuxeo.theme.protocol.nxtheme.Handler;
026import org.nuxeo.theme.relations.RelationStorage;
027import org.nuxeo.theme.resources.ResourceManager;
028import org.nuxeo.theme.services.ThemeService;
029import org.nuxeo.theme.themes.ThemeManager;
030import org.nuxeo.theme.types.TypeRegistry;
031import org.nuxeo.theme.uids.UidManager;
032import org.nuxeo.theme.vocabularies.VocabularyManager;
033
034public final class Manager {
035
036    private static final String PROTOCOL_HANDLER_PKG = "org.nuxeo.theme.protocol";
037
038    private Manager() {
039    }
040
041    public static ThemeService getThemeService() {
042        return (ThemeService) Framework.getRuntime().getComponent(ThemeService.ID);
043    }
044
045    private static Map<String, Registrable> getRegistries() {
046        // avoid error when clearing registries at shutdown
047        ThemeService service = getThemeService();
048        if (service != null) {
049            return service.getRegistries();
050        } else {
051            return Collections.emptyMap();
052        }
053    }
054
055    public static Registrable getRegistry(final String name) {
056        return getRegistries().get(name);
057    }
058
059    public static RelationStorage getRelationStorage() {
060        return (RelationStorage) getRegistry("relations");
061    }
062
063    public static UidManager getUidManager() {
064        return (UidManager) getRegistry("uids");
065    }
066
067    public static ThemeManager getThemeManager() {
068        return (ThemeManager) getRegistry("themes");
069    }
070
071    public static TypeRegistry getTypeRegistry() {
072        return (TypeRegistry) getRegistry("types");
073    }
074
075    public static ResourceManager getResourceManager() {
076        return (ResourceManager) getRegistry("resources");
077    }
078
079    public static PerspectiveManager getPerspectiveManager() {
080        return (PerspectiveManager) getRegistry("perspectives");
081    }
082
083    public static VocabularyManager getVocabularyManager() {
084        return (VocabularyManager) getRegistry("vocabularies");
085    }
086
087    protected static URLStreamHandlerFactory shf;
088
089    public static void initializeProtocols() {
090        shf = new URLStreamHandlerFactory() {
091            @Override
092            public URLStreamHandler createURLStreamHandler(String protocol) {
093                if ("nxtheme".equals(protocol)) {
094                    return new Handler();
095                }
096                return null;
097            }
098        };
099        URLStreamHandlerFactoryInstaller.installURLStreamHandlerFactory(shf);
100    }
101
102    public static void resetProtocols() {
103        URLStreamHandlerFactoryInstaller.uninstallURLStreamHandlerFactory(shf);
104        shf = null;
105    }
106}