001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.api.descriptor;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeList;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.template.api.context.ContextExtensionFactory;
030
031@XObject("contextFactory")
032public class ContextExtensionFactoryDescriptor {
033
034    protected static final Log log = LogFactory.getLog(ContextExtensionFactoryDescriptor.class);
035
036    @XNode("@name")
037    protected String name;
038
039    @XNode("@class")
040    protected Class<? extends ContextExtensionFactory> factoryClass;
041
042    @XNode("@enabled")
043    protected boolean enabled = true;
044
045    @XNodeList(value = "aliasName", type = ArrayList.class, componentType = String.class)
046    protected List<String> aliasNames = new ArrayList<>();
047
048    protected ContextExtensionFactory factory;
049
050    public boolean isEnabled() {
051        return enabled;
052    }
053
054    public ContextExtensionFactory getExtensionFactory() {
055        if (factory == null) {
056            if (factoryClass != null) {
057                try {
058                    factory = factoryClass.getDeclaredConstructor().newInstance();
059                } catch (ReflectiveOperationException e) {
060                    log.error("Unable to instanciate Processor", e);
061                }
062            }
063        }
064        return factory;
065    }
066
067    public String getName() {
068        return name;
069    }
070
071    @Override
072    public ContextExtensionFactoryDescriptor clone() {
073        ContextExtensionFactoryDescriptor copy = new ContextExtensionFactoryDescriptor();
074        copy.name = name;
075        copy.factoryClass = factoryClass;
076        copy.enabled = enabled;
077        copy.aliasNames = aliasNames;
078        return copy;
079    }
080
081    public void merge(ContextExtensionFactoryDescriptor src) {
082        if (src.factoryClass != null) {
083            factoryClass = src.factoryClass;
084        }
085        if (src.aliasNames != null) {
086            aliasNames.addAll(src.aliasNames);
087        }
088        enabled = src.enabled;
089    }
090
091    public List<String> getAliases() {
092        return aliasNames;
093    }
094
095}