KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > bridge > JDBCCMP1xFieldBridge


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software 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 software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.bridge;
23
24 import java.lang.reflect.Field JavaDoc;
25
26 import javax.ejb.EJBException JavaDoc;
27
28 import org.jboss.deployment.DeploymentException;
29 import org.jboss.ejb.EntityEnterpriseContext;
30
31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCContext;
32 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
33 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData;
34
35
36 /**
37  * JDBCCMP1xFieldBridge is a concrete implementation of JDBCCMPFieldBridge for
38  * CMP version 1.x. Getting and setting of instance fields set the
39  * corresponding field in bean instance. Dirty checking is performed by
40  * storing the current value in the entity persistence context when ever
41  * setClean is called, and comparing current value to the original value.
42  *
43  * Life-cycle:
44  * Tied to the EntityBridge.
45  *
46  * Multiplicity:
47  * One for each entity bean cmp field.
48  *
49  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
50  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
51  * @version $Revision: 37459 $
52  */

53 public class JDBCCMP1xFieldBridge extends JDBCAbstractCMPFieldBridge
54 {
55    private Field JavaDoc field;
56
57    public JDBCCMP1xFieldBridge(JDBCStoreManager manager,
58                                JDBCCMPFieldMetaData metadata)
59       throws DeploymentException
60    {
61       super(manager, metadata);
62
63       try
64       {
65          field = manager.getMetaData().getEntityClass().getField(getFieldName());
66       }
67       catch(NoSuchFieldException JavaDoc e)
68       {
69          // Non recoverable internal exception
70
throw new DeploymentException("No field named '" + getFieldName() +
71             "' found in entity class.");
72       }
73    }
74
75    public Object JavaDoc getInstanceValue(EntityEnterpriseContext ctx)
76    {
77       FieldState fieldState = getFieldState(ctx);
78       if(!fieldState.isLoaded())
79       {
80          throw new EJBException JavaDoc("CMP 1.1 field not loaded: " + getFieldName());
81       }
82
83       try
84       {
85          return field.get(ctx.getInstance());
86       }
87       catch(Exception JavaDoc e)
88       {
89          // Non recoverable internal exception
90
throw new EJBException JavaDoc("Internal error getting instance field " +
91             getFieldName(), e);
92       }
93    }
94
95    public void setInstanceValue(EntityEnterpriseContext ctx, Object JavaDoc value)
96    {
97       try
98       {
99          field.set(ctx.getInstance(), value);
100          FieldState fieldState = getFieldState(ctx);
101          fieldState.setLoaded();
102          fieldState.setCheckDirty();
103       }
104       catch(Exception JavaDoc e)
105       {
106          // Non recoverable internal exception
107
throw new EJBException JavaDoc("Internal error setting instance field " +
108             getFieldName(), e);
109       }
110    }
111
112    public Object JavaDoc getLockedValue(EntityEnterpriseContext ctx)
113    {
114       throw new UnsupportedOperationException JavaDoc("Optimistic locking is not supported in CMP1.1.");
115    }
116
117    public void lockInstanceValue(EntityEnterpriseContext ctx)
118    {
119       // not supported
120
}
121
122    public boolean isLoaded(EntityEnterpriseContext ctx)
123    {
124       return getFieldState(ctx).isLoaded();
125    }
126
127    /**
128     * Has the value of this field changes since the last time clean was called.
129     */

130    public boolean isDirty(EntityEnterpriseContext ctx)
131    {
132       // read only and primary key fields are never dirty
133
if(isReadOnly() || isPrimaryKeyMember())
134       {
135          return false;
136       }
137
138       // has the value changes since setClean
139
return isLoaded(ctx) && !stateFactory.isStateValid(getInstanceValue(ctx), getFieldState(ctx).originalValue);
140    }
141
142    /**
143     * Mark this field as clean.
144     * Saves the current state in context, so it can be compared when
145     * isDirty is called.
146     */

147    public void setClean(EntityEnterpriseContext ctx)
148    {
149       FieldState fieldState = getFieldState(ctx);
150       fieldState.originalValue = getInstanceValue(ctx);
151
152       // update last read time
153
if(isReadOnly())
154       {
155          fieldState.lastRead = System.currentTimeMillis();
156       }
157    }
158
159    public boolean isReadTimedOut(EntityEnterpriseContext ctx)
160    {
161       // if we are read/write then we are always timed out
162
if(!isReadOnly())
163       {
164          return true;
165       }
166
167       // if read-time-out is -1 then we never time out.
168
if(getReadTimeOut() == -1)
169       {
170          return false;
171       }
172
173       long readInterval = System.currentTimeMillis() -
174          getFieldState(ctx).lastRead;
175       return readInterval >= getReadTimeOut();
176    }
177
178    public void resetPersistenceContext(EntityEnterpriseContext ctx)
179    {
180       if(isReadTimedOut(ctx))
181       {
182          JDBCContext jdbcCtx = (JDBCContext)ctx.getPersistenceContext();
183          FieldState fieldState = (FieldState)jdbcCtx.getFieldState(jdbcContextIndex);
184          if(fieldState != null)
185             fieldState.reset();
186       }
187    }
188
189    protected void setDirtyAfterGet(EntityEnterpriseContext ctx)
190    {
191       getFieldState(ctx).setCheckDirty();
192    }
193
194    private FieldState getFieldState(EntityEnterpriseContext ctx)
195    {
196       JDBCContext jdbcCtx = (JDBCContext)ctx.getPersistenceContext();
197       FieldState fieldState = (FieldState)jdbcCtx.getFieldState(jdbcContextIndex);
198       if(fieldState == null)
199       {
200          fieldState = new FieldState(jdbcCtx);
201          jdbcCtx.setFieldState(jdbcContextIndex, fieldState);
202       }
203       return fieldState;
204    }
205
206    private class FieldState
207    {
208       private Object JavaDoc originalValue;
209       private long lastRead = -1;
210       private JDBCEntityBridge.EntityState entityState;
211
212       public FieldState(JDBCContext jdbcContext)
213       {
214          this.entityState = jdbcContext.getEntityState();
215       }
216
217       public boolean isLoaded()
218       {
219          return entityState.isLoaded(tableIndex);
220       }
221
222       public void setLoaded()
223       {
224          entityState.setLoaded(tableIndex);
225       }
226
227       public void setCheckDirty()
228       {
229          entityState.setCheckDirty(tableIndex);
230       }
231
232       public void reset()
233       {
234          originalValue = null;
235          lastRead = -1;
236          entityState.resetFlags(tableIndex);
237          log.debug("reset field state");
238       }
239    }
240 }
241
Popular Tags