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