User: dsundstrom
Date: 02/01/15 12:40:15
Modified: src/main/org/jboss/ejb/plugins/cmp/bridge EntityBridge.java
EntityBridgeInvocationHandler.java
SelectorBridge.java
Added: src/main/org/jboss/ejb/plugins/cmp/bridge FieldBridge.java
Removed: src/main/org/jboss/ejb/plugins/cmp/bridge
CMPFieldBridge.java CMRFieldBridge.java
Log:
Merged CMR field and CMP field interfaces, because from the view of the
invocation interceptor a field is a field is a field.
Converted field arrays to Lists for better safty and easier handling.
Revision Changes Path
1.5 +5 -23
jboss/src/main/org/jboss/ejb/plugins/cmp/bridge/EntityBridge.java
Index: EntityBridge.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/bridge/EntityBridge.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- EntityBridge.java 2001/11/02 18:21:22 1.4
+++ EntityBridge.java 2002/01/15 20:40:15 1.5
@@ -7,7 +7,8 @@
package org.jboss.ejb.plugins.cmp.bridge;
-import org.jboss.ejb.EntityEnterpriseContext;
+import java.util.Collection;
+import java.util.List;
/**
* EntityBridge follows the Bridge pattern [Gamma et. al, 1995].
@@ -23,30 +24,11 @@
* One per cmp entity bean type.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a>
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
*/
public interface EntityBridge {
public String getEntityName();
- public Class getPrimaryKeyClass();
- public CMPFieldBridge[] getPrimaryKeyFields();
-
- public CMPFieldBridge[] getCMPFields();
- public CMRFieldBridge[] getCMRFields();
- public SelectorBridge[] getSelectors();
-
- /**
- * Has the instance in the context passed through ejbCreate?
- */
- public boolean isCreated(EntityEnterpriseContext ctx);
-
- /**
- * Mark each field every as clean.
- */
- public void setClean(EntityEnterpriseContext ctx);
-
- /**
- * Get every field that isDirty
- */
- public CMPFieldBridge[] getDirtyFields(EntityEnterpriseContext ctx);
+ public List getFields();
+ public Collection getSelectors();
}
1.11 +58 -141
jboss/src/main/org/jboss/ejb/plugins/cmp/bridge/EntityBridgeInvocationHandler.java
Index: EntityBridgeInvocationHandler.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/bridge/EntityBridgeInvocationHandler.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- EntityBridgeInvocationHandler.java 2001/12/12 19:43:11 1.10
+++ EntityBridgeInvocationHandler.java 2002/01/15 20:40:15 1.11
@@ -10,8 +10,10 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.util.Map;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
import javax.ejb.EJBException;
@@ -33,16 +35,15 @@
* One per cmp entity bean instance, including beans in pool.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a>
- * @version $Revision: 1.10 $
+ * @version $Revision: 1.11 $
*/
public class EntityBridgeInvocationHandler implements InvocationHandler {
- protected EntityContainer container;
- protected EntityBridge entityBridge;
- protected Class beanClass;
- protected EntityEnterpriseContext ctx;
- protected Map cmpFieldMap;
- protected Map cmrFieldMap;
- protected Map selectorMap;
+ private EntityContainer container;
+ private EntityBridge entityBridge;
+ private Class beanClass;
+ private EntityEnterpriseContext ctx;
+ private Map fieldMap;
+ private Map selectorMap;
/**
* Creates an invocation handler for the specified entity.
@@ -61,10 +62,7 @@
this.entityBridge = entityBridge;
this.beanClass = beanClass;
- Map abstractAccessors = getAbstractAccessors();
- setupCMPFieldMap(abstractAccessors);
- setupCMRFieldMap(abstractAccessors);
-
+ setupFieldMap();
setupSelectorMap();
}
@@ -79,53 +77,27 @@
throws Throwable {
String methodName = method.getName();
-
- // is this a cmp field accessor
- CMPFieldBridge cmpField = (CMPFieldBridge) cmpFieldMap.get(method);
- if(cmpField != null) {
+
+ // is this a field accessor
+ FieldBridge field = (FieldBridge) fieldMap.get(method);
+ if(field != null) {
if(methodName.startsWith("get")) {
- return cmpField.getInstanceValue(ctx);
+ return field.getInstanceValue(ctx);
} else if(methodName.startsWith("set")) {
- if(cmpField.isReadOnly()) {
+ if(field.isReadOnly()) {
throw new EJBException("Field is read-only: " +
- cmpField.getFieldName());
+ field.getFieldName());
}
- if(cmpField.isPrimaryKeyMember() && entityBridge.isCreated(ctx)) {
- throw new IllegalStateException("A field that is a member " +
- "of the primary key can only be set in ejbCreate");
- }
-
- cmpField.setInstanceValue(ctx, args[0]);
+ field.setInstanceValue(ctx, args[0]);
return null;
}
- Exception e = new EJBException("Unknown cmp field method: " +
+ Exception e = new EJBException("Unknown field method: " +
methodName);
e.printStackTrace();
throw e;
}
- CMRFieldBridge cmrField = (CMRFieldBridge) cmrFieldMap.get(method);
- if(cmrField != null) {
- if(methodName.startsWith("get")) {
- return cmrField.getValue(ctx);
- } else if(methodName.startsWith("set")) {
- if(cmrField.isReadOnly()) {
- throw new EJBException("Field is read-only: " +
- cmrField.getFieldName());
- }
- if(!entityBridge.isCreated(ctx)) {
- throw new IllegalStateException("A CMR field cannot be set " +
- "in ejbCreate; this should be done in the ejbPostCreate " +
- "method instead.");
- }
-
- cmrField.setValue(ctx, args[0]);
- return null;
- }
- throw new EJBException("Unknown cmr field method: " + methodName);
- }
-
SelectorBridge selector = (SelectorBridge) selectorMap.get(method);
if(selector != null) {
container.synchronizeEntitiesWithinTransaction(ctx.getTransaction());
@@ -137,7 +109,7 @@
throw e;
}
- protected Map getAbstractAccessors() {
+ private Map getAbstractAccessors() {
Method[] methods = beanClass.getMethods();
Map abstractAccessors = new HashMap(methods.length);
@@ -152,106 +124,51 @@
return abstractAccessors;
}
- protected void setupCMPFieldMap(Map abstractAccessors)
- throws DeploymentException {
+ private void setupFieldMap() throws DeploymentException {
- CMPFieldBridge[] cmpFields = entityBridge.getCMPFields();
- cmpFieldMap = new HashMap(cmpFields.length * 2);
-
- for(int i=0; i<cmpFields.length; i++) {
- setupCMPFieldGetter(abstractAccessors, cmpFields[i]);
- setupCMPFieldSetter(abstractAccessors, cmpFields[i]);
- }
- }
-
- protected void setupCMPFieldGetter(
- Map abstractAccessors,
- CMPFieldBridge cmpField) throws DeploymentException {
-
- String fieldName = cmpField.getFieldName();
- String getterName = "get" + Character.toUpperCase(fieldName.charAt(0)) +
- fieldName.substring(1);
-
- Method getterMethod = (Method)abstractAccessors.get(getterName);
- if(getterMethod != null) {
- cmpFieldMap.put(getterMethod, cmpField);
- abstractAccessors.remove(getterName);
- } else {
- throw new DeploymentException("No getter found for cmp field: " +
- fieldName);
- }
- }
-
- protected void setupCMPFieldSetter(
- Map abstractAccessors,
- CMPFieldBridge cmpField) throws DeploymentException {
+ Map abstractAccessors = getAbstractAccessors();
- String fieldName = cmpField.getFieldName();
- String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) +
+ Collection fields = entityBridge.getFields();
+ fieldMap = new HashMap(fields.size() * 2);
+ for(Iterator iter = fields.iterator(); iter.hasNext();) {
+ FieldBridge field = (FieldBridge)iter.next();
+
+ // get the names
+ String fieldName = field.getFieldName();
+ String fieldBaseName = Character.toUpperCase(fieldName.charAt(0)) +
fieldName.substring(1);
-
- Method setterMethod = (Method)abstractAccessors.get(setterName);
- if(setterMethod != null) {
- cmpFieldMap.put(setterMethod, cmpField);
- abstractAccessors.remove(setterName);
- } else {
- throw new DeploymentException("No setter found for cmp field: " +
- fieldName);
- }
- }
-
- protected void setupCMRFieldMap(Map abstractAccessors)
- throws DeploymentException {
-
- CMRFieldBridge[] cmrFields = entityBridge.getCMRFields();
- cmrFieldMap = new HashMap(cmrFields.length * 2);
+ String getterName = "get" + fieldBaseName;
+ String setterName = "set" + fieldBaseName;
- for(int i=0; i<cmrFields.length; i++) {
- // in unidirectional relationships only one side has
- // a field name
- if(cmrFields[i].getFieldName() != null) {
- setupCMRFieldGetter(abstractAccessors, cmrFields[i]);
- setupCMRFieldSetter(abstractAccessors, cmrFields[i]);
+ // get the accessor methods
+ Method getterMethod = (Method)abstractAccessors.get(getterName);
+ Method setterMethod = (Method)abstractAccessors.get(setterName);
+
+ // getters and setters must come in pairs
+ if(getterMethod != null && setterMethod == null) {
+ throw new DeploymentException("Getter was found but, no setter " +
+ "was found for field: " + fieldName);
+ } else if(getterMethod == null && setterMethod != null) {
+ throw new DeploymentException("Setter was found but, no getter " +
+ "was found for field: " + fieldName);
+ } else if(getterMethod != null && setterMethod != null) {
+ // add methods
+ fieldMap.put(getterMethod, field);
+ fieldMap.put(setterMethod, field);
+
+ // remove the accessors (they have been used)
+ abstractAccessors.remove(getterName);
+ abstractAccessors.remove(setterName);
}
}
}
-
- protected void setupCMRFieldGetter(
- Map abstractAccessors,
- CMRFieldBridge cmrField) throws DeploymentException {
-
- String fieldName = cmrField.getFieldName();
- String getterName = "get" + Character.toUpperCase(fieldName.charAt(0)) +
- fieldName.substring(1);
-
- Method getterMethod = (Method)abstractAccessors.get(getterName);
- if(getterMethod != null) {
- cmrFieldMap.put(getterMethod, cmrField);
- abstractAccessors.remove(getterName);
- }
- }
-
- protected void setupCMRFieldSetter(
- Map abstractAccessors,
- CMRFieldBridge cmrField) throws DeploymentException {
-
- String fieldName = cmrField.getFieldName();
- String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) +
- fieldName.substring(1);
-
- Method setterMethod = (Method)abstractAccessors.get(setterName);
- if(setterMethod != null) {
- cmrFieldMap.put(setterMethod, cmrField);
- abstractAccessors.remove(setterName);
- }
- }
-
- protected void setupSelectorMap() {
- SelectorBridge[] selectors = entityBridge.getSelectors();
- selectorMap = new HashMap(selectors.length);
- for(int i=0; i<selectors.length; i++) {
- selectorMap.put(selectors[i].getMethod(), selectors[i]);
+ private void setupSelectorMap() {
+ Collection selectors = entityBridge.getSelectors();
+ selectorMap = new HashMap(selectors.size());
+ for(Iterator iter = selectors.iterator(); iter.hasNext();) {
+ SelectorBridge selector = (SelectorBridge)iter.next();
+ selectorMap.put(selector.getMethod(), selector);
}
}
}
1.5 +2 -3
jboss/src/main/org/jboss/ejb/plugins/cmp/bridge/SelectorBridge.java
Index: SelectorBridge.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/bridge/SelectorBridge.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SelectorBridge.java 2001/09/01 22:03:14 1.4
+++ SelectorBridge.java 2002/01/15 20:40:15 1.5
@@ -20,12 +20,11 @@
* One for each entity bean ejbSelect method.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a>
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
*/
public interface SelectorBridge {
public String getSelectorName();
public Method getMethod();
- public Class getReturnType();
public Object execute(Object[] args) throws FinderException;
-}
\ No newline at end of file
+}
1.1 jboss/src/main/org/jboss/ejb/plugins/cmp/bridge/FieldBridge.java
Index: FieldBridge.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ejb.plugins.cmp.bridge;
import org.jboss.ejb.EntityEnterpriseContext;
/**
* FieldBridge represents one field for one entity.
*
* Life-cycle:
* Tied to the EntityBridge.
*
* Multiplicity:
* One for each entity bean field.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a>
* @version $Revision: 1.1 $
*/
public interface FieldBridge {
/**
* Gets the name of this field.
* @return the name of this field
*/
public String getFieldName();
/**
* Is this field read only.
* @return true if this field is read only
*/
public boolean isReadOnly();
/**
* Gets the value of this field for the specified instance context.
* @param ctx the context for which this field's value should be fetched
* @return the value of this field
*/
public Object getInstanceValue(EntityEnterpriseContext ctx);
/**
* Sets the value of this field for the specified instance context.
* @param ctx the context for which this field's value should be set
* @param value the new value of this field
*/
public void setInstanceValue(EntityEnterpriseContext ctx, Object value);
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development