001/*
002 * (C) Copyright 2006-2015 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-2.1.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 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.usermapper.service;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeMap;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.usermapper.extension.GroovyUserMapper;
030import org.nuxeo.usermapper.extension.NashornUserMapper;
031import org.nuxeo.usermapper.extension.UserMapper;
032
033/**
034 * XMap descriptor for contributing {@link UserMapper} plugins
035 *
036 * @author tiry
037 * @since 7.4
038 */
039@XObject("mapper")
040public class UserMapperDescriptor implements Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    public enum Type {
045        java, groovy, javascript, none
046    }
047
048    @XNode("@name")
049    protected String name;
050
051    @XNode("@type")
052    protected String type;
053
054    @XNode("@class")
055    Class<UserMapper> mapperClass;
056
057    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
058    protected Map<String, String> params;
059
060    @XNode("mapperScript")
061    protected String mapperScript;
062
063    @XNode("wrapperScript")
064    protected String wrapperScript;
065
066    public UserMapper getInstance() throws Exception {
067        UserMapper mapper = null;
068        switch (getType()) {
069        case java:
070            if (mapperClass == null) {
071                throw new NuxeoException("Java Mapper must provide an implementation class ");
072            }
073            mapper = mapperClass.newInstance();
074            break;
075        case groovy:
076            mapper = new GroovyUserMapper(mapperScript, wrapperScript);
077            break;
078        case javascript:
079            mapper = new NashornUserMapper(mapperScript, wrapperScript);
080            break;
081        case none:
082            // fall-through
083        default:
084            throw new NuxeoException("Mapper has an unknown type");
085        }
086        // run init
087        mapper.init(params);
088        return mapper;
089    }
090
091    public Type getType() {
092        if (type == null) {
093            if (mapperClass != null) {
094                return Type.java;
095            }
096        }
097        if ("java".equalsIgnoreCase(type)) {
098            return Type.java;
099        }
100        if ("groovy".equalsIgnoreCase(type)) {
101            return Type.groovy;
102        }
103        if ("javascript".equalsIgnoreCase(type)) {
104            return Type.javascript;
105        }
106        if ("js".equalsIgnoreCase(type)) {
107            return Type.javascript;
108        }
109        return Type.none;
110    }
111}