001/*
002 * (C) Copyright 2006-2007 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 *     anguenot
018 *
019 * $Id: WSRemotingSessionManagerImpl.java 21703 2007-07-01 20:48:16Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ws.session;
023
024import java.util.HashMap;
025import java.util.Hashtable;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.platform.api.ws.DocumentLoader;
035import org.nuxeo.ecm.platform.api.ws.DocumentLoaderDescriptor;
036import org.nuxeo.ecm.platform.api.ws.DocumentProperty;
037import org.nuxeo.ecm.platform.api.ws.session.WSRemotingSession;
038import org.nuxeo.ecm.platform.api.ws.session.WSRemotingSessionManager;
039import org.nuxeo.ecm.platform.api.ws.session.impl.WSRemotingSessionImpl;
040import org.nuxeo.ecm.platform.usermanager.UserManager;
041import org.nuxeo.runtime.model.ComponentInstance;
042import org.nuxeo.runtime.model.ComponentName;
043import org.nuxeo.runtime.model.DefaultComponent;
044
045/**
046 * Web service Remoting session manager implemtation.
047 *
048 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
049 */
050public class WSRemotingSessionManagerImpl extends DefaultComponent implements WSRemotingSessionManager, DocumentLoader {
051
052    public static final ComponentName NAME = new ComponentName(
053            "org.nuxeo.ecm.platform.ws.session.WSRemotingSessionManagerImpl");
054
055    private static final Log log = LogFactory.getLog(WSRemotingSessionManagerImpl.class);
056
057    private static final Map<String, WSRemotingSession> sessions = new Hashtable<String, WSRemotingSession>();
058
059    protected final HashMap<String, DocumentLoader> loaders = new HashMap<String, DocumentLoader>();
060
061    public void addSession(String sid, WSRemotingSession session) {
062        log.debug("Adding a new Web Service remoting session for username=" + session.getUsername());
063        sessions.put(sid, session);
064    }
065
066    public WSRemotingSession createSession(String username, String password, String repository, UserManager um,
067            CoreSession session) {
068        return new WSRemotingSessionImpl(session, um, repository, username, password);
069    }
070
071    public WSRemotingSession getSession(String sid) {
072        if (sid == null) {
073            throw new NuxeoException("Invalid value for sid... null value");
074        }
075        WSRemotingSession session = sessions.get(sid);
076        if (session == null) {
077            throw new NuxeoException("Cannot find session for sid=" + sid);
078        }
079        log.debug("Found session for username=" + session.getUsername());
080        log.debug("Forwarding the session now...");
081        return session;
082    }
083
084    public void delSession(String sid) {
085        WSRemotingSession session = getSession(sid);
086        sessions.remove(sid);
087        log.debug("Removing session for username=" + session.getUsername());
088    }
089
090    @Override
091    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
092        if ("loaders".equals(extensionPoint)) {
093            registerLoader((DocumentLoaderDescriptor) contribution);
094        }
095    }
096
097    protected void registerLoader(DocumentLoaderDescriptor desc) {
098        loaders.put(desc.name, desc.instance);
099    }
100
101    @Override
102    public void fillProperties(DocumentModel doc, List<DocumentProperty> props, WSRemotingSession rs)
103            {
104        for (DocumentLoader loader : loaders.values()) {
105            loader.fillProperties(doc, props, rs);
106        }
107    }
108
109    @Override
110    public <T> T getAdapter(Class<T> adapter) {
111        if (DocumentLoader.class.isAssignableFrom(adapter)) {
112            return adapter.cast(this);
113        }
114        return super.getAdapter(adapter);
115    }
116}