KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > compiler > Conf


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi.compiler;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 public class Conf {
27     private String JavaDoc uri;
28
29     // Maps Class names to ClassConf
30
private HashMap JavaDoc classInfos = new HashMap JavaDoc();
31     private ArrayList JavaDoc classes;
32     private ClassLoader JavaDoc classLoader;
33
34     public Conf(ClassLoader JavaDoc cl, ArrayList JavaDoc classes) {
35         this.classes = classes;
36         classLoader = cl;
37     }
38
39     public void loadConfig(String JavaDoc uri) throws CompilerException {
40         this.uri = uri;
41         Iterator JavaDoc i;
42         try {
43             i = XMLTree.read(uri).childs.iterator();
44         } catch (Exception JavaDoc e1) {
45             throw new CompilerException("Cluster config loading failed", e1);
46         }
47         while (i.hasNext()) {
48             Object JavaDoc 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 JavaDoc l) throws CompilerException {
62         Iterator JavaDoc i = l.iterator();
63         while (i.hasNext()) {
64             Object JavaDoc 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 JavaDoc l) throws CompilerException {
79         String JavaDoc clName = null;
80         ClassConf ccc;
81         Iterator JavaDoc i = l.iterator();
82
83         if (i.hasNext()) {
84             Object JavaDoc 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 JavaDoc cl;
102         try {
103             cl = classLoader.loadClass(clName);
104         } catch (ClassNotFoundException JavaDoc 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 JavaDoc 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 JavaDoc l, String JavaDoc chooser)
136         throws CompilerException {
137         Iterator JavaDoc i = l.iterator();
138
139         while (i.hasNext()) {
140             Object JavaDoc 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 JavaDoc l)
152         throws CompilerException {
153         return checkMethod(ccc, l, ccc.getBalancer());
154     }
155
156     private MethodConf checkMethod(
157         ClassConf ccc,
158         LinkedList JavaDoc l,
159         String JavaDoc balancer)
160         throws CompilerException {
161         MethodProto mp = null;
162         MethodConf mc;
163         Iterator JavaDoc i = l.iterator();
164
165         if (i.hasNext()) {
166             Object JavaDoc o = i.next();
167             if (o instanceof XMLElement) {
168                 XMLElement e = (XMLElement) o;
169                 if (e.name.equals("signature")) {
170                     String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc checkOptString(XMLElement e) throws CompilerException {
209         Iterator JavaDoc i = e.childs.iterator();
210         if (!i.hasNext()) {
211             return null;
212         }
213         Object JavaDoc o = i.next();
214         if ((o instanceof String JavaDoc) && (!i.hasNext())) {
215             return (String JavaDoc) o;
216         }
217         throw new CompilerException(
218             uri + ": element <" + e.name + "> may be only a string");
219     }
220
221     private String JavaDoc checkString(XMLElement e) throws CompilerException {
222         Iterator JavaDoc i = e.childs.iterator();
223         if (i.hasNext()) {
224             Object JavaDoc o = i.next();
225             if ((o instanceof String JavaDoc) && (!i.hasNext())) {
226                 return (String JavaDoc) o;
227             }
228         }
229         throw new CompilerException(
230             uri + ": element <" + e.name + "> must be a string");
231     }
232
233     public ClassConf getClassConf(String JavaDoc 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