001/*
002 * (C) Copyright 2006-2008 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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model.impl;
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Vector;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class TypeConfigurationProvider {
032
033    protected final List<TypeDescriptor> types;
034
035    protected final List<AdapterDescriptor> services;
036
037    protected final List<TypeRegistry> registries;
038
039    public TypeConfigurationProvider() {
040        types = new ArrayList<>();
041        services = new ArrayList<>();
042        registries = new Vector<>();
043    }
044
045    public void flushCache() {
046        // do nothing
047    }
048
049    public synchronized boolean isEmpty() {
050        return types.isEmpty() && services.isEmpty();
051    }
052
053    public synchronized void registerType(TypeDescriptor td) {
054        types.add(td);
055        fireTypeRegistered(td);
056    }
057
058    public synchronized void unregisterType(TypeDescriptor td) {
059        if (types.remove(td)) {
060            fireTypeUnregistered(td);
061        }
062    }
063
064    public synchronized void registerAction(AdapterDescriptor ad) {
065        services.add(ad);
066        fireServiceRegistered(ad);
067    }
068
069    public synchronized void unregisterAction(AdapterDescriptor ad) {
070        if (services.remove(ad)) {
071            fireServiceUnregistered(ad);
072        }
073    }
074
075    public void addRegistry(TypeRegistry registry) {
076        registries.add(registry);
077    }
078
079    public void removeRegistry(TypeRegistry listener) {
080        registries.remove(listener);
081    }
082
083    public synchronized void install(TypeRegistry registry) {
084        for (TypeDescriptor td : types) {
085            registry.registerType(td);
086        }
087        for (AdapterDescriptor ad : services) {
088            registry.registerAdapter(ad);
089        }
090        addRegistry(registry);
091    }
092
093    protected void fireServiceRegistered(AdapterDescriptor ad) {
094        if (registries.isEmpty()) {
095            return;
096        }
097        for (TypeRegistry reg : registries.toArray(new TypeRegistry[registries.size()])) {
098            reg.registerAdapter(ad);
099        }
100    }
101
102    protected void fireServiceUnregistered(AdapterDescriptor ad) {
103        if (registries.isEmpty()) {
104            return;
105        }
106        for (TypeRegistry reg : registries.toArray(new TypeRegistry[registries.size()])) {
107            reg.unregisterAdapter(ad);
108        }
109    }
110
111    protected void fireTypeRegistered(TypeDescriptor td) {
112        if (registries.isEmpty()) {
113            return;
114        }
115        for (TypeRegistry listener : registries.toArray(new TypeRegistry[registries.size()])) {
116            listener.registerType(td);
117        }
118    }
119
120    protected void fireTypeUnregistered(TypeDescriptor td) {
121        if (registries.isEmpty()) {
122            return;
123        }
124        for (TypeRegistry listener : registries.toArray(new TypeRegistry[registries.size()])) {
125            listener.unregisterType(td);
126        }
127    }
128
129}