KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc2 > RelationInterceptor


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.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 JavaDoc;
35 import java.io.Serializable JavaDoc;
36 import java.io.ObjectStreamException JavaDoc;
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 37459 $</tt>
41  */

42 public class RelationInterceptor
43    extends AbstractInterceptor
44 {
45    private Logger log;
46
47    // AbstractInterceptor overrides
48

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    // Interceptor implementation
62

63    public Object JavaDoc invoke(Invocation mi) throws Exception JavaDoc
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       // We are going to work with the context a lot
73
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 JavaDoc 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          // call removeRelation
91
Object JavaDoc 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          // this should not be possible we are using a type safe enum
104
throw new EJBException JavaDoc("Unknown cmp2.0-relationship-message=" + msg);
105       }
106
107       return null;
108    }
109
110    // Inner
111

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 JavaDoc
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 JavaDoc name;
132       private final int ordinal;
133
134       private CMRMessage(String JavaDoc name)
135       {
136          this.name = name;
137          this.ordinal = nextOrdinal++;
138          VALUES[ordinal] = this;
139       }
140
141       public String JavaDoc toString()
142       {
143          return name;
144       }
145
146       Object JavaDoc readResolve() throws ObjectStreamException JavaDoc
147       {
148          return VALUES[ordinal];
149       }
150    }
151 }
152
Popular Tags