001/*
002 * (C) Copyright 2006-2011 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql.jdbc;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.query.sql.NXQL;
030import org.nuxeo.runtime.model.ComponentInstance;
031import org.nuxeo.runtime.model.DefaultComponent;
032
033/**
034 * Service for the registration of QueryMaker classes.
035 */
036public class QueryMakerServiceImpl extends DefaultComponent implements QueryMakerService {
037
038    private static final Log log = LogFactory.getLog(QueryMakerServiceImpl.class);
039
040    public static final String XP = "queryMaker";
041
042    protected final List<QueryMakerDescriptor> descriptors = new ArrayList<QueryMakerDescriptor>(2);
043
044    protected List<Class<? extends QueryMaker>> queryMakers;
045
046    @Override
047    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
048        if (XP.equals(extensionPoint)) {
049            registerQueryMaker((QueryMakerDescriptor) contribution);
050        }
051    }
052
053    @Override
054    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
055        if (XP.equals(extensionPoint)) {
056            unregisterQueryMaker((QueryMakerDescriptor) contribution);
057        }
058    }
059
060    @Override
061    public void registerQueryMaker(QueryMakerDescriptor descriptor) {
062        if (descriptor.enabled) {
063            log.info(String.format("Registering QueryMaker '%s': %s", descriptor.name, descriptor.queryMaker.getName()));
064        } else {
065            log.info(String.format("Disabling QueryMaker '%s'", descriptor.name));
066        }
067        descriptors.add(descriptor);
068        queryMakers = null;
069    }
070
071    @Override
072    public void unregisterQueryMaker(QueryMakerDescriptor descriptor) {
073        if (descriptor.enabled) {
074            log.info(String.format("Unregistering QueryMaker '%s': %s", descriptor.name,
075                    descriptor.queryMaker.getName()));
076        } else {
077            log.info(String.format("Unregistering disabled QueryMaker '%s'", descriptor.name));
078        }
079        descriptors.remove(descriptor);
080        queryMakers = null;
081    }
082
083    @Override
084    public synchronized List<Class<? extends QueryMaker>> getQueryMakers() {
085        if (queryMakers == null) {
086            // recompute queryMakers
087            queryMakers = new ArrayList<Class<? extends QueryMaker>>(2);
088            List<QueryMakerDescriptor> qmdl = new ArrayList<QueryMakerDescriptor>(descriptors);
089            Collections.reverse(qmdl);
090            Set<String> done = new HashSet<String>();
091            for (QueryMakerDescriptor descriptor : qmdl) {
092                if (!done.add(descriptor.name)) {
093                    continue;
094                }
095                if (descriptor.enabled) {
096                    queryMakers.add(descriptor.queryMaker);
097                }
098            }
099            Collections.reverse(queryMakers);
100            // BBB backward compat
101            if (queryMakers.isEmpty() && !done.contains(NXQL.NXQL)) {
102                queryMakers.add(NXQLQueryMaker.class);
103            }
104        }
105        return queryMakers;
106    }
107}