KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > jorm > JormAssignMapperRule


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, S. Chassande-Barrioz, A. Lefebvre
22  */

23 package org.objectweb.medor.optim.jorm;
24
25 import org.objectweb.jorm.api.PMapper;
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.metainfo.api.GenClassRef;
28 import org.objectweb.medor.api.MedorException;
29 import org.objectweb.medor.optim.lib.BasicRule;
30 import org.objectweb.medor.query.api.QueryNode;
31 import org.objectweb.medor.query.api.QueryTree;
32 import org.objectweb.medor.query.jorm.lib.ClassExtent;
33 import org.objectweb.medor.query.jorm.lib.GenClassExtent;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 import java.util.Map JavaDoc;
37
38 /**
39  * It permits to assign the mappers to the JORM leaves.
40  * <p>This rule must be executed
41  * before the rewriting of the JORM leaves. Two maps are assigned to this rule.
42  * <p>The first map has the following caracteristics:
43  * <ul><li>key = JORM class name</li>
44  * <li>value = the mapper instance which maps the jorm class.</li>
45  * </ul>
46  * <p>
47  * <p>The second map has the following caracteristics:
48  * <ul><li>key = JORM class name</li>
49  * <li>value = the project name for this JORM class.</li>
50  * </ul>
51  * <p> Currently this rule does not take in account the possibility to map the
52  * same class several times into several mapper.
53  * deprecated Should now use the JormQueryTreeHelper methods instead.
54  * @see org.objectweb.medor.query.jorm.lib.JormQueryTreeHelper
55  * @author S. Chassande-Barrioz
56  */

57 public class JormAssignMapperRule extends BasicRule {
58
59     protected Map JavaDoc jormName2Mapper = null;
60     protected Map JavaDoc jormName2Project = null;
61
62     public JormAssignMapperRule() {
63         super("JormAssignMapperRule");
64     }
65
66     public Map JavaDoc getJormName2Mapper() {
67         return jormName2Mapper;
68     }
69
70     /**
71      * It assigns the map used in this rule. (Read the class description for
72      * more details).
73      * @param jormName2Mapper is the map
74      */

75     public void setJormName2Mapper(Map JavaDoc jormName2Mapper, Map JavaDoc jormName2Project) {
76         this.jormName2Mapper = jormName2Mapper;
77         this.jormName2Project = jormName2Project;
78     }
79
80     public QueryTree rewrite(QueryTree qt, QueryNode _parent)
81             throws MedorException {
82         if (jormName2Mapper == null || jormName2Mapper.size()==0)
83             throw new MedorException("Unable to assign the mapper on the jorm leaves without non empty map");
84         if (debug)
85             log.log(BasicLevel.DEBUG, "Assignation of mappers");
86         assignMapper(qt);
87         return qt;
88     }
89
90     protected void assignMapper(QueryTree qt) throws MedorException {
91         if (qt instanceof ClassExtent) {
92             ClassExtent extent = (ClassExtent) qt;
93             String JavaDoc name = ((Class JavaDoc) extent.getMetaObject()).getFQName();
94             PMapper mapper = (PMapper) jormName2Mapper.get(name);
95             if (mapper != null) {
96                 extent.setPMapper(mapper, (String JavaDoc)jormName2Project.get(name));
97                 if (debug)
98                     log.log(BasicLevel.DEBUG, "Assign the mapper "
99                         + mapper.getMapperName() + "to the ClassExtent" + extent.getName());
100             } else
101                 throw new MedorException("No mapper specified for the class " + name);
102         }
103         else if (qt instanceof GenClassExtent) {
104             GenClassExtent gce = (GenClassExtent) qt;
105             GenClassRef gcr = (GenClassRef) gce.getMetaObject();
106             String JavaDoc name = ((Class JavaDoc) gcr.getParent()).getFQName() + "." + gcr.getName();
107             PMapper mapper = (PMapper) jormName2Mapper.get(name);
108             if (mapper != null) {
109                 gce.setPMapper(mapper, (String JavaDoc)jormName2Project.get(name));
110                 if (debug)
111                     log.log(BasicLevel.DEBUG, "Assign the mapper "
112                         + mapper.getMapperName() + "to the GenClassExtent" + gce.getName());
113             } else
114                 throw new MedorException("No mapper specified for the GenClass " + name);
115         }
116         else if (qt instanceof QueryNode) {
117             QueryTree[] qts = ((QueryNode) qt).getChildren();
118             for (int i = 0; i < qts.length; i++) {
119                 assignMapper(qts[i]);
120             }
121         }
122     }
123 }
124
Popular Tags