org/objectweb/proactive/core/component/Bindings.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;
00032 
00033 import java.io.Serializable;
00034 import java.util.ArrayList;
00035 import java.util.HashMap;
00036 import java.util.List;
00037 import java.util.Map;
00038 
00039 import org.objectweb.fractal.api.NoSuchInterfaceException;
00040 import org.objectweb.fractal.api.type.InterfaceType;
00041 import org.objectweb.proactive.core.ProActiveRuntimeException;
00042 
00043 
00058 public class Bindings implements Serializable {
00059     //   In case of collective bindings, the interfaces of the collection can be : 
00060     //    1. named (as in Fractal 2.0 spec) : they are mapped in clientInterfaceBindings according to their name
00061     //    2. anonymous : they are put in a list which is mapped in clientInterfaceBindings with the type name of the collective interface 
00062     private Map normalBindings;
00063     private Map exportBindings;
00064 
00065     // key = interfaceName ; value = binding
00066     // if collective binding : key = interfaceName ; value = Vector (Binding objects)
00067     public Bindings() {
00068         normalBindings = new HashMap();
00069         exportBindings = new HashMap();
00070     }
00071 
00075     public void add(Binding binding) {
00076         try {
00077             InterfaceType client_itf_type = (InterfaceType) binding.getClientInterface()
00078                                                                    .getFcItfType();
00079 
00080             // export bindings
00081             if (!client_itf_type.isFcClientItf()) {
00082                 if (Fractive.getComponentParametersController(
00083                             binding.getClientInterface().getFcItfOwner())
00084                                 .getComponentParameters().getHierarchicalType()
00085                                 .equals(Constants.PARALLEL)) {
00086                     addCollectiveBindingOnInternalClientItf(binding);
00087                 } else {
00088                     exportBindings.put(binding.getClientInterfaceName(), binding);
00089                 }
00090             } else {
00091                 // normal bindings
00092                 if (client_itf_type.isFcCollectionItf()) {
00093                     addCollectiveBindingOnExternalClientItf(binding);
00094                 } else {
00095                     normalBindings.put(binding.getClientInterfaceName(), binding);
00096                 }
00097             }
00098         } catch (NoSuchInterfaceException nsie) {
00099             throw new ProActiveRuntimeException("interface not found : " +
00100                 nsie.getMessage());
00101         }
00102     }
00103 
00104     // returns either a Binding or a List of Binding objects (collection interface case)
00105 
00111     public Object remove(String clientItfName) {
00112         if (normalBindings.containsKey(clientItfName)) {
00113             return normalBindings.remove(clientItfName);
00114         }
00115         if (exportBindings.containsKey(clientItfName)) {
00116             return exportBindings.remove(clientItfName);
00117         }
00118         return null;
00119     }
00120 
00126     public Object get(String clientItfName) {
00127         if (normalBindings.containsKey(clientItfName)) {
00128             return normalBindings.get(clientItfName);
00129         }
00130         if (exportBindings.containsKey(clientItfName)) {
00131             return exportBindings.get(clientItfName);
00132         }
00133         return null;
00134     }
00135 
00141     public boolean containsBindingOn(String clientItfName) {
00142         return (normalBindings.containsKey(clientItfName) ||
00143         exportBindings.containsKey(clientItfName));
00144     }
00145 
00151     public String[] getExternalClientBindings() {
00152         return (String[]) normalBindings.keySet().toArray(new String[normalBindings.keySet()
00153                                                                                    .size()]);
00154     }
00155 
00160     private static void addCollectiveBinding(Map bindingsTable, Binding binding) {
00161         String client_itf_name = binding.getClientInterfaceName();
00162         if (binding.getClientInterface().getFcItfName().equals(client_itf_name)) {
00163             if (bindingsTable.containsKey(client_itf_name)) {
00164                 // there should be a List for containing the bindings associated
00165                 ((List) bindingsTable.get(client_itf_name)).add(binding);
00166             } else { // we create a List for keeping the bindings
00167                 ArrayList bindings_collection = new ArrayList();
00168                 bindings_collection.add(binding);
00169                 bindingsTable.put(client_itf_name, bindings_collection);
00170             }
00171         } else {
00172             bindingsTable.put(client_itf_name, binding);
00173         }
00174     }
00175 
00180     private void addCollectiveBindingOnExternalClientItf(Binding binding) {
00181         addCollectiveBinding(normalBindings, binding);
00182     }
00183 
00187     private void addCollectiveBindingOnInternalClientItf(Binding binding) {
00188         if (exportBindings == null) {
00189             exportBindings = new HashMap();
00190         }
00191         addCollectiveBinding(exportBindings, binding);
00192     }
00193 }

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