001/*
002 * Copyright (c) 2006-2012 Nuxeo SA (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 *     Bogdan Stefanescu
011 *     Florent Guillaume
012 */
013package org.nuxeo.ecm.core.schema;
014
015import org.apache.commons.logging.Log;
016import org.apache.commons.logging.LogFactory;
017import org.nuxeo.runtime.api.Framework;
018import org.nuxeo.runtime.model.ComponentContext;
019import org.nuxeo.runtime.model.ComponentName;
020import org.nuxeo.runtime.model.DefaultComponent;
021import org.nuxeo.runtime.model.Extension;
022
023/**
024 * The TypeService is the component dealing with registration of schemas and document types (and facets and prefetch
025 * configuration).
026 * <p>
027 * The implementation is delegated to the SchemaManager.
028 */
029public class TypeService extends DefaultComponent {
030
031    /**
032     * @deprecated since 5.7 (unused)
033     */
034    @Deprecated
035    public static final ComponentName NAME = new ComponentName("org.nuxeo.ecm.core.schema.TypeService");
036
037    private static final Log log = LogFactory.getLog(TypeService.class);
038
039    private static final String XP_SCHEMA = "schema";
040
041    private static final String XP_DOCTYPE = "doctype";
042
043    private static final String XP_CONFIGURATION = "configuration";
044
045    private SchemaManagerImpl schemaManager;
046
047    /**
048     * @deprecated since 5.7, use {@code Framework.getLocalService(SchemaManager.class)} instead.
049     */
050    @Deprecated
051    public static SchemaManager getSchemaManager() {
052        return Framework.getLocalService(SchemaManager.class);
053    }
054
055    /**
056     * @deprecated since 5.7, use {@code Framework.getLocalService(SchemaManager.class)} instead.
057     */
058    @Deprecated
059    public SchemaManager getTypeManager() {
060        return schemaManager;
061    }
062
063    /**
064     * @deprecated since 5.7 (unused)
065     */
066    @Deprecated
067    public XSDLoader getSchemaLoader() {
068        return new XSDLoader(schemaManager);
069    }
070
071    @Override
072    public void activate(ComponentContext context) {
073        schemaManager = new SchemaManagerImpl();
074    }
075
076    @Override
077    public void deactivate(ComponentContext context) {
078        schemaManager = null;
079    }
080
081    @Override
082    public void registerExtension(Extension extension) {
083        String xp = extension.getExtensionPoint();
084        if (XP_DOCTYPE.equals(xp)) {
085            Object[] contribs = extension.getContributions();
086            for (Object contrib : contribs) {
087                if (contrib instanceof DocumentTypeDescriptor) {
088                    schemaManager.registerDocumentType((DocumentTypeDescriptor) contrib);
089                } else if (contrib instanceof FacetDescriptor) {
090                    schemaManager.registerFacet((FacetDescriptor) contrib);
091                } else if (contrib instanceof ProxiesDescriptor) {
092                    schemaManager.registerProxies((ProxiesDescriptor) contrib);
093                }
094            }
095        } else if (XP_SCHEMA.equals(xp)) {
096            Object[] contribs = extension.getContributions();
097            for (Object contrib : contribs) {
098                // use the context of the bundle contributing the extension
099                // to load schemas
100                SchemaBindingDescriptor sbd = (SchemaBindingDescriptor) contrib;
101                sbd.context = extension.getContext();
102                schemaManager.registerSchema(sbd);
103            }
104        } else if (XP_CONFIGURATION.equals(xp)) {
105            Object[] contribs = extension.getContributions();
106            for (Object contrib : contribs) {
107                schemaManager.registerConfiguration((TypeConfiguration) contrib);
108            }
109        }
110    }
111
112    @Override
113    public void unregisterExtension(Extension extension) {
114        String xp = extension.getExtensionPoint();
115        if (XP_DOCTYPE.equals(xp)) {
116            Object[] contribs = extension.getContributions();
117            for (Object contrib : contribs) {
118                if (contrib instanceof DocumentTypeDescriptor) {
119                    schemaManager.unregisterDocumentType((DocumentTypeDescriptor) contrib);
120                } else if (contrib instanceof FacetDescriptor) {
121                    schemaManager.unregisterFacet((FacetDescriptor) contrib);
122                } else if (contrib instanceof ProxiesDescriptor) {
123                    schemaManager.unregisterProxies((ProxiesDescriptor) contrib);
124                }
125            }
126        } else if (XP_SCHEMA.equals(xp)) {
127            Object[] contribs = extension.getContributions();
128            for (Object contrib : contribs) {
129                schemaManager.unregisterSchema((SchemaBindingDescriptor) contrib);
130            }
131        } else if (XP_CONFIGURATION.equals(xp)) {
132            Object[] contribs = extension.getContributions();
133            for (Object contrib : contribs) {
134                schemaManager.unregisterConfiguration((TypeConfiguration) contrib);
135            }
136        }
137    }
138
139    @Override
140    @SuppressWarnings("unchecked")
141    public <T> T getAdapter(Class<T> adapter) {
142        if (SchemaManager.class.isAssignableFrom(adapter)) {
143            return (T) schemaManager;
144        } else if (TypeProvider.class.isAssignableFrom(adapter)) {
145            return (T) schemaManager;
146        }
147        return null;
148    }
149
150    @Override
151    public void applicationStarted(ComponentContext context) {
152        schemaManager.flushPendingsRegistration();
153    }
154
155    @Override
156    public int getApplicationStartedOrder() {
157        return -100;
158    }
159}