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 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.util;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.model.ComponentName;
030import org.nuxeo.runtime.model.DefaultComponent;
031import org.nuxeo.runtime.model.Extension;
032
033/**
034 * @author Thierry Delprat
035 */
036public class LocationManagerService extends DefaultComponent {
037
038    public static final ComponentName NAME = new ComponentName("org.nuxeo.ecm.platform.util.LocationManagerService");
039
040    private static final Log log = LogFactory.getLog(LocationManagerService.class);
041
042    private Map<String, RepositoryLocation> locations = new HashMap<String, RepositoryLocation>();
043
044    @Override
045    public void registerExtension(Extension extension) {
046        Object[] contribs = extension.getContributions();
047        for (Object contrib : contribs) {
048            register((LocationManagerPluginExtension) contrib);
049        }
050    }
051
052    private void register(LocationManagerPluginExtension pluginExtension) {
053        String locationName = pluginExtension.getLocationName();
054        boolean locationEnabled = pluginExtension.getLocationEnabled();
055
056        log.info("Registering location manager: " + locationName + (locationEnabled ? "" : " (disabled)"));
057
058        RepositoryLocation locationTempPlugin = new RepositoryLocation(locationName);
059
060        if (locations.containsKey(locationName)) {
061            if (!locationEnabled) {
062                locations.remove(locationName);
063            }
064        } else {
065            locations.put(locationName, locationTempPlugin);
066        }
067    }
068
069    @Override
070    public void unregisterExtension(Extension extension) {
071        locations.clear();
072        locations = null;
073    }
074
075    public Map<String, RepositoryLocation> getAvailableLocations() {
076        return locations;
077    }
078
079}