001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.negotiation;
016
017import java.util.ArrayList;
018import java.util.List;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XNodeList;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.theme.types.Type;
026import org.nuxeo.theme.types.TypeFamily;
027
028@XObject("negotiation")
029public final class NegotiationType implements Type {
030
031    private static final Log log = LogFactory.getLog(NegotiationType.class);
032
033    @XNode("@object")
034    public String object;
035
036    @XNode("@strategy")
037    public String strategy;
038
039    @XNodeList(value = "scheme", type = ArrayList.class, componentType = String.class)
040    private List<String> schemeClassNames;
041
042    private List<Scheme> schemes;
043
044    public String getTypeName() {
045        return String.format("%s/%s", strategy, object);
046    }
047
048    public TypeFamily getTypeFamily() {
049        return TypeFamily.NEGOTIATION;
050    }
051
052    public synchronized List<Scheme> getSchemes() {
053        if (schemes == null && schemeClassNames != null) {
054            schemes = new ArrayList<Scheme>();
055            for (String schemeClassName : schemeClassNames) {
056                Scheme scheme = null;
057                try {
058                    scheme = (Scheme) Class.forName(schemeClassName).newInstance();
059                } catch (ReflectiveOperationException e) {
060                    log.error("Could not create instance: " + schemeClassName);
061                    continue;
062                }
063                schemes.add(scheme);
064            }
065        }
066        return schemes;
067    }
068
069    public String getStrategy() {
070        return strategy;
071    }
072
073    public String getObject() {
074        return object;
075    }
076
077}