1 24 25 package org.objectweb.dream.message.codec; 26 27 import java.io.DataInput ; 28 import java.io.DataOutput ; 29 import java.io.Externalizable ; 30 import java.io.IOException ; 31 import java.io.ObjectInput ; 32 import java.io.ObjectOutput ; 33 import java.util.Iterator ; 34 35 import org.objectweb.dream.message.Chunk; 36 import org.objectweb.dream.message.ChunkAlreadyExistsException; 37 import org.objectweb.dream.message.ChunkType; 38 import org.objectweb.dream.message.ExtensibleMessage; 39 import org.objectweb.dream.message.Message; 40 import org.objectweb.dream.message.MessageNC; 41 import org.objectweb.dream.message.MessageType; 42 import org.objectweb.dream.message.MessageTypeImpl; 43 import org.objectweb.dream.message.MessageTypeNCImpl; 44 import org.objectweb.dream.message.manager.MessageManager; 45 import org.objectweb.dream.pool.ObjectPool; 46 import org.objectweb.dream.pool.Recyclable; 47 import org.objectweb.dream.util.Error; 48 import org.objectweb.fractal.api.NoSuchInterfaceException; 49 import org.objectweb.fractal.api.control.IllegalBindingException; 50 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 51 52 66 public class MessageCodecOptimizedStreamImpl 67 extends 68 AbstractMessageCodecObjectStream 69 { 70 74 75 public static final String CHUNK_TYPE_POOL_ITF_NAME = "chunk-type-pool"; 76 protected ObjectPool chunkTypePoolItf = null; 77 78 82 85 protected Message doDecodeSingleMessage(DataInput input) throws IOException 86 { 87 ObjectInput objectInput = (ObjectInput ) input; 88 ExtensibleMessage message; 89 boolean messageNC = objectInput.readBoolean(); 90 if (messageNC) 91 { 92 message = (ExtensibleMessage) messageManagerItf 93 .createMessage(MessageTypeNCImpl.EMPTY_MESSAGE_TYPE_NC); 94 } 95 else 96 { 97 message = (ExtensibleMessage) messageManagerItf 98 .createMessage(MessageTypeImpl.EMPTY_MESSAGE_TYPE); 99 } 100 101 ChunkType chunkType = (ChunkType) chunkTypePoolItf.newInstance(); 102 boolean hasMoreChunk = objectInput.readBoolean(); 103 try 104 { 105 while (hasMoreChunk) 106 { 107 String chunkName = objectInput.readUTF(); 108 ((Externalizable ) chunkType).readExternal(objectInput); 109 Externalizable chunk = (Externalizable ) messageManagerItf 110 .createChunk(chunkType); 111 chunk.readExternal(objectInput); 112 message.addChunk(chunkName, ((Chunk) chunk).getType(), chunk); 113 hasMoreChunk = objectInput.readBoolean(); 114 } 115 } 116 catch (ClassNotFoundException e) 117 { 118 throw new IOException ("Unable to decode message : " + e.getMessage()); 119 } 120 catch (ChunkAlreadyExistsException e) 121 { 122 Error.bug(logger, e); 124 } 125 finally 126 { 127 chunkTypePoolItf.recycleInstance((Recyclable) chunkType); 128 } 129 message.setMessageManagerId(messageManagerItf.getMessageManagerId()); 130 return message; 131 } 132 133 137 protected void doEncodeSingleMessage(Message message, DataOutput output) 138 throws IOException 139 { 140 ObjectOutput objectOutput = (ObjectOutput ) output; 141 if (message instanceof MessageNC) 142 { 143 objectOutput.writeBoolean(true); 144 } 145 else 146 { 147 objectOutput.writeBoolean(false); 148 } 149 MessageType type = message.getMessageType(); 150 Iterator iterator = type.getChunkNamesIterator(); 151 while (iterator.hasNext()) 152 { 153 String chunkName = (String ) iterator.next(); 154 ChunkType chunkType = type.getChunkType(chunkName); 155 Object chunk = message.getChunk(chunkName); 156 objectOutput.writeBoolean(true); 157 objectOutput.writeUTF(chunkName); 158 ((Externalizable ) chunkType).writeExternal(objectOutput); 159 ((Externalizable ) chunk).writeExternal(objectOutput); 160 } 161 objectOutput.writeBoolean(false); 162 } 163 164 168 171 public String [] listFc() 172 { 173 return new String []{MessageManager.ITF_NAME, CHUNK_TYPE_POOL_ITF_NAME}; 174 } 175 176 180 public synchronized void bindFc(String clientItfName, Object serverItf) 181 throws NoSuchInterfaceException, IllegalBindingException, 182 IllegalLifeCycleException 183 { 184 super.bindFc(clientItfName, serverItf); 185 if (clientItfName.equals(CHUNK_TYPE_POOL_ITF_NAME)) 186 { 187 chunkTypePoolItf = (ObjectPool) serverItf; 188 } 189 } 190 191 } | Popular Tags |