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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.negotiation;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.xmap.annotation.XNode;
030import org.nuxeo.common.xmap.annotation.XNodeList;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.theme.types.Type;
033import org.nuxeo.theme.types.TypeFamily;
034
035@XObject("negotiation")
036public final class NegotiationType implements Type {
037
038    private static final Log log = LogFactory.getLog(NegotiationType.class);
039
040    @XNode("@object")
041    public String object;
042
043    @XNode("@strategy")
044    public String strategy;
045
046    @XNodeList(value = "scheme", type = ArrayList.class, componentType = String.class)
047    private List<String> schemeClassNames;
048
049    private List<Scheme> schemes;
050
051    public String getTypeName() {
052        return String.format("%s/%s", strategy, object);
053    }
054
055    public TypeFamily getTypeFamily() {
056        return TypeFamily.NEGOTIATION;
057    }
058
059    public synchronized List<Scheme> getSchemes() {
060        if (schemes == null && schemeClassNames != null) {
061            schemes = new ArrayList<Scheme>();
062            for (String schemeClassName : schemeClassNames) {
063                Scheme scheme = null;
064                try {
065                    scheme = (Scheme) Class.forName(schemeClassName).newInstance();
066                } catch (ReflectiveOperationException e) {
067                    log.error("Could not create instance: " + schemeClassName);
068                    continue;
069                }
070                schemes.add(scheme);
071            }
072        }
073        return schemes;
074    }
075
076    public String getStrategy() {
077        return strategy;
078    }
079
080    public String getObject() {
081        return object;
082    }
083
084}