001/*
002 * (C) Copyright 2006-2016 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.webengine.session;
020
021import java.io.Serializable;
022
023/**
024 * A stateful session component.
025 * <p>
026 * A component is instantiate and activated the first time it is requested. It is destroyed when the user session ends.
027 * <p>
028 * Stateful components are not necessarily thread safe.
029 *
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public interface Component extends Serializable {
033
034    /**
035     * The component was instantiated by the given session.
036     * <p>
037     * This method should initialize the component. After returning the component will become visible in the session.
038     *
039     * @param session the user session that created the component
040     * @param name the name under this component is registered. Can be null for unnamed component.
041     * @throws InvalidStateException if the component is not in an appropriate life cycle state
042     * @throws SessionException an internal error occurred
043     */
044    void initialize(UserSession session, String name) throws SessionException;
045
046    /**
047     * Destroy this component. This is called by the when the owning session is about to be destroyed. The component
048     * should release any allocated resources.
049     *
050     * @param session the session owning this component
051     * @throws InvalidStateException if the component is not in an appropriate life cycle state
052     * @throws SessionException an internal error occurred
053     */
054    void destroy(UserSession session) throws SessionException;
055
056    /**
057     * Get the component name if any. A component may be initialized under a name. For singleton components no name is
058     * needed so this method might return null.
059     *
060     * @return the name if any otherwise null
061     */
062    String getName();
063
064    /**
065     * Checks whether this component was initialized and can be used.
066     */
067    boolean isLive();
068
069}