001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (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.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 *     bstefanescu
016 */
017package org.nuxeo.ecm.automation.client.jaxrs.spi;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.ecm.automation.client.AutomationClient;
023import org.nuxeo.ecm.automation.client.LoginInfo;
024import org.nuxeo.ecm.automation.client.OperationRequest;
025import org.nuxeo.ecm.automation.client.Session;
026import org.nuxeo.ecm.automation.client.model.OperationDocumentation;
027import org.nuxeo.ecm.automation.client.model.OperationRegistry;
028
029/**
030 * Abstract class for sessions running on real JVMs.
031 * <p>
032 * When your implementation is designed for running in environment that supports limited Java API like GWT or portable
033 * devices you may need to directly implement the {@link Session} interface.
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public abstract class JavaSession implements Session {
038
039    protected final JavaClient client;
040
041    protected final LoginInfo login;
042
043    protected final OperationRegistry registry;
044
045    public JavaSession(JavaClient client, LoginInfo login, OperationRegistry registry) {
046        this.client = client;
047        this.login = login;
048        this.registry = registry;
049    }
050
051    protected OperationRequest createOperationRequest(JavaSession session, OperationDocumentation op,
052            Map<String, Object> ctx) {
053        return new JavaOperationRequest(session, op, ctx);
054    }
055
056    @Override
057    public AutomationClient getClient() {
058        return client;
059    }
060
061    @Override
062    public LoginInfo getLogin() {
063        return login;
064    }
065
066    @Override
067    public <T> T getAdapter(Class<T> type) {
068        return client.getAdapter(this, type);
069    }
070
071    public OperationRequest newRequest(String id) {
072        return newRequest(id, new HashMap<String, Object>());
073    }
074
075    public OperationRequest newRequest(String id, Map<String, Object> ctx) {
076        OperationDocumentation op = getOperation(id);
077        if (op == null) {
078            throw new IllegalArgumentException("No such operation: " + id);
079        }
080        return createOperationRequest(this, op, ctx);
081    }
082
083    protected OperationRegistry getRegistry() {
084        return registry;
085    }
086
087    public OperationDocumentation getOperation(String id) {
088        return registry.getOperation(id);
089    }
090
091    public Map<String, OperationDocumentation> getOperations() {
092        return registry.getOperations();
093    }
094
095    @Override
096    public void close() {
097        // do nothing
098    }
099
100}