1 22 package org.jboss.ejb.plugins.cmp.jdbc2; 23 24 import org.jboss.ejb.plugins.AbstractInterceptor; 25 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMRFieldBridge2; 26 import org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRInvocation; 27 import org.jboss.ejb.EntityContainer; 28 import org.jboss.ejb.Container; 29 import org.jboss.ejb.EntityEnterpriseContext; 30 import org.jboss.logging.Logger; 31 import org.jboss.invocation.Invocation; 32 import org.jboss.invocation.LocalEJBInvocation; 33 34 import javax.ejb.EJBException ; 35 import java.io.Serializable ; 36 import java.io.ObjectStreamException ; 37 38 42 public class RelationInterceptor 43 extends AbstractInterceptor 44 { 45 private Logger log; 46 47 49 public void setContainer(Container container) 50 { 51 this.container = (EntityContainer)container; 52 if(container != null) 53 { 54 log = Logger.getLogger( 55 this.getClass().getName() + 56 "." + 57 container.getBeanMetaData().getEjbName()); 58 } 59 } 60 61 63 public Object invoke(Invocation mi) throws Exception 64 { 65 if(!(mi instanceof CMRInvocation)) 66 { 67 return getNext().invoke(mi); 68 } 69 70 org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage msg = ((CMRInvocation)mi).getCmrMessage(); 71 72 EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext(); 74 JDBCCMRFieldBridge2 cmrField = (JDBCCMRFieldBridge2)mi.getArguments()[0]; 75 76 if(org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage.ADD_RELATION == msg) 77 { 78 Object relatedId = mi.getArguments()[1]; 79 if(log.isTraceEnabled()) 80 { 81 log.trace("Add relation: field=" + cmrField.getFieldName() + 82 " id=" + ctx.getId() + 83 " relatedId=" + relatedId); 84 } 85 86 cmrField.addRelatedId(ctx, relatedId); 87 } 88 else if(org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage.REMOVE_RELATION == msg) 89 { 90 Object relatedId = mi.getArguments()[1]; 92 if(log.isTraceEnabled()) 93 { 94 log.trace("Remove relation: field=" + cmrField.getFieldName() + 95 " id=" + ctx.getId() + 96 " relatedId=" + relatedId); 97 } 98 99 cmrField.removeRelatedId(ctx, relatedId); 100 } 101 else 102 { 103 throw new EJBException ("Unknown cmp2.0-relationship-message=" + msg); 105 } 106 107 return null; 108 } 109 110 112 public static class RelationInvocation extends LocalEJBInvocation 113 { 114 public final CMRMessage msg; 115 116 public RelationInvocation(CMRMessage msg) 117 { 118 this.msg = msg; 119 } 120 } 121 122 public static final class CMRMessage implements Serializable 123 { 124 private static int nextOrdinal = 0; 125 private static final CMRMessage[] VALUES = new CMRMessage[5]; 126 127 public static final CMRMessage ADD_RELATED_ID = new CMRMessage("ADD_RELATED_ID"); 128 public static final CMRMessage REMOVE_RELATED_ID = new CMRMessage("REMOVE_RELATED_ID"); 129 public static final CMRMessage DESTROY_EXISTING_RELATIONSHIPS = new CMRMessage("DESTROY_EXISTING_RELATIONSHIPS"); 130 131 private final transient String name; 132 private final int ordinal; 133 134 private CMRMessage(String name) 135 { 136 this.name = name; 137 this.ordinal = nextOrdinal++; 138 VALUES[ordinal] = this; 139 } 140 141 public String toString() 142 { 143 return name; 144 } 145 146 Object readResolve() throws ObjectStreamException 147 { 148 return VALUES[ordinal]; 149 } 150 } 151 } 152 | Popular Tags |