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