001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
016 * $Id$
017 */
018
019package org.nuxeo.ecm.directory;
020
021import java.util.List;
022
023import org.nuxeo.runtime.api.Framework;
024
025/**
026 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
027 */
028public class DirectoryFactoryProxy implements DirectoryFactory {
029
030    private DirectoryFactory factory;
031
032    private final String componentName;
033
034    public DirectoryFactoryProxy(String componentName) {
035        this.componentName = componentName;
036    }
037
038    /**
039     * Returns the name of the component that's going to be lookup up, as it's the one knowing about its own registered
040     * directories.
041     *
042     * @since 5.6
043     */
044    public String getComponentName() {
045        return componentName;
046    }
047
048    private DirectoryFactory getRealObject() {
049        if (factory == null) {
050            factory = (DirectoryFactory) Framework.getRuntime().getComponent(componentName);
051        }
052        if (factory == null) {
053            throw new RuntimeException(String.format("No Runtime component found for directory " + "factory '%s'",
054                    componentName));
055        }
056        return factory;
057    }
058
059    public Directory getDirectory(String name) throws DirectoryException {
060        return getRealObject().getDirectory(name);
061    }
062
063    public String getName() {
064        return getRealObject().getName();
065    }
066
067    public void shutdown() throws DirectoryException {
068        if (factory != null) {
069            factory.shutdown();
070            factory = null;
071        }
072    }
073
074    public List<Directory> getDirectories() throws DirectoryException {
075        return getRealObject().getDirectories();
076    }
077
078}