001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email ian.dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 11-Sep-2003
009: * Filename $RCSfile: DIGConnectionPool.java,v $
010: * Revision $Revision: 1.8 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:07:11 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * [See end of file]
018: *****************************************************************************/package com.hp.hpl.jena.reasoner.dig;
019:
020: // Imports
021: ///////////////
022: import java.util.*;
023:
024: /**
025: * <p>
026: * Maintains a pool of active DIG connections and whether they are allocated or not.
027: * Implements Singleton pattern.
028: * </p>
029: *
030: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com">email</a>)
031: * @version Release @release@ ($Id: DIGConnectionPool.java,v 1.8 2008/01/02 12:07:11 andy_seaborne Exp $)
032: */
033: public class DIGConnectionPool {
034: // Constants
035: //////////////////////////////////
036:
037: // Static variables
038: //////////////////////////////////
039:
040: /** The singleton instance */
041: private static DIGConnectionPool s_instance = new DIGConnectionPool();
042:
043: // Instance variables
044: //////////////////////////////////
045:
046: /** The adapter pool - unallocated adapters */
047: private List m_pool = new ArrayList();
048:
049: /** The allocated adapter list */
050: private List m_allocated = new ArrayList();
051:
052: // Constructors
053: //////////////////////////////////
054:
055: /** Private constructor to enforce Singleton pattern. */
056: private DIGConnectionPool() {
057: }
058:
059: // External signature methods
060: //////////////////////////////////
061:
062: /**
063: * <p>Answer the singleton instance of the adapter pool.</p>
064: */
065: public static DIGConnectionPool getInstance() {
066: return s_instance;
067: }
068:
069: public DIGConnection allocate() {
070: DIGConnection dc = m_pool.isEmpty() ? new DIGConnection()
071: : (DIGConnection) m_pool.remove(0);
072: m_allocated.add(dc);
073:
074: return dc;
075: }
076:
077: public DIGConnection allocate(String connectionURL) {
078: // first we try to find an existing connection to the same URL
079: for (int i = 0; i < m_pool.size(); i++) {
080: DIGConnection c = (DIGConnection) m_pool.get(i);
081:
082: if (connectionURL.equals(c.getReasonerURL())) {
083: m_pool.remove(i);
084: m_allocated.add(c);
085: return c;
086: }
087: }
088:
089: // otherwise, form a new connection
090: DIGConnection dc = m_pool.isEmpty() ? new DIGConnection()
091: : (DIGConnection) m_pool.remove(0);
092: m_allocated.add(dc);
093: dc.setReasonerURL(connectionURL);
094:
095: return dc;
096: }
097:
098: public void release(DIGConnection dc) {
099: m_allocated.remove(dc);
100: m_pool.add(dc);
101: }
102:
103: // Internal implementation methods
104: //////////////////////////////////
105:
106: //==============================================================================
107: // Inner class definitions
108: //==============================================================================
109:
110: }
111:
112: /*
113: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
114: * All rights reserved.
115: *
116: * Redistribution and use in source and binary forms, with or without
117: * modification, are permitted provided that the following conditions
118: * are met:
119: * 1. Redistributions of source code must retain the above copyright
120: * notice, this list of conditions and the following disclaimer.
121: * 2. Redistributions in binary form must reproduce the above copyright
122: * notice, this list of conditions and the following disclaimer in the
123: * documentation and/or other materials provided with the distribution.
124: * 3. The name of the author may not be used to endorse or promote products
125: * derived from this software without specific prior written permission.
126: *
127: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
128: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
129: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
130: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
131: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
132: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
133: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
134: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
135: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
136: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
137: */
|