KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > rdb > RdbAssignRdbAdapterRule


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre, S. Chassande-Barrioz
22  */

23 package org.objectweb.medor.optim.rdb;
24
25 import org.objectweb.medor.api.MedorException;
26 import org.objectweb.medor.optim.lib.BasicRule;
27 import org.objectweb.medor.query.api.QueryNode;
28 import org.objectweb.medor.query.api.QueryTree;
29 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter;
32
33 import java.util.Map JavaDoc;
34
35 /**
36  * @author S.Chassande-Barrioz
37  */

38 public class RdbAssignRdbAdapterRule extends BasicRule {
39
40     protected Map JavaDoc name2Adapter = null;
41
42     public RdbAssignRdbAdapterRule() {
43         super("RdbAssignRdbAdapterRule");
44     }
45
46     public RdbAssignRdbAdapterRule(Map JavaDoc m) {
47         this();
48         name2Adapter = m;
49     }
50
51     public void setName2Adapter(Map JavaDoc m) {
52         name2Adapter = m;
53     }
54
55     public Map JavaDoc getName2Adapter() {
56         return name2Adapter;
57     }
58
59     public QueryTree rewrite(QueryTree qt, QueryNode _parent)
60             throws MedorException {
61         log.log(BasicLevel.DEBUG, "Assignation of relational adapters");
62         if (name2Adapter != null && name2Adapter.size()>0)
63             assignAdapter(qt);
64         return qt;
65     }
66
67     protected void assignAdapter(QueryTree qt) throws MedorException {
68         if (qt instanceof RdbExpQueryLeaf) {
69             RdbExpQueryLeaf ql = (RdbExpQueryLeaf) qt;
70             String JavaDoc name = ql.getRdbAdapterName();
71             if (name == null) {
72                 log.log(BasicLevel.DEBUG,
73                     "No adapter specified for the RdbExpQueryLeaf: " + ql.getName());
74                 return;
75             }
76             RdbAdapter adapter = (RdbAdapter)
77                 name2Adapter.get(name);
78             if (adapter != null) {
79                 ql.setRdbAdapter(adapter);
80                 log.log(BasicLevel.DEBUG, "Assign the adapter "
81                     + ql.getRdbAdapterName() + "to the RdbExpQueryLeaf: "
82                     + ql.getName());
83             } else
84                 throw new MedorException("No adapter " + name
85                     + " found for the RdbExpQueryLeaf: " + ql.getName());
86         } else if (qt instanceof QueryNode) {
87             QueryTree[] qts = ((QueryNode) qt).getChildren();
88             for (int i = 0; i < qts.length; i++) {
89                 assignAdapter(qts[i]);
90             }
91         }
92     }
93
94 }
95
Popular Tags