KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > TCMessageFactoryImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.protocol.tcm;
5
6 import org.apache.commons.lang.ArrayUtils;
7
8 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
9
10 import com.tc.bytes.TCByteBuffer;
11 import com.tc.io.TCByteBufferOutput;
12 import com.tc.io.TCByteBufferOutputStream;
13 import com.tc.object.session.SessionID;
14 import com.tc.object.session.SessionProvider;
15
16 import java.lang.reflect.Constructor JavaDoc;
17 import java.util.Map JavaDoc;
18
19 public class TCMessageFactoryImpl implements TCMessageFactory {
20   private static final Class JavaDoc[] SIG1 = new Class JavaDoc[] { MessageMonitor.class, TCByteBufferOutput.class,
21       MessageChannel.class, TCMessageType.class };
22   private static final Class JavaDoc[] SIG2 = new Class JavaDoc[] { SessionID.class, MessageMonitor.class,
23       MessageChannel.class, TCMessageHeader.class, TCByteBuffer[].class };
24   private static final TCMessageFinalizer NULL_FINALIZER = new NullFinalizer();
25
26   private final Map typeOnlyCstr = new ConcurrentReaderHashMap();
27   private final Map typeAndDataCstr = new ConcurrentReaderHashMap();
28   private final TCMessageFinalizer finalizer;
29   private final MessageMonitor monitor;
30   private final SessionProvider sessionProvider;
31
32   public TCMessageFactoryImpl(SessionProvider sessionProvider, MessageMonitor monitor) {
33     this(sessionProvider, monitor, NULL_FINALIZER);
34   }
35
36   public TCMessageFactoryImpl(SessionProvider sessionProvider, MessageMonitor monitor, TCMessageFinalizer finalizer) {
37     this.sessionProvider = sessionProvider;
38     this.monitor = monitor;
39     this.finalizer = finalizer;
40   }
41
42   public TCMessage createMessage(MessageChannel source, TCMessageType type) throws UnsupportedMessageTypeException {
43     return createMessage(lookupConstructor(type, typeOnlyCstr), new Object JavaDoc[] { monitor,
44         new TCByteBufferOutputStream(4, 4096, false), source, type });
45   }
46
47   public TCMessage createMessage(MessageChannel source, TCMessageType type, TCMessageHeader header, TCByteBuffer[] data) {
48     return createMessage(lookupConstructor(type, typeAndDataCstr), new Object JavaDoc[] { sessionProvider.getSessionID(), monitor, source, header, data });
49   }
50
51   public void addClassMapping(TCMessageType type, Class JavaDoc msgClass) {
52     if ((type == null) || (msgClass == null)) { throw new IllegalArgumentException JavaDoc(); }
53
54     Constructor JavaDoc cstr1 = getConstructor(msgClass, SIG1);
55     Constructor JavaDoc cstr2 = getConstructor(msgClass, SIG2);
56
57     synchronized (this) {
58       typeOnlyCstr.put(type, cstr1);
59       typeAndDataCstr.put(type, cstr2);
60     }
61   }
62
63   private static Constructor JavaDoc lookupConstructor(TCMessageType type, Map map) {
64     Constructor JavaDoc rv = (Constructor JavaDoc) map.get(type);
65     if (rv == null) { throw new RuntimeException JavaDoc("No class registerted for type " + type); }
66     return rv;
67   }
68
69   private static Constructor JavaDoc getConstructor(Class JavaDoc msgClass, Class JavaDoc[] signature) {
70     try {
71       return msgClass.getDeclaredConstructor(signature);
72     } catch (Exception JavaDoc e) {
73       throw new IllegalArgumentException JavaDoc(e.getClass().getName() + ": " + e.getMessage());
74     }
75   }
76
77   private TCMessage createMessage(Constructor JavaDoc cstr, Object JavaDoc[] args) {
78     try {
79       TCMessage rv = (TCMessage) cstr.newInstance(args);
80       finalizer.finalizeMessage(rv);
81       return rv;
82     } catch (Exception JavaDoc e) {
83       System.err.println("Args; " + ArrayUtils.toString(args));
84       throw new RuntimeException JavaDoc(e);
85     }
86   }
87
88   private static final class NullFinalizer implements TCMessageFinalizer {
89     public final void finalizeMessage(TCMessage message) {
90       return;
91     }
92   }
93
94 }
95
Popular Tags