KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > aop > WriteReplacer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.aop;
9
10 //import java.util.Iterator;
11
//import java.util.List;
12

13 import org.jboss.aop.Advised;
14 import org.jboss.aop.InstanceAdvisor;
15 import org.jboss.aop.advice.Interceptor;
16
17 import java.io.NotSerializableException JavaDoc;
18 import java.io.ObjectStreamException JavaDoc;
19
20 /**
21  * @author <a HREF="mailto:harald@gliebe.de">Harald Gliebe</a>
22  */

23
24 public class WriteReplacer implements WriteReplaceable /*, Interceptor*/
25 {
26
27    // interceptor
28
/*
29        public WriteReplacer() {
30        }
31
32        public String getName() {
33       return "WriteReplacer";
34        }
35
36        public InvocationResponse invoke(Invocation invocation) throws Throwable {
37       Method method = MethodInvocation.getMethod(invocation);
38
39       if (method != null
40           && method.getName().equals("writeReplace")
41           && method.getReturnType().equals(Object.class)
42           && method.getParameterTypes().length == 0) {
43           //hack
44           this.obj = MethodInvocation.getTargetObject(invocation);
45           this.writeReplace(); // fills Fieldvalues
46       }
47
48       return invocation.invokeNext();
49
50        }
51    */

52    // mixin
53

54    Object JavaDoc obj;
55
56    public WriteReplacer(Object JavaDoc obj)
57    {
58       this.obj = obj;
59    }
60
61    public Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
62    {
63       if (obj instanceof Advised) {
64          InstanceAdvisor advisor = ((Advised) obj)._getInstanceAdvisor();
65          org.jboss.aop.advice.Interceptor[] interceptors = advisor.getInterceptors();
66          CacheInterceptor cacheInterceptor = null;
67          for (int i = 0; i < interceptors.length; i++) {
68             if (interceptors[i] instanceof CacheInterceptor) {
69                cacheInterceptor = (CacheInterceptor) interceptors[i];
70                break;
71             }
72          }
73          if (cacheInterceptor != null) {
74             try {
75                cacheInterceptor.beforeSerialization(obj);
76             } catch (Exception JavaDoc e) {
77                e.printStackTrace();
78                throw new NotSerializableException JavaDoc(e.getMessage());
79             }
80          }
81       }
82       return obj;
83    }
84
85 }
86
Popular Tags