org/objectweb/proactive/core/component/config/ComponentConfigurationHandler.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.config;
00032 
00033 import java.io.File;
00034 import java.io.IOException;
00035 import java.util.ArrayList;
00036 import java.util.HashMap;
00037 import java.util.List;
00038 import java.util.Map;
00039 
00040 import org.apache.log4j.Logger;
00041 import org.objectweb.proactive.core.ProActiveException;
00042 import org.objectweb.proactive.core.util.log.Loggers;
00043 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00044 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
00045 import org.objectweb.proactive.core.xml.handler.CollectionUnmarshaller;
00046 import org.objectweb.proactive.core.xml.handler.SingleValueUnmarshaller;
00047 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
00048 import org.objectweb.proactive.core.xml.io.Attributes;
00049 import org.objectweb.proactive.core.xml.io.StreamReader;
00050 import org.xml.sax.InputSource;
00051 import org.xml.sax.SAXException;
00052 
00053 
00059 public class ComponentConfigurationHandler extends AbstractUnmarshallerDecorator
00060     implements ComponentConfigurationConstants {
00061     Map controllers = new HashMap();
00062     List inputInterceptors = new ArrayList();
00063     List outputInterceptors = new ArrayList();
00064     public static Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS);
00065 
00066     //private ComponentsDescriptor componentsDescriptor;
00067     //private ComponentsCache componentsCache;
00068     //private HashMap componentTypes;
00069     public ComponentConfigurationHandler() {
00070         //super(true);
00071         addHandler(CONTROLLERS_ELEMENT, new ControllersHandler());
00072     }
00073 
00074     public List getInputInterceptors() {
00075         return inputInterceptors;
00076     }
00077 
00078     public List getOutputInterceptors() {
00079         return outputInterceptors;
00080     }
00081 
00082     public Map getControllers() {
00083         return controllers;
00084     }
00085 
00086     public static ComponentConfigurationHandler createComponentConfigurationHandler(
00087         String componentsConfigurationLocation)
00088         throws IOException, SAXException, ProActiveException {
00089         String url = null;
00090         try {
00091             InitialHandler initial_handler = new InitialHandler();
00092 
00093             if (ComponentConfigurationHandler.class.getResource(
00094                         componentsConfigurationLocation) != null) {
00095                 // it's in the classpath
00096                 url = ComponentConfigurationHandler.class.getResource(componentsConfigurationLocation)
00097                                                          .toString();
00098             } else {
00099                 // user-specified
00100                 url = new File(componentsConfigurationLocation).getAbsolutePath();
00101             }
00102             StreamReader stream_reader = new StreamReader(new InputSource(url),
00103                     initial_handler);
00104             stream_reader.read();
00105             return (ComponentConfigurationHandler) initial_handler.getResultObject();
00106         } catch (SAXException se) {
00107             logger.fatal(
00108                 "a problem occured while parsing the components descriptor \""+url+"\": " +
00109                 se.getMessage());
00110             se.printStackTrace();
00111             throw se;
00112         }
00113     }
00114 
00118     protected void notifyEndActiveHandler(String name,
00119         UnmarshallerHandler activeHandler) throws SAXException {
00120     }
00121 
00125     public void startContextElement(String name, Attributes attributes)
00126         throws SAXException {
00127     }
00128 
00129     public Object getResultObject() throws SAXException {
00130         return null;
00131     }
00132 
00133     //
00134     // -- INNER CLASSES ------------------------------------------------------
00135     //
00136     private static class InitialHandler extends AbstractUnmarshallerDecorator {
00137         private ComponentConfigurationHandler componentConfigurationHandler;
00138 
00139         private InitialHandler() {
00140             componentConfigurationHandler = new ComponentConfigurationHandler();
00141             this.addHandler(COMPONENT_CONFIGURATION_ELEMENT,
00142                 componentConfigurationHandler);
00143         }
00144 
00145         public Object getResultObject() throws org.xml.sax.SAXException {
00146             return componentConfigurationHandler;
00147         }
00148 
00149         protected void notifyEndActiveHandler(String name,
00150             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00151         }
00152 
00153         public void startContextElement(String name, Attributes attributes)
00154             throws SAXException {
00155         }
00156     }
00157 
00158     /******************************************************************************************************************/
00159     private class ControllersHandler extends CollectionUnmarshaller {
00160         public ControllersHandler() {
00161             addHandler(ComponentConfigurationConstants.CONTROLLER_ELEMENT,
00162                 new ControllerHandler());
00163         }
00164 
00165         public void startContextElement(String name, Attributes attributes)
00166             throws SAXException {
00167         }
00168 
00169         /* (non-Javadoc)
00170          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
00171          */
00172         protected void notifyEndActiveHandler(String name,
00173             UnmarshallerHandler activeHandler) throws SAXException {
00174             if (name.equals(ComponentConfigurationConstants.CONTROLLER_ELEMENT)) {
00175                 activeHandler.getResultObject();
00176             }
00177         }
00178 
00179         /* (non-Javadoc)
00180          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
00181          */
00182         public Object getResultObject() throws SAXException {
00183             return null;
00184         }
00185     }
00186 
00187     /******************************************************************************************************************/
00188     public class ControllerHandler extends AbstractUnmarshallerDecorator {
00189         String interfaceSignature = null;
00190         String implementationSignature = null;
00191         boolean inputInterception = false;
00192         boolean outputInterception = false;
00193 
00194         public ControllerHandler() {
00195             UnmarshallerHandler singleValueHandler = new SingleValueUnmarshaller();
00196             addHandler(INTERFACE_ELEMENT, singleValueHandler);
00197             addHandler(IMPLEMENTATION_ELEMENT, singleValueHandler);
00198         }
00199 
00200         public void startContextElement(String name, Attributes attributes)
00201             throws SAXException {
00202             if ("true".equals(attributes.getValue(INPUT_INTERCEPTOR_ATTRIBUTE))) {
00203                 inputInterception = true;
00204             }
00205             if ("true".equals(attributes.getValue(OUTPUT_INTERCEPTOR_ATTRIBUTE))) {
00206                 outputInterception = true;
00207             }
00208         }
00209 
00210         protected void notifyEndActiveHandler(String name,
00211             UnmarshallerHandler activeHandler) throws SAXException {
00212             if (name.equals(INTERFACE_ELEMENT)) {
00213                 interfaceSignature = (String) activeHandler.getResultObject();
00214             }
00215             if (name.equals(IMPLEMENTATION_ELEMENT)) {
00216                 implementationSignature = (String) activeHandler.getResultObject();
00217             }
00218         }
00219 
00220         public Object getResultObject() throws SAXException {
00221             controllers.put(interfaceSignature, implementationSignature);
00222             if (inputInterception) {
00223                 inputInterceptors.add(implementationSignature);
00224             }
00225             if (outputInterception) {
00226                 outputInterceptors.add(implementationSignature);
00227             }
00228             interfaceSignature = null;
00229             implementationSignature = null;
00230             inputInterception = false;
00231             outputInterception = false;
00232             return null;
00233         }
00234     }
00235 }

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