001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model.impl;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Vector;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class TypeConfigurationProvider {
030
031    protected final List<TypeDescriptor> types;
032
033    protected final List<AdapterDescriptor> services;
034
035    protected final List<TypeRegistry> registries;
036
037    public TypeConfigurationProvider() {
038        types = new ArrayList<TypeDescriptor>();
039        services = new ArrayList<AdapterDescriptor>();
040        registries = new Vector<TypeRegistry>();
041    }
042
043    public void flushCache() {
044        // do nothing
045    }
046
047    public synchronized boolean isEmpty() {
048        return types.isEmpty() && services.isEmpty();
049    }
050
051    public synchronized void registerType(TypeDescriptor td) {
052        types.add(td);
053        fireTypeRegistered(td);
054    }
055
056    public synchronized void unregisterType(TypeDescriptor td) {
057        if (types.remove(td)) {
058            fireTypeUnregistered(td);
059        }
060    }
061
062    public synchronized void registerAction(AdapterDescriptor ad) {
063        services.add(ad);
064        fireServiceRegistered(ad);
065    }
066
067    public synchronized void unregisterAction(AdapterDescriptor ad) {
068        if (services.remove(ad)) {
069            fireServiceUnregistered(ad);
070        }
071    }
072
073    public void addRegistry(TypeRegistry registry) {
074        registries.add(registry);
075    }
076
077    public void removeRegistry(TypeRegistry listener) {
078        registries.remove(listener);
079    }
080
081    public synchronized void install(TypeRegistry registry) {
082        for (TypeDescriptor td : types) {
083            registry.registerType(td);
084        }
085        for (AdapterDescriptor ad : services) {
086            registry.registerAdapter(ad);
087        }
088        addRegistry(registry);
089    }
090
091    protected void fireServiceRegistered(AdapterDescriptor ad) {
092        if (registries.isEmpty()) {
093            return;
094        }
095        for (TypeRegistry reg : registries.toArray(new TypeRegistry[registries.size()])) {
096            reg.registerAdapter(ad);
097        }
098    }
099
100    protected void fireServiceUnregistered(AdapterDescriptor ad) {
101        if (registries.isEmpty()) {
102            return;
103        }
104        for (TypeRegistry reg : registries.toArray(new TypeRegistry[registries.size()])) {
105            reg.unregisterAdapter(ad);
106        }
107    }
108
109    protected void fireTypeRegistered(TypeDescriptor td) {
110        if (registries.isEmpty()) {
111            return;
112        }
113        for (TypeRegistry listener : registries.toArray(new TypeRegistry[registries.size()])) {
114            listener.registerType(td);
115        }
116    }
117
118    protected void fireTypeUnregistered(TypeDescriptor td) {
119        if (registries.isEmpty()) {
120            return;
121        }
122        for (TypeRegistry listener : registries.toArray(new TypeRegistry[registries.size()])) {
123            listener.unregisterType(td);
124        }
125    }
126
127}