1 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 ; 36 import java.util.HashSet ; 37 import java.util.Map ; 38 import java.util.Set ; 39 40 45 46 public class BindingLoader extends AbstractLoader { 47 48 52 public Definition load (final String name, final Map context) 53 throws ADLException 54 { 55 Definition d = clientLoader.load(name, context); 56 checkNode(d, context); 57 return d; 58 } 59 60 64 private void checkNode (final Object node, final Map context) 65 throws ADLException 66 { 67 if (node instanceof BindingContainer) { 68 Map itfMap = new HashMap (); 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 fromItfs = new HashSet (); 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 itfs, 115 final Map 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 context) throws ADLException 148 { 149 } 150 151 Interface getInterface (final String name, final Map itfs) { 152 return (Interface)itfs.get(name); 153 } 154 } 155 | Popular Tags |