KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > dig > DIGConnectionPool


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email ian.dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 11-Sep-2003
9  * Filename $RCSfile: DIGConnectionPool.java,v $
10  * Revision $Revision: 1.5 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 12:16:18 $
14  * by $Author: andy_seaborne $
15  *
16  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * [See end of file]
18  *****************************************************************************/

19
20
21 // Package
22
///////////////
23
package com.hp.hpl.jena.reasoner.dig;
24
25
26
27 // Imports
28
///////////////
29
import java.util.*;
30
31
32
33 /**
34  * <p>
35  * Maintains a pool of active DIG connections and whether they are allocated or not.
36  * Implements Singleton pattern.
37  * </p>
38  *
39  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
40  * @version Release @release@ ($Id: DIGConnectionPool.java,v 1.5 2005/02/21 12:16:18 andy_seaborne Exp $)
41  */

42 public class DIGConnectionPool {
43     // Constants
44
//////////////////////////////////
45

46     // Static variables
47
//////////////////////////////////
48

49     /** The singleton instance */
50     private static DIGConnectionPool s_instance = new DIGConnectionPool();
51     
52     
53     // Instance variables
54
//////////////////////////////////
55

56     /** The adapter pool - unallocated adapters */
57     private List m_pool = new ArrayList();
58     
59     /** The allocated adapter list */
60     private List m_allocated = new ArrayList();
61     
62     
63     // Constructors
64
//////////////////////////////////
65

66     /** Private constructor to enforce Singleton pattern. */
67     private DIGConnectionPool() {}
68     
69     
70     // External signature methods
71
//////////////////////////////////
72

73     /**
74      * <p>Answer the singleton instance of the adapter pool.</p>
75      */

76     public static DIGConnectionPool getInstance() {
77         return s_instance;
78     }
79     
80     
81     public DIGConnection allocate() {
82         DIGConnection dc = m_pool.isEmpty() ? new DIGConnection() : (DIGConnection) m_pool.remove( 0 );
83         m_allocated.add( dc );
84             
85         return dc;
86     }
87     
88     
89     public DIGConnection allocate( String JavaDoc connectionURL ) {
90         // first we try to find an existing connection to the same URL
91
for (int i = 0; i < m_pool.size(); i++) {
92             DIGConnection c = (DIGConnection) m_pool.get( i );
93             
94             if (connectionURL.equals( c.getReasonerURL() )) {
95                 m_pool.remove( i );
96                 m_allocated.add( c );
97                 return c;
98             }
99         }
100         
101         // otherwise, form a new connection
102
DIGConnection dc = m_pool.isEmpty() ? new DIGConnection() : (DIGConnection) m_pool.remove( 0 );
103         m_allocated.add( dc );
104         dc.setReasonerURL( connectionURL );
105         
106         return dc;
107     }
108     
109     
110     public void release( DIGConnection dc ) {
111         m_allocated.remove( dc );
112         m_pool.add( dc );
113     }
114     
115     
116     // Internal implementation methods
117
//////////////////////////////////
118

119     //==============================================================================
120
// Inner class definitions
121
//==============================================================================
122

123 }
124
125
126 /*
127  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
128  * All rights reserved.
129  *
130  * Redistribution and use in source and binary forms, with or without
131  * modification, are permitted provided that the following conditions
132  * are met:
133  * 1. Redistributions of source code must retain the above copyright
134  * notice, this list of conditions and the following disclaimer.
135  * 2. Redistributions in binary form must reproduce the above copyright
136  * notice, this list of conditions and the following disclaimer in the
137  * documentation and/or other materials provided with the distribution.
138  * 3. The name of the author may not be used to endorse or promote products
139  * derived from this software without specific prior written permission.
140  *
141  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
142  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
143  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
144  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
145  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
146  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
147  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
148  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
149  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
150  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
151  */

152
Popular Tags