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: */
017: package org.apache.catalina.tribes.io;
018:
019: import org.apache.juli.logging.Log;
020: import org.apache.juli.logging.LogFactory;
021:
022: /**
023: *
024: * @author Filip Hanik
025: *
026: * @version 1.0
027: */
028: public class BufferPool {
029: protected static Log log = LogFactory.getLog(BufferPool.class);
030:
031: public static int DEFAULT_POOL_SIZE = 100 * 1024 * 1024; //100MB
032:
033: protected static volatile BufferPool instance = null;
034: protected BufferPoolAPI pool = null;
035:
036: private BufferPool(BufferPoolAPI pool) {
037: this .pool = pool;
038: }
039:
040: public XByteBuffer getBuffer(int minSize, boolean discard) {
041: if (pool != null)
042: return pool.getBuffer(minSize, discard);
043: else
044: return new XByteBuffer(minSize, discard);
045: }
046:
047: public void returnBuffer(XByteBuffer buffer) {
048: if (pool != null)
049: pool.returnBuffer(buffer);
050: }
051:
052: public void clear() {
053: if (pool != null)
054: pool.clear();
055: }
056:
057: public static BufferPool getBufferPool() {
058: if ((instance == null)) {
059: synchronized (BufferPool.class) {
060: if (instance == null) {
061: BufferPoolAPI pool = null;
062: Class clazz = null;
063: try {
064: clazz = Class
065: .forName("org.apache.catalina.tribes.io.BufferPool15Impl");
066: pool = (BufferPoolAPI) clazz.newInstance();
067: } catch (Throwable x) {
068: try {
069: clazz = Class
070: .forName("org.apache.catalina.tribes.io.BufferPool14Impl");
071: pool = (BufferPoolAPI) clazz.newInstance();
072: } catch (Throwable e) {
073: log
074: .warn("Unable to initilize BufferPool, not pooling XByteBuffer objects:"
075: + x.getMessage());
076: if (log.isDebugEnabled())
077: log
078: .debug(
079: "Unable to initilize BufferPool, not pooling XByteBuffer objects:",
080: x);
081: }
082: }
083: pool.setMaxSize(DEFAULT_POOL_SIZE);
084: log
085: .info("Created a buffer pool with max size:"
086: + DEFAULT_POOL_SIZE
087: + " bytes of type:"
088: + (clazz != null ? clazz.getName()
089: : "null"));
090: instance = new BufferPool(pool);
091: }//end if
092: }//sync
093: }//end if
094: return instance;
095: }
096:
097: public static interface BufferPoolAPI {
098: public void setMaxSize(int bytes);
099:
100: public XByteBuffer getBuffer(int minSize, boolean discard);
101:
102: public void returnBuffer(XByteBuffer buffer);
103:
104: public void clear();
105: }
106: }
|