001/*
002 * (C) Copyright 2006-2008 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 *     Razvan Caraghin
018 *     Olivier Grisel
019 *     Thierry Delprat
020 *     Florent Guillaume
021 */
022
023package org.nuxeo.ecm.webapp.delegate;
024
025import static org.jboss.seam.ScopeType.CONVERSATION;
026
027import java.io.Serializable;
028import java.util.HashMap;
029import java.util.Map;
030import java.util.Map.Entry;
031
032import javax.annotation.security.PermitAll;
033import javax.security.auth.login.LoginContext;
034import javax.security.auth.login.LoginException;
035
036import org.apache.commons.logging.Log;
037import org.apache.commons.logging.LogFactory;
038import org.jboss.seam.Component;
039import org.jboss.seam.annotations.Destroy;
040import org.jboss.seam.annotations.Name;
041import org.jboss.seam.annotations.Scope;
042import org.jboss.seam.annotations.Unwrap;
043import org.jboss.seam.contexts.Lifecycle;
044import org.nuxeo.ecm.core.api.CoreInstance;
045import org.nuxeo.ecm.core.api.CoreSession;
046import org.nuxeo.ecm.core.api.repository.Repository;
047import org.nuxeo.ecm.platform.util.RepositoryLocation;
048import org.nuxeo.runtime.api.Framework;
049
050/**
051 * Acquires a {@link CoreSession} connection.
052 *
053 * @author Razvan Caraghin
054 * @author Olivier Grisel
055 * @author Thierry Delprat
056 * @author Florent Guillaume
057 */
058@Name("documentManager")
059@Scope(CONVERSATION)
060public class DocumentManagerBusinessDelegate implements Serializable {
061
062    private static final long serialVersionUID = 1L;
063
064    private static final Log log = LogFactory.getLog(DocumentManagerBusinessDelegate.class);
065
066    /**
067     * Map holding the open session for each repository location.
068     */
069    protected final Map<RepositoryLocation, CoreSession> sessions = new HashMap<RepositoryLocation, CoreSession>();
070
071    public void initialize() {
072        log.debug("Seam component initialized...");
073    }
074
075    @Unwrap
076    public CoreSession getDocumentManager() {
077        /*
078         * Explicit lookup, as this method is the only user of the Seam component. Also, in some cases (Seam remoting),
079         * it seems that the injection is not done correctly.
080         */
081        RepositoryLocation currentServerLocation = (RepositoryLocation) Component.getInstance("currentServerLocation");
082        return getDocumentManager(currentServerLocation);
083    }
084
085    public CoreSession getDocumentManager(RepositoryLocation serverLocation) {
086
087        if (serverLocation == null) {
088            /*
089             * currentServerLocation (factory in ServerContextBean) is set through navigationContext, which itself
090             * injects documentManager, so it will be null the first time.
091             */
092            return null;
093        }
094
095        CoreSession session = sessions.get(serverLocation);
096        if (session == null) {
097            if (Lifecycle.isDestroying()) {
098                /*
099                 * During Seam component destroy phases, we don't want to recreate a core session just for injection.
100                 * This happens during logout when the session context is destroyed; we don't want to cause EJB calls in
101                 * this case as the authentication wouldn't work.
102                 */
103                return null;
104            }
105            String serverName = serverLocation.getName();
106            session = CoreInstance.openCoreSession(serverName);
107            log.debug("Opened session for repository " + serverName);
108            sessions.put(serverLocation, session);
109        }
110        return session;
111    }
112
113    @Destroy
114    @PermitAll
115    public void remove() {
116        LoginContext lc = null;
117        try {
118            try {
119                if (Framework.getRuntime() != null) {
120                    lc = Framework.login();
121                }
122            } catch (LoginException le) {
123                log.error("Unable to login as System", le);
124                log.warn("...try to feed CoreSession(s) without system login ...");
125            }
126            for (Entry<RepositoryLocation, CoreSession> entry : sessions.entrySet()) {
127                String serverName = entry.getKey().getName();
128                CoreSession session = entry.getValue();
129                session.close();
130                log.debug("Closed session for repository " + serverName);
131            }
132        } finally {
133            if (lc != null) {
134                try {
135                    lc.logout();
136                } catch (LoginException lo) {
137                    log.error("Error when logout", lo);
138                }
139            }
140            sessions.clear();
141        }
142    }
143}