001/*
002 * (C) Copyright 2006-2017 Nuxeo (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 static final String XP_DEPRECATION = "deprecation";
041
042    private SchemaManagerImpl schemaManager;
043
044    @Override
045    public void activate(ComponentContext context) {
046        schemaManager = new SchemaManagerImpl();
047    }
048
049    @Override
050    public void deactivate(ComponentContext context) {
051        schemaManager = null;
052    }
053
054    @Override
055    public void registerExtension(Extension extension) {
056        String xp = extension.getExtensionPoint();
057        Object[] contribs = extension.getContributions();
058        switch (xp) {
059        case XP_DOCTYPE:
060            for (Object contrib : contribs) {
061                if (contrib instanceof DocumentTypeDescriptor) {
062                    schemaManager.registerDocumentType((DocumentTypeDescriptor) contrib);
063                } else if (contrib instanceof FacetDescriptor) {
064                    schemaManager.registerFacet((FacetDescriptor) contrib);
065                } else if (contrib instanceof ProxiesDescriptor) {
066                    schemaManager.registerProxies((ProxiesDescriptor) contrib);
067                }
068            }
069            break;
070        case XP_SCHEMA:
071            for (Object contrib : contribs) {
072                // use the context of the bundle contributing the extension
073                // to load schemas
074                SchemaBindingDescriptor sbd = (SchemaBindingDescriptor) contrib;
075                sbd.context = extension.getContext();
076                schemaManager.registerSchema(sbd);
077            }
078            break;
079        case XP_CONFIGURATION:
080            for (Object contrib : contribs) {
081                schemaManager.registerConfiguration((TypeConfiguration) contrib);
082            }
083            break;
084        case XP_DEPRECATION:
085            for (Object contrib : contribs) {
086                schemaManager.registerPropertyDeprecation((PropertyDeprecationDescriptor) contrib);
087            }
088            break;
089        }
090    }
091
092    @Override
093    public void unregisterExtension(Extension extension) {
094        String xp = extension.getExtensionPoint();
095        Object[] contribs = extension.getContributions();
096        switch (xp) {
097        case XP_DOCTYPE:
098            for (Object contrib : contribs) {
099                if (contrib instanceof DocumentTypeDescriptor) {
100                    schemaManager.unregisterDocumentType((DocumentTypeDescriptor) contrib);
101                } else if (contrib instanceof FacetDescriptor) {
102                    schemaManager.unregisterFacet((FacetDescriptor) contrib);
103                } else if (contrib instanceof ProxiesDescriptor) {
104                    schemaManager.unregisterProxies((ProxiesDescriptor) contrib);
105                }
106            }
107            break;
108        case XP_SCHEMA:
109            for (Object contrib : contribs) {
110                schemaManager.unregisterSchema((SchemaBindingDescriptor) contrib);
111            }
112            break;
113        case XP_CONFIGURATION:
114            for (Object contrib : contribs) {
115                schemaManager.unregisterConfiguration((TypeConfiguration) contrib);
116            }
117            break;
118        case XP_DEPRECATION:
119            for (Object contrib : contribs) {
120                schemaManager.unregisterPropertyDeprecation((PropertyDeprecationDescriptor) contrib);
121            }
122            break;
123        }
124    }
125
126    @Override
127    @SuppressWarnings("unchecked")
128    public <T> T getAdapter(Class<T> adapter) {
129        if (SchemaManager.class.isAssignableFrom(adapter)) {
130            return (T) schemaManager;
131        } else if (TypeProvider.class.isAssignableFrom(adapter)) {
132            return (T) schemaManager;
133        }
134        return null;
135    }
136
137    @Override
138    public void start(ComponentContext context) {
139        schemaManager.flushPendingsRegistration();
140    }
141
142    @Override
143    public int getApplicationStartedOrder() {
144        return -100;
145    }
146}