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

org.objectweb.dream.router.AbstractRouterImpl 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): Vivien Quema, Romain Lenglet
 */

package org.objectweb.dream.router;

import java.util.HashMap;
import java.util.Map;

import org.objectweb.dream.Push;
import org.objectweb.dream.PushException;
import org.objectweb.dream.message.Message;
import org.objectweb.dream.message.MessageManagerType;
import org.objectweb.fractal.fraclet.annotation.annotations.type.Cardinality;
import org.objectweb.fractal.fraclet.annotation.annotations.type.Contingency;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.dream.dreamannotation.DreamComponent;
import org.objectweb.dream.dreamannotation.DreamMonolog;
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;

/**
 * Abstract implementation of a basic Push/Push router. A Push/Push router
 * routes messages received on its Push input to one of its Push outputs. A
 * Push/Push router has also a defaultOutPushItf that can be bound
 * to route messages for which no routes are found. 
* Outputs are stored in the outPushMap Map. The output * corresponding to an incoming message is retrieved using the abstract {@link * #getOutput(Message)}method. To avoid unnecessary building of routes the * {@link AbstractRouterImpl#getOutput(Message)}can test the boolean * initialized which is set to false each time the * router is bound to a new output. */ @DreamComponent() @Provides(interfaces = @Interface(name = "in-push", signature = Push.class)) public abstract class AbstractRouterImpl implements Push { /** true if the route table is initialized. */ protected boolean initialized = false; // ------------------------------------------------------------------------ // --- // Client interfaces // ------------------------------------------------------------------------ // --- /** The name of the default {@link Push}client interface. */ public static final String DEFAULT_OUT_PUSH_ITF_NAME = "default-out-push"; /** * Default Push interface to be used if no Push interface can be found in * outPushTable. */ @Requires(name = DEFAULT_OUT_PUSH_ITF_NAME, contingency = Contingency.OPTIONAL) protected Push defaultOutPushItf = null; /** Map of out-push interfaces binded to this router */ @Requires(name = "out-push", cardinality = Cardinality.COLLECTION) protected Map outPushMap = new HashMap(); /** the message manager client interface of this component */ @Requires(name = "message-manager") protected MessageManagerType messageManagerItf; // ------------------------------------------------------------------------ // -- // Services interfaces // ------------------------------------------------------------------------ // -- /** * Component reference */ @Service Component weaveableC; /** * Logger of the component */ @DreamMonolog() protected Logger logger; // ------------------------------------------------------------------------ // --- // Abstract method // ------------------------------------------------------------------------ // --- protected abstract Push getOutput(Message message) throws PushException; // ------------------------------------------------------------------------ // --- // Implementation of the Push interface // ------------------------------------------------------------------------ // --- /** * Sends the incoming message to one of the router's output. The appropriate * output is retrieved using the {@link #getOutput(Message)}method. If this * method returns null, the defaultOutPushItf is * used if it is bound. * * @param message * the pushed message * @throws PushException * if no route can be found, or thrown by client push. * @see Push#push(Message) */ public void push(Message message) throws PushException { Push outPush = getOutput(message); if (outPush == null) { if (defaultOutPushItf == null) { logger.log(BasicLevel.ERROR, "No route for message " + message + ". Drop message."); messageManagerItf.deleteMessage(message); return; } defaultOutPushItf.push(message); } else { outPush.push(message); } } // ------------------------------------------------------------------------ // --- // Overrideing of the BindingController interface generated by // Fraclet to manage reconfguration of the router with the' initialized' // variable // ------------------------------------------------------------------------ // --- /** * @see org.objectweb.fractal.api.control.BindingController#bindFc(String, * Object) */ public synchronized void bindFc(String clientItfName, Object serverItf) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException { if (clientItfName.equals("component")) { weaveableC = ((Component) (serverItf)); } else if (clientItfName.startsWith(OUT_PUSH_ITF_NAME) && clientItfName.length() > OUT_PUSH_ITF_NAME.length()) { outPushMap.put(clientItfName, (Push) serverItf); initialized = false; } else if (clientItfName.equals(DEFAULT_OUT_PUSH_ITF_NAME)) { defaultOutPushItf = (Push) serverItf; initialized = false; } else if (clientItfName.equals("message-manager")) { messageManagerItf = (MessageManagerType) serverItf; } throw new NoSuchInterfaceException(("Client interface \'" + clientItfName) + "\' is undefined."); } /** * @see org.objectweb.fractal.api.control.BindingController#unbindFc(String) */ public synchronized void unbindFc(String clientItfName) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException { if (clientItfName.startsWith(OUT_PUSH_ITF_NAME)) { outPushMap.remove(clientItfName); initialized = false; } else if (clientItfName.equals(DEFAULT_OUT_PUSH_ITF_NAME)) { defaultOutPushItf = null; initialized = false; } throw new NoSuchInterfaceException(("Client interface \'" + clientItfName) + "\' is undefined."); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy