KickJava   Java API By Example, From Geeks To Geeks.

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


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.amber.field.AmberField;
33 import com.caucho.amber.type.EntityType;
34 import com.caucho.bytecode.JClass;
35 import com.caucho.bytecode.JMethod;
36 import com.caucho.config.ConfigException;
37 import com.caucho.ejb.gen.EntityBean;
38 import com.caucho.java.JavaWriter;
39 import com.caucho.util.L10N;
40
41 import javax.ejb.EJBLocalObject JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.Collection JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * Abstract relation.
48  */

49 public class CmrRelation extends CmpProperty {
50   private static final L10N L = new L10N(CmrRelation.class);
51
52   private String JavaDoc _relationName;
53   
54   private String JavaDoc _fieldName;
55   private boolean _hasGetter;
56
57   private CmrRelation _targetRelation;
58
59   /**
60    * Creates a new cmp-relation
61    */

62   public CmrRelation(EjbEntityBean entityBean)
63     throws ConfigException
64   {
65     super(entityBean);
66   }
67
68   /**
69    * Creates a new cmp-relation
70    */

71   public CmrRelation(EjbEntityBean entityBean, String JavaDoc fieldName)
72     throws ConfigException
73   {
74     super(entityBean);
75
76     setFieldName(fieldName);
77
78     JMethod getter = getGetter();
79
80     if (! getter.isAbstract() && ! entityBean.isAllowPOJO())
81       throw new ConfigException(L.l("{0}: '{1}' must have an abstract getter method. cmr-relations must have abstract getter methods returning a local interface.",
82                     entityBean.getEJBClass().getName(),
83                     getter.getFullName()));
84
85     JClass retType = getter.getReturnType();
86
87     if (! retType.isAssignableTo(EJBLocalObject JavaDoc.class) &&
88     ! retType.isAssignableTo(Collection JavaDoc.class) &&
89     ! retType.isAssignableTo(Map JavaDoc.class)) {
90       throw new ConfigException(L.l("{0}: '{1}' must return an EJBLocalObject or a Collection. cmr-relations must have abstract getter methods returning a local interface.",
91                     entityBean.getEJBClass().getName(),
92                     getter.getFullName()));
93     }
94
95     JMethod setter = getSetter();
96
97     if (setter == null)
98       return;
99
100     JClass []paramTypes = setter.getParameterTypes();
101
102     if (! retType.equals(paramTypes[0]))
103       throw new ConfigException(L.l("{0}: '{1}' must return an '{2}'. Persistent setters must match the getter types .",
104                     entityBean.getEJBClass().getName(),
105                     setter.getFullName(),
106                     retType.getName()));
107   }
108
109   /**
110    * Returns the relation name.
111    */

112   public String JavaDoc getRelationName()
113   {
114     return _relationName;
115   }
116
117   /**
118    * Sets the relation name.
119    */

120   public void setRelationName(String JavaDoc name)
121   {
122     _relationName = name;
123   }
124
125   /**
126    * Returns the target bean
127    */

128   public EjbEntityBean getTargetBean()
129   {
130     return null;
131   }
132
133   /**
134    * Returns the target type.
135    */

136   public JClass getTargetType()
137   {
138     throw new UnsupportedOperationException JavaDoc(getClass().getName());
139   }
140
141   /**
142    * Sets a target relation.
143    */

144   public void setTarget(CmrRelation target)
145   {
146     throw new UnsupportedOperationException JavaDoc(getClass().getName());
147   }
148
149   /**
150    * Returns true for a collection.
151    */

152   public boolean isCollection()
153   {
154     return false;
155   }
156
157   /**
158    * Sets the paired target relation.
159    */

160   public void setTargetRelation(CmrRelation target)
161   {
162     _targetRelation = target;
163   }
164
165   /**
166    * Gets the paired target relation.
167    */

168   public CmrRelation getTargetRelation()
169   {
170     return _targetRelation;
171   }
172
173   /**
174    * Link amber.
175    */

176   public void linkAmber()
177     throws ConfigException
178   {
179   }
180
181   /**
182    * Assemble any bean methods.
183    */

184   public void assemble(EntityBean bean)
185   {
186     
187   }
188
189   /**
190    * Set true for having a getter.
191    */

192   public void setHasGetter(boolean hasGetter)
193   {
194     _hasGetter = true;
195   }
196
197   /**
198    * Set true for having a getter.
199    */

200   public boolean getHasGetter()
201   {
202     return _hasGetter;
203   }
204   
205   /**
206    * Create any bean methods.
207    */

208   public EjbMethod createGetter(EjbView view,
209                 JMethod apiMethod,
210                 JMethod implMethod)
211     throws ConfigException
212   {
213     return new CmpGetter(view, apiMethod, implMethod);
214   }
215
216   /**
217    * Creates the amber type.
218    */

219   public AmberField assembleAmber(EntityType type)
220     throws ConfigException
221   {
222     throw new UnsupportedOperationException JavaDoc(getClass().getName());
223   }
224
225   /**
226    * Generates the after commit code.
227    */

228   public void generateAfterCommit(JavaWriter out)
229     throws IOException JavaDoc
230   {
231   }
232
233   /**
234    * Generates the destroy method.
235    */

236   public void generateDestroy(JavaWriter out)
237     throws IOException JavaDoc
238   {
239   }
240
241   public String JavaDoc toString()
242   {
243     return "CmrRelation[" + getName() + "]";
244   }
245 }
246
Popular Tags