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