All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.dream.protocol.messagePassing.overChannel.MessagePassingOverChannelImpl Maven / Gradle / Ivy

/**
 * Dream
 * Copyright (C) 2003-2004 INRIA Rhone-Alpes
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: [email protected]
 *
 * Initial developer(s): Matthieu Leclercq
 * Contributor(s): Pierre Garcia
 */

package org.objectweb.dream.protocol.messagePassing.overChannel;

import java.io.IOException;
import java.util.Map;

import org.objectweb.dream.cache.api.CacheManager;
import org.objectweb.dream.cache.api.UnbindManager;
import org.objectweb.dream.cache.lib.BasicCacheManagerAttributeController;
import org.objectweb.dream.dreamannotation.DreamComponent;
import org.objectweb.dream.dreamannotation.DreamLifeCycle;
import org.objectweb.dream.dreamannotation.DreamMonolog;
import org.objectweb.dream.dreamannotation.util.DreamLifeCycleType;
import org.objectweb.dream.message.MessageManagerType;
import org.objectweb.dream.protocol.ExportException;
import org.objectweb.dream.protocol.ExportIdentifier;
import org.objectweb.dream.protocol.IncomingPush;
import org.objectweb.dream.protocol.InvalidExportIdentifierException;
import org.objectweb.dream.protocol.channel.ChannelProtocol;
import org.objectweb.dream.protocol.messagePassing.MessagePassingOutgoingPush;
import org.objectweb.dream.protocol.messagePassing.MessagePassingProtocol;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.fraclet.annotation.annotations.Interface;
import org.objectweb.fractal.fraclet.annotation.annotations.Provides;
import org.objectweb.fractal.fraclet.annotation.annotations.Requires;
import org.objectweb.fractal.fraclet.annotation.annotations.Service;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

/**
 * Implementation of a message passing protocol using an underlying
 * {@link ChannelProtocol}. This protocol manage a cache of lower channel
 * sessions.
 */
@DreamComponent(controllerDesc = "activeDreamUnstoppablePrimitive")
@Provides(interfaces = { @Interface(name = MessagePassingProtocol.ITF_NAME, signature = MessagePassingProtocol.class) })
public class MessagePassingOverChannelImpl implements MessagePassingProtocol {
    // ------------------------------------------------------------------------
    // ---
    // Client interfaces
    // ------------------------------------------------------------------------
    // ---

    /**
     * Interface for the management of dream messages.
     */
    @Requires(name = "message-manager")
    protected MessageManagerType messageManagerItf;

    /**
     * Interface related to the export/bind pattern.
     */
    @Requires(name = ChannelProtocol.LOWER_PROTOCOL_ITF_NAME)
    protected ChannelProtocol lowerProtocolItf;

    /**
     * Interface used to bind, lookup, fix, touch an entry from the cache
     */
    @Requires(name = "cache-manager")
    protected CacheManager cacheManagerItf;

    /**
     * Interface used to unbind an entry from the cache
     */
    @Requires(name = "unbind-manager")
    protected UnbindManager unbindManagerItf;

    /**
     * Control interface used to manage parameters of the cache like the
     * maxSize, the currentSize...
     */
    @Requires(name = "cache-attributes")
    protected BasicCacheManagerAttributeController cacheAttributeControllerItf;

    /**
     * The manager of session
     */
    SessionManager sessionManager;

    // ------------------------------------------------------------------------
    // --
    // Services interfaces
    // ------------------------------------------------------------------------
    // --

    /**
     * Component reference
     */
    @Service
    protected Component weaveableC;

    /**
     * Logger of the component
     */
    @DreamMonolog()
    protected Logger logger;

    // ------------------------------------------------------------------------
    // ---
    // Implementation of the MessagePassingProtocol interface
    // ------------------------------------------------------------------------
    // ---

    /**
     * @see MessagePassingProtocol#export(IncomingPush, Map)
     */
    public MessagePassingOutgoingPush export(IncomingPush incomingPushItf, Map hints)
            throws ExportException {

        Object fromChunkName = (hints == null) ? null : hints.get(FROM_CHUNK_NAME);
        if (fromChunkName != null && !(fromChunkName instanceof String)) {
            throw new ExportException("Invalid '" + FROM_CHUNK_NAME
                    + "' hint value, must be a String");
        }

        this.logger.log(BasicLevel.DEBUG, "Export the channel for '" + fromChunkName + "'");

        sessionManager = new SessionManager(this, incomingPushItf, (String) fromChunkName);
        if (incomingPushItf != null) {
            ExportIdentifier id = lowerProtocolItf.export(sessionManager, hints);
            sessionManager.localExportIdentifier = id;
        }

        return sessionManager;
    }

    /**
     * @see org.objectweb.dream.protocol.Protocol#createExportIdentifier(Map,
     *      ExportIdentifier[])
     */
    public ExportIdentifier createExportIdentifier(Map info, ExportIdentifier[] next)
            throws InvalidExportIdentifierException {

        return lowerProtocolItf.createExportIdentifier(info, next);
    }

    /**
     * @see org.objectweb.dream.control.lifecycle.PrepareStopLifeCycleController#
     *      prepareStopFc()
     */
    @DreamLifeCycle(on = DreamLifeCycleType.PREPARE_TO_STOP)
    public void prepareStopFc() throws IllegalLifeCycleException {

        logger.log(BasicLevel.DEBUG, "Prepare stop : Close the protocol");

        if (this.sessionManager != null) {
            try {
                this.sessionManager.outgoingClose(null);

            } catch (IOException e) {
                this.logger.log(BasicLevel.WARN,
                        "Failed to prepare the stop of the protocol properly", e);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy