KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > pool > ObjectPoolImpl


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): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.pool;
26
27 import org.objectweb.dream.AbstractComponent;
28 import org.objectweb.dream.util.EmptyStringArray;
29 import org.objectweb.dream.util.Error;
30 import org.objectweb.util.monolog.api.BasicLevel;
31
32 /**
33  * This class provides an implementation of message managers for non extensible
34  * messages. It pools message instances.
35  * <p>
36  * The class of messages to be managed is given using the
37  * <code>NonExtensibleMessageManagerAttributeController</code>.
38  */

39 public class ObjectPoolImpl extends AbstractComponent
40     implements
41       ObjectPool,
42       ObjectPoolAttributeController
43 {
44   /** The pool of objects. */
45   private Recyclable pool[];
46
47   /** The number of elements stored in the pool. */
48   private int elementCount = 0;
49
50   /** The capacity of the pool. */
51   private int capacity;
52
53   /** The name of the class of managed objects. */
54   private String JavaDoc className = null;
55
56   /** The class of objects managed by this pool. */
57   private Class JavaDoc objectClass;
58
59   private final Object JavaDoc lock = new Object JavaDoc();
60
61   // ---------------------------------------------------------------------------
62
// Implementation of the ObjectPool interface
63
// ---------------------------------------------------------------------------
64

65   /**
66    * @see ObjectPool#newInstance()
67    */

68   public Recyclable newInstance()
69   {
70     Recyclable obj = null;
71     synchronized (lock)
72     {
73       if (elementCount == 0)
74       {
75         if (logger.isLoggable(BasicLevel.DEBUG))
76         {
77           logger.log(BasicLevel.DEBUG, "Creates new instance of class "
78               + className);
79         }
80         try
81         {
82           obj = (Recyclable) objectClass.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       }
91       else
92       {
93         elementCount -= 1;
94         obj = pool[elementCount];
95         pool[elementCount] = null;
96       }
97     }
98     return obj;
99   }
100
101   /**
102    * @see ObjectPool#recycleInstance(Recyclable)
103    */

104   public void recycleInstance(Recyclable recyclable)
105   {
106     if (recyclable != null && objectClass.isInstance(recyclable))
107     {
108       recyclable.recycle();
109       synchronized (lock)
110       {
111         if (elementCount == pool.length)
112         {
113           if (logger.isLoggable(BasicLevel.WARN))
114           {
115             logger.log(BasicLevel.WARN, "Pool Full");
116           }
117           return;
118         }
119         pool[elementCount] = recyclable;
120         elementCount += 1;
121       }
122     }
123   }
124
125   // ---------------------------------------------------------------------------
126
// Implementation of the PoolAttributeController interface
127
// ---------------------------------------------------------------------------
128

129   /**
130    * @see ObjectPoolAttributeController#getCapacity()
131    */

132   public int getCapacity()
133   {
134     return capacity;
135   }
136
137   /**
138    * @see ObjectPoolAttributeController#setCapacity(int)
139    */

140   public void setCapacity(int capacity)
141   {
142     // TODO try to re-use previous instance
143
synchronized (lock)
144     {
145       this.capacity = capacity;
146       pool = new Recyclable[capacity];
147       elementCount = 0;
148     }
149   }
150
151   /**
152    * @see ObjectPoolAttributeController#getObjectClassName()
153    */

154   public String JavaDoc getObjectClassName()
155   {
156     return className;
157   }
158
159   /**
160    * @see ObjectPoolAttributeController#setObjectClassName(String)
161    */

162   public void setObjectClassName(String JavaDoc name)
163   {
164     if (className != null)
165     {
166       logger.log(BasicLevel.ERROR,
167           "This object pool already manages objects of class " + className);
168       new IllegalArgumentException JavaDoc(
169           "This object pool already manages objects of class " + className);
170     }
171     this.className = name;
172     try
173     {
174       objectClass = Class.forName(name);
175       // Test that the class is correct
176
Recyclable r = (Recyclable) objectClass.newInstance();
177     }
178     catch (Exception JavaDoc e)
179     {
180       logger.log(BasicLevel.ERROR, "Invalid class " + className, e);
181       new IllegalArgumentException JavaDoc("Invalid class " + className);
182     }
183   }
184
185   // ---------------------------------------------------------------------------
186
// Implementation of the BindingController interface
187
// ---------------------------------------------------------------------------
188
/**
189    * @see org.objectweb.fractal.api.control.BindingController#listFc()
190    */

191   public String JavaDoc[] listFc()
192   {
193     return EmptyStringArray.EMPTY_STRING_ARRAY;
194   }
195
196 }
Popular Tags