List of usage examples for org.apache.commons.pool2 BasePooledObjectFactory BasePooledObjectFactory
BasePooledObjectFactory
From source file:com.mycila.xmltool.XMLDocumentBuilderFactory.java
public static void setPoolConfig(GenericObjectPoolConfig config) { ignoreNamespaceDocumentBuilderPool = new GenericObjectPool<DocumentBuilder>( new BasePooledObjectFactory<DocumentBuilder>() { @Override// w w w .j a v a2s.c o m public DocumentBuilder create() throws Exception { return newDocumentBuilder(true); } @Override public PooledObject<DocumentBuilder> wrap(DocumentBuilder obj) { return new DefaultPooledObject<DocumentBuilder>(obj); } }, config); namespaceAwareDocumentBuilderPool = new GenericObjectPool<DocumentBuilder>( new BasePooledObjectFactory<DocumentBuilder>() { @Override public DocumentBuilder create() throws Exception { return newDocumentBuilder(false); } @Override public PooledObject<DocumentBuilder> wrap(DocumentBuilder obj) { return new DefaultPooledObject<DocumentBuilder>(obj); } }, config); }
From source file:com.github.chrishantha.microbenchmark.objectpool.CommonsPool2SoftReferenceObjectPoolBenchmark.java
@Override public void setupObjectPool() { objectPool = new SoftReferenceObjectPool<>(new BasePooledObjectFactory<TestObject>() { @Override/*from w w w. j a v a2s. c om*/ public TestObject create() throws Exception { return new TestObject(true); } @Override public PooledObject<TestObject> wrap(TestObject testObject) { return new DefaultPooledObject<>(testObject); } }); }
From source file:com.github.chrishantha.microbenchmark.objectpool.CommonsPool2GenericObjectPoolBenchmark.java
@Override public void setupObjectPool() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(poolSize);//from ww w . j a v a 2 s .c o m objectPool = new GenericObjectPool<>(new BasePooledObjectFactory<TestObject>() { @Override public TestObject create() throws Exception { return new TestObject(true); } @Override public PooledObject<TestObject> wrap(TestObject testObject) { return new DefaultPooledObject<>(testObject); } }, config); }
From source file:com.skynetcomputing.database.Database.java
/** * Creates a new database connection pool so that statements and queries can be executed. *//*from w w w. j a va2 s .co m*/ @SuppressWarnings("unchecked") private Database() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxIdle(5); config.setMaxTotal(10); // Configurable connectionstring. String connString = MiscUtils.getPropertyOrDefault("dbConnection", URL); String connUser = MiscUtils.getPropertyOrDefault("dbUser", USERNAME); String connPassword = MiscUtils.getPropertyOrDefault("dbPassword", PASSWORD); pool = new GenericObjectPool<>(new BasePooledObjectFactory() { @Override public Object create() throws Exception { return DriverManager.getConnection(connString, connUser, connPassword); } @Override public PooledObject wrap(Object o) { return new PooledSoftReference<>(new SoftReference<>(o)); } }, config); }
From source file:com.lambdaworks.redis.RedisConnectionPool.java
private PooledObjectFactory<T> createFactory(final RedisConnectionProvider<T> redisConnectionProvider) { return new BasePooledObjectFactory<T>() { @SuppressWarnings("unchecked") @Override//from w w w . j a v a 2 s.co m public T create() throws Exception { T connection = redisConnectionProvider.createConnection(); PooledConnectionInvocationHandler<T> h = new PooledConnectionInvocationHandler<>(connection, RedisConnectionPool.this); Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { redisConnectionProvider.getComponentType() }, h); return (T) proxy; } @Override public PooledObject<T> wrap(T obj) { return new DefaultPooledObject<>(obj); } @Override public boolean validateObject(PooledObject<T> p) { return Connections.isOpen(p.getObject()); } @Override @SuppressWarnings("unchecked") public void destroyObject(PooledObject<T> p) throws Exception { T object = p.getObject(); if (Proxy.isProxyClass(object.getClass())) { PooledConnectionInvocationHandler<T> invocationHandler = (PooledConnectionInvocationHandler<T>) Proxy .getInvocationHandler(object); object = invocationHandler.getConnection(); } Connections.close(object); } }; }
From source file:de.qucosa.dissemination.epicur.servlet.EpicurDisseminationServlet.java
@Override public void init() { httpClient = HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager()) .build();/* w w w . ja v a 2 s. c o m*/ marshallerPool = new GenericObjectPool<>(new BasePooledObjectFactory<Marshaller>() { @Override public Marshaller create() throws Exception { Marshaller marshaller = JAXBContext.newInstance(Epicur.class).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XEPICUR_SCHEMA_LOCATION); return marshaller; } @Override public PooledObject<Marshaller> wrap(Marshaller marshaller) { return new DefaultPooledObject<>(marshaller); } }); }
From source file:com.ibm.broker.analytics.r.RNode.java
/** * Constructor for an RNode - creates the input and output terminals for this node. * @throws MbException if the input and output terminals cannot be created. *///w w w . j a v a2 s .co m public RNode() throws MbException { // Create the input terminal. createInputTerminal("in"); // Create the output and failure terminals. iOutputTerminal = createOutputTerminal("out"); createOutputTerminal("failure"); // Create the connection factory for this node. iConnectionFactory = new BasePooledObjectFactory<Connection>() { /** * Called when the connection pool requests a new connection to Rserve. * @return a new connection to Rserve. */ @Override public Connection create() throws RNodeException, MbException { final String methodName = "create"; // Open a new connection to the specified Rserve server. RNodeLog.logUserTrace(this, methodName, 7836, "Connecting to Rserve server", getName(), iServerHostName, iServerPort); Connection connection = null; try { connection = new Connection(iServerHostName, iServerPort); } catch (RserveException e) { throw new RNodeException(this, methodName, 7839, "Failed to connect to Rserve server", getName(), iServerHostName, iServerPort, e.toString()); } RNodeLog.logUserTrace(this, methodName, 7837, "Connected to Rserve server", getName(), iServerHostName, iServerPort); // If a RData file has been provided, load that now. if (iRDataFile != null) { try { // Check to see if the RData file has been updated. iRDataFile.update(); // Assign the RData file contents to an R variable. String scriptVariable = ".iib_r_data_" + iRDataFile.getKey(); connection.assign(scriptVariable, iRDataFile.getContent()); // Load the RData file contents on the Rserve server. // We use try, as it also allows us to retrieve the error messages from the R runtime. REXP result = connection .parseAndEval("try(load(rawConnection(" + scriptVariable + ")),silent=TRUE)"); if (result.inherits("try-error")) { throw new RNodeException(this, methodName, 7867, "R runtime failed to load file contents", getName(), iRDataFile.getFileName(), result.asString()); } } catch (REngineException | REXPMismatchException e) { throw new RNodeException(this, methodName, 7867, "R runtime failed to load file contents", getName(), iRDataFile.getFileName(), e.getMessage()); } } // If a connect script has been provided, run that before returning the connection to the pool for use. if (iConnectScript != null) { runScript(connection, iConnectScript); } return connection; } /** * Called by the connection pool to wrap a new connection. * @param the connection to wrap. * @return the wrapped connection. */ @Override public PooledObject<Connection> wrap(Connection connection) { return new DefaultPooledObject<Connection>(connection); } /** * Called by the connection pool to close an established connection. * This occurs if the connection has remained idle for too long, or the node * is being deleted. * @param pooledConnection */ @Override public void destroyObject(PooledObject<Connection> pooledConnection) throws MbException { final String methodName = "destroyObject"; // If a disconnect script has been provided, run that before closing the connection. Connection connection = pooledConnection.getObject(); if (iDisconnectScript != null) { try { runScript(connection, iDisconnectScript); } catch (MbException e) { RNodeLog.logError(this, methodName, Long.valueOf(e.getMessageKey()), e.getTraceText(), e.getInserts()); } } // Close the connection. connection.close(); RNodeLog.logUserTrace(this, methodName, 7838, "Disconnected from Rserve server", getName(), iServerHostName, iServerPort); } }; }
From source file:com.bytelightning.opensource.pokerface.PokerFace.java
/** * Configures all the needed components, but does not actually start the server. * @param config Contains all information needed to fully wire up the http, https, and httpclient components of this reverse proxy. * @throws Exception Yeah, a lot can go wrong here, but at least it will be caught immediately :-) *///from www . j av a 2 s. c om public void config(HierarchicalConfiguration config) throws Exception { List<HierarchicalConfiguration> lconf; HttpAsyncRequester executor = null; BasicNIOConnPool connPool = null; ObjectPool<ByteBuffer> byteBufferPool = null; LinkedHashMap<String, TargetDescriptor> mappings = null; ConcurrentMap<String, HttpHost> hosts = null; handlerRegistry = new UriHttpAsyncRequestHandlerMapper(); // Initialize the keystore (if one was specified) KeyStore keystore = null; char[] keypass = null; String keystoreUri = config.getString("keystore"); if ((keystoreUri != null) && (keystoreUri.trim().length() > 0)) { Path keystorePath = Utils.MakePath(keystoreUri); if (!Files.exists(keystorePath)) throw new ConfigurationException("Keystore does not exist."); if (Files.isDirectory(keystorePath)) throw new ConfigurationException("Keystore is not a file"); String storepass = config.getString("storepass"); if ((storepass != null) && "null".equals(storepass)) storepass = null; keystore = KeyStore.getInstance(KeyStore.getDefaultType()); try (InputStream keyStoreStream = Files.newInputStream(keystorePath)) { keystore.load(keyStoreStream, storepass == null ? null : storepass.trim().toCharArray()); } catch (IOException ex) { Logger.error("Unable to load https server keystore from " + keystoreUri); return; } keypass = config.getString("keypass").trim().toCharArray(); } // Wire up the listening reactor lconf = config.configurationsAt("server"); if ((lconf == null) || (lconf.size() != 1)) throw new ConfigurationException("One (and only one) server configuration element is allowed."); else { Builder builder = IOReactorConfig.custom(); builder.setIoThreadCount(ComputeReactorProcessors(config.getDouble("server[@cpu]", 0.667))); builder.setSoTimeout(config.getInt("server[@soTimeout]", 0)); builder.setSoLinger(config.getInt("server[@soLinger]", -1)); builder.setSoReuseAddress(true); builder.setTcpNoDelay(false); builder.setSelectInterval(100); IOReactorConfig rconfig = builder.build(); Logger.info("Configuring server with options: " + rconfig.toString()); listeningReactor = new DefaultListeningIOReactor(rconfig); lconf = config.configurationsAt("server.listen"); InetSocketAddress addr; boolean hasNonWildcardSecure = false; LinkedHashMap<SocketAddress, SSLContext> addrSSLContext = new LinkedHashMap<SocketAddress, SSLContext>(); if ((lconf == null) || (lconf.size() == 0)) { addr = new InetSocketAddress("127.0.0.1", 8080); ListenerEndpoint ep = listeningReactor.listen(addr); Logger.warn("Configured " + ep.getAddress()); } else { TrustManager[] trustManagers = null; KeyManagerFactory kmf = null; // Create all the specified listeners. for (HierarchicalConfiguration hc : lconf) { String addrStr = hc.getString("[@address]"); if ((addrStr == null) || (addrStr.length() == 0)) addrStr = "0.0.0.0"; String alias = hc.getString("[@alias]"); int port = hc.getInt("[@port]", alias != null ? 443 : 80); addr = new InetSocketAddress(addrStr, port); ListenerEndpoint ep = listeningReactor.listen(addr); String protocol = hc.containsKey("[@protocol]") ? hc.getString("[@protocol]") : null; Boolean secure = hc.containsKey("[@secure]") ? hc.getBoolean("[@secure]") : null; if ((alias != null) && (secure == null)) secure = true; if ((protocol != null) && (secure == null)) secure = true; if ((secure != null) && secure) { if (protocol == null) protocol = "TLS"; if (keystore == null) throw new ConfigurationException( "An https listening socket was requested, but no keystore was specified."); if (kmf == null) { kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keystore, keypass); } // Are we going to trust all clients or just specific ones? if (hc.getBoolean("[@trustAny]", true)) trustManagers = new TrustManager[] { new X509TrustAllManager() }; else { TrustManagerFactory instance = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); instance.init(keystore); trustManagers = instance.getTrustManagers(); } KeyManager[] keyManagers = kmf.getKeyManagers(); if (alias != null) for (int i = 0; i < keyManagers.length; i++) { if (keyManagers[i] instanceof X509ExtendedKeyManager) keyManagers[i] = new PokerFaceKeyManager(alias, (X509ExtendedKeyManager) keyManagers[i]); } SSLContext sslCtx = SSLContext.getInstance(protocol); sslCtx.init(keyManagers, trustManagers, new SecureRandom()); if (addr.getAddress().isAnyLocalAddress()) { // This little optimization helps us respond faster for every connection as we don't have to extrapolate a local connection address to wild card. for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { addr = new InetSocketAddress(enumIpAddr.nextElement(), port); addrSSLContext.put(addr, sslCtx); } } } else { addrSSLContext.put(addr, sslCtx); hasNonWildcardSecure = true; } } Logger.warn("Configured " + (alias == null ? "" : (protocol + " on")) + ep.getAddress()); } } // We will need an HTTP protocol processor for the incoming connections String serverAgent = config.getString("server.serverAgent", "PokerFace/" + Utils.Version); HttpProcessor inhttpproc = new ImmutableHttpProcessor( new HttpResponseInterceptor[] { new ResponseDateInterceptor(), new ResponseServer(serverAgent), new ResponseContent(), new ResponseConnControl() }); HttpAsyncService serviceHandler = new HttpAsyncService(inhttpproc, new DefaultConnectionReuseStrategy(), null, handlerRegistry, null) { public void exception(final NHttpServerConnection conn, final Exception cause) { Logger.warn(cause.getMessage()); super.exception(conn, cause); } }; if (addrSSLContext.size() > 0) { final SSLContext defaultCtx = addrSSLContext.values().iterator().next(); final Map<SocketAddress, SSLContext> sslMap; if ((!hasNonWildcardSecure) || (addrSSLContext.size() == 1)) sslMap = null; else sslMap = addrSSLContext; listeningDispatcher = new DefaultHttpServerIODispatch(serviceHandler, new SSLNHttpServerConnectionFactory(defaultCtx, null, ConnectionConfig.DEFAULT) { protected SSLIOSession createSSLIOSession(IOSession iosession, SSLContext sslcontext, SSLSetupHandler sslHandler) { SSLIOSession retVal; SSLContext sktCtx = sslcontext; if (sslMap != null) { SocketAddress la = iosession.getLocalAddress(); if (la != null) { sktCtx = sslMap.get(la); if (sktCtx == null) sktCtx = sslcontext; } retVal = new SSLIOSession(iosession, SSLMode.SERVER, sktCtx, sslHandler); } else retVal = super.createSSLIOSession(iosession, sktCtx, sslHandler); if (sktCtx != null) retVal.setAttribute("com.bytelightning.opensource.pokerface.secure", true); return retVal; } }); } else listeningDispatcher = new DefaultHttpServerIODispatch(serviceHandler, ConnectionConfig.DEFAULT); } // Configure the httpclient reactor that will be used to do reverse proxing to the specified targets. lconf = config.configurationsAt("targets"); if ((lconf != null) && (lconf.size() > 0)) { HierarchicalConfiguration conf = lconf.get(0); Builder builder = IOReactorConfig.custom(); builder.setIoThreadCount(ComputeReactorProcessors(config.getDouble("targets[@cpu]", 0.667))); builder.setSoTimeout(conf.getInt("targets[@soTimeout]", 0)); builder.setSoLinger(config.getInt("targets[@soLinger]", -1)); builder.setConnectTimeout(conf.getInt("targets[@connectTimeout]", 0)); builder.setSoReuseAddress(true); builder.setTcpNoDelay(false); connectingReactor = new DefaultConnectingIOReactor(builder.build()); final int bufferSize = conf.getInt("targets[@bufferSize]", 1024) * 1024; byteBufferPool = new SoftReferenceObjectPool<ByteBuffer>(new BasePooledObjectFactory<ByteBuffer>() { @Override public ByteBuffer create() throws Exception { return ByteBuffer.allocateDirect(bufferSize); } @Override public PooledObject<ByteBuffer> wrap(ByteBuffer buffer) { return new DefaultPooledObject<ByteBuffer>(buffer); } }); KeyManager[] keyManagers = null; TrustManager[] trustManagers = null; if (keystore != null) { KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keystore, keypass); keyManagers = kmf.getKeyManagers(); } // Will the httpclient's trust any remote target, or only specific ones. if (conf.getBoolean("targets[@trustAny]", false)) trustManagers = new TrustManager[] { new X509TrustAllManager() }; else if (keystore != null) { TrustManagerFactory instance = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); instance.init(keystore); trustManagers = instance.getTrustManagers(); } SSLContext clientSSLContext = SSLContext.getInstance(conf.getString("targets[@protocol]", "TLS")); clientSSLContext.init(keyManagers, trustManagers, new SecureRandom()); // Setup an SSL capable connection pool for the httpclients. connPool = new BasicNIOConnPool(connectingReactor, new BasicNIOConnFactory(clientSSLContext, null, ConnectionConfig.DEFAULT), conf.getInt("targets[@connectTimeout]", 0)); connPool.setMaxTotal(conf.getInt("targets[@connMaxTotal]", 1023)); connPool.setDefaultMaxPerRoute(conf.getInt("targets[@connMaxPerRoute]", 1023)); // Set up HTTP protocol processor for outgoing connections String userAgent = conf.getString("targets.userAgent", "PokerFace/" + Utils.Version); HttpProcessor outhttpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { new RequestContent(), new RequestTargetHost(), new RequestConnControl(), new RequestUserAgent(userAgent), new RequestExpectContinue(true) }); executor = new HttpAsyncRequester(outhttpproc, new DefaultConnectionReuseStrategy()); // Now set up all the configured targets. mappings = new LinkedHashMap<String, TargetDescriptor>(); hosts = new ConcurrentHashMap<String, HttpHost>(); String[] scheme = { null }; String[] host = { null }; int[] port = { 0 }; String[] path = { null }; int[] stripPrefixCount = { 0 }; for (HierarchicalConfiguration targetConfig : conf.configurationsAt("target")) { String match = targetConfig.getString("[@pattern]"); if ((match == null) || (match.trim().length() < 1)) { Logger.error("Unable to configure target; Invalid url match pattern"); continue; } String key = RequestForTargetConsumer.UriToTargetKey(targetConfig.getString("[@url]"), scheme, host, port, path, stripPrefixCount); if (key == null) { Logger.error("Unable to configure target"); continue; } HttpHost targetHost = hosts.get(key); if (targetHost == null) { targetHost = new HttpHost(host[0], port[0], scheme[0]); hosts.put(key, targetHost); } TargetDescriptor desc = new TargetDescriptor(targetHost, path[0], stripPrefixCount[0]); mappings.put(match, desc); } connectionDispatcher = new DefaultHttpClientIODispatch(new HttpAsyncRequestExecutor(), ConnectionConfig.DEFAULT); } // Allocate the script map which will be populated by it's own executor thread. if (config.containsKey("scripts.rootDirectory")) { Path tmp = Utils.MakePath(config.getProperty("scripts.rootDirectory")); if (!Files.exists(tmp)) throw new FileNotFoundException("Scripts directory does not exist."); if (!Files.isDirectory(tmp)) throw new FileNotFoundException("'scripts' path is not a directory."); scripts = new ConcurrentSkipListMap<String, ScriptObjectMirror>(); boolean watch = config.getBoolean("scripts.dynamicWatch", false); List<Path> jsLibs; Object prop = config.getProperty("scripts.library"); if (prop != null) { jsLibs = new ArrayList<Path>(); if (prop instanceof Collection<?>) { @SuppressWarnings("unchecked") Collection<Object> oprop = (Collection<Object>) prop; for (Object obj : oprop) jsLibs.add(Utils.MakePath(obj)); } else { jsLibs.add(Utils.MakePath(prop)); } } else jsLibs = null; lconf = config.configurationsAt("scripts.scriptConfig"); if (lconf != null) { if (lconf.size() > 1) throw new ConfigurationException("Only one scriptConfig element is allowed."); if (lconf.size() == 0) lconf = null; } HierarchicalConfiguration scriptConfig; if (lconf == null) scriptConfig = new HierarchicalConfiguration(); else scriptConfig = lconf.get(0); scriptConfig.setProperty("pokerface.scripts.rootDirectory", tmp.toString()); configureScripts(jsLibs, scriptConfig, tmp, watch); if (watch) ScriptDirectoryWatcher = new DirectoryWatchService(); } // Configure the static file directory (if any) Path staticFilesPath = null; if (config.containsKey("files.rootDirectory")) { Path tmp = Utils.MakePath(config.getProperty("files.rootDirectory")); if (!Files.exists(tmp)) throw new FileNotFoundException("Files directory does not exist."); if (!Files.isDirectory(tmp)) throw new FileNotFoundException("'files' path is not a directory."); staticFilesPath = tmp; List<HierarchicalConfiguration> mimeEntries = config.configurationsAt("files.mime-entry"); if (mimeEntries != null) { for (HierarchicalConfiguration entry : mimeEntries) { entry.setDelimiterParsingDisabled(true); String type = entry.getString("[@type]", "").trim(); if (type.length() == 0) throw new ConfigurationException("Invalid mime type entry"); String extensions = entry.getString("[@extensions]", "").trim(); if (extensions.length() == 0) throw new ConfigurationException("Invalid mime extensions for: " + type); ScriptHelperImpl.AddMimeEntry(type, extensions); } } } handlerRegistry.register("/*", new RequestHandler(executor, connPool, byteBufferPool, staticFilesPath, mappings, scripts != null ? Collections.unmodifiableNavigableMap(scripts) : null, config.getBoolean("scripts.allowScriptsToSpecifyDynamicHosts", false) ? hosts : null)); }