org/objectweb/proactive/core/component/controller/ProActiveLifeCycleControllerImpl.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.component.controller;
00032 
00033 import java.io.Serializable;
00034 
00035 import org.apache.log4j.Logger;
00036 import org.objectweb.fractal.api.Component;
00037 import org.objectweb.fractal.api.Interface;
00038 import org.objectweb.fractal.api.NoSuchInterfaceException;
00039 import org.objectweb.fractal.api.control.ContentController;
00040 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
00041 import org.objectweb.fractal.api.control.LifeCycleController;
00042 import org.objectweb.fractal.api.factory.InstantiationException;
00043 import org.objectweb.fractal.api.type.ComponentType;
00044 import org.objectweb.fractal.api.type.InterfaceType;
00045 import org.objectweb.fractal.api.type.TypeFactory;
00046 import org.objectweb.fractal.util.Fractal;
00047 import org.objectweb.proactive.core.ProActiveRuntimeException;
00048 import org.objectweb.proactive.core.component.Constants;
00049 import org.objectweb.proactive.core.component.Fractive;
00050 import org.objectweb.proactive.core.component.group.ProxyForComponentInterfaceGroup;
00051 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
00052 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
00053 import org.objectweb.proactive.core.component.type.ProActiveTypeFactory;
00054 import org.objectweb.proactive.core.component.type.ProActiveTypeFactoryImpl;
00055 import org.objectweb.proactive.core.util.log.Loggers;
00056 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00057 
00058 
00065 public class ProActiveLifeCycleControllerImpl
00066     extends AbstractProActiveController implements ProActiveLifeCycleController,
00067         Serializable {
00068     static final Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS_CONTROLLERS);
00069     protected String fcState = LifeCycleController.STOPPED;
00070 
00071     public ProActiveLifeCycleControllerImpl(Component owner) {
00072         super(owner);
00073     }
00074 
00075     protected void setControllerItfType() {
00076         try {
00077             setItfType(ProActiveTypeFactoryImpl.instance()
00078                                                .createFcItfType(Constants.LIFECYCLE_CONTROLLER,
00079                     ProActiveLifeCycleController.class.getName(),
00080                     TypeFactory.SERVER, TypeFactory.MANDATORY,
00081                     TypeFactory.SINGLE));
00082         } catch (InstantiationException e) {
00083             throw new ProActiveRuntimeException("cannot create controller " +
00084                 this.getClass().getName());
00085         }
00086     }
00087 
00088     /*
00089     * @see org.objectweb.fractal.api.control.LifeCycleController#getFcState()
00090     */
00091     public String getFcState() {
00092         return fcState;
00093         //        return getRequestQueue().isStarted() ? LifeCycleController.STARTED
00094         //                                             : LifeCycleController.STOPPED;
00095     }
00096 
00097     /*
00098      *@see org.objectweb.fractal.api.control.LifeCycleController#startFc()
00099      * recursive if composite
00100      * ( recursivity is allowed here as we do not implement sharing )
00101      */
00102     public void startFc() {
00103         try {
00104             //check that all mandatory client interfaces are bound
00105             //            Object[] itfs = getFcItfOwner().getFcInterfaces();
00106             InterfaceType[] itfTypes = ((ComponentType) getFcItfOwner()
00107                                                             .getFcType()).getFcInterfaceTypes();
00108 
00109             for (int i = 0; i < itfTypes.length; i++) {
00110                 if (itfTypes[i].isFcClientItf() &&
00111                         !itfTypes[i].isFcOptionalItf()) {
00112                     if (itfTypes[i].isFcCollectionItf()) {
00113                         // look for collection members
00114                         Object[] itfs = owner.getFcInterfaces();
00115                         for (int j = 0; j < itfs.length; j++) {
00116                             Interface itf = (Interface) itfs[j];
00117                             if (itf.getFcItfName()
00118                                        .startsWith(itfTypes[i].getFcItfName())) {
00119                                 if (itf.getFcItfName()
00120                                            .equals(itfTypes[i].getFcItfName())) {
00121                                     throw new IllegalLifeCycleException(
00122                                         "invalid collection interface name at runtime (suffix required)");
00123                                 }
00124                                 if (Fractal.getBindingController(owner)
00125                                                .lookupFc(itf.getFcItfName()) == null) {
00126                                     throw new IllegalLifeCycleException(
00127                                         "compulsory collection client interface " +
00128                                         itfTypes[i].getFcItfName() +
00129                                         " in component " +
00130                                         Fractal.getNameController(
00131                                             getFcItfOwner()).getFcName() +
00132                                         " is not bound. ");
00133                                 }
00134                             }
00135                         }
00136                     } else if (((ProActiveInterfaceType) itfTypes[i]).isFcMulticastItf() &&
00137                             !!itfTypes[i].isFcOptionalItf()) {
00138                         ProxyForComponentInterfaceGroup clientSideProxy = Fractive.getMulticastController(getFcItfOwner())
00139                                                                                   .lookupFcMulticast(itfTypes[i].getFcItfName());
00140 
00141                         //                        if (clientSideProxy == null) {
00142                         //                              System.out.println("client side proxy is null from " + ProActiveRuntimeImpl.getProActiveRuntime().getURL());
00143                         //                        }
00144                         if (clientSideProxy.getDelegatee().isEmpty()) {
00145                             throw new IllegalLifeCycleException(
00146                                 "compulsory multicast client interface " +
00147                                 itfTypes[i].getFcItfName() + " in component " +
00148                                 Fractal.getNameController(getFcItfOwner())
00149                                        .getFcName() + " is not bound. ");
00150                         }
00151                     } else if ((((ProActiveInterfaceType) itfTypes[i]).getFcCardinality()
00152                                      .equals(ProActiveTypeFactory.SINGLETON_CARDINALITY) ||
00153                             ((ProActiveInterfaceType) itfTypes[i]).getFcCardinality()
00154                                  .equals(ProActiveTypeFactory.GATHER_CARDINALITY)) &&
00155                             (Fractal.getBindingController(getFcItfOwner())
00156                                         .lookupFc(itfTypes[i].getFcItfName()) == null)) {
00157                         throw new IllegalLifeCycleException(
00158                             "compulsory client interface " +
00159                             itfTypes[i].getFcItfName() + " in component " +
00160                             Fractal.getNameController(getFcItfOwner())
00161                                    .getFcName() + " is not bound. ");
00162                     }
00163 
00164                     // TODO check compulsory client gathercast interface in composite
00165                     // TODO add a test for client gathercast interface in composite
00166                 }
00167             }
00168 
00169             String hierarchical_type = Fractive.getComponentParametersController((ProActiveComponent) getFcItfOwner())
00170                                                .getComponentParameters()
00171                                                .getHierarchicalType();
00172             if (hierarchical_type.equals(Constants.COMPOSITE)) {
00173                 // start all inner components
00174                 Component[] inner_components = Fractal.getContentController(getFcItfOwner())
00175                                                       .getFcSubComponents();
00176                 if (inner_components != null) {
00177                     for (int i = 0; i < inner_components.length; i++) {
00178                         ((LifeCycleController) inner_components[i].getFcInterface(Constants.LIFECYCLE_CONTROLLER)).startFc();
00179                     }
00180                 }
00181             }
00182 
00183             //getRequestQueue().start();
00184             fcState = LifeCycleController.STARTED;
00185             if (logger.isDebugEnabled()) {
00186                 logger.debug("started " +
00187                     ((ComponentParametersController) getFcItfOwner()
00188                      .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
00189                      .getName());
00190             }
00191         } catch (NoSuchInterfaceException nsie) {
00192             logger.error("interface not found : " + nsie.getMessage());
00193             nsie.printStackTrace();
00194         } catch (IllegalLifeCycleException ilce) {
00195             logger.error("illegal life cycle operation : " + ilce.getMessage());
00196             ilce.printStackTrace();
00197         }
00198     }
00199 
00200     /*
00201      *@see org.objectweb.fractal.api.control.LifeCycleController#stopFc()
00202      * recursive if composite
00203      */
00204     public void stopFc() {
00205         try {
00206             String hierarchical_type = ((ComponentParametersController) getFcItfOwner()
00207                                         .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
00208                                         .getHierarchicalType();
00209             if (hierarchical_type.equals(Constants.COMPOSITE)) {
00210                 // stop all inner components
00211                 Component[] inner_components = ((ContentController) getFcItfOwner()
00212                                                                         .getFcInterface(Constants.CONTENT_CONTROLLER)).getFcSubComponents();
00213                 if (inner_components != null) {
00214                     for (int i = 0; i < inner_components.length; i++) {
00215                         ((LifeCycleController) inner_components[i].getFcInterface(Constants.LIFECYCLE_CONTROLLER)).stopFc();
00216                     }
00217                 }
00218             }
00219 
00220             //getRequestQueue().stop();
00221             fcState = LifeCycleController.STOPPED;
00222             if (logger.isDebugEnabled()) {
00223                 logger.debug("stopped" +
00224                     ((ComponentParametersController) getFcItfOwner()
00225                      .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
00226                      .getName());
00227             }
00228         } catch (NoSuchInterfaceException nsie) {
00229             logger.error("interface not found : " + nsie.getMessage());
00230         } catch (IllegalLifeCycleException ilce) {
00231             logger.error("illegal life cycle operation : " + ilce.getMessage());
00232         }
00233     }
00234 
00235     /*
00236      * @see org.objectweb.proactive.core.component.controller.ProActiveLifeCycleController#getFcState(short)
00237      */
00238     public String getFcState(short priority) {
00239         return getFcState();
00240     }
00241 
00242     /*
00243      * @see org.objectweb.proactive.core.component.controller.ProActiveLifeCycleController#startFc(short)
00244      */
00245     public void startFc(short priority) {
00246         startFc();
00247     }
00248 
00249     /*
00250      * @see org.objectweb.proactive.core.component.controller.ProActiveLifeCycleController#stopFc(short)
00251      */
00252     public void stopFc(short priority) {
00253         stopFc();
00254     }
00255 }

Generated on Mon Jan 22 15:16:06 2007 for ProActive by  doxygen 1.5.1