001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.service.impl;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XNodeMap;
025import org.nuxeo.common.xmap.annotation.XObject;
026import org.nuxeo.drive.service.FileSystemItemAdapterService;
027import org.nuxeo.drive.service.TopLevelFolderItemFactory;
028
029/**
030 * XMap descriptor for factories contributed to the {@code topLevelFolderItemFactory} extension point of the
031 * {@link FileSystemItemAdapterService}.
032 *
033 * @author Antoine Taillefer
034 */
035@XObject("topLevelFolderItemFactory")
036public class TopLevelFolderItemFactoryDescriptor implements Serializable {
037
038    private static final long serialVersionUID = -7837197812448232426L;
039
040    @XNode("@class")
041    protected Class<? extends TopLevelFolderItemFactory> factoryClass;
042
043    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
044    protected Map<String, String> parameters = new HashMap<String, String>();
045
046    public TopLevelFolderItemFactory getFactory() throws InstantiationException, IllegalAccessException {
047        TopLevelFolderItemFactory factory = factoryClass.newInstance();
048        factory.setName(factory.getClass().getName());
049        factory.handleParameters(parameters);
050        return factory;
051    }
052
053    public Class<? extends TopLevelFolderItemFactory> getFactoryClass() {
054        return factoryClass;
055    }
056
057    public void setFactoryClass(Class<? extends TopLevelFolderItemFactory> factoryClass) {
058        this.factoryClass = factoryClass;
059    }
060
061    public Map<String, String> getParameters() {
062        return parameters;
063    }
064
065    public String getparameter(String name) {
066        return parameters.get(name);
067    }
068
069    public void setParameters(Map<String, String> parameters) {
070        this.parameters = parameters;
071    }
072
073    public void setParameter(String name, String value) {
074        parameters.put(name, value);
075    }
076
077    public String getName() {
078        return factoryClass.getName();
079    }
080
081    @Override
082    public boolean equals(Object obj) {
083        if (this == obj) {
084            return true;
085        }
086        if (!(obj instanceof TopLevelFolderItemFactoryDescriptor)) {
087            return false;
088        }
089        return this.factoryClass.getName().equals(((TopLevelFolderItemFactoryDescriptor) obj).factoryClass.getName());
090    }
091
092    @Override
093    public int hashCode() {
094        return factoryClass.getName().hashCode();
095    }
096
097}