KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > message > manager > NonExtensibleMessageManagerPoolImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

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 /**
37  * This class provides an implementation of message managers for non extensible
38  * messages. It pools message instances.
39  * <p>
40  * The class of messages to be managed is given using the
41  * <code>NonExtensibleMessageManagerAttributeController</code>.
42  */

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   // the id of this message manager.
55
private short id;
56
57   // The name of the class of managed messages
58
private String JavaDoc className = null;
59   // The class of messages managed by this message manager
60
private Class JavaDoc messageClass;
61
62   private final Object JavaDoc lock = new Object JavaDoc();
63
64   // ---------------------------------------------------------------------------
65
// Implementation of the MessageManager interface
66
// ---------------------------------------------------------------------------
67

68   /**
69    * @see MessageManager#createMessage(MessageType)
70    */

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 JavaDoc e)
85         {
86           // Should not happen since it has been tested in the setClassName
87
// method
88
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   /**
103    * @see MessageManager#deleteMessage(Message)
104    */

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   /**
126    * @see MessageManager#duplicateMessage(Message, boolean)
127    */

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   /**
141    * @see MessageManager#createChunk(ChunkType)
142    */

143   public Object JavaDoc createChunk(ChunkType type) throws UnknownChunkTypeError
144   {
145     Error
146         .error("This message manager handle only inextensible message", logger);
147     return null;
148   }
149
150   /**
151    * @see MessageManager#deleteChunk(Object)
152    */

153   public void deleteChunk(Object JavaDoc chunk)
154   {
155     Error
156         .error("This message manager handle only inextensible message", logger);
157   }
158
159   /**
160    * @see MessageManager#duplicateChunk(Object, boolean)
161    */

162   public Object JavaDoc duplicateChunk(Object JavaDoc chunk, boolean clone)
163   {
164     Error
165         .error("This message manager handle only inextensible message", logger);
166     return null;
167   }
168
169   /**
170    * @see MessageManager#getMessageManagerId()
171    */

172   public short getMessageManagerId()
173   {
174     return id;
175   }
176
177   // ---------------------------------------------------------------------------
178
// Implementation of the MessageManagerAttributeController interface
179
// ---------------------------------------------------------------------------
180

181   /**
182    * @see MessageManagerAttributeController#getId()
183    */

184   public short getId()
185   {
186     return id;
187   }
188
189   /**
190    * @see MessageManagerAttributeController#setId(short)
191    */

192   public void setId(short id)
193   {
194     this.id = id;
195   }
196
197   // ---------------------------------------------------------------------------
198
// Implementation of the MessageManagerPoolAttributeController interface
199
// ---------------------------------------------------------------------------
200

201   /**
202    * @see MessageManagerPoolAttributeController#getCapacity()
203    */

204   public int getCapacity()
205   {
206     return capacity;
207   }
208
209   /**
210    * @see MessageManagerPoolAttributeController#setCapacity(int)
211    */

212   public void setCapacity(int capacity)
213   {
214     // TODO try to re-use previous instance
215
synchronized (lock)
216     {
217       this.capacity = capacity;
218       pool = new Message[capacity];
219       elementCount = 0;
220     }
221   }
222
223   // ---------------------------------------------------------------------------
224
// Implementation of the NonExtensibleMessageManagerAttributeController
225
// interface
226
// ---------------------------------------------------------------------------
227

228   /**
229    * @see NonExtensibleMessageManagerAttributeController#getMessageClassName()
230    */

231   public String JavaDoc getMessageClassName()
232   {
233     return className;
234   }
235
236   /**
237    * @see NonExtensibleMessageManagerAttributeController#setMessageClassName(String)
238    */

239   public void setMessageClassName(String JavaDoc 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 JavaDoc(
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       // Test that the class is correct
255
Object JavaDoc o = messageClass.newInstance();
256       if (!(o instanceof Message))
257       {
258         new IllegalArgumentException JavaDoc("Invalid class " + className
259             + " not a Message");
260       }
261       if (!(o instanceof MessageReferenceCounter))
262       {
263         new IllegalArgumentException JavaDoc(
264             "Invalid class "
265                 + className
266                 + " does not implement org.objectweb.dream.message.MessageReferenceCounter");
267       }
268     }
269     catch (Exception JavaDoc e)
270     {
271       new IllegalArgumentException JavaDoc("Invalid class " + className);
272     }
273   }
274
275   // ---------------------------------------------------------------------------
276
// Implementation of the BindingController interface
277
// ---------------------------------------------------------------------------
278
/**
279    * @see org.objectweb.fractal.api.control.BindingController#listFc()
280    */

281   public String JavaDoc[] listFc()
282   {
283     return EmptyStringArray.EMPTY_STRING_ARRAY;
284   }
285
286 }
Popular Tags