KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 public class ClassConf {
27     private HashMap JavaDoc mthInfos = new HashMap JavaDoc();
28     private boolean lookupChoice = false;
29     private ArrayList JavaDoc rrList = new ArrayList JavaDoc();
30     private ArrayList JavaDoc randList = new ArrayList JavaDoc();
31     private int balancerNo = 0;
32     private String JavaDoc defaultBalancer = null;
33     private Class JavaDoc cl;
34
35     public ClassConf(Class JavaDoc cl) {
36         this.cl = cl;
37     }
38
39     public String JavaDoc getClassName() {
40         return MethodProto.getName(cl);
41     }
42
43     public boolean containsMethod(MethodProto mp) {
44         return mthInfos.containsKey(mp);
45     }
46
47     public void putMethod(MethodProto mp, MethodConf cmc) {
48         mthInfos.put(mp, cmc);
49     }
50
51     public Iterator JavaDoc getMethodConfs() {
52         return mthInfos.values().iterator();
53     }
54
55     public void setLookupChoice() {
56         lookupChoice = true;
57     }
58
59     public MethodConf getMethodConf(MethodProto mp)
60         throws CompilerException {
61         MethodConf cmc = (MethodConf) mthInfos.get(mp);
62         if (cmc == null) {
63             throw new CompilerException(
64                 "No configuration found for method "
65                     + mp
66                     + " in class "
67                     + getClassName());
68         }
69         return cmc;
70     }
71
72     public String JavaDoc getRemoteItfString() {
73         Iterator JavaDoc it = Compiler.getRemoteItfs(cl).iterator();
74         String JavaDoc s = "";
75         while (it.hasNext()) {
76             Class JavaDoc itf = (Class JavaDoc) it.next();
77             if (s.equals("")) {
78                 s = MethodProto.getName(itf);
79             } else {
80                 s += ", " + MethodProto.getName(itf);
81             }
82         }
83         return s;
84     }
85
86     public String JavaDoc addRR() {
87         String JavaDoc s = "rr" + balancerNo;
88         balancerNo++;
89         rrList.add(s);
90         return s;
91     }
92
93     public String JavaDoc addRandom() {
94         String JavaDoc s = "rand" + balancerNo;
95         balancerNo++;
96         randList.add(s);
97         return s;
98     }
99
100     public ArrayList JavaDoc getRR() {
101         return rrList;
102     }
103
104     public ArrayList JavaDoc getRandom() {
105         return randList;
106     }
107
108     public String JavaDoc getBalancer() {
109         if (defaultBalancer == null) {
110             defaultBalancer = "balancer";
111         }
112         randList.add(defaultBalancer);
113         return defaultBalancer;
114     }
115
116     public void validate() throws CompilerException {
117         Method JavaDoc[] m = Compiler.getRemoteMethods(cl);
118         if (m.length == 0) {
119             throw new CompilerException("class " + cl + " does not implement remote methods");
120         }
121         for (int i=0; i<m.length; i++) {
122             MethodProto mp = new MethodProto(m[i]);
123             MethodConf mc = (MethodConf) mthInfos.get(mp);
124             if (mc == null) {
125                 throw new CompilerException("no configuration given for method " + mp);
126             }
127             mc.setMethod(m[i]);
128         }
129         Iterator JavaDoc it = getMethodConfs();
130         while (it.hasNext()) {
131             MethodConf mc = (MethodConf) it.next();
132             if (mc.getMethod() == null) {
133                 throw new CompilerException("class " + mc.getClassConf().getClassName() + " has no method " + mc.getMethodProto());
134             }
135         }
136     }
137 }
138
Popular Tags