KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > amber > AmberConfig


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.amber;
31
32 import com.caucho.amber.field.AmberField;
33 import com.caucho.amber.field.CompositeId;
34 import com.caucho.amber.field.Id;
35 import com.caucho.amber.field.IdField;
36 import com.caucho.amber.field.PropertyField;
37 import com.caucho.amber.field.StubMethod;
38 import com.caucho.amber.manager.AmberPersistenceUnit;
39 import com.caucho.amber.table.Column;
40 import com.caucho.amber.table.Table;
41 import com.caucho.amber.type.EntityType;
42 import com.caucho.amber.type.Type;
43 import com.caucho.bytecode.JClass;
44 import com.caucho.bytecode.JMethod;
45 import com.caucho.config.ConfigException;
46 import com.caucho.ejb.cfg.CmpField;
47 import com.caucho.ejb.cfg.CmpProperty;
48 import com.caucho.ejb.cfg.CmrRelation;
49 import com.caucho.ejb.cfg.EjbConfig;
50 import com.caucho.ejb.cfg.EjbEntityBean;
51 import com.caucho.java.gen.JavaClassGenerator;
52 import com.caucho.util.L10N;
53 import com.caucho.vfs.PersistentDependency;
54
55 import java.io.IOException JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.HashMap JavaDoc;
58
59 /**
60  * Configuration manager for amber.
61  */

