📄 poolutils.html
字号:
<a name="524" href="#524">524</a> <a name="525" href="#525">525</a> <strong>private</strong> <strong>static</strong> <strong>class</strong> KeyedObjectPoolAdaptor implements <a href="../../../../org/apache/commons/pool/KeyedObjectPool.html">KeyedObjectPool</a> {<a name="526" href="#526">526</a> <strong>private</strong> <strong>final</strong> <a href="../../../../org/apache/commons/pool/ObjectPool.html">ObjectPool</a> pool;<a name="527" href="#527">527</a> <a name="528" href="#528">528</a> KeyedObjectPoolAdaptor(<strong>final</strong> <a href="../../../../org/apache/commons/pool/ObjectPool.html">ObjectPool</a> pool) throws IllegalArgumentException {<a name="529" href="#529">529</a> <strong>if</strong> (pool == <strong>null</strong>) {<a name="530" href="#530">530</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<span class="string">"keyedPool must not be null."</span>);<a name="531" href="#531">531</a> }<a name="532" href="#532">532</a> <strong>this</strong>.pool = pool;<a name="533" href="#533">533</a> }<a name="534" href="#534">534</a> <a name="535" href="#535">535</a> <strong>public</strong> Object borrowObject(<strong>final</strong> Object key) throws Exception, NoSuchElementException, IllegalStateException {<a name="536" href="#536">536</a> <strong>return</strong> pool.borrowObject();<a name="537" href="#537">537</a> }<a name="538" href="#538">538</a> <a name="539" href="#539">539</a> <strong>public</strong> <strong>void</strong> returnObject(<strong>final</strong> Object key, <strong>final</strong> Object obj) throws Exception {<a name="540" href="#540">540</a> pool.returnObject(obj);<a name="541" href="#541">541</a> }<a name="542" href="#542">542</a> <a name="543" href="#543">543</a> <strong>public</strong> <strong>void</strong> invalidateObject(<strong>final</strong> Object key, <strong>final</strong> Object obj) throws Exception {<a name="544" href="#544">544</a> pool.invalidateObject(obj);<a name="545" href="#545">545</a> }<a name="546" href="#546">546</a> <a name="547" href="#547">547</a> <strong>public</strong> <strong>void</strong> addObject(<strong>final</strong> Object key) throws Exception, IllegalStateException {<a name="548" href="#548">548</a> pool.addObject();<a name="549" href="#549">549</a> }<a name="550" href="#550">550</a> <a name="551" href="#551">551</a> <strong>public</strong> <strong>int</strong> getNumIdle(<strong>final</strong> Object key) throws UnsupportedOperationException {<a name="552" href="#552">552</a> <strong>return</strong> pool.getNumIdle();<a name="553" href="#553">553</a> }<a name="554" href="#554">554</a> <a name="555" href="#555">555</a> <strong>public</strong> <strong>int</strong> getNumActive(<strong>final</strong> Object key) throws UnsupportedOperationException {<a name="556" href="#556">556</a> <strong>return</strong> pool.getNumActive();<a name="557" href="#557">557</a> }<a name="558" href="#558">558</a> <a name="559" href="#559">559</a> <strong>public</strong> <strong>int</strong> getNumIdle() throws UnsupportedOperationException {<a name="560" href="#560">560</a> <strong>return</strong> pool.getNumIdle();<a name="561" href="#561">561</a> }<a name="562" href="#562">562</a> <a name="563" href="#563">563</a> <strong>public</strong> <strong>int</strong> getNumActive() throws UnsupportedOperationException {<a name="564" href="#564">564</a> <strong>return</strong> pool.getNumActive();<a name="565" href="#565">565</a> }<a name="566" href="#566">566</a> <a name="567" href="#567">567</a> <strong>public</strong> <strong>void</strong> clear() throws Exception, UnsupportedOperationException {<a name="568" href="#568">568</a> pool.clear();<a name="569" href="#569">569</a> }<a name="570" href="#570">570</a> <a name="571" href="#571">571</a> <strong>public</strong> <strong>void</strong> clear(<strong>final</strong> Object key) throws Exception, UnsupportedOperationException {<a name="572" href="#572">572</a> pool.clear();<a name="573" href="#573">573</a> }<a name="574" href="#574">574</a> <a name="575" href="#575">575</a> <strong>public</strong> <strong>void</strong> close() throws Exception {<a name="576" href="#576">576</a> pool.close();<a name="577" href="#577">577</a> }<a name="578" href="#578">578</a> <a name="579" href="#579">579</a> <strong>public</strong> <strong>void</strong> setFactory(<strong>final</strong> <a href="../../../../org/apache/commons/pool/KeyedPoolableObjectFactory.html">KeyedPoolableObjectFactory</a> factory) throws IllegalStateException, UnsupportedOperationException {<a name="580" href="#580">580</a> pool.setFactory(adapt(factory));<a name="581" href="#581">581</a> }<a name="582" href="#582">582</a> <a name="583" href="#583">583</a> <strong>public</strong> String toString() {<a name="584" href="#584">584</a> <strong>final</strong> StringBuffer sb = <strong>new</strong> StringBuffer();<a name="585" href="#585">585</a> sb.append(<span class="string">"KeyedObjectPoolAdaptor"</span>);<a name="586" href="#586">586</a> sb.append(<span class="string">"{keyedPool="</span>).append(pool);<a name="587" href="#587">587</a> sb.append('}');<a name="588" href="#588">588</a> <strong>return</strong> sb.toString();<a name="589" href="#589">589</a> }<a name="590" href="#590">590</a> }<a name="591" href="#591">591</a> <a name="592" href="#592">592</a> <strong>private</strong> <strong>static</strong> <strong>class</strong> CheckedObjectPool implements <a href="../../../../org/apache/commons/pool/ObjectPool.html">ObjectPool</a> {<a name="593" href="#593">593</a> <strong>private</strong> <strong>final</strong> Class type;<a name="594" href="#594">594</a> <strong>private</strong> <strong>final</strong> <a href="../../../../org/apache/commons/pool/ObjectPool.html">ObjectPool</a> pool;<a name="595" href="#595">595</a> <a name="596" href="#596">596</a> CheckedObjectPool(<strong>final</strong> <a href="../../../../org/apache/commons/pool/ObjectPool.html">ObjectPool</a> pool, <strong>final</strong> Class type) {<a name="597" href="#597">597</a> <strong>if</strong> (pool == <strong>null</strong>) {<a name="598" href="#598">598</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<span class="string">"pool must not be null."</span>);<a name="599" href="#599">599</a> }<a name="600" href="#600">600</a> <strong>if</strong> (type == <strong>null</strong>) {<a name="601" href="#601">601</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<span class="string">"type must not be null."</span>);<a name="602" href="#602">602</a> }<a name="603" href="#603">603</a> <strong>this</strong>.pool = pool;<a name="604" href="#604">604</a> <strong>this</strong>.type = type;<a name="605" href="#605">605</a> }<a name="606" href="#606">606</a> <a name="607" href="#607">607</a> <strong>public</strong> Object borrowObject() throws Exception, NoSuchElementException, IllegalStateException {<a name="608" href="#608">608</a> <strong>final</strong> Object obj = pool.borrowObject();<a name="609" href="#609">609</a> <strong>if</strong> (type.isInstance(obj)) {<a name="610" href="#610">610</a> <strong>return</strong> obj;<a name="611" href="#611">611</a> } <strong>else</strong> {<a name="612" href="#612">612</a> <strong>throw</strong> <strong>new</strong> ClassCastException(<span class="string">"Borrowed object is not of type: "</span> + type.getName() + <span class="string">" was: "</span> + obj);<a name="613" href="#613">613</a> }<a name="614" href="#614">614</a> }<a name="615" href="#615">615</a> <a name="616" href="#616">616</a> <strong>public</strong> <strong>void</strong> returnObject(<strong>final</strong> Object obj) throws Exception {<a name="617" href="#617">617</a> <strong>if</strong> (type.isInstance(obj)) {<a name="618" href="#618">618</a> pool.returnObject(obj);<a name="619" href="#619">619</a> } <strong>else</strong> {<a name="620" href="#620">620</a> <strong>throw</strong> <strong>new</strong> ClassCastException(<span class="string">"Returned object is not of type: "</span> + type.getName() + <span class="string">" was: "</span> + obj);<a name="621" href="#621">621</a> }<a name="622" href="#622">622</a> }<a name="623" href="#623">623</a> <a name="624" href="#624">624</a> <strong>public</strong> <strong>void</strong> invalidateObject(<strong>final</strong> Object obj) throws Exception {<a name="625" href="#625">625</a> <strong>if</strong> (type.isInstance(obj)) {<a name="626" href="#626">626</a> pool.invalidateObject(obj);<a name="627" href="#627">627</a> } <strong>else</strong> {<a name="628" href="#628">628</a> <strong>throw</strong> <strong>new</strong> ClassCastException(<span class="string">"Invalidated object is not of type: "</span> + type.getName() + <span class="string">" was: "</span> + obj);<a name="629" href="#629">629</a> }<a name="630" href="#630">630</a> }<a name="631" href="#631">631</a> <a name="632" href="#632">632</a> <strong>public</strong> <strong>void</strong> addObject() throws Exception, IllegalStateException, UnsupportedOperationException {<a name="633" href="#633">633</a> pool.addObject();<a name="634" href="#634">634</a> }<a name="635" href="#635">635</a> <a name="636" href="#636">636</a> <strong>public</strong> <strong>int</strong> getNumIdle() throws UnsupportedOperationException {<a name="637" href="#637">637</a> <strong>return</strong> pool.getNumIdle();<a name="638" href="#638">638</a> }<a name="639" href="#639">639</a> <a name="640" href="#640">640</a> <strong>public</strong> <strong>int</strong> getNumActive() throws UnsupportedOperationException {<a name="641" href="#641">641</a> <strong>return</strong> pool.getNumActive();<a name="642" href="#642">642</a> }<a name="643" href="#643">643</a> <a name="644" href="#644">644</a> <strong>public</strong> <strong>void</strong> clear() throws Exception, UnsupportedOperationException {<a name="645" href="#645">645</a> pool.clear();<a name="646" href="#646">646</a> }<a name="647" href="#647">647</a> <a name="648" href="#648">648</a> <strong>public</strong> <strong>void</strong> close() throws Exception {<a name="649" href="#649">649</a> pool.close();<a name="650" href="#650">650</a> }<a name="651" href="#651">651</a> <a name="652" href="#652">652</a> <strong>public</strong> <strong>void</strong> setFactory(<strong>final</strong> <a href="../../../../org/apache/commons/pool/PoolableObjectFactory.html">PoolableObjectFactory</a> factory) throws IllegalStateException, UnsupportedOperationException {<a name="653" href="#653">653</a> pool.setFactory(factory);<a name="654" href="#654">654</a> }<a name="655" href="#655">655</a> <a name="656" href="#656">656</a> <strong>public</strong> String t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -