1 19 package org.objectweb.carol.cmi.compiler; 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.LinkedList ; 24 import java.util.Iterator ; 25 26 public class Conf { 27 private String uri; 28 29 private HashMap classInfos = new HashMap (); 31 private ArrayList classes; 32 private ClassLoader classLoader; 33 34 public Conf(ClassLoader cl, ArrayList classes) { 35 this.classes = classes; 36 classLoader = cl; 37 } 38 39 public void loadConfig(String uri) throws CompilerException { 40 this.uri = uri; 41 Iterator i; 42 try { 43 i = XMLTree.read(uri).childs.iterator(); 44 } catch (Exception e1) { 45 throw new CompilerException("Cluster config loading failed", e1); 46 } 47 while (i.hasNext()) { 48 Object o = i.next(); 49 if (!(o instanceof XMLElement)) { 50 throw new CompilerException("Text not expected at top level"); 51 } 52 XMLElement e = (XMLElement) o; 53 if (!e.name.equals("cluster-config")) { 54 throw new CompilerException( 55 "Element " + e.name + " not expected at top level"); 56 } 57 checkTop(e.childs); 58 } 59 } 60 61 private void checkTop(LinkedList l) throws CompilerException { 62 Iterator i = l.iterator(); 63 while (i.hasNext()) { 64 Object o = i.next(); 65 if (!(o instanceof XMLElement)) { 66 throw new CompilerException("Text not expected at top level"); 67 } 68 XMLElement e = (XMLElement) o; 69 if (e.name.equals("class")) { 70 checkClass(e.childs); 71 } else { 72 throw new CompilerException( 73 "Element " + e.name + " not expected here"); 74 } 75 } 76 } 77 78 private void checkClass(LinkedList l) throws CompilerException { 79 String clName = null; 80 ClassConf ccc; 81 Iterator i = l.iterator(); 82 83 if (i.hasNext()) { 84 Object o = i.next(); 85 if (o instanceof XMLElement) { 86 XMLElement e = (XMLElement) o; 87 if (e.name.equals("name")) 88 clName = checkString(e); 89 } 90 } 91 92 if (clName == null) 93 throw new CompilerException( 94 uri 95 + ": a class name must be provided first in a <class> element"); 96 97 if (!classes.contains(clName)) { 98 return; 99 } 100 101 Class cl; 102 try { 103 cl = classLoader.loadClass(clName); 104 } catch (ClassNotFoundException e1) { 105 throw new CompilerException("class not found " + clName, e1); 106 } 107 108 ccc = new ClassConf(cl); 109 if (classInfos.put(clName, ccc) != null) 110 throw new CompilerException( 111 uri + ": only one definition expected for class " + clName); 112 113 while (i.hasNext()) { 114 Object o = i.next(); 115 if (!(o instanceof XMLElement)) { 116 throw new CompilerException(uri + ": text not expected"); 117 } 118 XMLElement e = (XMLElement) o; 119 if (e.name.equals("method")) { 120 checkMethod(ccc, e.childs, null); 121 } else if (e.name.equals("lookup")) { 122 checkEmpty(e); 123 ccc.setLookupChoice(); 124 } else if (e.name.equals("rr")) { 125 checkChooser(ccc, e.childs, ccc.addRR()); 126 } else if (e.name.equals("random")) { 127 checkChooser(ccc, e.childs, ccc.addRandom()); 128 } else 129 throw new CompilerException( 130 uri + ": unexpected element : " + e.name); 131 } 132 ccc.validate(); 133 } 134 135 private void checkChooser(ClassConf ccc, LinkedList l, String chooser) 136 throws CompilerException { 137 Iterator i = l.iterator(); 138 139 while (i.hasNext()) { 140 Object o = i.next(); 141 if (!(o instanceof XMLElement)) { 142 throw new CompilerException(uri + ": text not expected"); 143 } 144 XMLElement e = (XMLElement) o; 145 if (e.name.equals("method")) { 146 checkMethod(ccc, e.childs, chooser); 147 } 148 } 149 } 150 151 private MethodConf checkMethod(ClassConf ccc, LinkedList l) 152 throws CompilerException { 153 return checkMethod(ccc, l, ccc.getBalancer()); 154 } 155 156 private MethodConf checkMethod( 157 ClassConf ccc, 158 LinkedList l, 159 String balancer) 160 throws CompilerException { 161 MethodProto mp = null; 162 MethodConf mc; 163 Iterator i = l.iterator(); 164 165 if (i.hasNext()) { 166 Object o = i.next(); 167 if (o instanceof XMLElement) { 168 XMLElement e = (XMLElement) o; 169 if (e.name.equals("signature")) { 170 String sign = checkString(e); 171 mp = new MethodProto(sign); 172 } 173 } 174 } 175 176 if (mp == null) 177 throw new CompilerException( 178 uri 179 + ": a signature must be provided first in a <method> element"); 180 181 if (ccc.containsMethod(mp)) { 182 throw new CompilerException( 183 uri + ": method already defined: " + mp.toString()); 184 } 185 186 mc = new MethodConf(ccc, mp, balancer); 187 ccc.putMethod(mp, mc); 188 189 while (i.hasNext()) { 190 Object o = i.next(); 191 if (!(o instanceof XMLElement)) { 192 throw new CompilerException(uri + ": text not expected"); 193 } 194 XMLElement e = (XMLElement) o; 195 throw new CompilerException(uri + ": bad sub-element: " + e.name); 196 } 197 198 return mc; 199 } 200 201 private void checkEmpty(XMLElement e) throws CompilerException { 202 Iterator i = e.childs.iterator(); 203 if (i.hasNext()) 204 throw new CompilerException( 205 uri + ": element <" + e.name + "> should be empty"); 206 } 207 208 private String checkOptString(XMLElement e) throws CompilerException { 209 Iterator i = e.childs.iterator(); 210 if (!i.hasNext()) { 211 return null; 212 } 213 Object o = i.next(); 214 if ((o instanceof String ) && (!i.hasNext())) { 215 return (String ) o; 216 } 217 throw new CompilerException( 218 uri + ": element <" + e.name + "> may be only a string"); 219 } 220 221 private String checkString(XMLElement e) throws CompilerException { 222 Iterator i = e.childs.iterator(); 223 if (i.hasNext()) { 224 Object o = i.next(); 225 if ((o instanceof String ) && (!i.hasNext())) { 226 return (String ) o; 227 } 228 } 229 throw new CompilerException( 230 uri + ": element <" + e.name + "> must be a string"); 231 } 232 233 public ClassConf getClassConf(String className) throws CompilerException { 234 ClassConf ccc = (ClassConf) classInfos.get(className); 235 if (ccc == null) { 236 throw new CompilerException( 237 "No configuration found for class " + className); 238 } 239 return ccc; 240 } 241 } 242 | Popular Tags |