KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > EjbConfig


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.cfg;
31
32 import com.caucho.bytecode.JClass;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.config.Config;
35 import com.caucho.config.ConfigException;
36 import com.caucho.config.LineConfigException;
37 import com.caucho.config.types.FileSetType;
38 import com.caucho.ejb.AbstractServer;
39 import com.caucho.ejb.EjbServerManager;
40 import com.caucho.ejb.amber.AmberConfig;
41 import com.caucho.ejb.ql.FunExpr;
42 import com.caucho.java.WorkDir;
43 import com.caucho.java.gen.JavaClassGenerator;
44 import com.caucho.loader.Environment;
45 import com.caucho.loader.EnvironmentClassLoader;
46 import com.caucho.log.Log;
47 import com.caucho.util.L10N;
48 import com.caucho.vfs.Path;
49 import com.caucho.vfs.Vfs;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.Collection JavaDoc;
53 import java.util.Collections JavaDoc;
54 import java.util.Comparator JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Map JavaDoc;
57 import java.util.logging.Logger JavaDoc;
58
59 /**
60  * Manages the EJB configuration files.
61  */

62 public class EjbConfig {
63   private static final L10N L = new L10N(EjbConfig.class);
64   private static final Logger JavaDoc log = Log.open(EjbConfig.class);
65
66   private EjbServerManager _ejbManager;
67   
68   private ArrayList JavaDoc<FileSetType> _fileSetList = new ArrayList JavaDoc<FileSetType>();
69   private ArrayList JavaDoc<Path> _pathList = new ArrayList JavaDoc<Path>();
70   
71   private HashMap JavaDoc<String JavaDoc,EjbBean> _cfgBeans = new HashMap JavaDoc<String JavaDoc,EjbBean>();
72   private ArrayList JavaDoc<CmpRelation> _relations = new ArrayList JavaDoc<CmpRelation>();
73
74   private ArrayList JavaDoc<EjbBean> _pendingBeans = new ArrayList JavaDoc<EjbBean>();
75   private ArrayList JavaDoc<EjbBean> _deployingBeans = new ArrayList JavaDoc<EjbBean>();
76
77   private ArrayList JavaDoc<FunctionSignature> _functions =
78     new ArrayList JavaDoc<FunctionSignature>();
79   
80   private String JavaDoc _booleanTrue = "1";
81   private String JavaDoc _booleanFalse = "0";
82
83   private boolean _isAllowPOJO;
84
85   public EjbConfig(EjbServerManager ejbManager)
86   {
87     _ejbManager = ejbManager;
88
89     _functions.addAll(FunExpr.getStandardFunctions());
90   }
91
92   public String JavaDoc getRemoteJndiName()
93   {
94     return _ejbManager.getRemoteJndiPrefix();
95   }
96
97   public String JavaDoc getLocalJndiName()
98   {
99     return _ejbManager.getLocalJndiPrefix();
100   }
101
102   /**
103    * Adds a path for an EJB config file to the config list.
104    */

105   public void addFileSet(FileSetType fileSet)
106   {
107     if (_fileSetList.contains(fileSet))
108       return;
109
110     _fileSetList.add(fileSet);
111   }
112   
113   /**
114    * Adds a path for an EJB config file to the config list.
115    */

116   public void addEJBPath(Path ejbModulePath, Path path)
117     throws ConfigException
118   {
119     if (_pathList.contains(path))
120       return;
121
122     _pathList.add(path);
123
124     if (path.getScheme().equals("jar"))
125       path.setUserPath(path.getURL());
126     
127     Environment.addDependency(path);
128
129     String JavaDoc ejbModuleName = ejbModulePath.getPath();
130     String JavaDoc pwd = Vfs.getPwd().getPath();
131
132     if (ejbModuleName.startsWith(pwd))
133       ejbModuleName = ejbModuleName.substring(pwd.length());
134     
135     if (_ejbManager != null)
136       _ejbManager.addEJBModule(ejbModuleName);
137
138     // XXX: merge?
139
EjbJar ejbJar = new EjbJar(this, ejbModuleName);
140
141     try {
142       new Config().configure(ejbJar, path, getSchema());
143     } catch (ConfigException e) {
144       throw e;
145     } catch (Exception JavaDoc e) {
146       throw new ConfigException(e);
147     }
148   }
149
150   /**
151    * Returns the schema name.
152    */

153   public String JavaDoc getSchema()
154   {
155     return "com/caucho/ejb/cfg/resin-ejb.rnc";
156   }
157   
158
159   /**
160    * Returns the EJB manager.
161    */

162   public EjbServerManager getEJBManager()
163   {
164     return _ejbManager;
165   }
166
167   /**
168    * Sets the boolean true literal.
169    */

170   public void setBooleanTrue(String JavaDoc trueLiteral)
171   {
172     _booleanTrue = trueLiteral;
173   }
174
175   /**
176    * Gets the boolean true literal.
177    */

178   public String JavaDoc getBooleanTrue()
179   {
180     return _booleanTrue;
181   }
182
183   /**
184    * Sets the boolean false literal.
185    */

186   public void setBooleanFalse(String JavaDoc falseLiteral)
187   {
188     _booleanFalse = falseLiteral;
189   }
190
191   /**
192    * Gets the boolean false literal.
193    */

194   public String JavaDoc getBooleanFalse()
195   {
196     return _booleanFalse;
197   }
198
199   /**
200    * Returns the cfg bean with the given name.
201    */

202   public EjbBean getBeanConfig(String JavaDoc name)
203   {
204     return _cfgBeans.get(name);
205   }
206
207   /**
208    * Sets the cfg bean with the given name.
209    */

210   public void setBeanConfig(String JavaDoc name, EjbBean bean)
211   {
212     if (name == null || bean == null)
213       throw new NullPointerException JavaDoc();
214
215     if (_cfgBeans.get(name) == null)
216       _pendingBeans.add(bean);
217     
218     _cfgBeans.put(name, bean);
219   }
220
221   /**
222    * Sets true if POJO are allowed.
223    */

224   public void setAllowPOJO(boolean allowPOJO)
225   {
226     _isAllowPOJO = allowPOJO;
227   }
228
229   /**
230    * Return true if POJO are allowed.
231    */

232   public boolean isAllowPOJO()
233   {
234     return _isAllowPOJO;
235   }
236
237   /**
238    * Finds an entity bean by its abstract schema.
239    */

240   public EjbEntityBean findEntityBySchema(String JavaDoc schemaName)
241   {
242     for (EjbBean bean : _cfgBeans.values()) {
243       if (bean instanceof EjbEntityBean) {
244     EjbEntityBean entity = (EjbEntityBean) bean;
245
246     if (schemaName.equals(entity.getAbstractSchemaName()))
247       return entity;
248       }
249     }
250
251     return null;
252   }
253
254   /**
255    * Finds an entity bean by its abstract schema.
256    */

257   public EjbEntityBean findEntityByLocal(JClass cl)
258   {
259     for (EjbBean bean : _cfgBeans.values()) {
260       if (bean instanceof EjbEntityBean) {
261     EjbEntityBean entity = (EjbEntityBean) bean;
262
263     if (cl.equals(entity.getLocal()))
264       return entity;
265       }
266     }
267
268     return null;
269   }
270
271   /**
272    * Adds a relation.
273    */

274   public CmpRelation addRelation(String JavaDoc relationName,
275                                  String JavaDoc sourceEJB, String JavaDoc sourceField)
276   {
277     CmpRelation relation = findRelation(relationName, sourceEJB, sourceField);
278
279     if (relation != null)
280       return relation;
281
282     relation = new CmpRelation();
283     relation.setName(relationName);
284     relation.setSourceEJB(sourceEJB);
285     relation.setSourceField(sourceField);
286
287     _relations.add(relation);
288
289     return relation;
290   }
291
292   /**
293    * Adds a relation.
294    */

295   public CmpRelation findRelation(String JavaDoc relationName,
296                                  String JavaDoc sourceEJB, String JavaDoc sourceField)
297   {
298     for (int i = 0; i < _relations.size(); i++) {
299       CmpRelation relation = _relations.get(i);
300
301       if (relationName != null &&
302           relationName.equals(relation.getName()))
303         return relation;
304
305       if (relation.getSourceEJB().equals(sourceEJB) &&
306           relation.getSourceField().equals(sourceField))
307         return relation;
308     }
309
310     return null;
311   }
312
313   public void addRelation(CmpRelation rel)
314     throws ConfigException
315   {
316     CmpRelation oldRel = findRelation(rel.getName(),
317                       rel.getSourceEJB(),
318                       rel.getSourceField());
319
320     if (oldRel == null) {
321       _relations.add(rel);
322       
323       return;
324     }
325
326     if (! rel.getTargetEJB().equals(oldRel.getTargetEJB())) {
327       throw new ConfigException(L.l("relationship '{0}.{1}' target EJB '{2}' does not match old target EJB '{3}' from {4}",
328                     rel.getSourceEJB(),
329                     rel.getSourceField(),
330                     rel.getTargetEJB(),
331                     oldRel.getTargetEJB(),
332                     oldRel.getLocation()));
333     }
334     else if (rel.getTargetField() != oldRel.getTargetField() &&
335          (rel.getTargetField() == null ||
336           ! rel.getTargetField().equals(oldRel.getTargetField()))) {
337       throw new ConfigException(L.l("relationship '{0}.{1}' target field '{2}' does not match old target field '{3}' from {4}",
338                     rel.getSourceEJB(),
339                     rel.getSourceField(),
340                     rel.getTargetEJB(),
341                     oldRel.getTargetEJB(),
342                     oldRel.getLocation()));
343     }
344
345     oldRel.merge(rel);
346   }
347
348   public CmpRelation []getRelations()
349   {
350     return _relations.toArray(new CmpRelation[_relations.size()]);
351   }
352
353   /**
354    * Adds a function.
355    */

356   public void addFunction(FunctionSignature sig, String JavaDoc sql)
357   {
358     _functions.add(sig);
359   }
360
361   /**
362    * Gets the function list.
363    */

364   public ArrayList JavaDoc<FunctionSignature> getFunctions()
365   {
366     return _functions;
367   }
368
369   /**
370    * Configures the pending beans.
371    */

372   public void configure()
373     throws ConfigException
374   {
375     findConfigurationFiles();
376     
377     try {
378       ArrayList JavaDoc<EjbBean> beanConfig = new ArrayList JavaDoc<EjbBean>(_pendingBeans);
379       _pendingBeans.clear();
380
381       _deployingBeans.addAll(beanConfig);
382
383       EnvironmentClassLoader parentLoader = _ejbManager.getClassLoader();
384       
385       Path workPath = _ejbManager.getWorkPath();
386
387       JavaClassGenerator javaGen = new JavaClassGenerator();
388       // need to be compatible with enhancement
389
javaGen.setWorkDir(workPath);
390       javaGen.setParentLoader(parentLoader);
391       
392       configureRelations();
393       
394       for (EjbBean bean : beanConfig) {
395     bean.init();
396       }
397
398       Collections.sort(beanConfig, new BeanComparator());
399
400       AmberConfig amberConfig = new AmberConfig(this);
401
402       for (EjbBean bean : beanConfig) {
403     bean.configureAmber(amberConfig);
404       }
405
406       amberConfig.configureRelations();
407
408       if (_ejbManager.isAutoCompile())
409     amberConfig.generate(javaGen);
410
411       for (EjbBean bean : beanConfig) {
412     bean.generate(javaGen, _ejbManager.isAutoCompile());
413       }
414
415       javaGen.compilePendingJava();
416     } catch (RuntimeException JavaDoc e) {
417       throw e;
418     } catch (Exception JavaDoc e) {
419       throw new ConfigException(e);
420     }
421   }
422
423
424   /**
425    * Configures the pending beans.
426    */

427   private void findConfigurationFiles()
428     throws ConfigException
429   {
430     for (FileSetType fileSet : _fileSetList) {
431       for (Path path : fileSet.getPaths()) {
432     addEJBPath(fileSet.getDir(), path);
433       }
434     }
435   }
436   
437   /**
438    * Configures the pending beans.
439    */

440   public void deploy()
441     throws ConfigException
442   {
443     try {
444       ClassLoader JavaDoc parentLoader = _ejbManager.getClassLoader();
445       
446       Path workPath = _ejbManager.getWorkPath();
447
448       if (workPath == null)
449     workPath = WorkDir.getLocalWorkDir();
450
451       JavaClassGenerator javaGen = new JavaClassGenerator();
452       javaGen.setWorkDir(workPath);
453       javaGen.setParentLoader(parentLoader);
454       
455       deployBeans(_deployingBeans, javaGen);
456     } catch (RuntimeException JavaDoc e) {
457       throw e;
458     } catch (Throwable JavaDoc e) {
459       throw new ConfigException(e);
460     }
461   }
462
463   /**
464    * Configures the pending beans.
465    */

466   public void deployBeans(ArrayList JavaDoc<EjbBean> beanConfig,
467               JavaClassGenerator javaGen)
468     throws Throwable JavaDoc
469   {
470     Thread JavaDoc thread = Thread.currentThread();
471     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
472
473     try {
474       thread.setContextClassLoader(_ejbManager.getClassLoader());
475                    
476       _ejbManager.getAmberManager().initEntityHomes();
477
478       for (EjbBean bean : beanConfig) {
479     AbstractServer server = bean.deployServer(_ejbManager, javaGen);
480     
481     thread.setContextClassLoader(server.getClassLoader());
482
483     /* XXX: handled by bean.deployServer
484     if (bean.getServerProgram() != null)
485       bean.getServerProgram().configure(server);
486     */

487
488     server.init();
489
490     _ejbManager.addServer(server);
491     /*
492     if (bean.getJndiName() != null)
493       _ejbManager.getProtocolManager().deployJNDI(bean.getJndiName(),
494                              server);
495     */

496       }
497     } finally {
498       thread.setContextClassLoader(oldLoader);
499     }
500   }
501
502   /**
503    * Match up the relations.
504    */

505   protected void configureRelations()
506     throws ConfigException
507   {
508     for (CmpRelation relation : _relations) {
509       try {
510     CmpRelationRole sourceRole = relation.getSourceRole();
511     CmpRelationRole targetRole = relation.getTargetRole();
512
513     String JavaDoc sourceEJB = sourceRole.getEJBName();
514     EjbEntityBean sourceEntity = (EjbEntityBean) _cfgBeans.get(sourceEJB);
515
516     if (sourceEntity == null)
517       throw new ConfigException(L.l("'{0}' is an unknown EJB bean.",
518                     sourceEJB));
519       
520     String JavaDoc sourceField = sourceRole.getFieldName();
521     JMethod sourceMethod = sourceEntity.getFieldGetter(sourceField);
522
523     JMethod sourceMapMethod = null;
524
525     if (sourceField != null)
526       sourceMapMethod = getMapMethod(sourceEntity, sourceField);
527
528     if (sourceField != null &&
529         sourceMethod == null && sourceMapMethod == null)
530       throw new ConfigException(L.l("{0}: relation field '{1}' does not have a corresponding getter method. cmr-relations must define abstract getter methods returning a local interface.",
531                     sourceEntity.getEJBClass().getName(),
532                     sourceField));
533
534     String JavaDoc targetEJB = targetRole.getEJBName();
535     EjbEntityBean targetEntity = (EjbEntityBean) _cfgBeans.get(targetEJB);
536
537     if (targetEntity == null)
538       throw new ConfigException(L.l("'{0}' is an unknown EJB bean.",
539                     targetEJB));
540       
541     String JavaDoc targetField = targetRole.getFieldName();
542     JMethod targetMethod = targetEntity.getFieldGetter(targetField);
543
544     JMethod targetMapMethod = null;
545
546     if (targetField != null)
547       targetMapMethod = getMapMethod(targetEntity, targetField);
548
549     if (targetField != null &&
550         targetMethod == null && targetMapMethod == null)
551       throw new ConfigException(L.l("{0}: relation field '{1}' does not have a corresponding getter method. cmr-relations must define abstract getter methods returning a local interface.",
552                     targetEntity.getEJBClass().getName(),
553                     targetField));
554
555     boolean sourceOneToMany = false;
556     boolean sourceManyToOne = false;
557     boolean sourceMap = false;
558
559     if (sourceMethod == null) {
560     }
561     else if (sourceMethod.getReturnType().isAssignableTo(Collection JavaDoc.class))
562       sourceOneToMany = true;
563     else if (sourceMethod.getReturnType().isAssignableTo(Map JavaDoc.class))
564       sourceMap = true;
565     else
566       sourceManyToOne = true;
567
568     boolean targetOneToMany = false;
569     boolean targetManyToOne = false;
570     boolean targetMap = false;
571
572     if (targetMapMethod != null)
573       targetMap = true;
574
575     if (targetMethod == null) {
576     }
577     else if (targetMethod.getReturnType().isAssignableTo(Collection JavaDoc.class))
578       targetOneToMany = true;
579     else if (targetMethod.getReturnType().isAssignableTo(Map JavaDoc.class))
580       targetMap = true;
581     else
582       targetManyToOne = true;
583
584     if (sourceMap) {
585       createMap(targetEntity, targetField, targetRole,
586             sourceEntity, sourceField, sourceRole, sourceMapMethod);
587     }
588     else if (targetMap) {
589       createMap(sourceEntity, sourceField, sourceRole,
590             targetEntity, targetField, targetRole, targetMapMethod);
591     }
592     else if (sourceOneToMany && targetManyToOne) {
593       CmrOneToMany srcRel = new CmrOneToMany(sourceEntity,
594                          sourceField,
595                          targetEntity,
596                          targetField);
597
598       srcRel.setSQLColumns(sourceRole.getSQLColumns());
599       srcRel.setOrderBy(sourceRole.getOrderBy());
600       // srcRel.setCascadeDelete(sourceRole.getCascadeDelete());
601

602       sourceEntity.addRelation(srcRel);
603     
604       CmrManyToOne dstRel = new CmrManyToOne(targetEntity,
605                          targetField,
606                          sourceEntity);
607
608       dstRel.setSQLColumns(targetRole.getSQLColumns());
609       dstRel.setSourceCascadeDelete(targetRole.getCascadeDelete());
610       dstRel.setTargetCascadeDelete(sourceRole.getCascadeDelete());
611       
612       targetEntity.addRelation(dstRel);
613
614       srcRel.setTargetRelation(dstRel);
615       dstRel.setTargetRelation(srcRel);
616     }
617     else if (sourceOneToMany && targetOneToMany) {
618       CmrManyToMany srcRel = new CmrManyToMany(sourceEntity,
619                            sourceField,
620                            targetEntity,
621                            targetField);
622
623       srcRel.setLocation(relation.getLocation());
624       
625       srcRel.setRelationName(relation.getName());
626       srcRel.setSQLTable(relation.getSQLTable());
627       srcRel.setOrderBy(sourceRole.getOrderBy());
628
629       srcRel.setKeySQLColumns(sourceRole.getSQLColumns());
630       srcRel.setDstSQLColumns(targetRole.getSQLColumns());
631       
632       sourceEntity.addRelation(srcRel);
633       
634       CmrManyToMany dstRel = new CmrManyToMany(targetEntity,
635                            targetField,
636                            sourceEntity,
637                            sourceField);
638
639       dstRel.setLocation(relation.getLocation());
640
641       dstRel.setRelationName(relation.getName());
642       dstRel.setSQLTable(relation.getSQLTable());
643       dstRel.setOrderBy(targetRole.getOrderBy());
644
645       dstRel.setKeySQLColumns(targetRole.getSQLColumns());
646       dstRel.setDstSQLColumns(sourceRole.getSQLColumns());
647       
648       targetEntity.addRelation(dstRel);
649       /*
650       
651       srcRel.setTargetRelation(dstRel);
652       dstRel.setTargetRelation(srcRel);
653       CmrOneToMany srcRel = new CmrOneToMany(sourceEntity,
654                          sourceField,
655                          targetEntity,
656                          targetField);
657
658       // manyToOne.setSQLColumns(sourceRole.getSQLColumns());
659       
660       sourceEntity.addRelation(srcRel);
661     
662       CmrOneToMany dstRel = new CmrOneToMany(targetEntity,
663                          targetField,
664                          sourceEntity,
665                          sourceField);
666
667       // dstRel.setSQLColumns(sourceRole.getSQLColumns());
668       
669       targetEntity.addRelation(dstRel);
670
671       srcRel.setTargetRelation(dstRel);
672       dstRel.setTargetRelation(srcRel);
673       */

674     }
675     else if (sourceOneToMany) {
676       CmrManyToMany srcRel = new CmrManyToMany(sourceEntity,
677                            sourceField,
678                            targetEntity,
679                            targetField);
680
681       srcRel.setLocation(relation.getLocation());
682       
683       if (relation.getName() != null)
684         srcRel.setRelationName(relation.getName());
685       else if (relation.getSQLTable() != null)
686         srcRel.setRelationName(relation.getSQLTable());
687       else
688         srcRel.setRelationName(sourceField);
689
690       if (relation.getSQLTable() != null || relation.getName() != null)
691         srcRel.setSQLTable(relation.getSQLTable());
692       else
693         srcRel.setSQLTable(sourceField);
694       
695       srcRel.setOrderBy(sourceRole.getOrderBy());
696
697       srcRel.setKeySQLColumns(sourceRole.getSQLColumns());
698       srcRel.setDstSQLColumns(targetRole.getSQLColumns());
699       
700       srcRel.setTargetUnique("One".equals(sourceRole.getMultiplicity()));
701
702       sourceEntity.addRelation(srcRel);
703     }
704     else if (sourceManyToOne && targetManyToOne) {
705       if (relation.getSQLTable() != null)
706         throw new ConfigException(L.l("cmr-field '{0}' may not have a sql-table '{1}'. one-to-one relations do not have association tables.",
707                       sourceField,
708                       relation.getSQLTable()));
709       
710       CmrManyToOne srcRel = new CmrManyToOne(sourceEntity,
711                          sourceField,
712                          targetEntity);
713
714       srcRel.setLocation(relation.getLocation());
715       
716       srcRel.setSQLColumns(sourceRole.getSQLColumns());
717
718       /*
719       if (targetRole.getCascadeDelete() &&
720           "Many".equals(sourceRole.getMultiplicity()))
721         throw new ConfigException(L.l("'{0}' may not set cascade-delete because '{0}' has multiplicity 'Many'",
722                       targetField,
723                       sourceField));
724       */

725                       
726           
727       srcRel.setSourceCascadeDelete(sourceRole.getCascadeDelete());
728       srcRel.setTargetCascadeDelete(targetRole.getCascadeDelete());
729
730       sourceEntity.addRelation(srcRel);
731
732       CmrManyToOne dstRel = new CmrManyToOne(targetEntity,
733                          targetField,
734                          sourceEntity);
735       
736       dstRel.setLocation(relation.getLocation());
737       
738       dstRel.setSQLColumns(targetRole.getSQLColumns());
739       
740       targetEntity.addRelation(dstRel);
741       
742       if ((sourceRole.getSQLColumns() == null ||
743            sourceRole.getSQLColumns().length == 0) &&
744           targetRole.getSQLColumns() != null &&
745           targetRole.getSQLColumns().length > 0) {
746         srcRel.setDependent(true);
747         
748         dstRel.setSourceCascadeDelete(targetRole.getCascadeDelete());
749         dstRel.setTargetCascadeDelete(sourceRole.getCascadeDelete());
750       }
751
752       if ((targetRole.getSQLColumns() == null ||
753            targetRole.getSQLColumns().length == 0)) {
754         // ejb/06h4
755
// ejb/06hm
756
dstRel.setDependent(true);
757         
758         srcRel.setSourceCascadeDelete(sourceRole.getCascadeDelete());
759         srcRel.setTargetCascadeDelete(targetRole.getCascadeDelete());
760       }
761       
762       srcRel.setTargetRelation(dstRel);
763       dstRel.setTargetRelation(srcRel);
764     }
765     else if (sourceManyToOne && targetOneToMany) {
766       CmrManyToOne srcRel = new CmrManyToOne(sourceEntity,
767                          sourceField,
768                          targetEntity);
769
770       srcRel.setLocation(relation.getLocation());
771       
772       srcRel.setSQLColumns(sourceRole.getSQLColumns());
773       
774       sourceEntity.addRelation(srcRel);
775     
776       CmrOneToMany dstRel = new CmrOneToMany(targetEntity,
777                          targetField,
778                          sourceEntity,
779                          sourceField);
780
781       dstRel.setLocation(relation.getLocation());
782       dstRel.setSQLColumns(sourceRole.getSQLColumns());
783       dstRel.setOrderBy(targetRole.getOrderBy());
784
785       targetEntity.addRelation(dstRel);
786
787       srcRel.setTargetRelation(dstRel);
788       dstRel.setTargetRelation(srcRel);
789
790       if (targetRole.getCascadeDelete() &&
791           "Many".equals(sourceRole.getMultiplicity()))
792         throw new ConfigException(L.l("'{0}' may not set cascade-delete because '{1}' has multiplicity 'Many'",
793                       targetField,
794                       sourceField));
795     }
796     else if (sourceManyToOne) {
797       CmrManyToOne srcRel = new CmrManyToOne(sourceEntity,
798                          sourceField,
799                          targetEntity);
800
801       srcRel.setSQLColumns(sourceRole.getSQLColumns());
802       
803       srcRel.setSourceCascadeDelete(sourceRole.getCascadeDelete());
804       srcRel.setTargetCascadeDelete(targetRole.getCascadeDelete());
805
806       sourceEntity.addRelation(srcRel);
807     }
808     else if (targetManyToOne) {
809       CmrManyToOne dstRel = new CmrManyToOne(targetEntity,
810                          targetField,
811                          sourceEntity);
812
813       dstRel.setSQLColumns(targetRole.getSQLColumns());
814       
815       dstRel.setSourceCascadeDelete(targetRole.getCascadeDelete());
816       dstRel.setTargetCascadeDelete(sourceRole.getCascadeDelete());
817       
818       targetEntity.addRelation(dstRel);
819     }
820     else if (targetOneToMany) {
821       CmrOneToMany dstRel = new CmrOneToMany(targetEntity,
822                          targetField,
823                          sourceEntity,
824                          sourceField);
825
826       dstRel.setSQLColumns(targetRole.getSQLColumns());
827       dstRel.setOrderBy(targetRole.getOrderBy());
828       
829       targetEntity.addRelation(dstRel);
830     }
831     else {
832       throw new ConfigException(L.l("unsupported relation"));
833     }
834       } catch (LineConfigException e) {
835     throw e;
836       } catch (ConfigException e) {
837     throw new LineConfigException(relation.getLocation() + e.getMessage(), e);
838       }
839     }
840   }
841
842   private void createMap(EjbEntityBean sourceEntity,
843              String JavaDoc idField,
844              CmpRelationRole sourceRole,
845              EjbEntityBean targetEntity,
846              String JavaDoc targetField,
847              CmpRelationRole targetRole,
848              JMethod targetMapMethod)
849     throws ConfigException
850   {
851     CmrManyToOne srcRel = new CmrManyToOne(sourceEntity,
852                        idField,
853                        targetEntity);
854
855     srcRel.setSQLColumns(sourceRole.getSQLColumns());
856     /*
857     dstRel.setSQLColumns(targetRole.getSQLColumns());
858     dstRel.setCascadeDelete(targetRole.getCascadeDelete());
859     */

860       
861     sourceEntity.addRelation(srcRel);
862     
863     CmrMap map = new CmrMap(targetEntity, targetField,
864                 sourceEntity, srcRel);
865
866     map.setMapMethod(targetMapMethod);
867
868     targetEntity.addRelation(map);
869   }
870
871   /**
872    * Returns the map method.
873    */

874   public JMethod getMapMethod(EjbEntityBean entityBean, String JavaDoc field)
875   {
876     String JavaDoc methodName = ("get" +
877              Character.toUpperCase(field.charAt(0)) +
878              field.substring(1));
879
880     JMethod []methods = entityBean.getMethods(entityBean.getEJBClassWrapper());
881
882     for (int i = 0; i < methods.length; i++) {
883       JMethod method = methods[i];
884
885       if (! method.getName().equals(methodName))
886     continue;
887       else if (method.getParameterTypes().length != 1)
888     continue;
889       else if ("void".equals(method.getReturnType().getName()))
890     continue;
891       else if (! method.isAbstract())
892     continue;
893       else
894     return method;
895     }
896
897     return null;
898   }
899
900   static class BeanComparator implements Comparator JavaDoc {
901     public int compare(Object JavaDoc a, Object JavaDoc b)
902     {
903       if (a == b)
904         return 0;
905
906       EjbBean beanA = (EjbBean) a;
907       EjbBean beanB = (EjbBean) b;
908
909       if (! (a instanceof EjbEntityBean) && ! (b instanceof EjbEntityBean))
910         return beanA.getEJBName().compareTo(beanB.getEJBName());
911       else if (! (a instanceof EjbEntityBean))
912         return 1;
913       else if (! (b instanceof EjbEntityBean))
914         return -1;
915
916       EjbEntityBean entityA = (EjbEntityBean) a;
917       EjbEntityBean entityB = (EjbEntityBean) b;
918
919       if (entityB.dependsOn(entityA))
920         return -1;
921       else if (entityA.dependsOn(entityB))
922         return 1;
923       else
924         return entityA.getEJBName().compareTo(entityB.getEJBName());
925     }
926   }
927 }
928
929
Popular Tags