Modified: websites/production/turbine/content/fulcrum/fulcrum-pool/apidocs/src-html/org/apache/fulcrum/pool/DefaultPoolService.html ============================================================================== --- websites/production/turbine/content/fulcrum/fulcrum-pool/apidocs/src-html/org/apache/fulcrum/pool/DefaultPoolService.html (original) +++ websites/production/turbine/content/fulcrum/fulcrum-pool/apidocs/src-html/org/apache/fulcrum/pool/DefaultPoolService.html Tue May 21 03:41:41 2019 @@ -1,655 +1,461 @@ -<HTML> -<BODY BGCOLOR="white"> -<PRE> -<FONT color="green">001</FONT> package org.apache.fulcrum.pool;<a name="line.1"></a> -<FONT color="green">002</FONT> <a name="line.2"></a> -<FONT color="green">003</FONT> /*<a name="line.3"></a> -<FONT color="green">004</FONT> * Licensed to the Apache Software Foundation (ASF) under one<a name="line.4"></a> -<FONT color="green">005</FONT> * or more contributor license agreements. See the NOTICE file<a name="line.5"></a> -<FONT color="green">006</FONT> * distributed with this work for additional information<a name="line.6"></a> -<FONT color="green">007</FONT> * regarding copyright ownership. The ASF licenses this file<a name="line.7"></a> -<FONT color="green">008</FONT> * to you under the Apache License, Version 2.0 (the<a name="line.8"></a> -<FONT color="green">009</FONT> * "License"); you may not use this file except in compliance<a name="line.9"></a> -<FONT color="green">010</FONT> * with the License. You may obtain a copy of the License at<a name="line.10"></a> -<FONT color="green">011</FONT> *<a name="line.11"></a> -<FONT color="green">012</FONT> * http://www.apache.org/licenses/LICENSE-2.0<a name="line.12"></a> -<FONT color="green">013</FONT> *<a name="line.13"></a> -<FONT color="green">014</FONT> * Unless required by applicable law or agreed to in writing,<a name="line.14"></a> -<FONT color="green">015</FONT> * software distributed under the License is distributed on an<a name="line.15"></a> -<FONT color="green">016</FONT> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY<a name="line.16"></a> -<FONT color="green">017</FONT> * KIND, either express or implied. See the License for the<a name="line.17"></a> -<FONT color="green">018</FONT> * specific language governing permissions and limitations<a name="line.18"></a> -<FONT color="green">019</FONT> * under the License.<a name="line.19"></a> -<FONT color="green">020</FONT> */<a name="line.20"></a> -<FONT color="green">021</FONT> <a name="line.21"></a> -<FONT color="green">022</FONT> import java.lang.reflect.Method;<a name="line.22"></a> -<FONT color="green">023</FONT> import java.util.ArrayList;<a name="line.23"></a> -<FONT color="green">024</FONT> import java.util.HashMap;<a name="line.24"></a> -<FONT color="green">025</FONT> import java.util.Iterator;<a name="line.25"></a> -<FONT color="green">026</FONT> import java.util.Map;<a name="line.26"></a> -<FONT color="green">027</FONT> import org.apache.avalon.framework.activity.Disposable;<a name="line.27"></a> -<FONT color="green">028</FONT> import org.apache.avalon.framework.activity.Initializable;<a name="line.28"></a> -<FONT color="green">029</FONT> import org.apache.avalon.framework.service.ServiceManager;<a name="line.29"></a> -<FONT color="green">030</FONT> import org.apache.avalon.framework.service.Serviceable;<a name="line.30"></a> -<FONT color="green">031</FONT> import org.apache.avalon.framework.configuration.Configurable;<a name="line.31"></a> -<FONT color="green">032</FONT> import org.apache.avalon.framework.configuration.Configuration;<a name="line.32"></a> -<FONT color="green">033</FONT> import org.apache.avalon.framework.logger.AbstractLogEnabled;<a name="line.33"></a> -<FONT color="green">034</FONT> import org.apache.fulcrum.factory.FactoryException;<a name="line.34"></a> -<FONT color="green">035</FONT> import org.apache.fulcrum.factory.FactoryService;<a name="line.35"></a> -<FONT color="green">036</FONT> <a name="line.36"></a> -<FONT color="green">037</FONT> /**<a name="line.37"></a> -<FONT color="green">038</FONT> * The Pool Service extends the Factory Service by adding support<a name="line.38"></a> -<FONT color="green">039</FONT> * for pooling instantiated objects. When a new instance is<a name="line.39"></a> -<FONT color="green">040</FONT> * requested, the service first checks its pool if one is available.<a name="line.40"></a> -<FONT color="green">041</FONT> * If the the pool is empty, a new instance will be requested<a name="line.41"></a> -<FONT color="green">042</FONT> * from the FactoryService.<a name="line.42"></a> -<FONT color="green">043</FONT> *<a name="line.43"></a> -<FONT color="green">044</FONT> * For objects implementing the Recyclable interface, a recycle<a name="line.44"></a> -<FONT color="green">045</FONT> * method will be called, when they taken from the pool, and<a name="line.45"></a> -<FONT color="green">046</FONT> * a dispose method, when they are returned to the pool.<a name="line.46"></a> -<FONT color="green">047</FONT> *<a name="line.47"></a> -<FONT color="green">048</FONT> * @author <a href="mailto:[email protected]">Ilkka Priha</a><a name="line.48"></a> -<FONT color="green">049</FONT> * @author <a href="mailto:[email protected]">Stephen McConnell</a><a name="line.49"></a> -<FONT color="green">050</FONT> * @version $Id: DefaultPoolService.java 670330 2008-06-22 09:37:21Z tv $<a name="line.50"></a> -<FONT color="green">051</FONT> *<a name="line.51"></a> -<FONT color="green">052</FONT> * @avalon.component name="pool" lifestyle="transient"<a name="line.52"></a> -<FONT color="green">053</FONT> * @avalon.service type="org.apache.fulcrum.pool.PoolService"<a name="line.53"></a> -<FONT color="green">054</FONT> */<a name="line.54"></a> -<FONT color="green">055</FONT> public class DefaultPoolService extends AbstractLogEnabled implements PoolService, Serviceable, Disposable, Initializable, Configurable<a name="line.55"></a> -<FONT color="green">056</FONT> {<a name="line.56"></a> -<FONT color="green">057</FONT> /**<a name="line.57"></a> -<FONT color="green">058</FONT> * The property specifying the pool capacity.<a name="line.58"></a> -<FONT color="green">059</FONT> */<a name="line.59"></a> -<FONT color="green">060</FONT> public static final String POOL_CAPACITY = "capacity";<a name="line.60"></a> -<FONT color="green">061</FONT> /**<a name="line.61"></a> -<FONT color="green">062</FONT> * An inner class for class specific pools.<a name="line.62"></a> -<FONT color="green">063</FONT> */<a name="line.63"></a> -<FONT color="green">064</FONT> private class PoolBuffer<a name="line.64"></a> -<FONT color="green">065</FONT> {<a name="line.65"></a> -<FONT color="green">066</FONT> /**<a name="line.66"></a> -<FONT color="green">067</FONT> * An inner class for cached recycle methods.<a name="line.67"></a> -<FONT color="green">068</FONT> */<a name="line.68"></a> -<FONT color="green">069</FONT> private class Recycler<a name="line.69"></a> -<FONT color="green">070</FONT> {<a name="line.70"></a> -<FONT color="green">071</FONT> /**<a name="line.71"></a> -<FONT color="green">072</FONT> * The method.<a name="line.72"></a> -<FONT color="green">073</FONT> */<a name="line.73"></a> -<FONT color="green">074</FONT> private final Method recycle;<a name="line.74"></a> -<FONT color="green">075</FONT> /**<a name="line.75"></a> -<FONT color="green">076</FONT> * The signature.<a name="line.76"></a> -<FONT color="green">077</FONT> */<a name="line.77"></a> -<FONT color="green">078</FONT> private final String[] signature;<a name="line.78"></a> -<FONT color="green">079</FONT> /**<a name="line.79"></a> -<FONT color="green">080</FONT> * Constructs a new recycler.<a name="line.80"></a> -<FONT color="green">081</FONT> *<a name="line.81"></a> -<FONT color="green">082</FONT> * @param rec the recycle method.<a name="line.82"></a> -<FONT color="green">083</FONT> * @param sign the signature.<a name="line.83"></a> -<FONT color="green">084</FONT> */<a name="line.84"></a> -<FONT color="green">085</FONT> public Recycler(Method rec, String[] sign)<a name="line.85"></a> -<FONT color="green">086</FONT> {<a name="line.86"></a> -<FONT color="green">087</FONT> recycle = rec;<a name="line.87"></a> -<FONT color="green">088</FONT> signature = (sign != null) && (sign.length > 0) ? sign : null;<a name="line.88"></a> -<FONT color="green">089</FONT> }<a name="line.89"></a> -<FONT color="green">090</FONT> /**<a name="line.90"></a> -<FONT color="green">091</FONT> * Matches the given signature against<a name="line.91"></a> -<FONT color="green">092</FONT> * that of the recycle method of this recycler.<a name="line.92"></a> -<FONT color="green">093</FONT> *<a name="line.93"></a> -<FONT color="green">094</FONT> * @param sign the signature.<a name="line.94"></a> -<FONT color="green">095</FONT> * @return the matching recycle method or null.<a name="line.95"></a> -<FONT color="green">096</FONT> */<a name="line.96"></a> -<FONT color="green">097</FONT> public Method match(String[] sign)<a name="line.97"></a> -<FONT color="green">098</FONT> {<a name="line.98"></a> -<FONT color="green">099</FONT> if ((sign != null) && (sign.length > 0))<a name="line.99"></a> -<FONT color="green">100</FONT> {<a name="line.100"></a> -<FONT color="green">101</FONT> if ((signature != null) && (sign.length == signature.length))<a name="line.101"></a> -<FONT color="green">102</FONT> {<a name="line.102"></a> -<FONT color="green">103</FONT> for (int i = 0; i < signature.length; i++)<a name="line.103"></a> -<FONT color="green">104</FONT> {<a name="line.104"></a> -<FONT color="green">105</FONT> if (!signature[i].equals(sign[i]))<a name="line.105"></a> -<FONT color="green">106</FONT> {<a name="line.106"></a> -<FONT color="green">107</FONT> return null;<a name="line.107"></a> -<FONT color="green">108</FONT> }<a name="line.108"></a> -<FONT color="green">109</FONT> }<a name="line.109"></a> -<FONT color="green">110</FONT> return recycle;<a name="line.110"></a> -<FONT color="green">111</FONT> }<a name="line.111"></a> -<FONT color="green">112</FONT> else<a name="line.112"></a> -<FONT color="green">113</FONT> {<a name="line.113"></a> -<FONT color="green">114</FONT> return null;<a name="line.114"></a> -<FONT color="green">115</FONT> }<a name="line.115"></a> -<FONT color="green">116</FONT> }<a name="line.116"></a> -<FONT color="green">117</FONT> else if (signature == null)<a name="line.117"></a> -<FONT color="green">118</FONT> {<a name="line.118"></a> -<FONT color="green">119</FONT> return recycle;<a name="line.119"></a> -<FONT color="green">120</FONT> }<a name="line.120"></a> -<FONT color="green">121</FONT> else<a name="line.121"></a> -<FONT color="green">122</FONT> {<a name="line.122"></a> -<FONT color="green">123</FONT> return null;<a name="line.123"></a> -<FONT color="green">124</FONT> }<a name="line.124"></a> -<FONT color="green">125</FONT> }<a name="line.125"></a> -<FONT color="green">126</FONT> }<a name="line.126"></a> -<FONT color="green">127</FONT> /**<a name="line.127"></a> -<FONT color="green">128</FONT> * A buffer for class instances.<a name="line.128"></a> -<FONT color="green">129</FONT> */<a name="line.129"></a> -<FONT color="green">130</FONT> private BoundedBuffer pool;<a name="line.130"></a> -<FONT color="green">131</FONT> /**<a name="line.131"></a> -<FONT color="green">132</FONT> * A flag to determine if a more efficient recycler is implemented.<a name="line.132"></a> -<FONT color="green">133</FONT> */<a name="line.133"></a> -<FONT color="green">134</FONT> private boolean arrayCtorRecyclable;<a name="line.134"></a> -<FONT color="green">135</FONT> /**<a name="line.135"></a> -<FONT color="green">136</FONT> * A cache for recycling methods.<a name="line.136"></a> -<FONT color="green">137</FONT> */<a name="line.137"></a> -<FONT color="green">138</FONT> private ArrayList recyclers;<a name="line.138"></a> -<FONT color="green">139</FONT> /**<a name="line.139"></a> -<FONT color="green">140</FONT> * Contructs a new pool buffer with a specific capacity.<a name="line.140"></a> -<FONT color="green">141</FONT> *<a name="line.141"></a> -<FONT color="green">142</FONT> * @param capacity a capacity.<a name="line.142"></a> -<FONT color="green">143</FONT> */<a name="line.143"></a> -<FONT color="green">144</FONT> public PoolBuffer(int capacity)<a name="line.144"></a> -<FONT color="green">145</FONT> {<a name="line.145"></a> -<FONT color="green">146</FONT> pool = new BoundedBuffer(capacity);<a name="line.146"></a> -<FONT color="green">147</FONT> }<a name="line.147"></a> -<FONT color="green">148</FONT> /**<a name="line.148"></a> -<FONT color="green">149</FONT> * Tells pool that it contains objects which can be<a name="line.149"></a> -<FONT color="green">150</FONT> * initialized using an Object array.<a name="line.150"></a> -<FONT color="green">151</FONT> *<a name="line.151"></a> -<FONT color="green">152</FONT> * @param isArrayCtor a <code>boolean</code> value<a name="line.152"></a> -<FONT color="green">153</FONT> */<a name="line.153"></a> -<FONT color="green">154</FONT> public void setArrayCtorRecyclable(boolean isArrayCtor)<a name="line.154"></a> -<FONT color="green">155</FONT> {<a name="line.155"></a> -<FONT color="green">156</FONT> arrayCtorRecyclable = isArrayCtor;<a name="line.156"></a> -<FONT color="green">157</FONT> }<a name="line.157"></a> -<FONT color="green">158</FONT> /**<a name="line.158"></a> -<FONT color="green">159</FONT> * Polls for an instance from the pool.<a name="line.159"></a> -<FONT color="green">160</FONT> *<a name="line.160"></a> -<FONT color="green">161</FONT> * @return an instance or null.<a name="line.161"></a> -<FONT color="green">162</FONT> */<a name="line.162"></a> -<FONT color="green">163</FONT> public Object poll(Object[] params, String[] signature) throws PoolException<a name="line.163"></a> -<FONT color="green">164</FONT> {<a name="line.164"></a> -<FONT color="green">165</FONT> Object instance = pool.poll();<a name="line.165"></a> -<FONT color="green">166</FONT> if (instance != null)<a name="line.166"></a> -<FONT color="green">167</FONT> {<a name="line.167"></a> -<FONT color="green">168</FONT> if (arrayCtorRecyclable)<a name="line.168"></a> -<FONT color="green">169</FONT> {<a name="line.169"></a> -<FONT color="green">170</FONT> ((ArrayCtorRecyclable) instance).recycle(params);<a name="line.170"></a> -<FONT color="green">171</FONT> }<a name="line.171"></a> -<FONT color="green">172</FONT> else if (instance instanceof Recyclable)<a name="line.172"></a> -<FONT color="green">173</FONT> {<a name="line.173"></a> -<FONT color="green">174</FONT> try<a name="line.174"></a> -<FONT color="green">175</FONT> {<a name="line.175"></a> -<FONT color="green">176</FONT> if ((signature != null) && (signature.length > 0))<a name="line.176"></a> -<FONT color="green">177</FONT> {<a name="line.177"></a> -<FONT color="green">178</FONT> /* Get the recycle method from the cache. */<a name="line.178"></a> -<FONT color="green">179</FONT> Method recycle = getRecycle(signature);<a name="line.179"></a> -<FONT color="green">180</FONT> if (recycle == null)<a name="line.180"></a> -<FONT color="green">181</FONT> {<a name="line.181"></a> -<FONT color="green">182</FONT> synchronized (this)<a name="line.182"></a> -<FONT color="green">183</FONT> {<a name="line.183"></a> -<FONT color="green">184</FONT> /* Make a synchronized recheck. */<a name="line.184"></a> -<FONT color="green">185</FONT> recycle = getRecycle(signature);<a name="line.185"></a> -<FONT color="green">186</FONT> if (recycle == null)<a name="line.186"></a> -<FONT color="green">187</FONT> {<a name="line.187"></a> -<FONT color="green">188</FONT> Class clazz = instance.getClass();<a name="line.188"></a> -<FONT color="green">189</FONT> recycle =<a name="line.189"></a> -<FONT color="green">190</FONT> clazz.getMethod(<a name="line.190"></a> -<FONT color="green">191</FONT> "recycle",<a name="line.191"></a> -<FONT color="green">192</FONT> getFactory().getSignature(clazz, params, signature));<a name="line.192"></a> -<FONT color="green">193</FONT> ArrayList cache =<a name="line.193"></a> -<FONT color="green">194</FONT> recyclers != null ? (ArrayList) recyclers.clone() : new ArrayList();<a name="line.194"></a> -<FONT color="green">195</FONT> cache.add(new Recycler(recycle, signature));<a name="line.195"></a> -<FONT color="green">196</FONT> recyclers = cache;<a name="line.196"></a> -<FONT color="green">197</FONT> }<a name="line.197"></a> -<FONT color="green">198</FONT> }<a name="line.198"></a> -<FONT color="green">199</FONT> }<a name="line.199"></a> -<FONT color="green">200</FONT> recycle.invoke(instance, params);<a name="line.200"></a> -<FONT color="green">201</FONT> }<a name="line.201"></a> -<FONT color="green">202</FONT> else<a name="line.202"></a> -<FONT color="green">203</FONT> {<a name="line.203"></a> -<FONT color="green">204</FONT> ((Recyclable) instance).recycle();<a name="line.204"></a> -<FONT color="green">205</FONT> }<a name="line.205"></a> -<FONT color="green">206</FONT> }<a name="line.206"></a> -<FONT color="green">207</FONT> catch (Exception x)<a name="line.207"></a> -<FONT color="green">208</FONT> {<a name="line.208"></a> -<FONT color="green">209</FONT> throw new PoolException("Recycling failed for " + instance.getClass().getName(), x);<a name="line.209"></a> -<FONT color="green">210</FONT> }<a name="line.210"></a> -<FONT color="green">211</FONT> }<a name="line.211"></a> -<FONT color="green">212</FONT> }<a name="line.212"></a> -<FONT color="green">213</FONT> return instance;<a name="line.213"></a> -<FONT color="green">214</FONT> }<a name="line.214"></a> -<FONT color="green">215</FONT> /**<a name="line.215"></a> -<FONT color="green">216</FONT> * Offers an instance to the pool.<a name="line.216"></a> -<FONT color="green">217</FONT> *<a name="line.217"></a> -<FONT color="green">218</FONT> * @param instance an instance.<a name="line.218"></a> -<FONT color="green">219</FONT> */<a name="line.219"></a> -<FONT color="green">220</FONT> public boolean offer(Object instance)<a name="line.220"></a> -<FONT color="green">221</FONT> {<a name="line.221"></a> -<FONT color="green">222</FONT> if (instance instanceof Recyclable)<a name="line.222"></a> -<FONT color="green">223</FONT> {<a name="line.223"></a> -<FONT color="green">224</FONT> try<a name="line.224"></a> -<FONT color="green">225</FONT> {<a name="line.225"></a> -<FONT color="green">226</FONT> ((Recyclable) instance).dispose();<a name="line.226"></a> -<FONT color="green">227</FONT> }<a name="line.227"></a> -<FONT color="green">228</FONT> catch (Exception x)<a name="line.228"></a> -<FONT color="green">229</FONT> {<a name="line.229"></a> -<FONT color="green">230</FONT> return false;<a name="line.230"></a> -<FONT color="green">231</FONT> }<a name="line.231"></a> -<FONT color="green">232</FONT> }<a name="line.232"></a> -<FONT color="green">233</FONT> return pool.offer(instance);<a name="line.233"></a> -<FONT color="green">234</FONT> }<a name="line.234"></a> -<FONT color="green">235</FONT> /**<a name="line.235"></a> -<FONT color="green">236</FONT> * Returns the capacity of the pool.<a name="line.236"></a> -<FONT color="green">237</FONT> *<a name="line.237"></a> -<FONT color="green">238</FONT> * @return the capacity.<a name="line.238"></a> -<FONT color="green">239</FONT> */<a name="line.239"></a> -<FONT color="green">240</FONT> public int capacity()<a name="line.240"></a> -<FONT color="green">241</FONT> {<a name="line.241"></a> -<FONT color="green">242</FONT> return pool.capacity();<a name="line.242"></a> -<FONT color="green">243</FONT> }<a name="line.243"></a> -<FONT color="green">244</FONT> /**<a name="line.244"></a> -<FONT color="green">245</FONT> * Returns the size of the pool.<a name="line.245"></a> -<FONT color="green">246</FONT> *<a name="line.246"></a> -<FONT color="green">247</FONT> * @return the size.<a name="line.247"></a> -<FONT color="green">248</FONT> */<a name="line.248"></a> -<FONT color="green">249</FONT> public int size()<a name="line.249"></a> -<FONT color="green">250</FONT> {<a name="line.250"></a> -<FONT color="green">251</FONT> return pool.size();<a name="line.251"></a> -<FONT color="green">252</FONT> }<a name="line.252"></a> -<FONT color="green">253</FONT> /**<a name="line.253"></a> -<FONT color="green">254</FONT> * Returns a cached recycle method<a name="line.254"></a> -<FONT color="green">255</FONT> * corresponding to the given signature.<a name="line.255"></a> -<FONT color="green">256</FONT> *<a name="line.256"></a> -<FONT color="green">257</FONT> * @param signature the signature.<a name="line.257"></a> -<FONT color="green">258</FONT> * @return the recycle method or null.<a name="line.258"></a> -<FONT color="green">259</FONT> */<a name="line.259"></a> -<FONT color="green">260</FONT> private Method getRecycle(String[] signature)<a name="line.260"></a> -<FONT color="green">261</FONT> {<a name="line.261"></a> -<FONT color="green">262</FONT> ArrayList cache = recyclers;<a name="line.262"></a> -<FONT color="green">263</FONT> if (cache != null)<a name="line.263"></a> -<FONT color="green">264</FONT> {<a name="line.264"></a> -<FONT color="green">265</FONT> Method recycle;<a name="line.265"></a> -<FONT color="green">266</FONT> for (Iterator i = cache.iterator(); i.hasNext();)<a name="line.266"></a> -<FONT color="green">267</FONT> {<a name="line.267"></a> -<FONT color="green">268</FONT> recycle = ((Recycler) i.next()).match(signature);<a name="line.268"></a> -<FONT color="green">269</FONT> if (recycle != null)<a name="line.269"></a> -<FONT color="green">270</FONT> {<a name="line.270"></a> -<FONT color="green">271</FONT> return recycle;<a name="line.271"></a> -<FONT color="green">272</FONT> }<a name="line.272"></a> -<FONT color="green">273</FONT> }<a name="line.273"></a> -<FONT color="green">274</FONT> }<a name="line.274"></a> -<FONT color="green">275</FONT> return null;<a name="line.275"></a> -<FONT color="green">276</FONT> }<a name="line.276"></a> -<FONT color="green">277</FONT> }<a name="line.277"></a> -<FONT color="green">278</FONT> /**<a name="line.278"></a> -<FONT color="green">279</FONT> * The default capacity of pools.<a name="line.279"></a> -<FONT color="green">280</FONT> */<a name="line.280"></a> -<FONT color="green">281</FONT> private int poolCapacity = DEFAULT_POOL_CAPACITY;<a name="line.281"></a> -<FONT color="green">282</FONT> /**<a name="line.282"></a> -<FONT color="green">283</FONT> * The pool repository, one pool for each class.<a name="line.283"></a> -<FONT color="green">284</FONT> */<a name="line.284"></a> -<FONT color="green">285</FONT> private HashMap poolRepository = new HashMap();<a name="line.285"></a> -<FONT color="green">286</FONT> private Map capacityMap;<a name="line.286"></a> -<FONT color="green">287</FONT> private FactoryService factoryService;<a name="line.287"></a> -<FONT color="green">288</FONT> private ServiceManager manager;<a name="line.288"></a> -<FONT color="green">289</FONT> <a name="line.289"></a> -<FONT color="green">290</FONT> /**<a name="line.290"></a> -<FONT color="green">291</FONT> * Gets an instance of a named class either from the pool<a name="line.291"></a> -<FONT color="green">292</FONT> * or by calling the Factory Service if the pool is empty.<a name="line.292"></a> -<FONT color="green">293</FONT> *<a name="line.293"></a> -<FONT color="green">294</FONT> * @param className the name of the class.<a name="line.294"></a> -<FONT color="green">295</FONT> * @return the instance.<a name="line.295"></a> -<FONT color="green">296</FONT> * @throws PoolException if recycling fails.<a name="line.296"></a> -<FONT color="green">297</FONT> */<a name="line.297"></a> -<FONT color="green">298</FONT> public Object getInstance(String className) throws PoolException<a name="line.298"></a> -<FONT color="green">299</FONT> {<a name="line.299"></a> -<FONT color="green">300</FONT> try<a name="line.300"></a> -<FONT color="green">301</FONT> {<a name="line.301"></a> -<FONT color="green">302</FONT> Object instance = pollInstance(className, null, null);<a name="line.302"></a> -<FONT color="green">303</FONT> return instance == null ? getFactory().getInstance(className) : instance;<a name="line.303"></a> -<FONT color="green">304</FONT> }<a name="line.304"></a> -<FONT color="green">305</FONT> catch (FactoryException fe)<a name="line.305"></a> -<FONT color="green">306</FONT> {<a name="line.306"></a> -<FONT color="green">307</FONT> throw new PoolException(fe);<a name="line.307"></a> -<FONT color="green">308</FONT> }<a name="line.308"></a> -<FONT color="green">309</FONT> }<a name="line.309"></a> -<FONT color="green">310</FONT> /**<a name="line.310"></a> -<FONT color="green">311</FONT> * Gets an instance of a named class either from the pool<a name="line.311"></a> -<FONT color="green">312</FONT> * or by calling the Factory Service if the pool is empty.<a name="line.312"></a> -<FONT color="green">313</FONT> * The specified class loader will be passed to the Factory Service.<a name="line.313"></a> -<FONT color="green">314</FONT> *<a name="line.314"></a> -<FONT color="green">315</FONT> * @param className the name of the class.<a name="line.315"></a> -<FONT color="green">316</FONT> * @param loader the class loader.<a name="line.316"></a> -<FONT color="green">317</FONT> * @return the instance.<a name="line.317"></a> -<FONT color="green">318</FONT> * @throws PoolException if recycling fails.<a name="line.318"></a> -<FONT color="green">319</FONT> */<a name="line.319"></a> -<FONT color="green">320</FONT> public Object getInstance(String className, ClassLoader loader) throws PoolException<a name="line.320"></a> -<FONT color="green">321</FONT> {<a name="line.321"></a> -<FONT color="green">322</FONT> try<a name="line.322"></a> -<FONT color="green">323</FONT> {<a name="line.323"></a> -<FONT color="green">324</FONT> Object instance = pollInstance(className, null, null);<a name="line.324"></a> -<FONT color="green">325</FONT> return instance == null ? getFactory().getInstance(className, loader) : instance;<a name="line.325"></a> -<FONT color="green">326</FONT> }<a name="line.326"></a> -<FONT color="green">327</FONT> catch (FactoryException fe)<a name="line.327"></a> -<FONT color="green">328</FONT> {<a name="line.328"></a> -<FONT color="green">329</FONT> throw new PoolException(fe);<a name="line.329"></a> -<FONT color="green">330</FONT> }<a name="line.330"></a> -<FONT color="green">331</FONT> }<a name="line.331"></a> -<FONT color="green">332</FONT> /**<a name="line.332"></a> -<FONT color="green">333</FONT> * Gets an instance of a named class either from the pool<a name="line.333"></a> -<FONT color="green">334</FONT> * or by calling the Factory Service if the pool is empty.<a name="line.334"></a> -<FONT color="green">335</FONT> * Parameters for its constructor are given as an array of objects,<a name="line.335"></a> -<FONT color="green">336</FONT> * primitive types must be wrapped with a corresponding class.<a name="line.336"></a> -<FONT color="green">337</FONT> *<a name="line.337"></a> -<FONT color="green">338</FONT> * @param className the name of the class.<a name="line.338"></a> -<FONT color="green">339</FONT> * @param loader the class loader.<a name="line.339"></a> -<FONT color="green">340</FONT> * @param params an array containing the parameters of the constructor.<a name="line.340"></a> -<FONT color="green">341</FONT> * @param signature an array containing the signature of the constructor.<a name="line.341"></a> -<FONT color="green">342</FONT> * @return the instance.<a name="line.342"></a> -<FONT color="green">343</FONT> * @throws PoolException if recycling fails.<a name="line.343"></a> -<FONT color="green">344</FONT> */<a name="line.344"></a> -<FONT color="green">345</FONT> public Object getInstance(String className, Object[] params, String[] signature) throws PoolException<a name="line.345"></a> -<FONT color="green">346</FONT> {<a name="line.346"></a> -<FONT color="green">347</FONT> try<a name="line.347"></a> -<FONT color="green">348</FONT> {<a name="line.348"></a> -<FONT color="green">349</FONT> Object instance = pollInstance(className, params, signature);<a name="line.349"></a> -<FONT color="green">350</FONT> return instance == null ? getFactory().getInstance(className, params, signature) : instance;<a name="line.350"></a> -<FONT color="green">351</FONT> }<a name="line.351"></a> -<FONT color="green">352</FONT> catch (FactoryException fe)<a name="line.352"></a> -<FONT color="green">353</FONT> {<a name="line.353"></a> -<FONT color="green">354</FONT> throw new PoolException(fe);<a name="line.354"></a> -<FONT color="green">355</FONT> }<a name="line.355"></a> -<FONT color="green">356</FONT> }<a name="line.356"></a> -<FONT color="green">357</FONT> /**<a name="line.357"></a> -<FONT color="green">358</FONT> * Gets an instance of a named class either from the pool<a name="line.358"></a> -<FONT color="green">359</FONT> * or by calling the Factory Service if the pool is empty.<a name="line.359"></a> -<FONT color="green">360</FONT> * Parameters for its constructor are given as an array of objects,<a name="line.360"></a> -<FONT color="green">361</FONT> * primitive types must be wrapped with a corresponding class.<a name="line.361"></a> -<FONT color="green">362</FONT> * The specified class loader will be passed to the Factory Service.<a name="line.362"></a> -<FONT color="green">363</FONT> *<a name="line.363"></a> -<FONT color="green">364</FONT> * @param className the name of the class.<a name="line.364"></a> -<FONT color="green">365</FONT> * @param loader the class loader.<a name="line.365"></a> -<FONT color="green">366</FONT> * @param params an array containing the parameters of the constructor.<a name="line.366"></a> -<FONT color="green">367</FONT> * @param signature an array containing the signature of the constructor.<a name="line.367"></a> -<FONT color="green">368</FONT> * @return the instance.<a name="line.368"></a> -<FONT color="green">369</FONT> * @throws PoolException if recycling fails.<a name="line.369"></a> -<FONT color="green">370</FONT> */<a name="line.370"></a> -<FONT color="green">371</FONT> public Object getInstance(String className, ClassLoader loader, Object[] params, String[] signature)<a name="line.371"></a> -<FONT color="green">372</FONT> throws PoolException<a name="line.372"></a> -<FONT color="green">373</FONT> {<a name="line.373"></a> -<FONT color="green">374</FONT> try<a name="line.374"></a> -<FONT color="green">375</FONT> {<a name="line.375"></a> -<FONT color="green">376</FONT> Object instance = pollInstance(className, params, signature);<a name="line.376"></a> -<FONT color="green">377</FONT> return instance == null ? getFactory().getInstance(className, loader, params, signature) : instance;<a name="line.377"></a> -<FONT color="green">378</FONT> }<a name="line.378"></a> -<FONT color="green">379</FONT> catch (FactoryException fe)<a name="line.379"></a> -<FONT color="green">380</FONT> {<a name="line.380"></a> -<FONT color="green">381</FONT> throw new PoolException(fe);<a name="line.381"></a> -<FONT color="green">382</FONT> }<a name="line.382"></a> -<FONT color="green">383</FONT> }<a name="line.383"></a> -<FONT color="green">384</FONT> /**<a name="line.384"></a> -<FONT color="green">385</FONT> * Tests if specified class loaders are supported for a named class.<a name="line.385"></a> -<FONT color="green">386</FONT> *<a name="line.386"></a> -<FONT color="green">387</FONT> * @param className the name of the class.<a name="line.387"></a> -<FONT color="green">388</FONT> * @return true if class loaders are supported, false otherwise.<a name="line.388"></a> -<FONT color="green">389</FONT> * @throws PoolException if test fails.<a name="line.389"></a> -<FONT color="green">390</FONT> */<a name="line.390"></a> -<FONT color="green">391</FONT> public boolean isLoaderSupported(String className) throws FactoryException<a name="line.391"></a> -<FONT color="green">392</FONT> {<a name="line.392"></a> -<FONT color="green">393</FONT> return getFactory().isLoaderSupported(className);<a name="line.393"></a> -<FONT color="green">394</FONT> }<a name="line.394"></a> -<FONT color="green">395</FONT> /**<a name="line.395"></a> -<FONT color="green">396</FONT> * Gets an instance of a specified class either from the pool<a name="line.396"></a> -<FONT color="green">397</FONT> * or by instatiating from the class if the pool is empty.<a name="line.397"></a> -<FONT color="green">398</FONT> *<a name="line.398"></a> -<FONT color="green">399</FONT> * @param clazz the class.<a name="line.399"></a> -<FONT color="green">400</FONT> * @return the instance.<a name="line.400"></a> -<FONT color="green">401</FONT> * @throws PoolException if recycling fails.<a name="line.401"></a> -<FONT color="green">402</FONT> */<a name="line.402"></a> -<FONT color="green">403</FONT> public Object getInstance(Class clazz) throws PoolException<a name="line.403"></a> -<FONT color="green">404</FONT> {<a name="line.404"></a> -<FONT color="green">405</FONT> try<a name="line.405"></a> -<FONT color="green">406</FONT> {<a name="line.406"></a> -<FONT color="green">407</FONT> Object instance = pollInstance(clazz.getName(), null, null);<a name="line.407"></a> -<FONT color="green">408</FONT> return instance == null ? factoryService.getInstance(clazz) : instance;<a name="line.408"></a> -<FONT color="green">409</FONT> }<a name="line.409"></a> -<FONT color="green">410</FONT> catch (FactoryException fe)<a name="line.410"></a> -<FONT color="green">411</FONT> {<a name="line.411"></a> -<FONT color="green">412</FONT> throw new PoolException(fe);<a name="line.412"></a> -<FONT color="green">413</FONT> }<a name="line.413"></a> -<FONT color="green">414</FONT> }<a name="line.414"></a> -<FONT color="green">415</FONT> /**<a name="line.415"></a> -<FONT color="green">416</FONT> * Gets an instance of a specified class either from the pool<a name="line.416"></a> -<FONT color="green">417</FONT> * or by instatiating from the class if the pool is empty.<a name="line.417"></a> -<FONT color="green">418</FONT> *<a name="line.418"></a> -<FONT color="green">419</FONT> * @todo There is a whacky .toString() on the clazzz, but otherwise it<a name="line.419"></a> -<FONT color="green">420</FONT> * won't compile..<a name="line.420"></a> -<FONT color="green">421</FONT> * @param clazz the class.<a name="line.421"></a> -<FONT color="green">422</FONT> * @param params an array containing the parameters of the constructor.<a name="line.422"></a> -<FONT color="green">423</FONT> * @param signature an array containing the signature of the constructor.<a name="line.423"></a> -<FONT color="green">424</FONT> * @return the instance.<a name="line.424"></a> -<FONT color="green">425</FONT> * @throws PoolException if recycling fails.<a name="line.425"></a> -<FONT color="green">426</FONT> */<a name="line.426"></a> -<FONT color="green">427</FONT> public Object getInstance(Class clazz, Object params[], String signature[]) throws PoolException<a name="line.427"></a> -<FONT color="green">428</FONT> {<a name="line.428"></a> -<FONT color="green">429</FONT> try<a name="line.429"></a> -<FONT color="green">430</FONT> {<a name="line.430"></a> -<FONT color="green">431</FONT> Object instance = pollInstance(clazz.getName(), params, signature);<a name="line.431"></a> -<FONT color="green">432</FONT> //FactoryService fs = getFactory();<a name="line.432"></a> -<FONT color="green">433</FONT> return instance == null ? getFactory().getInstance(clazz.toString(), params, signature) : instance;<a name="line.433"></a> -<FONT color="green">434</FONT> }<a name="line.434"></a> -<FONT color="green">435</FONT> catch (FactoryException fe)<a name="line.435"></a> -<FONT color="green">436</FONT> {<a name="line.436"></a> -<FONT color="green">437</FONT> throw new PoolException(fe);<a name="line.437"></a> -<FONT color="green">438</FONT> }<a name="line.438"></a> -<FONT color="green">439</FONT> }<a name="line.439"></a> -<FONT color="green">440</FONT> /**<a name="line.440"></a> -<FONT color="green">441</FONT> * Puts a used object back to the pool. Objects implementing<a name="line.441"></a> -<FONT color="green">442</FONT> * the Recyclable interface can provide a recycle method to<a name="line.442"></a> -<FONT color="green">443</FONT> * be called when they are reused and a dispose method to be<a name="line.443"></a> -<FONT color="green">444</FONT> * called when they are returned to the pool.<a name="line.444"></a> -<FONT color="green">445</FONT> *<a name="line.445"></a> -<FONT color="green">446</FONT> * @param instance the object instance to recycle.<a name="line.446"></a> -<FONT color="green">447</FONT> * @return true if the instance was accepted.<a name="line.447"></a> -<FONT color="green">448</FONT> */<a name="line.448"></a> -<FONT color="green">449</FONT> public boolean putInstance(Object instance)<a name="line.449"></a> -<FONT color="green">450</FONT> {<a name="line.450"></a> -<FONT color="green">451</FONT> if (instance != null)<a name="line.451"></a> -<FONT color="green">452</FONT> {<a name="line.452"></a> -<FONT color="green">453</FONT> HashMap repository = poolRepository;<a name="line.453"></a> -<FONT color="green">454</FONT> String className = instance.getClass().getName();<a name="line.454"></a> -<FONT color="green">455</FONT> PoolBuffer pool = (PoolBuffer) repository.get(className);<a name="line.455"></a> -<FONT color="green">456</FONT> if (pool == null)<a name="line.456"></a> -<FONT color="green">457</FONT> {<a name="line.457"></a> -<FONT color="green">458</FONT> pool = new PoolBuffer(getCapacity(className));<a name="line.458"></a> -<FONT color="green">459</FONT> repository = (HashMap) repository.clone();<a name="line.459"></a> -<FONT color="green">460</FONT> repository.put(className, pool);<a name="line.460"></a> -<FONT color="green">461</FONT> poolRepository = repository;<a name="line.461"></a> -<FONT color="green">462</FONT> if (instance instanceof ArrayCtorRecyclable)<a name="line.462"></a> -<FONT color="green">463</FONT> {<a name="line.463"></a> -<FONT color="green">464</FONT> pool.setArrayCtorRecyclable(true);<a name="line.464"></a> -<FONT color="green">465</FONT> }<a name="line.465"></a> -<FONT color="green">466</FONT> }<a name="line.466"></a> -<FONT color="green">467</FONT> return pool.offer(instance);<a name="line.467"></a> -<FONT color="green">468</FONT> }<a name="line.468"></a> -<FONT color="green">469</FONT> else<a name="line.469"></a> -<FONT color="green">470</FONT> {<a name="line.470"></a> -<FONT color="green">471</FONT> return false;<a name="line.471"></a> -<FONT color="green">472</FONT> }<a name="line.472"></a> -<FONT color="green">473</FONT> }<a name="line.473"></a> -<FONT color="green">474</FONT> /**<a name="line.474"></a> -<FONT color="green">475</FONT> * Gets the capacity of the pool for a named class.<a name="line.475"></a> -<FONT color="green">476</FONT> *<a name="line.476"></a> -<FONT color="green">477</FONT> * @param className the name of the class.<a name="line.477"></a> -<FONT color="green">478</FONT> */<a name="line.478"></a> -<FONT color="green">479</FONT> public int getCapacity(String className)<a name="line.479"></a> -<FONT color="green">480</FONT> {<a name="line.480"></a> -<FONT color="green">481</FONT> PoolBuffer pool = (PoolBuffer) poolRepository.get(className);<a name="line.481"></a> -<FONT color="green">482</FONT> if (pool == null)<a name="line.482"></a> -<FONT color="green">483</FONT> {<a name="line.483"></a> -<FONT color="green">484</FONT> /* Check class specific capacity. */<a name="line.484"></a> -<FONT color="green">485</FONT> int capacity = poolCapacity;<a name="line.485"></a> -<FONT color="green">486</FONT> if (capacityMap != null)<a name="line.486"></a> -<FONT color="green">487</FONT> {<a name="line.487"></a> -<FONT color="green">488</FONT> Integer cap = (Integer) capacityMap.get(className);<a name="line.488"></a> -<FONT color="green">489</FONT> if (cap != null)<a name="line.489"></a> -<FONT color="green">490</FONT> {<a name="line.490"></a> -<FONT color="green">491</FONT> capacity = cap.intValue();<a name="line.491"></a> -<FONT color="green">492</FONT> }<a name="line.492"></a> -<FONT color="green">493</FONT> }<a name="line.493"></a> -<FONT color="green">494</FONT> return capacity;<a name="line.494"></a> -<FONT color="green">495</FONT> }<a name="line.495"></a> -<FONT color="green">496</FONT> else<a name="line.496"></a> -<FONT color="green">497</FONT> {<a name="line.497"></a> -<FONT color="green">498</FONT> return pool.capacity();<a name="line.498"></a> -<FONT color="green">499</FONT> }<a name="line.499"></a> -<FONT color="green">500</FONT> }<a name="line.500"></a> -<FONT color="green">501</FONT> /**<a name="line.501"></a> -<FONT color="green">502</FONT> * Sets the capacity of the pool for a named class.<a name="line.502"></a> -<FONT color="green">503</FONT> * Note that the pool will be cleared after the change.<a name="line.503"></a> -<FONT color="green">504</FONT> *<a name="line.504"></a> -<FONT color="green">505</FONT> * @param className the name of the class.<a name="line.505"></a> -<FONT color="green">506</FONT> * @param capacity the new capacity.<a name="line.506"></a> -<FONT color="green">507</FONT> */<a name="line.507"></a> -<FONT color="green">508</FONT> public void setCapacity(String className, int capacity)<a name="line.508"></a> -<FONT color="green">509</FONT> {<a name="line.509"></a> -<FONT color="green">510</FONT> HashMap repository = poolRepository;<a name="line.510"></a> -<FONT color="green">511</FONT> repository = repository != null ? (HashMap) repository.clone() : new HashMap();<a name="line.511"></a> -<FONT color="green">512</FONT> repository.put(className, new PoolBuffer(capacity));<a name="line.512"></a> -<FONT color="green">513</FONT> poolRepository = repository;<a name="line.513"></a> -<FONT color="green">514</FONT> }<a name="line.514"></a> -<FONT color="green">515</FONT> /**<a name="line.515"></a> -<FONT color="green">516</FONT> * Gets the current size of the pool for a named class.<a name="line.516"></a> -<FONT color="green">517</FONT> *<a name="line.517"></a> -<FONT color="green">518</FONT> * @param className the name of the class.<a name="line.518"></a> -<FONT color="green">519</FONT> */<a name="line.519"></a> -<FONT color="green">520</FONT> public int getSize(String className)<a name="line.520"></a> -<FONT color="green">521</FONT> {<a name="line.521"></a> -<FONT color="green">522</FONT> PoolBuffer pool = (PoolBuffer) poolRepository.get(className);<a name="line.522"></a> -<FONT color="green">523</FONT> return pool != null ? pool.size() : 0;<a name="line.523"></a> -<FONT color="green">524</FONT> }<a name="line.524"></a> -<FONT color="green">525</FONT> /**<a name="line.525"></a> -<FONT color="green">526</FONT> * Clears instances of a named class from the pool.<a name="line.526"></a> -<FONT color="green">527</FONT> *<a name="line.527"></a> -<FONT color="green">528</FONT> * @param className the name of the class.<a name="line.528"></a> -<FONT color="green">529</FONT> */<a name="line.529"></a> -<FONT color="green">530</FONT> public void clearPool(String className)<a name="line.530"></a> -<FONT color="green">531</FONT> {<a name="line.531"></a> -<FONT color="green">532</FONT> HashMap repository = poolRepository;<a name="line.532"></a> -<FONT color="green">533</FONT> if (repository.get(className) != null)<a name="line.533"></a> -<FONT color="green">534</FONT> {<a name="line.534"></a> -<FONT color="green">535</FONT> repository = (HashMap) repository.clone();<a name="line.535"></a> -<FONT color="green">536</FONT> repository.remove(className);<a name="line.536"></a> -<FONT color="green">537</FONT> poolRepository = repository;<a name="line.537"></a> -<FONT color="green">538</FONT> }<a name="line.538"></a> -<FONT color="green">539</FONT> }<a name="line.539"></a> -<FONT color="green">540</FONT> /**<a name="line.540"></a> -<FONT color="green">541</FONT> * Clears all instances from the pool.<a name="line.541"></a> -<FONT color="green">542</FONT> */<a name="line.542"></a> -<FONT color="green">543</FONT> public void clearPool()<a name="line.543"></a> -<FONT color="green">544</FONT> {<a name="line.544"></a> -<FONT color="green">545</FONT> poolRepository = new HashMap();<a name="line.545"></a> -<FONT color="green">546</FONT> }<a name="line.546"></a> -<FONT color="green">547</FONT> /**<a name="line.547"></a> -<FONT color="green">548</FONT> * Polls and recycles an object of the named class from the pool.<a name="line.548"></a> -<FONT color="green">549</FONT> *<a name="line.549"></a> -<FONT color="green">550</FONT> * @param className the name of the class.<a name="line.550"></a> -<FONT color="green">551</FONT> * @param params an array containing the parameters of the constructor.<a name="line.551"></a> -<FONT color="green">552</FONT> * @param signature an array containing the signature of the constructor.<a name="line.552"></a> -<FONT color="green">553</FONT> * @return the object or null.<a name="line.553"></a> -<FONT color="green">554</FONT> * @throws PoolException if recycling fails.<a name="line.554"></a> -<FONT color="green">555</FONT> */<a name="line.555"></a> -<FONT color="green">556</FONT> private Object pollInstance(String className, Object[] params, String[] signature) throws PoolException<a name="line.556"></a> -<FONT color="green">557</FONT> {<a name="line.557"></a> -<FONT color="green">558</FONT> PoolBuffer pool = (PoolBuffer) poolRepository.get(className);<a name="line.558"></a> -<FONT color="green">559</FONT> return pool != null ? pool.poll(params, signature) : null;<a name="line.559"></a> -<FONT color="green">560</FONT> }<a name="line.560"></a> -<FONT color="green">561</FONT> /**<a name="line.561"></a> -<FONT color="green">562</FONT> * Gets the factory service.<a name="line.562"></a> -<FONT color="green">563</FONT> *<a name="line.563"></a> -<FONT color="green">564</FONT> * @return the factory service.<a name="line.564"></a> -<FONT color="green">565</FONT> */<a name="line.565"></a> -<FONT color="green">566</FONT> protected FactoryService getFactory()<a name="line.566"></a> -<FONT color="green">567</FONT> {<a name="line.567"></a> -<FONT color="green">568</FONT> return factoryService;<a name="line.568"></a> -<FONT color="green">569</FONT> }<a name="line.569"></a> -<FONT color="green">570</FONT> <a name="line.570"></a> -<FONT color="green">571</FONT> // ---------------- Avalon Lifecycle Methods ---------------------<a name="line.571"></a> -<FONT color="green">572</FONT> /**<a name="line.572"></a> -<FONT color="green">573</FONT> * Avalon component lifecycle method<a name="line.573"></a> -<FONT color="green">574</FONT> */<a name="line.574"></a> -<FONT color="green">575</FONT> public void configure(Configuration conf)<a name="line.575"></a> -<FONT color="green">576</FONT> {<a name="line.576"></a> -<FONT color="green">577</FONT> final Configuration capacities = conf.getChild(POOL_CAPACITY, false);<a name="line.577"></a> -<FONT color="green">578</FONT> if (capacities != null)<a name="line.578"></a> -<FONT color="green">579</FONT> {<a name="line.579"></a> -<FONT color="green">580</FONT> Configuration defaultConf = capacities.getChild("default");<a name="line.580"></a> -<FONT color="green">581</FONT> int capacity = defaultConf.getValueAsInteger(DEFAULT_POOL_CAPACITY);<a name="line.581"></a> -<FONT color="green">582</FONT> if (capacity <= 0)<a name="line.582"></a> -<FONT color="green">583</FONT> {<a name="line.583"></a> -<FONT color="green">584</FONT> throw new IllegalArgumentException("Capacity must be >0");<a name="line.584"></a> -<FONT color="green">585</FONT> }<a name="line.585"></a> -<FONT color="green">586</FONT> poolCapacity = capacity;<a name="line.586"></a> -<FONT color="green">587</FONT> Configuration[] nameVal = capacities.getChildren();<a name="line.587"></a> -<FONT color="green">588</FONT> for (int i = 0; i < nameVal.length; i++)<a name="line.588"></a> -<FONT color="green">589</FONT> {<a name="line.589"></a> -<FONT color="green">590</FONT> String key = nameVal[i].getName();<a name="line.590"></a> -<FONT color="green">591</FONT> if (!"default".equals(key))<a name="line.591"></a> -<FONT color="green">592</FONT> {<a name="line.592"></a> -<FONT color="green">593</FONT> capacity = nameVal[i].getValueAsInteger(poolCapacity);<a name="line.593"></a> -<FONT color="green">594</FONT> if (capacity < 0)<a name="line.594"></a> -<FONT color="green">595</FONT> {<a name="line.595"></a> -<FONT color="green">596</FONT> capacity = poolCapacity;<a name="line.596"></a> -<FONT color="green">597</FONT> }<a name="line.597"></a> -<FONT color="green">598</FONT> if (capacityMap == null)<a name="line.598"></a> -<FONT color="green">599</FONT> {<a name="line.599"></a> -<FONT color="green">600</FONT> capacityMap = new HashMap();<a name="line.600"></a> -<FONT color="green">601</FONT> }<a name="line.601"></a> -<FONT color="green">602</FONT> capacityMap.put(key, new Integer(capacity));<a name="line.602"></a> -<FONT color="green">603</FONT> }<a name="line.603"></a> -<FONT color="green">604</FONT> }<a name="line.604"></a> -<FONT color="green">605</FONT> }<a name="line.605"></a> -<FONT color="green">606</FONT> }<a name="line.606"></a> -<FONT color="green">607</FONT> <a name="line.607"></a> -<FONT color="green">608</FONT> /**<a name="line.608"></a> -<FONT color="green">609</FONT> * Avalon component lifecycle method<a name="line.609"></a> -<FONT color="green">610</FONT> * @avalon.dependency type="org.apache.fulcrum.factory.FactoryService"<a name="line.610"></a> -<FONT color="green">611</FONT> */<a name="line.611"></a> -<FONT color="green">612</FONT> public void service(ServiceManager manager)<a name="line.612"></a> -<FONT color="green">613</FONT> {<a name="line.613"></a> -<FONT color="green">614</FONT> this.manager = manager;<a name="line.614"></a> -<FONT color="green">615</FONT> }<a name="line.615"></a> -<FONT color="green">616</FONT> <a name="line.616"></a> -<FONT color="green">617</FONT> /**<a name="line.617"></a> -<FONT color="green">618</FONT> * Avalon component lifecycle method<a name="line.618"></a> -<FONT color="green">619</FONT> * Initializes the service by loading default class loaders<a name="line.619"></a> -<FONT color="green">620</FONT> * and customized object factories.<a name="line.620"></a> -<FONT color="green">621</FONT> *<a name="line.621"></a> -<FONT color="green">622</FONT> * @throws InitializationException if initialization fails.<a name="line.622"></a> -<FONT color="green">623</FONT> */<a name="line.623"></a> -<FONT color="green">624</FONT> public void initialize() throws Exception<a name="line.624"></a> -<FONT color="green">625</FONT> {<a name="line.625"></a> -<FONT color="green">626</FONT> try<a name="line.626"></a> -<FONT color="green">627</FONT> {<a name="line.627"></a> -<FONT color="green">628</FONT> factoryService = (FactoryService) manager.lookup(FactoryService.ROLE);<a name="line.628"></a> -<FONT color="green">629</FONT> }<a name="line.629"></a> -<FONT color="green">630</FONT> catch (Exception e)<a name="line.630"></a> -<FONT color="green">631</FONT> {<a name="line.631"></a> -<FONT color="green">632</FONT> throw new Exception(<a name="line.632"></a> -<FONT color="green">633</FONT> "DefaultPoolService.initialize: Failed to get a Factory object", e);<a name="line.633"></a> -<FONT color="green">634</FONT> }<a name="line.634"></a> -<FONT color="green">635</FONT> }<a name="line.635"></a> -<FONT color="green">636</FONT> <a name="line.636"></a> -<FONT color="green">637</FONT> /**<a name="line.637"></a> -<FONT color="green">638</FONT> * Avalon component lifecycle method<a name="line.638"></a> -<FONT color="green">639</FONT> */<a name="line.639"></a> -<FONT color="green">640</FONT> public void dispose()<a name="line.640"></a> -<FONT color="green">641</FONT> {<a name="line.641"></a> -<FONT color="green">642</FONT> if (factoryService != null)<a name="line.642"></a> -<FONT color="green">643</FONT> {<a name="line.643"></a> -<FONT color="green">644</FONT> manager.release(factoryService);<a name="line.644"></a> -<FONT color="green">645</FONT> }<a name="line.645"></a> -<FONT color="green">646</FONT> factoryService = null;<a name="line.646"></a> -<FONT color="green">647</FONT> manager = null;<a name="line.647"></a> -<FONT color="green">648</FONT> }<a name="line.648"></a> -<FONT color="green">649</FONT> }<a name="line.649"></a> +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html lang="en"> +<head> +<title>Source code</title> +<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style"> +</head> +<body> +<div class="sourceContainer"> +<pre><span class="sourceLineNo">001</span>package org.apache.fulcrum.pool;<a name="line.1"></a> +<span class="sourceLineNo">002</span><a name="line.2"></a> +<span class="sourceLineNo">003</span>import java.util.HashMap;<a name="line.3"></a> +<span class="sourceLineNo">004</span><a name="line.4"></a> +<span class="sourceLineNo">005</span>/*<a name="line.5"></a> +<span class="sourceLineNo">006</span> * Licensed to the Apache Software Foundation (ASF) under one<a name="line.6"></a> +<span class="sourceLineNo">007</span> * or more contributor license agreements. See the NOTICE file<a name="line.7"></a> +<span class="sourceLineNo">008</span> * distributed with this work for additional information<a name="line.8"></a> +<span class="sourceLineNo">009</span> * regarding copyright ownership. The ASF licenses this file<a name="line.9"></a> +<span class="sourceLineNo">010</span> * to you under the Apache License, Version 2.0 (the<a name="line.10"></a> +<span class="sourceLineNo">011</span> * "License"); you may not use this file except in compliance<a name="line.11"></a> +<span class="sourceLineNo">012</span> * with the License. You may obtain a copy of the License at<a name="line.12"></a> +<span class="sourceLineNo">013</span> *<a name="line.13"></a> +<span class="sourceLineNo">014</span> * http://www.apache.org/licenses/LICENSE-2.0<a name="line.14"></a> +<span class="sourceLineNo">015</span> *<a name="line.15"></a> +<span class="sourceLineNo">016</span> * Unless required by applicable law or agreed to in writing,<a name="line.16"></a> +<span class="sourceLineNo">017</span> * software distributed under the License is distributed on an<a name="line.17"></a> +<span class="sourceLineNo">018</span> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY<a name="line.18"></a> +<span class="sourceLineNo">019</span> * KIND, either express or implied. See the License for the<a name="line.19"></a> +<span class="sourceLineNo">020</span> * specific language governing permissions and limitations<a name="line.20"></a> +<span class="sourceLineNo">021</span> * under the License.<a name="line.21"></a> +<span class="sourceLineNo">022</span> */<a name="line.22"></a> +<span class="sourceLineNo">023</span><a name="line.23"></a> +<span class="sourceLineNo">024</span>import java.util.Map;<a name="line.24"></a> +<span class="sourceLineNo">025</span><a name="line.25"></a> +<span class="sourceLineNo">026</span>import org.apache.avalon.framework.activity.Disposable;<a name="line.26"></a> +<span class="sourceLineNo">027</span>import org.apache.avalon.framework.activity.Initializable;<a name="line.27"></a> +<span class="sourceLineNo">028</span>import org.apache.avalon.framework.configuration.Configurable;<a name="line.28"></a> +<span class="sourceLineNo">029</span>import org.apache.avalon.framework.configuration.Configuration;<a name="line.29"></a> +<span class="sourceLineNo">030</span>import org.apache.avalon.framework.logger.AbstractLogEnabled;<a name="line.30"></a> +<span class="sourceLineNo">031</span>import org.apache.avalon.framework.service.ServiceManager;<a name="line.31"></a> +<span class="sourceLineNo">032</span>import org.apache.avalon.framework.service.Serviceable;<a name="line.32"></a> +<span class="sourceLineNo">033</span>import org.apache.fulcrum.factory.FactoryException;<a name="line.33"></a> +<span class="sourceLineNo">034</span>import org.apache.fulcrum.factory.FactoryService;<a name="line.34"></a> +<span class="sourceLineNo">035</span><a name="line.35"></a> +<span class="sourceLineNo">036</span>/**<a name="line.36"></a> +<span class="sourceLineNo">037</span> * The Pool Service extends the Factory Service by adding support for pooling<a name="line.37"></a> +<span class="sourceLineNo">038</span> * instantiated objects. When a new instance is requested, the service first<a name="line.38"></a> +<span class="sourceLineNo">039</span> * checks its pool if one is available. If the the pool is empty, a new instance<a name="line.39"></a> +<span class="sourceLineNo">040</span> * will be requested from the FactoryService.<a name="line.40"></a> +<span class="sourceLineNo">041</span> *<a name="line.41"></a> +<span class="sourceLineNo">042</span> * For objects implementing the Recyclable interface, a recycle method will be<a name="line.42"></a> +<span class="sourceLineNo">043</span> * called, when they taken from the pool, and a dispose method, when they are<a name="line.43"></a> +<span class="sourceLineNo">044</span> * returned to the pool.<a name="line.44"></a> +<span class="sourceLineNo">045</span> *<a name="line.45"></a> +<span class="sourceLineNo">046</span> * @author <a href="mailto:[email protected]">Ilkka Priha</a><a name="line.46"></a> +<span class="sourceLineNo">047</span> * @author <a href="mailto:[email protected]">Stephen McConnell</a><a name="line.47"></a> +<span class="sourceLineNo">048</span> * @version $Id: DefaultPoolService.java 1850691 2019-01-07 21:25:15Z painter $<a name="line.48"></a> +<span class="sourceLineNo">049</span> *<a name="line.49"></a> +<span class="sourceLineNo">050</span> * avalon.component name="pool" lifestyle="transient"<a name="line.50"></a> +<span class="sourceLineNo">051</span> * avalon.service type="org.apache.fulcrum.pool.PoolService"<a name="line.51"></a> +<span class="sourceLineNo">052</span> */<a name="line.52"></a> +<span class="sourceLineNo">053</span>public class DefaultPoolService extends AbstractLogEnabled<a name="line.53"></a> +<span class="sourceLineNo">054</span> implements PoolService, Serviceable, Disposable, Initializable, Configurable {<a name="line.54"></a> +<span class="sourceLineNo">055</span> /**<a name="line.55"></a> +<span class="sourceLineNo">056</span> * The property specifying the pool capacity.<a name="line.56"></a> +<span class="sourceLineNo">057</span> */<a name="line.57"></a> +<span class="sourceLineNo">058</span> public static final String POOL_CAPACITY = "capacity";<a name="line.58"></a> +<span class="sourceLineNo">059</span> <a name="line.59"></a> +<span class="sourceLineNo">060</span> /**<a name="line.60"></a> +<span class="sourceLineNo">061</span> * The default capacity of pools.<a name="line.61"></a> +<span class="sourceLineNo">062</span> */<a name="line.62"></a> +<span class="sourceLineNo">063</span> private int poolCapacity = DEFAULT_POOL_CAPACITY;<a name="line.63"></a> +<span class="sourceLineNo">064</span> <a name="line.64"></a> +<span class="sourceLineNo">065</span> /**<a name="line.65"></a> +<span class="sourceLineNo">066</span> * The pool repository, one pool for each class.<a name="line.66"></a> +<span class="sourceLineNo">067</span> */<a name="line.67"></a> +<span class="sourceLineNo">068</span> private HashMap<String, PoolBuffer> poolRepository = new HashMap<>();<a name="line.68"></a> +<span class="sourceLineNo">069</span> private Map<String, Integer> capacityMap;<a name="line.69"></a> +<span class="sourceLineNo">070</span> private FactoryService factoryService;<a name="line.70"></a> +<span class="sourceLineNo">071</span> private ServiceManager manager;<a name="line.71"></a> +<span class="sourceLineNo">072</span><a name="line.72"></a> +<span class="sourceLineNo">073</span> /**<a name="line.73"></a> +<span class="sourceLineNo">074</span> * Gets an instance of a named class either from the pool or by calling the<a name="line.74"></a> +<span class="sourceLineNo">075</span> * Factory Service if the pool is empty.<a name="line.75"></a> +<span class="sourceLineNo">076</span> *<a name="line.76"></a> +<span class="sourceLineNo">077</span> * @param className the name of the class.<a name="line.77"></a> +<span class="sourceLineNo">078</span> * @return the instance.<a name="line.78"></a> +<span class="sourceLineNo">079</span> * @throws PoolException if recycling fails.<a name="line.79"></a> +<span class="sourceLineNo">080</span> */<a name="line.80"></a> +<span class="sourceLineNo">081</span> public Object getInstance(String className) throws PoolException <a name="line.81"></a> +<span class="sourceLineNo">082</span> {<a name="line.82"></a> +<span class="sourceLineNo">083</span> try <a name="line.83"></a> +<span class="sourceLineNo">084</span> {<a name="line.84"></a> +<span class="sourceLineNo">085</span> Object instance = pollInstance(className, null, null);<a name="line.85"></a> +<span class="sourceLineNo">086</span> return instance == null ? getFactory().getInstance(className) : instance;<a name="line.86"></a> +<span class="sourceLineNo">087</span> } <a name="line.87"></a> +<span class="sourceLineNo">088</span> catch (FactoryException fe) <a name="line.88"></a> +<span class="sourceLineNo">089</span> {<a name="line.89"></a> +<span class="sourceLineNo">090</span> throw new PoolException(fe);<a name="line.90"></a> +<span class="sourceLineNo">091</span> }<a name="line.91"></a> +<span class="sourceLineNo">092</span> }<a name="line.92"></a> +<span class="sourceLineNo">093</span><a name="line.93"></a> +<span class="sourceLineNo">094</span> /**<a name="line.94"></a> +<span class="sourceLineNo">095</span> * Gets an instance of a named class either from the pool or by calling the<a name="line.95"></a> +<span class="sourceLineNo">096</span> * Factory Service if the pool is empty. The specified class loader will be<a name="line.96"></a> +<span class="sourceLineNo">097</span> * passed to the Factory Service.<a name="line.97"></a> +<span class="sourceLineNo">098</span> *<a name="line.98"></a> +<span class="sourceLineNo">099</span> * @param className the name of the class.<a name="line.99"></a> +<span class="sourceLineNo">100</span> * @param loader the class loader.<a name="line.100"></a> +<span class="sourceLineNo">101</span> * @return the instance.<a name="line.101"></a> +<span class="sourceLineNo">102</span> * @throws PoolException if recycling fails.<a name="line.102"></a> +<span class="sourceLineNo">103</span> */<a name="line.103"></a> +<span class="sourceLineNo">104</span> public Object getInstance(String className, ClassLoader loader) throws PoolException <a name="line.104"></a> +<span class="sourceLineNo">105</span> {<a name="line.105"></a> +<span class="sourceLineNo">106</span> try <a name="line.106"></a> +<span class="sourceLineNo">107</span> {<a name="line.107"></a> +<span class="sourceLineNo">108</span> Object instance = pollInstance(className, null, null);<a name="line.108"></a> +<span class="sourceLineNo">109</span> return instance == null ? getFactory().getInstance(className, loader) : instance;<a name="line.109"></a> +<span class="sourceLineNo">110</span> } <a name="line.110"></a> +<span class="sourceLineNo">111</span> catch (FactoryException fe) <a name="line.111"></a> +<span class="sourceLineNo">112</span> {<a name="line.112"></a> +<span class="sourceLineNo">113</span> throw new PoolException(fe);<a name="line.113"></a> +<span class="sourceLineNo">114</span> }<a name="line.114"></a> +<span class="sourceLineNo">115</span> }<a name="line.115"></a> +<span class="sourceLineNo">116</span><a name="line.116"></a> +<span class="sourceLineNo">117</span> /**<a name="line.117"></a> +<span class="sourceLineNo">118</span> * Gets an instance of a named class either from the pool or by calling the<a name="line.118"></a> +<span class="sourceLineNo">119</span> * Factory Service if the pool is empty. Parameters for its constructor are<a name="line.119"></a> +<span class="sourceLineNo">120</span> * given as an array of objects, primitive types must be wrapped with a<a name="line.120"></a> +<span class="sourceLineNo">121</span> * corresponding class.<a name="line.121"></a> +<span class="sourceLineNo">122</span> *<a name="line.122"></a> +<span class="sourceLineNo">123</span> * @param className the name of the class.<a name="line.123"></a> +<span class="sourceLineNo">124</span> * @param params an array containing the parameters of the constructor.<a name="line.124"></a> +<span class="sourceLineNo">125</span> * @param signature an array containing the signature of the constructor.<a name="line.125"></a> +<span class="sourceLineNo">126</span> * @return the instance.<a name="line.126"></a> +<span class="sourceLineNo">127</span> * @throws PoolException if recycling fails.<a name="line.127"></a> +<span class="sourceLineNo">128</span> */<a name="line.128"></a> +<span class="sourceLineNo">129</span> public Object getInstance(String className, Object[] params, String[] signature) throws PoolException <a name="line.129"></a> +<span class="sourceLineNo">130</span> {<a name="line.130"></a> +<span class="sourceLineNo">131</span> try <a name="line.131"></a> +<span class="sourceLineNo">132</span> {<a name="line.132"></a> +<span class="sourceLineNo">133</span> Object instance = pollInstance(className, params, signature);<a name="line.133"></a> +<span class="sourceLineNo">134</span> return instance == null ? getFactory().getInstance(className, params, signature) : instance;<a name="line.134"></a> +<span class="sourceLineNo">135</span> } <a name="line.135"></a> +<span class="sourceLineNo">136</span> catch (FactoryException fe) <a name="line.136"></a> +<span class="sourceLineNo">137</span> {<a name="line.137"></a> +<span class="sourceLineNo">138</span> throw new PoolException(fe);<a name="line.138"></a> +<span class="sourceLineNo">139</span> }<a name="line.139"></a> +<span class="sourceLineNo">140</span> }<a name="line.140"></a> +<span class="sourceLineNo">141</span><a name="line.141"></a> +<span class="sourceLineNo">142</span> /**<a name="line.142"></a> +<span class="sourceLineNo">143</span> * Gets an instance of a named class either from the pool or by calling the<a name="line.143"></a> +<span class="sourceLineNo">144</span> * Factory Service if the pool is empty. Parameters for its constructor are<a name="line.144"></a> +<span class="sourceLineNo">145</span> * given as an array of objects, primitive types must be wrapped with a<a name="line.145"></a> +<span class="sourceLineNo">146</span> * corresponding class. The specified class loader will be passed to the Factory<a name="line.146"></a> +<span class="sourceLineNo">147</span> * Service.<a name="line.147"></a> +<span class="sourceLineNo">148</span> *<a name="line.148"></a> +<span class="sourceLineNo">149</span> * @param className the name of the class.<a name="line.149"></a> +<span class="sourceLineNo">150</span> * @param loader the class loader.<a name="line.150"></a> +<span class="sourceLineNo">151</span> * @param params an array containing the parameters of the constructor.<a name="line.151"></a> +<span class="sourceLineNo">152</span> * @param signature an array containing the signature of the constructor.<a name="line.152"></a> +<span class="sourceLineNo">153</span> * @return the instance.<a name="line.153"></a> +<span class="sourceLineNo">154</span> * @throws PoolException if recycling fails.<a name="line.154"></a> +<span class="sourceLineNo">155</span> */<a name="line.155"></a> +<span class="sourceLineNo">156</span> public Object getInstance(String className, ClassLoader loader, Object[] params, String[] signature)<a name="line.156"></a> +<span class="sourceLineNo">157</span> throws PoolException <a name="line.157"></a> +<span class="sourceLineNo">158</span> {<a name="line.158"></a> +<span class="sourceLineNo">159</span> try <a name="line.159"></a> +<span class="sourceLineNo">160</span> {<a name="line.160"></a> +<span class="sourceLineNo">161</span> Object instance = pollInstance(className, params, signature);<a name="line.161"></a> +<span class="sourceLineNo">162</span> return instance == null ? getFactory().getInstance(className, loader, params, signature) : instance;<a name="line.162"></a> +<span class="sourceLineNo">163</span> } <a name="line.163"></a> +<span class="sourceLineNo">164</span> catch (FactoryException fe) <a name="line.164"></a> +<span class="sourceLineNo">165</span> {<a name="line.165"></a> +<span class="sourceLineNo">166</span> throw new PoolException(fe);<a name="line.166"></a> +<span class="sourceLineNo">167</span> }<a name="line.167"></a> +<span class="sourceLineNo">168</span> }<a name="line.168"></a> +<span class="sourceLineNo">169</span><a name="line.169"></a> +<span class="sourceLineNo">170</span> /**<a name="line.170"></a> +<span class="sourceLineNo">171</span> * Tests if specified class loaders are supported for a named class.<a name="line.171"></a> +<span class="sourceLineNo">172</span> *<a name="line.172"></a> +<span class="sourceLineNo">173</span> * @param className the name of the class.<a name="line.173"></a> +<span class="sourceLineNo">174</span> * @return true if class loaders are supported, false otherwise.<a name="line.174"></a> +<span class="sourceLineNo">175</span> * @throws FactoryException if test fails.<a name="line.175"></a> +<span class="sourceLineNo">176</span> */<a name="line.176"></a> +<span class="sourceLineNo">177</span> public boolean isLoaderSupported(String className) throws FactoryException <a name="line.177"></a> +<span class="sourceLineNo">178</span> {<a name="line.178"></a> +<span class="sourceLineNo">179</span> return getFactory().isLoaderSupported(className);<a name="line.179"></a> +<span class="sourceLineNo">180</span> }<a name="line.180"></a> +<span class="sourceLineNo">181</span><a name="line.181"></a> +<span class="sourceLineNo">182</span> /**<a name="line.182"></a> +<span class="sourceLineNo">183</span> * Gets an instance of a specified class either from the pool or by instatiating<a name="line.183"></a> +<span class="sourceLineNo">184</span> * from the class if the pool is empty.<a name="line.184"></a> +<span class="sourceLineNo">185</span> *<a name="line.185"></a> +<span class="sourceLineNo">186</span> * @param clazz the class.<a name="line.186"></a> +<span class="sourceLineNo">187</span> * @return the instance.<a name="line.187"></a> +<span class="sourceLineNo">188</span> * @throws PoolException if recycling fails.<a name="line.188"></a> +<span class="sourceLineNo">189</span> */<a name="line.189"></a> +<span class="sourceLineNo">190</span> @SuppressWarnings("unchecked")<a name="line.190"></a> +<span class="sourceLineNo">191</span> public Object getInstance(Class clazz) throws PoolException <a name="line.191"></a> +<span class="sourceLineNo">192</span> {<a name="line.192"></a> +<span class="sourceLineNo">193</span> try <a name="line.193"></a> +<span class="sourceLineNo">194</span> {<a name="line.194"></a> +<span class="sourceLineNo">195</span> Object instance = pollInstance(clazz.getName(), null, null);<a name="line.195"></a> +<span class="sourceLineNo">196</span> return instance == null ? factoryService.getInstance(clazz) : instance;<a name="line.196"></a> +<span class="sourceLineNo">197</span> } <a name="line.197"></a> +<span class="sourceLineNo">198</span> catch (FactoryException fe) <a name="line.198"></a> +<span class="sourceLineNo">199</span> {<a name="line.199"></a> +<span class="sourceLineNo">200</span> throw new PoolException(fe);<a name="line.200"></a> +<span class="sourceLineNo">201</span> }<a name="line.201"></a> +<span class="sourceLineNo">202</span> }<a name="line.202"></a> +<span class="sourceLineNo">203</span><a name="line.203"></a> +<span class="sourceLineNo">204</span> /**<a name="line.204"></a> +<span class="sourceLineNo">205</span> * Gets an instance of a specified class either from the pool or by instatiating<a name="line.205"></a> +<span class="sourceLineNo">206</span> * from the class if the pool is empty.<a name="line.206"></a> +<span class="sourceLineNo">207</span> *<a name="line.207"></a> +<span class="sourceLineNo">208</span> * @param clazz the class.<a name="line.208"></a> +<span class="sourceLineNo">209</span> * @param params an array containing the parameters of the constructor.<a name="line.209"></a> +<span class="sourceLineNo">210</span> * @param signature an array containing the signature of the constructor.<a name="line.210"></a> +<span class="sourceLineNo">211</span> * @return the instance.<a name="line.211"></a> +<span class="sourceLineNo">212</span> * @throws PoolException if recycling fails.<a name="line.212"></a> +<span class="sourceLineNo">213</span> */<a name="line.213"></a> +<span class="sourceLineNo">214</span> public Object getInstance(Class clazz, Object params[], String signature[]) throws PoolException <a name="line.214"></a> +<span class="sourceLineNo">215</span> {<a name="line.215"></a> +<span class="sourceLineNo">216</span> try <a name="line.216"></a> +<span class="sourceLineNo">217</span> {<a name="line.217"></a> +<span class="sourceLineNo">218</span> Object instance = pollInstance(clazz.getName(), params, signature);<a name="line.218"></a> +<span class="sourceLineNo">219</span> <a name="line.219"></a> +<span class="sourceLineNo">220</span> // TODO There is a whacky .toString() on the clazz object, <a name="line.220"></a> +<span class="sourceLineNo">221</span> // but otherwise it won't compile<a name="line.221"></a> +<span class="sourceLineNo">222</span> return instance == null ? getFactory().getInstance(clazz.toString(), params, signature) : instance;<a name="line.222"></a> +<span class="sourceLineNo">223</span> <a name="line.223"></a> +<span class="sourceLineNo">224</span> } <a name="line.224"></a> +<span class="sourceLineNo">225</span> catch (FactoryException fe) <a name="line.225"></a> +<span class="sourceLineNo">226</span> {<a name="line.226"></a> +<span class="sourceLineNo">227</span> throw new PoolException(fe);<a name="line.227"></a> +<span class="sourceLineNo">228</span> }<a name="line.228"></a> +<span class="sourceLineNo">229</span> }<a name="line.229"></a> +<span class="sourceLineNo">230</span><a name="line.230"></a> +<span class="sourceLineNo">231</span> /**<a name="line.231"></a> +<span class="sourceLineNo">232</span> * Puts a used object back to the pool. Objects implementing the Recyclable<a name="line.232"></a> +<span class="sourceLineNo">233</span> * interface can provide a recycle method to be called when they are reused and<a name="line.233"></a> +<span class="sourceLineNo">234</span> * a dispose method to be called when they are returned to the pool.<a name="line.234"></a> +<span class="sourceLineNo">235</span> *<a name="line.235"></a> +<span class="sourceLineNo">236</span> * @param instance the object instance to recycle.<a name="line.236"></a> +<span class="sourceLineNo">237</span> * @return true if the instance was accepted.<a name="line.237"></a> +<span class="sourceLineNo">238</span> */<a name="line.238"></a> +<span class="sourceLineNo">239</span> @SuppressWarnings("unchecked")<a name="line.239"></a> +<span class="sourceLineNo">240</span> public boolean putInstance(Object instance) <a name="line.240"></a> +<span class="sourceLineNo">241</span> {<a name="line.241"></a> +<span class="sourceLineNo">242</span> if (instance != null) <a name="line.242"></a> +<span class="sourceLineNo">243</span> {<a name="line.243"></a> +<span class="sourceLineNo">244</span> HashMap<String, PoolBuffer> repository = poolRepository;<a name="line.244"></a> +<span class="sourceLineNo">245</span> String className = instance.getClass().getName();<a name="line.245"></a> +<span class="sourceLineNo">246</span> PoolBuffer pool = (PoolBuffer) repository.get(className);<a name="line.246"></a> +<span class="sourceLineNo">247</span> if (pool == null) <a name="line.247"></a> +<span class="sourceLineNo">248</span> {<a name="line.248"></a> +<span class="sourceLineNo">249</span> pool = new PoolBuffer(getCapacity(className));<a name="line.249"></a> +<span class="sourceLineNo">250</span> repository = (HashMap<String, PoolBuffer>) repository.clone();<a name="line.250"></a> +<span class="sourceLineNo">251</span> repository.put(className, pool);<a name="line.251"></a> +<span class="sourceLineNo">252</span> poolRepository = repository;<a name="line.252"></a> +<span class="sourceLineNo">253</span> if (instance instanceof ArrayCtorRecyclable) <a name="line.253"></a> +<span class="sourceLineNo">254</span> {<a name="line.254"></a> +<span class="sourceLineNo">255</span> pool.setArrayCtorRecyclable(true);<a name="line.255"></a> +<span class="sourceLineNo">256</span> }<a name="line.256"></a> +<span class="sourceLineNo">257</span> }<a name="line.257"></a> +<span class="sourceLineNo">258</span> return pool.offer(instance);<a name="line.258"></a> +<span class="sourceLineNo">259</span> } else {<a name="line.259"></a> +<span class="sourceLineNo">260</span> return false;<a name="line.260"></a> +<span class="sourceLineNo">261</span> }<a name="line.261"></a> +<span class="sourceLineNo">262</span> }<a name="line.262"></a> +<span class="sourceLineNo">263</span><a name="line.263"></a> +<span class="sourceLineNo">264</span> /**<a name="line.264"></a> +<span class="sourceLineNo">265</span> * Gets the capacity of the pool for a named class.<a name="line.265"></a> +<span class="sourceLineNo">266</span> *<a name="line.266"></a> +<span class="sourceLineNo">267</span> * @param className the name of the class.<a name="line.267"></a> +<span class="sourceLineNo">268</span> */<a name="line.268"></a> +<span class="sourceLineNo">269</span> public int getCapacity(String className) <a name="line.269"></a> +<span class="sourceLineNo">270</span> {<a name="line.270"></a> +<span class="sourceLineNo">271</span> PoolBuffer pool = (PoolBuffer) poolRepository.get(className);<a name="line.271"></a> +<span class="sourceLineNo">272</span> if (pool == null) <a name="line.272"></a> +<span class="sourceLineNo">273</span> {<a name="line.273"></a> +<span class="sourceLineNo">274</span> /* Check class specific capacity. */<a name="line.274"></a> +<span class="sourceLineNo">275</span> int capacity = poolCapacity;<a name="line.275"></a> +<span class="sourceLineNo">276</span> if (capacityMap != null) <a name="line.276"></a> +<span class="sourceLineNo">277</span> {<a name="line.277"></a> +<span class="sourceLineNo">278</span> Integer cap = (Integer) capacityMap.get(className);<a name="line.278"></a> +<span class="sourceLineNo">279</span> if (cap != null) <a name="line.279"></a> +<span class="sourceLineNo">280</span> {<a name="line.280"></a> +<span class="sourceLineNo">281</span> capacity = cap.intValue();<a name="line.281"></a> +<span class="sourceLineNo">282</span> }<a name="line.282"></a> +<span class="sourceLineNo">283</span> }<a name="line.283"></a> +<span class="sourceLineNo">284</span> return capacity;<a name="line.284"></a> +<span class="sourceLineNo">285</span> } else {<a name="line.285"></a>
[... 181 lines stripped ...]
