001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc2;
023:
024: import org.jboss.ejb.plugins.AbstractInterceptor;
025: import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMRFieldBridge2;
026: import org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRInvocation;
027: import org.jboss.ejb.EntityContainer;
028: import org.jboss.ejb.Container;
029: import org.jboss.ejb.EntityEnterpriseContext;
030: import org.jboss.logging.Logger;
031: import org.jboss.invocation.Invocation;
032: import org.jboss.invocation.LocalEJBInvocation;
033:
034: import javax.ejb.EJBException;
035: import java.io.Serializable;
036: import java.io.ObjectStreamException;
037:
038: /**
039: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
040: * @version <tt>$Revision: 57209 $</tt>
041: */
042: public class RelationInterceptor extends AbstractInterceptor {
043: private Logger log;
044:
045: // AbstractInterceptor overrides
046:
047: public void setContainer(Container container) {
048: this .container = (EntityContainer) container;
049: if (container != null) {
050: log = Logger.getLogger(this .getClass().getName() + "."
051: + container.getBeanMetaData().getEjbName());
052: }
053: }
054:
055: // Interceptor implementation
056:
057: public Object invoke(Invocation mi) throws Exception {
058: if (!(mi instanceof CMRInvocation)) {
059: return getNext().invoke(mi);
060: }
061:
062: org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage msg = ((CMRInvocation) mi)
063: .getCmrMessage();
064:
065: // We are going to work with the context a lot
066: EntityEnterpriseContext ctx = (EntityEnterpriseContext) mi
067: .getEnterpriseContext();
068: JDBCCMRFieldBridge2 cmrField = (JDBCCMRFieldBridge2) mi
069: .getArguments()[0];
070:
071: if (org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage.ADD_RELATION == msg) {
072: Object relatedId = mi.getArguments()[1];
073: if (log.isTraceEnabled()) {
074: log.trace("Add relation: field="
075: + cmrField.getFieldName() + " id="
076: + ctx.getId() + " relatedId=" + relatedId);
077: }
078:
079: cmrField.addRelatedId(ctx, relatedId);
080: } else if (org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage.REMOVE_RELATION == msg) {
081: // call removeRelation
082: Object relatedId = mi.getArguments()[1];
083: if (log.isTraceEnabled()) {
084: log.trace("Remove relation: field="
085: + cmrField.getFieldName() + " id="
086: + ctx.getId() + " relatedId=" + relatedId);
087: }
088:
089: cmrField.removeRelatedId(ctx, relatedId);
090: } else {
091: // this should not be possible we are using a type safe enum
092: throw new EJBException(
093: "Unknown cmp2.0-relationship-message=" + msg);
094: }
095:
096: return null;
097: }
098:
099: // Inner
100:
101: public static class RelationInvocation extends LocalEJBInvocation {
102: public final CMRMessage msg;
103:
104: public RelationInvocation(CMRMessage msg) {
105: this .msg = msg;
106: }
107: }
108:
109: public static final class CMRMessage implements Serializable {
110: private static final long serialVersionUID = -5471105958369857594L;
111:
112: private static int nextOrdinal = 0;
113: private static final CMRMessage[] VALUES = new CMRMessage[5];
114:
115: public static final CMRMessage ADD_RELATED_ID = new CMRMessage(
116: "ADD_RELATED_ID");
117: public static final CMRMessage REMOVE_RELATED_ID = new CMRMessage(
118: "REMOVE_RELATED_ID");
119: public static final CMRMessage DESTROY_EXISTING_RELATIONSHIPS = new CMRMessage(
120: "DESTROY_EXISTING_RELATIONSHIPS");
121:
122: private final transient String name;
123: private final int ordinal;
124:
125: private CMRMessage(String name) {
126: this .name = name;
127: this .ordinal = nextOrdinal++;
128: VALUES[ordinal] = this ;
129: }
130:
131: public String toString() {
132: return name;
133: }
134:
135: Object readResolve() throws ObjectStreamException {
136: return VALUES[ordinal];
137: }
138: }
139: }
|