KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > Bindings


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2004 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.component;
32
33 import org.objectweb.fractal.api.NoSuchInterfaceException;
34 import org.objectweb.fractal.api.type.InterfaceType;
35
36 import org.objectweb.proactive.core.ProActiveRuntimeException;
37 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
38
39 import java.io.Serializable JavaDoc;
40
41 import java.util.Hashtable JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.Vector JavaDoc;
44
45
46 /**
47  * A bindings container.
48  * This class stores the following bindings for a given component :
49  * - thisComponent.clientInterface --> serverComponent.serverInterface
50  * (it also takes in charge collective bindings, ie 1 client to serveral servers)
51  * - thisParallelComponent.serverInterface --> serverComponents.serverInterface
52  * (in the case of a parallel component, requests on a server port are forwarded to
53  * the inner components)
54  *
55  *
56  *
57  * @author Matthieu Morel
58  */

59 public class Bindings implements Serializable JavaDoc {
60     private Hashtable JavaDoc clientInterfaceBindings;
61     private Hashtable JavaDoc parallelInternalClientInterfaceBindings;
62
63     // key = interfaceName ; value = binding
64
// if collective binding : key = interfaceName ; value = Vector (Binding objects)
65
public Bindings() {
66         clientInterfaceBindings = new Hashtable JavaDoc();
67     }
68
69     /**
70      * @param binding the binding to add
71      */

72     public void add(Binding binding) {
73         try {
74             InterfaceType client_itf_type = (InterfaceType) binding.getClientInterface()
75                                                                    .getFcItfType();
76             if (client_itf_type.isFcCollectionItf()) {
77                 addCollectiveBindingOnExternalClientItf(binding);
78             } else if (((ComponentParametersController) binding.getClientInterface()
79                             .getFcItfOwner().getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
80                             .getHierarchicalType().equals(Constants.PARALLEL)) {
81                 addCollectiveBindingOnInternalClientItf(binding);
82             } else {
83                 clientInterfaceBindings.put(client_itf_type.getFcItfName(),
84                     binding);
85             }
86         } catch (NoSuchInterfaceException nsie) {
87             throw new ProActiveRuntimeException("interface not found : " +
88                 nsie.getMessage());
89         }
90     }
91
92     // returns either a Binding or a Vector of Binding objects (collection interface case)
93

94     /**
95      * removes the binding on the given client interface
96      */

97     public Object JavaDoc remove(String JavaDoc clientItfName) {
98         return clientInterfaceBindings.remove(clientItfName);
99     }
100
101     public Object JavaDoc get(String JavaDoc clientItfName) {
102         return clientInterfaceBindings.get(clientItfName);
103     }
104
105     /**
106      * tests if binding exists on the given interface
107      * @param clientItfName the client inteface to check
108      * @return true if binding exists
109      */

110     public boolean containsBindingOn(String JavaDoc clientItfName) {
111         return clientInterfaceBindings.containsKey(clientItfName);
112     }
113
114     // /**
115
// * returns all the bindings, including the bindings for the
116
// * collective interfaces (meaning there can be several Binding objects
117
// * with the same client interface)
118
// *
119
// * @return all the bindings
120
// */
121
// public Binding[] getBindings() {
122
// Vector list_of_bindings = new Vector();
123
// Enumeration enum = clientInterfaceBindings.elements();
124
// while (enum.hasMoreElements()) {
125
// Object elem = enum.nextElement();
126
// if (elem instanceof Collection) {
127
// // a collective binding : add all the elements of the corresponding collection
128
// list_of_bindings.addAll((Collection) elem);
129
// } else {
130
// list_of_bindings.addElement(elem);
131
// }
132
// }
133
//
134
// list_of_bindings.trimToSize();
135
// return (Binding[]) (list_of_bindings.toArray(new Binding[list_of_bindings.size()]));
136
// }
137

138     /**
139      * Returns the names of the external client bindings for this component.
140      * In case of a collective interface, the names of each of its constituing interfaces are not returned ;
141      * only the name of the collective interface is returned.
142      */

143     public String JavaDoc[] getExternalClientBindings() {
144         return (String JavaDoc[]) clientInterfaceBindings.keySet().toArray(new String JavaDoc[clientInterfaceBindings.keySet()
145                                                                                                      .size()]);
146     }
147
148     private void addCollectiveBinding(Map JavaDoc bindingsTable, Binding binding) {
149         String JavaDoc clientItfName = binding.getClientInterface().getFcItfName();
150         if (bindingsTable.containsKey(clientItfName)) {
151             // there should be a Vector for containing the bindings associated
152
((Vector JavaDoc) bindingsTable.get(clientItfName)).add(binding);
153         } else { // we create a Vector for keeping the bindings
154
Vector JavaDoc bindings_collection = new Vector JavaDoc();
155             bindings_collection.add(binding);
156             bindingsTable.put(clientItfName, bindings_collection);
157         }
158     }
159
160     private void addCollectiveBindingOnExternalClientItf(Binding binding) {
161         addCollectiveBinding(clientInterfaceBindings, binding);
162     }
163
164     private void addCollectiveBindingOnInternalClientItf(Binding binding) {
165         if (parallelInternalClientInterfaceBindings == null) {
166             parallelInternalClientInterfaceBindings = new Hashtable JavaDoc();
167         }
168         addCollectiveBinding(parallelInternalClientInterfaceBindings, binding);
169     }
170 }
171
Popular Tags