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