package com.perforce.workspace.tjuricek.p4oauth.storage; import com.perforce.p4java.PropertyDefs; import com.perforce.p4java.server.IOptionsServer; import com.perforce.p4java.server.IServerAddress; import com.perforce.p4java.server.ServerFactory; import com.perforce.workspace.tjuricek.p4oauth.config.P4OAuthConfig; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.DefaultPooledObject; import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import java.util.Properties; import java.util.function.Supplier; /** * Uses a local object pool of threads to reuse IOptionsServer instances. */ public class IOptionsServerSupplier implements Supplier<IOptionsServer> { private P4OAuthConfig config; private GenericObjectPool<IOptionsServer> serverPool; public IOptionsServerSupplier(P4OAuthConfig config) { this.config = config; OptionsServerFactory factory = new OptionsServerFactory(config); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMinIdle(0); serverPool = new GenericObjectPool<>(factory, poolConfig); } @Override public IOptionsServer get() { try { return serverPool.borrowObject(); } catch (Exception e) { throw new IllegalStateException(e); } } public void release(IOptionsServer optionsServer) { serverPool.returnObject(optionsServer); } static class OptionsServerFactory implements PooledObjectFactory<IOptionsServer> { private P4OAuthConfig config; OptionsServerFactory(P4OAuthConfig config) { this.config = config; } public String getServerLocation() { String protocol = IServerAddress.Protocol.P4JRPCNTS.toString(); if (config.getPerforce().isSsl()) { protocol = IServerAddress.Protocol.P4JRPCNTSSSL.toString(); } String url = String.format("%s://%s:%d", protocol, config.getPerforce().getHostname(), config.getPerforce().getPort()); return url; } public Properties getServerProperties() { Properties properties = new Properties(); properties.put("relaxCmdNameChecks", "true"); properties.put(PropertyDefs.PROG_NAME_KEY, "p4oauth"); properties.put(PropertyDefs.PROG_VERSION_KEY, "0.1"); properties.put(PropertyDefs.PROG_NAME_KEY_SHORTFORM, "p4oauth"); properties.put(PropertyDefs.PROG_VERSION_KEY_SHORTFORM, "0.1"); properties.put("applicationName", "p4oauth"); return properties; } @Override public PooledObject<IOptionsServer> makeObject() throws Exception { IOptionsServer optionsServer = ServerFactory.getOptionsServer(getServerLocation(), getServerProperties()); return new DefaultPooledObject<>(optionsServer); } @Override public void destroyObject(PooledObject<IOptionsServer> p) throws Exception { p.getObject().disconnect(); } @Override public boolean validateObject(PooledObject<IOptionsServer> p) { return p.getObject().isConnected(); } @Override public void activateObject(PooledObject<IOptionsServer> p) throws Exception { if (!p.getObject().isConnected()) { p.getObject().connect(); } } @Override public void passivateObject(PooledObject<IOptionsServer> p) throws Exception { } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9095 | tjuricek | Added some basic test data and renamed "workspace" to "workshop" in package name | ||
#1 | 9089 | tjuricek |
Moving some code that worked via some manual validation to the workshop. This just implements a basic code authorization grant scheme. Automated tests are forthcoming, awating some gradle plugin work that should sit outside of this project. |