KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.fractal.adl.ADLException;
27 import org.objectweb.fractal.adl.Definition;
28 import org.objectweb.fractal.adl.AbstractLoader;
29 import org.objectweb.fractal.adl.Node;
30 import org.objectweb.fractal.adl.components.Component;
31 import org.objectweb.fractal.adl.components.ComponentContainer;
32 import org.objectweb.fractal.adl.interfaces.Interface;
33 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
34
35 import java.util.HashMap JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Set JavaDoc;
39
40 /**
41  * A {@link org.objectweb.fractal.adl.Loader} to check {@link Binding} nodes
42  * in definitions. This loader checks that the from and to interfaces
43  * specified in these nodes exist.
44  */

45
46 public class BindingLoader extends AbstractLoader {
47
48   // --------------------------------------------------------------------------
49
// Implementation of the Loader interface
50
// --------------------------------------------------------------------------
51

52   public Definition load (final String JavaDoc name, final Map JavaDoc context)
53     throws ADLException
54   {
55     Definition d = clientLoader.load(name, context);
56     checkNode(d, context);
57     return d;
58   }
59
60   // --------------------------------------------------------------------------
61
// Checking methods
62
// --------------------------------------------------------------------------
63

64   private void checkNode (final Object JavaDoc node, final Map JavaDoc context)
65     throws ADLException
66   {
67     if (node instanceof BindingContainer) {
68       Map JavaDoc itfMap = new HashMap JavaDoc();
69       if (node instanceof InterfaceContainer) {
70         Interface[] itfs = ((InterfaceContainer)node).getInterfaces();
71         for (int i = 0; i < itfs.length; i++) {
72           Interface itf = itfs[i];
73           itfMap.put("this." + itf.getName(), itf);
74         }
75       }
76       if (node instanceof ComponentContainer) {
77         Component[] comps = ((ComponentContainer)node).getComponents();
78         for (int i = 0; i < comps.length; i++) {
79           Component comp = comps[i];
80           if (comp instanceof InterfaceContainer) {
81             Interface[] itfs = ((InterfaceContainer)comp).getInterfaces();
82             for (int j = 0; j < itfs.length; j++) {
83               Interface itf = itfs[j];
84               itfMap.put(comp.getName() + "." + itf.getName(), itf);
85             }
86           }
87         }
88       }
89       Binding[] bindings = ((BindingContainer)node).getBindings();
90       for (int i = 0; i < bindings.length; i++) {
91         Binding binding = bindings[i];
92         checkBinding(binding, itfMap, context);
93       }
94       Set JavaDoc fromItfs = new HashSet JavaDoc();
95       for (int i = 0; i < bindings.length; i++) {
96         if (fromItfs.contains(bindings[i].getFrom())) {
97           throw new ADLException(
98             "Multiple bindings from the same interface", (Node)bindings[i]);
99         } else {
100           fromItfs.add(bindings[i].getFrom());
101         }
102       }
103     }
104     if (node instanceof ComponentContainer) {
105       Component[] comps = ((ComponentContainer)node).getComponents();
106       for (int i = 0; i < comps.length; i++) {
107         checkNode(comps[i], context);
108       }
109     }
110   }
111
112   private void checkBinding (
113     final Binding binding,
114     final Map JavaDoc itfs,
115     final Map JavaDoc context) throws ADLException
116   {
117     if (binding.getFrom() == null) {
118       throw new ADLException("'from' interface missing", (Node)binding);
119     }
120     if (binding.getTo() == null) {
121       throw new ADLException("'to' interface missing", (Node)binding);
122     }
123     Interface fromItf = getInterface(binding.getFrom(), itfs);
124     Interface toItf = getInterface(binding.getTo(), itfs);
125     if (fromItf == null &&
126         !binding.getFrom().endsWith(".component") &&
127         !binding.getFrom().endsWith("-controller"))
128     {
129       throw new ADLException(
130         "No such interface '" + binding.getFrom() + "'", (Node)binding);
131     } else if (toItf == null &&
132         !binding.getTo().endsWith(".component") &&
133         !binding.getTo().endsWith("-controller"))
134     {
135       throw new ADLException(
136         "No such interface '" + binding.getTo() + "'", (Node)binding);
137     }
138     if (fromItf != null && toItf != null) {
139       checkBinding(binding, fromItf, toItf, context);
140     }
141   }
142
143   void checkBinding (
144     final Binding binding,
145     final Interface fromItf,
146     final Interface toItf,
147     final Map JavaDoc context) throws ADLException
148   {
149   }
150
151   Interface getInterface (final String JavaDoc name, final Map JavaDoc itfs) {
152     return (Interface)itfs.get(name);
153   }
154 }
155
Popular Tags