KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > bindings > JavaBindingBuilder


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.bindings;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.WeakHashMap JavaDoc;
31
32 /**
33  * A Java based implementation of the {@link BindingBuilder} interface.
34  */

35
36 public class JavaBindingBuilder implements BindingBuilder {
37
38   /**
39    * Associates a list of bindings to each context. The contexts are the last
40    * argument of the bindComponent method. The list of bindings for a context
41    * is the list of the primitive bindings that have been build so far in this
42    * context, as a list of Binding objects.
43    */

44     
45   private Map JavaDoc bindingLists = new WeakHashMap JavaDoc();
46   
47   // --------------------------------------------------------------------------
48
// Implementation of the BindingBuilder interface
49
// --------------------------------------------------------------------------
50

51   public void bindComponent (
52     final int type,
53     final Object JavaDoc clientComp,
54     final String JavaDoc clientItf,
55     final Object JavaDoc serverComp,
56     final String JavaDoc serverItf,
57     final Object JavaDoc context) throws Exception JavaDoc
58   {
59     Binding b = new Binding();
60     b.client = clientComp;
61     b.clientInterface = clientItf;
62     b.server = serverComp;
63     b.serverInterface = serverItf;
64     
65     List JavaDoc bindings = (List JavaDoc)bindingLists.get(context);
66     if (bindings == null) {
67       bindings = new ArrayList JavaDoc();
68       bindingLists.put(context, bindings);
69     }
70     bindings.add(b);
71         
72     for (int i = 0; i < bindings.size(); ++i) {
73       b = (Binding)bindings.get(i);
74       Object JavaDoc client = b.client;
75       String JavaDoc clientInterface = b.clientInterface;
76       Object JavaDoc server = b.server;
77       String JavaDoc serverInterface = b.serverInterface;
78       top: while (server != null && server instanceof Map JavaDoc) {
79         for (int j = 0; j < bindings.size(); ++j) {
80           b = (Binding)bindings.get(j);
81           if (b.client == server && b.clientInterface.equals(serverInterface)) {
82             server = b.server;
83             serverInterface = b.serverInterface;
84             continue top;
85           }
86         }
87         server = null;
88       }
89       if (server != null) {
90         if (client instanceof Map JavaDoc) {
91           ((Map JavaDoc)client).put(clientInterface, server);
92         } else {
93           Method JavaDoc m = client.getClass().getMethod("bindFc", new Class JavaDoc[] {
94             String JavaDoc.class, Object JavaDoc.class
95           });
96           m.invoke(client, new Object JavaDoc[] { clientInterface, server });
97           bindings.remove(i--);
98         }
99       }
100     }
101   }
102   
103   // --------------------------------------------------------------------------
104
// Inner classes
105
// --------------------------------------------------------------------------
106

107   static class Binding {
108     Object JavaDoc client;
109     String JavaDoc clientInterface;
110     Object JavaDoc server;
111     String JavaDoc serverInterface;
112   }
113 }
114
Popular Tags