org.apache.camel.component.netty.SharedSingletonObjectPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-netty Show documentation
Show all versions of camel-netty Show documentation
Camel Netty NIO based socket communication component
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.netty;
import java.util.NoSuchElementException;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
/**
* An {@link org.apache.commons.pool.ObjectPool} that uses a single shared instance.
*
* This implementation will always return 1 in {@link #getNumActive()} and
* return 0 in {@link #getNumIdle()}.
*/
public class SharedSingletonObjectPool implements ObjectPool {
private final PoolableObjectFactory factory;
private volatile T t;
public SharedSingletonObjectPool(PoolableObjectFactory factory) {
this.factory = factory;
}
@Override
public synchronized T borrowObject() throws Exception, NoSuchElementException, IllegalStateException {
if (t == null) {
t = factory.makeObject();
}
return t;
}
@Override
public void returnObject(T obj) throws Exception {
// noop
}
@Override
public void invalidateObject(T obj) throws Exception {
t = null;
}
@Override
public void addObject() throws Exception, IllegalStateException, UnsupportedOperationException {
// noop
}
@Override
public int getNumIdle() throws UnsupportedOperationException {
return 0;
}
@Override
public int getNumActive() throws UnsupportedOperationException {
return 1;
}
@Override
public void clear() throws Exception, UnsupportedOperationException {
t = null;
}
@Override
public void close() throws Exception {
t = null;
}
@Override
public void setFactory(PoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException {
// noop
}
}