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