KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.WeakHashMap JavaDoc;
33
34 /**
35  * A Java based, static implementation of the {@link BindingBuilder} interface.
36  * This implementation produces standard Java code that binds components.
37  */

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

47   
48   private Map JavaDoc bindingLists = new WeakHashMap JavaDoc();
49   
50   /**
51    * Associates a set of String to each context. This set contains code
52    * fragments that have already been generated; it is used to avoid generating
53    * the same fragment several times.
54    */

55   
56   private Map JavaDoc itfSets = new WeakHashMap JavaDoc();
57   
58   // --------------------------------------------------------------------------
59
// Implementation of the BindingBuilder interface
60
// --------------------------------------------------------------------------
61

62   public void bindComponent (
63     final int type,
64     final Object JavaDoc clientComp,
65     final String JavaDoc clientItf,
66     final Object JavaDoc serverComp,
67     final String JavaDoc serverItf,
68     final Object JavaDoc context)
69   {
70     Binding b = new Binding();
71     b.client = (String JavaDoc)clientComp;
72     b.clientInterface = clientItf;
73     b.server = (String JavaDoc)serverComp;
74     b.serverInterface = serverItf;
75         
76     List JavaDoc bindings = (List JavaDoc)bindingLists.get(context);
77     if (bindings == null) {
78       bindings = new ArrayList JavaDoc();
79       bindingLists.put(context, bindings);
80     }
81     bindings.add(b);
82     Set JavaDoc itfs = (Set JavaDoc)itfSets.get(context);
83     if (itfs == null) {
84       itfs = new HashSet JavaDoc();
85       itfSets.put(context, itfs);
86     }
87
88     PrintWriter JavaDoc pw = (PrintWriter JavaDoc)((Map JavaDoc)context).get("printwriter");
89     
90     // generate code to create direct bindings between the primitive components
91
for (int i = 0; i < bindings.size(); ++i) {
92       b = (Binding)bindings.get(i);
93       String JavaDoc client = (String JavaDoc)b.client;
94       String JavaDoc clientInterface = b.clientInterface;
95       String JavaDoc server = (String JavaDoc)b.server;
96       String JavaDoc serverInterface = b.serverInterface;
97       top: while (server != null && !server.startsWith("P")) {
98         for (int j = 0; j < bindings.size(); ++j) {
99           b = (Binding)bindings.get(j);
100           if (b.client.equals(server) && b.clientInterface.equals(serverInterface)) {
101             server = b.server;
102             serverInterface = b.serverInterface;
103             continue top;
104           }
105         }
106         server = null;
107       }
108       if (server != null) {
109         if (client.startsWith("P") ){
110           pw.print(client);
111           pw.print(".bindFc(\"");
112           pw.print(clientInterface);
113           pw.print("\",");
114           pw.print(server);
115           pw.println(");");
116           bindings.remove(i--);
117         } else {
118           StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
119           buf.append(client);
120           buf.append(".put(\"");
121           buf.append(clientInterface);
122           buf.append("\",");
123           buf.append(server);
124           buf.append(");\n");
125           String JavaDoc s = buf.toString();
126           if (!itfs.contains(s)) {
127             itfs.add(s);
128             pw.write(s);
129           }
130         }
131       }
132     }
133   }
134
135   // --------------------------------------------------------------------------
136
// Inner classes
137
// --------------------------------------------------------------------------
138

139   static class Binding {
140     String JavaDoc client;
141     String JavaDoc clientInterface;
142     String JavaDoc server;
143     String JavaDoc serverInterface;
144   }
145 }
146
Popular Tags