001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Map;
022import java.util.concurrent.ArrayBlockingQueue;
023import java.util.concurrent.BlockingQueue;
024
025import org.apache.camel.spi.ServicePool;
026import org.apache.camel.support.ServiceSupport;
027import org.apache.camel.util.LRUCache;
028import org.apache.camel.util.ServiceHelper;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Default implementation to inherit for a basic service pool.
034 *
035 * @version 
036 */
037public abstract class DefaultServicePool<Key, Service> extends ServiceSupport implements ServicePool<Key, Service> {
038    protected final Logger log = LoggerFactory.getLogger(getClass());
039    protected final Map<Key, BlockingQueue<Service>> pool;
040    protected int capacity = 100;
041
042    protected DefaultServicePool() {
043        this.pool = new LRUCache(capacity);
044    }
045
046    public DefaultServicePool(int capacity) {
047        this.capacity = capacity;
048        this.pool = new LRUCache(capacity);
049    }
050
051    public int getCapacity() {
052        return capacity;
053    }
054
055    public void setCapacity(int capacity) {
056        this.capacity = capacity;
057    }
058
059    public synchronized int size() {
060        int size = 0;
061        for (BlockingQueue<Service> entry : pool.values()) {
062            size += entry.size();
063        }
064        return size;
065    }
066
067    public synchronized Service addAndAcquire(Key key, Service service) {
068        BlockingQueue<Service> entry = pool.get(key);
069        if (entry == null) {
070            entry = new ArrayBlockingQueue<Service>(capacity);
071            pool.put(key, entry);
072        }
073        log.trace("AddAndAcquire key: {} service: {}", key, service);
074
075        // test if queue will be full
076        if (entry.size() >= capacity) {
077            throw new IllegalStateException("Queue full");
078        }
079        return service;
080    }
081
082    public synchronized Service acquire(Key key) {
083        BlockingQueue<Service> services = pool.get(key);
084        if (services == null || services.isEmpty()) {
085            log.trace("No free services in pool to acquire for key: {}", key);
086            return null;
087        }
088
089        Service answer = services.poll();
090        log.trace("Acquire: {} service: {}", key, answer);
091        return answer;
092    }
093
094    public synchronized void release(Key key, Service service) {
095        log.trace("Release: {} service: {}", key, service);
096        BlockingQueue<Service> services = pool.get(key);
097        if (services != null) {
098            services.add(service);
099        }
100    }
101
102    public void purge() {
103        pool.clear();
104    }
105
106    protected void doStart() throws Exception {
107        log.debug("Starting service pool: {}", this);
108    }
109
110    protected void doStop() throws Exception {
111        log.debug("Stopping service pool: {}", this);
112        for (BlockingQueue<Service> entry : pool.values()) {
113            Collection<Service> values = new ArrayList<Service>();
114            entry.drainTo(values);
115            ServiceHelper.stopServices(values);
116            entry.clear();
117        }
118        pool.clear();
119    }
120
121}