62 public class AmberConfig {
63   private static final L10N L = new L10N(AmberConfig.class);
64
65   private EjbConfig _ejbConfig;
66   private AmberPersistenceUnit _manager;
67
68   private ArrayList JavaDoc<EjbEntityBean> _beans = new ArrayList JavaDoc<EjbEntityBean>();
69   
70   private HashMap JavaDoc<String JavaDoc,EntityType> _entityMap =
71     new HashMap JavaDoc<String JavaDoc,EntityType>();
72
73   /**
74    * Sets the data source.
75    */

76   public AmberConfig(EjbConfig ejbConfig)
77   {
78     _ejbConfig = ejbConfig;
79     _manager = _ejbConfig.getEJBManager().getAmberManager();
80   }
81
82   public void init()
83     throws ConfigException, IOException JavaDoc
84   {
85   }
86
87   /**
88    * Returns the manager.
89    */

90   public AmberPersistenceUnit getManager()
91   {
92     return _manager;
93   }
94   
95   /**
96    * Adds a new bean config.
97    */

98   public void addBean(EjbEntityBean bean)
99     throws ConfigException
100   {
101     _beans.add(bean);
102
103     EntityType type = bean.getEntityType();
104
105     type.setInstanceClassName(bean.getFullImplName());
106     type.setProxyClass(bean.getLocal());
107
108     String JavaDoc sqlTable;
109
110     if (bean.getAbstractSchemaName() != null) {
111       type.setName(bean.getAbstractSchemaName());
112       sqlTable = bean.getAbstractSchemaName();
113     }
114     else {
115       String JavaDoc name = bean.getEJBName();
116       int p = name.lastIndexOf('/');
117       if (p > 0)
118     sqlTable = name.substring(p + 1);
119       else
120     sqlTable = name;
121     }
122
123     if (bean.getSQLTable() != null)
124       sqlTable = bean.getSQLTable();
125
126     Table table = _manager.createTable(sqlTable);
127     table.setConfigLocation(bean.getLocation());
128                 
129     type.setTable(table);
130
131     type.setReadOnly(bean.isReadOnly());
132     table.setReadOnly(bean.isReadOnly());
133     type.setCacheTimeout(bean.getCacheTimeout());
134
135     if (hasMethod(bean.getEJBClassWrapper(), "ejbLoad", new JClass[0])) {
136       type.setHasLoadCallback(true);
137     }
138     
139     _entityMap.put(bean.getEJBName(), type);
140
141     ArrayList JavaDoc<CmpProperty> ids = new ArrayList JavaDoc<CmpProperty>();
142
143     for (CmpField cmpField : bean.getCmpFields()) {
144       if (cmpField.isId())
145     ids.add(cmpField);
146       else
147     configureField(type, cmpField);
148     }
149
150     for (CmrRelation cmrRelation : bean.getRelations()) {
151       if (cmrRelation.isId())
152     ids.add(cmrRelation);
153     }
154
155     configureId(bean, type, ids);
156
157     for (JMethod method : bean.getStubMethods()) {
158       type.addStubMethod(new StubMethod(method));
159     }
160
161     for (PersistentDependency depend : bean.getDependList()) {
162       type.addDependency(depend);
163     }
164   }
165
166   /**
167    * Configure the field.
168    */

169   private void configureField(EntityType type, CmpField cmpField)
170     throws ConfigException
171   {
172     String JavaDoc fieldName = cmpField.getName();
173     String JavaDoc sqlName = cmpField.getSQLColumn();
174
175     if (sqlName == null)
176       sqlName = fieldName;
177       
178     JClass dataType = cmpField.getJavaType();
179
180     if (dataType == null)
181       throw new NullPointerException JavaDoc(L.l("'{0}' is an unknown field",
182                      fieldName));
183
184     Type amberType = _manager.createType(dataType);
185     Column column = type.getTable().createColumn(sqlName, amberType);
186
187     column.setConfigLocation(cmpField.getLocation());
188       
189     PropertyField field = new PropertyField(type, fieldName);
190     field.setLazy(false);
191     field.setColumn(column);
192
193     type.addField(field);
194   }
195
196   /**
197    * Configure the field.
198    */

199   private void configureId(EjbEntityBean bean,
200                EntityType type,
201                ArrayList JavaDoc<CmpProperty> fields)
202     throws ConfigException
203   {
204     ArrayList JavaDoc<IdField> keys = new ArrayList JavaDoc<IdField>();
205     
206     for (CmpProperty cmpProperty : fields) {
207       IdField idField = cmpProperty.createId(_manager, type);
208       if (fields.size() > 1 || bean.getCompositeKeyClass() != null)
209     idField.setKeyField(true);
210
211       keys.add(idField);
212     }
213
214     if (keys.size() == 1 && bean.getCompositeKeyClass() == null) {
215       Id id = new Id(type, keys.get(0));
216       type.setId(id);
217     }
218     else {
219       CompositeId id = new CompositeId(type, keys);
220       id.setKeyClass(bean.getPrimKeyClass());
221       type.setId(id);
222     }
223   }
224   
225   public void configureRelations()
226     throws ConfigException
227   {
228     for (EjbEntityBean bean : _beans) {
229       configureRelations(bean);
230     }
231     
232     for (EjbEntityBean bean : _beans) {
233       linkRelations(bean);
234     }
235   }
236   
237   private void configureRelations(EjbEntityBean bean)
238     throws ConfigException
239   {
240     EntityType type = bean.getEntityType();
241
242     for (CmrRelation rel : bean.getRelations()) {
243       if (! rel.isId())
244     configureRelation(type, rel);
245     }
246   }
247   
248   private void linkRelations(EjbEntityBean bean)
249     throws ConfigException
250   {
251     EntityType type = _entityMap.get(bean.getEJBName());
252     
253     for (CmrRelation rel : bean.getRelations()) {
254       rel.linkAmber();
255     }
256   }
257
258   /**
259    * Configure the relation rolen.
260    */

261   private void configureRelation(EntityType type, CmrRelation rel)
262     throws ConfigException
263   {
264     String JavaDoc fieldName = rel.getName();
265     String JavaDoc targetName = rel.getTargetBean().getEJBName();
266
267     EntityType targetType = _entityMap.get(targetName);
268
269     if (targetType == null)
270       throw new ConfigException(L.l("'{0}' is an unknown entity type",
271                     targetName));
272
273     AmberField field = rel.assembleAmber(type);
274
275     if (field != null)
276       type.addField(field);
277   }
278
279   /**
280    * Generates the beans.
281    */

282   public void generate(JavaClassGenerator javaGen)
283     throws Exception JavaDoc
284   {
285     _manager.generate(javaGen);
286   }
287
288   private static boolean hasMethod(JClass cl, String JavaDoc name, JClass []param)
289   {
290     try {
291       return cl.getMethod(name, param) != null;
292     } catch (Throwable JavaDoc e) {
293       return false;
294     }
295   }
296 }
297
Popular Tags