001/*
002 * (C) Copyright 2006-2016 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.nuxeo.runtime.model.ComponentContext;
023import org.nuxeo.runtime.model.DefaultComponent;
024import org.nuxeo.runtime.model.Extension;
025
026/**
027 * The TypeService is the component dealing with registration of schemas and document types (and facets and prefetch
028 * configuration).
029 * <p>
030 * The implementation is delegated to the SchemaManager.
031 */
032public class TypeService extends DefaultComponent {
033
034    private static final String XP_SCHEMA = "schema";
035
036    private static final String XP_DOCTYPE = "doctype";
037
038    private static final String XP_CONFIGURATION = "configuration";
039
040    private SchemaManagerImpl schemaManager;
041
042    @Override
043    public void activate(ComponentContext context) {
044        schemaManager = new SchemaManagerImpl();
045    }
046
047    @Override
048    public void deactivate(ComponentContext context) {
049        schemaManager = null;
050    }
051
052    @Override
053    public void registerExtension(Extension extension) {
054        String xp = extension.getExtensionPoint();
055        if (XP_DOCTYPE.equals(xp)) {
056            Object[] contribs = extension.getContributions();
057            for (Object contrib : contribs) {
058                if (contrib instanceof DocumentTypeDescriptor) {
059                    schemaManager.registerDocumentType((DocumentTypeDescriptor) contrib);
060                } else if (contrib instanceof FacetDescriptor) {
061                    schemaManager.registerFacet((FacetDescriptor) contrib);
062                } else if (contrib instanceof ProxiesDescriptor) {
063                    schemaManager.registerProxies((ProxiesDescriptor) contrib);
064                }
065            }
066        } else if (XP_SCHEMA.equals(xp)) {
067            Object[] contribs = extension.getContributions();
068            for (Object contrib : contribs) {
069                // use the context of the bundle contributing the extension
070                // to load schemas
071                SchemaBindingDescriptor sbd = (SchemaBindingDescriptor) contrib;
072                sbd.context = extension.getContext();
073                schemaManager.registerSchema(sbd);
074            }
075        } else if (XP_CONFIGURATION.equals(xp)) {
076            Object[] contribs = extension.getContributions();
077            for (Object contrib : contribs) {
078                schemaManager.registerConfiguration((TypeConfiguration) contrib);
079            }
080        }
081    }
082
083    @Override
084    public void unregisterExtension(Extension extension) {
085        String xp = extension.getExtensionPoint();
086        if (XP_DOCTYPE.equals(xp)) {
087            Object[] contribs = extension.getContributions();
088            for (Object contrib : contribs) {
089                if (contrib instanceof DocumentTypeDescriptor) {
090                    schemaManager.unregisterDocumentType((DocumentTypeDescriptor) contrib);
091                } else if (contrib instanceof FacetDescriptor) {
092                    schemaManager.unregisterFacet((FacetDescriptor) contrib);
093                } else if (contrib instanceof ProxiesDescriptor) {
094                    schemaManager.unregisterProxies((ProxiesDescriptor) contrib);
095                }
096            }
097        } else if (XP_SCHEMA.equals(xp)) {
098            Object[] contribs = extension.getContributions();
099            for (Object contrib : contribs) {
100                schemaManager.unregisterSchema((SchemaBindingDescriptor) contrib);
101            }
102        } else if (XP_CONFIGURATION.equals(xp)) {
103            Object[] contribs = extension.getContributions();
104            for (Object contrib : contribs) {
105                schemaManager.unregisterConfiguration((TypeConfiguration) contrib);
106            }
107        }
108    }
109
110    @Override
111    @SuppressWarnings("unchecked")
112    public <T> T getAdapter(Class<T> adapter) {
113        if (SchemaManager.class.isAssignableFrom(adapter)) {
114            return (T) schemaManager;
115        } else if (TypeProvider.class.isAssignableFrom(adapter)) {
116            return (T) schemaManager;
117        }
118        return null;
119    }
120
121    @Override
122    public void applicationStarted(ComponentContext context) {
123        schemaManager.flushPendingsRegistration();
124    }
125
126    @Override
127    public int getApplicationStartedOrder() {
128        return -100;
129    }
130}