1 24 25 package org.objectweb.dream.message.manager; 26 27 import org.objectweb.dream.AbstractComponent; 28 import org.objectweb.dream.message.ChunkType; 29 import org.objectweb.dream.message.Message; 30 import org.objectweb.dream.message.MessageReferenceCounter; 31 import org.objectweb.dream.message.MessageType; 32 import org.objectweb.dream.util.EmptyStringArray; 33 import org.objectweb.dream.util.Error; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 36 43 public class NonExtensibleMessageManagerPoolImpl extends AbstractComponent 44 implements 45 MessageManager, 46 NonExtensibleMessageManagerPoolAttributeController 47 { 48 49 private Message pool[]; 50 private int elementCount = 0; 51 52 private int capacity; 53 54 private short id; 56 57 private String className = null; 59 private Class messageClass; 61 62 private final Object lock = new Object (); 63 64 68 71 public synchronized Message createMessage(MessageType type) 72 throws UnknownChunkTypeError 73 { 74 synchronized (lock) 75 { 76 if (elementCount == 0) 77 { 78 logger.log(BasicLevel.DEBUG, "Create new message"); 79 Message m = null; 80 try 81 { 82 m = (Message) messageClass.newInstance(); 83 } 84 catch (Exception e) 85 { 86 Error.bug(logger, e); 89 } 90 m.setMessageManagerId(id); 91 ((MessageReferenceCounter) m).incrementReferenceCounter(); 92 return m; 93 } 94 elementCount -= 1; 95 Message message = pool[elementCount]; 96 pool[elementCount] = null; 97 ((MessageReferenceCounter) message).incrementReferenceCounter(); 98 return message; 99 } 100 } 101 102 105 public synchronized void deleteMessage(Message message) 106 { 107 if (message != null) 108 { 109 if (((MessageReferenceCounter) message).decrementReferenceCounter()) 110 { 111 message.recycle(); 112 synchronized (lock) 113 { 114 if (elementCount == pool.length || message == null) 115 { 116 return; 117 } 118 pool[elementCount] = message; 119 elementCount += 1; 120 } 121 } 122 } 123 } 124 125 128 public Message duplicateMessage(Message message, boolean clone) 129 { 130 if (clone) 131 { 132 Message newInstace = createMessage(null); 133 message.transfertChunkStates(newInstace); 134 return newInstace; 135 } 136 ((MessageReferenceCounter) message).incrementReferenceCounter(); 137 return message; 138 } 139 140 143 public Object createChunk(ChunkType type) throws UnknownChunkTypeError 144 { 145 Error 146 .error("This message manager handle only inextensible message", logger); 147 return null; 148 } 149 150 153 public void deleteChunk(Object chunk) 154 { 155 Error 156 .error("This message manager handle only inextensible message", logger); 157 } 158 159 162 public Object duplicateChunk(Object chunk, boolean clone) 163 { 164 Error 165 .error("This message manager handle only inextensible message", logger); 166 return null; 167 } 168 169 172 public short getMessageManagerId() 173 { 174 return id; 175 } 176 177 181 184 public short getId() 185 { 186 return id; 187 } 188 189 192 public void setId(short id) 193 { 194 this.id = id; 195 } 196 197 201 204 public int getCapacity() 205 { 206 return capacity; 207 } 208 209 212 public void setCapacity(int capacity) 213 { 214 synchronized (lock) 216 { 217 this.capacity = capacity; 218 pool = new Message[capacity]; 219 elementCount = 0; 220 } 221 } 222 223 228 231 public String getMessageClassName() 232 { 233 return className; 234 } 235 236 239 public void setMessageClassName(String name) 240 { 241 if (className != null) 242 { 243 logger.log(BasicLevel.DEBUG, 244 "This message manager already manages messaeges of class " 245 + className); 246 new IllegalArgumentException ( 247 "This message manager already manages messaeges of class " 248 + className); 249 } 250 this.className = name; 251 try 252 { 253 messageClass = Class.forName(name); 254 Object o = messageClass.newInstance(); 256 if (!(o instanceof Message)) 257 { 258 new IllegalArgumentException ("Invalid class " + className 259 + " not a Message"); 260 } 261 if (!(o instanceof MessageReferenceCounter)) 262 { 263 new IllegalArgumentException ( 264 "Invalid class " 265 + className 266 + " does not implement org.objectweb.dream.message.MessageReferenceCounter"); 267 } 268 } 269 catch (Exception e) 270 { 271 new IllegalArgumentException ("Invalid class " + className); 272 } 273 } 274 275 281 public String [] listFc() 282 { 283 return EmptyStringArray.EMPTY_STRING_ARRAY; 284 } 285 286 } | Popular Tags |