KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > Marshaller


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.marshall;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.GlobalTransaction;
13 import org.jboss.cache.Region;
14 import org.jboss.cache.RegionManager;
15 import org.jboss.cache.buddyreplication.BuddyManager;
16
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.ObjectOutputStream JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.concurrent.ConcurrentHashMap JavaDoc;
22
23 /**
24  * Abstract Marshaller for JBoss Cache.
25  *
26  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
27  */

28 public abstract class Marshaller
29 {
30    protected boolean useRegionBasedMarshalling;
31    protected RegionManager regionManager;
32    protected boolean defaultInactive;
33    private static Log log = LogFactory.getLog(Marshaller.class);
34
35    /**
36     * Map<GlobalTransaction, Fqn> for prepared tx that have not committed
37     */

38    private Map JavaDoc<GlobalTransaction, String JavaDoc> transactions = new ConcurrentHashMap JavaDoc<GlobalTransaction, String JavaDoc>(16);
39
40    protected void init(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling)
41    {
42       this.useRegionBasedMarshalling = useRegionBasedMarshalling;
43       this.defaultInactive = defaultInactive;
44       this.regionManager = manager;
45    }
46
47    /**
48     * Implementation classes will need to marshall the object passed in and write the object
49     * into the given stream.
50     *
51     * @param obj
52     * @param out
53     * @throws Exception
54     */

55    public abstract void objectToStream(Object JavaDoc obj, ObjectOutputStream JavaDoc out) throws Exception JavaDoc;
56
57    /**
58     * Implementation classes will need to parse the given stream and create an object from it.
59     *
60     * @param in
61     * @throws Exception
62     */

63    public abstract Object JavaDoc objectFromStream(ObjectInputStream JavaDoc in) throws Exception JavaDoc;
64
65
66    /**
67     * This is "replicate" call with a single MethodCall argument.
68     *
69     * @param call
70     */

71    protected String JavaDoc extractFqnFromMethodCall(MethodCall call)
72    {
73       MethodCall c0 = (MethodCall) call.getArgs()[0];
74       return extractFqn(c0);
75    }
76
77    /**
78     * This is "replicate" call with a list of MethodCall argument.
79     *
80     * @param call
81     */

82    protected String JavaDoc extractFqnFromListOfMethodCall(MethodCall call)
83    {
84       Object JavaDoc[] args = call.getArgs();
85       // We simply pick the first one and assume everyone will need to operate under the same region!
86
MethodCall c0 = (MethodCall) ((List JavaDoc) args[0]).get(0);
87       return extractFqn(c0);
88    }
89
90    protected String JavaDoc extractFqn(MethodCall methodCall)
91    {
92       if (methodCall == null)
93       {
94          throw new NullPointerException JavaDoc("method call is null");
95       }
96
97       String JavaDoc fqnStr = null;
98       Object JavaDoc[] args = methodCall.getArgs();
99       switch (methodCall.getMethodId())
100       {
101          case MethodDeclarations.optimisticPrepareMethod_id:
102          case MethodDeclarations.prepareMethod_id:
103             // Prepare method has a list of modifications. We will just take the first one and extract.
104
List JavaDoc modifications = (List JavaDoc) args[1];
105             fqnStr = extractFqn((MethodCall) modifications.get(0));
106
107             // the last arg of a prepare call is the one-phase flag
108
boolean one_phase_commit = (Boolean JavaDoc) args[args.length - 1];
109
110             // If this is two phase commit, map the FQN to the GTX so
111
// we can find it when the commit/rollback comes through
112
if (!one_phase_commit)
113             {
114                transactions.put((GlobalTransaction) args[0], fqnStr);
115             }
116             break;
117          case MethodDeclarations.rollbackMethod_id:
118          case MethodDeclarations.commitMethod_id:
119             // We stored the fqn in the transactions map during the prepare phase
120
fqnStr = transactions.remove(args[0]);
121             break;
122          case MethodDeclarations.getPartialStateMethod_id:
123          case MethodDeclarations.dataGravitationMethod_id:
124          case MethodDeclarations.evictNodeMethodLocal_id:
125          case MethodDeclarations.evictVersionedNodeMethodLocal_id:
126             Fqn fqn = (Fqn) args[0];
127             fqnStr = fqn.toString();
128             break;
129          case MethodDeclarations.dataGravitationCleanupMethod_id:
130             Fqn fqn1 = (Fqn) args[1];
131             fqnStr = fqn1.toString();
132             break;
133          case MethodDeclarations.remoteAnnounceBuddyPoolNameMethod_id:
134          case MethodDeclarations.remoteAssignToBuddyGroupMethod_id:
135          case MethodDeclarations.remoteRemoveFromBuddyGroupMethod_id:
136             break;
137          case MethodDeclarations.replicateMethod_id:
138             // possible when we have a replication queue.
139
fqnStr = extractFqn((MethodCall) args[0]);
140             break;
141          default:
142             if (MethodDeclarations.isCrudMethod(methodCall.getMethodId()))
143             {
144                Fqn fqn2 = (Fqn) args[1];
145                fqnStr = fqn2.toString();
146             }
147             else
148             {
149                throw new IllegalArgumentException JavaDoc("Marshaller.extractFqn(): Unknown id in method call: " + methodCall);
150             }
151             break;
152
153       }
154
155       if (log.isTraceEnabled())
156       {
157          log.trace("extract(): received " + methodCall + "extracted fqn: " + fqnStr);
158       }
159
160       return fqnStr;
161    }
162
163    /**
164     * Retrieves the {@link Region} from the {@link RegionManager} after taking into account that this may be a Buddy Backup FQN
165     *
166     * @param fqnString
167     * @return
168     */

169    protected Region getRegion(String JavaDoc fqnString)
170    {
171       Fqn fqn = Fqn.fromString(fqnString);
172
173       if (BuddyManager.isBackupFqn(fqn))
174       {
175          // Strip out the buddy group portion
176
fqn = fqn.getFqnChild(2, fqn.size());
177       }
178       return regionManager.getRegion(fqn, false);
179    }
180
181    /**
182     * Gets the classloader previously registered for <code>fqn</code>.
183     *
184     * @param fqn the fqn
185     * @return the classloader associated with the cache region rooted by
186     * <code>fqn</code>, or <code>null</code> if no classloader has
187     * been associated with the region.
188     */

189    public ClassLoader JavaDoc getClassLoader(String JavaDoc fqn)
190    {
191       ClassLoader JavaDoc result = null;
192       Region region = regionManager.getRegion(Fqn.fromString(fqn), false);
193       if (region != null) result = region.getClassLoader();
194       return result;
195    }
196
197    /**
198     * Gets whether unmarshalling has been disabled for the region
199     * rooted in the given Fqn.
200     *
201     * @param fqn
202     * @return <code>true</code> if unmarshalling is disabled;
203     * <code>false</code> otherwise.
204     */

205    public boolean isInactive(String JavaDoc fqn)
206    {
207       Region region = regionManager.getRegion(Fqn.fromString(fqn), false);
208       return region == null ? defaultInactive : !region.isActive();
209    }
210 }
211
Popular Tags