001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.rendering;
021
022import java.util.Arrays;
023import java.util.Collection;
024import java.util.Comparator;
025
026import org.nuxeo.ecm.core.schema.DocumentType;
027import org.nuxeo.ecm.core.schema.SchemaManager;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.model.ComponentName;
030import org.nuxeo.runtime.model.RegistrationInfo;
031import org.osgi.framework.Bundle;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class APIHelper implements RenderingExtensionFactory {
037
038    public static final APIHelper INSTANCE = new APIHelper();
039
040    public static final Comparator<DocumentType> DOCTYPE_COMPARATOR = new Comparator<DocumentType>() {
041        public int compare(DocumentType o1, DocumentType o2) {
042            return o1.getName().compareTo(o2.getName());
043        }
044    };
045
046    public Object createTemplate() {
047        return this;
048    }
049
050    public static DocumentType[] getSortedDocumentTypes() {
051        DocumentType[] doctypes = Framework.getLocalService(SchemaManager.class).getDocumentTypes();
052        Arrays.sort(doctypes, DOCTYPE_COMPARATOR);
053        return doctypes;
054    }
055
056    public static Bundle[] getBundles() {
057        return Framework.getRuntime().getContext().getBundle().getBundleContext().getBundles();
058    }
059
060    public static Collection<RegistrationInfo> getComponents() {
061        return Framework.getRuntime().getComponentManager().getRegistrations();
062    }
063
064    public static Collection<ComponentName> getPendingComponents() {
065        return Framework.getRuntime().getComponentManager().getPendingRegistrations().keySet();
066    }
067
068}