FastCGIHandlerFactory { public static FastCGIHandler create(final Map<String, String> config) { final FastCGIHandler handler = new FastCGIHandler(); if (config.get(PARAM_SERVER_ADDRESS) != null) { getLog().info( "configuring fastcgi servlet using default single connection handler"); handler.setConnectionFactory(new SingleConnectionFactory(config .get(PARAM_SERVER_ADDRESS))); } else if (config.get(PARAM_CONNECTION_FACTORY) != null) { final String className = config.get(PARAM_CONNECTION_FACTORY) .trim(); getLog().info( "configuring fastCGI handler using custom class '" + className + "'"); handler.setConnectionFactory(buildConnectionFactoryForClass(className)); } else if (config.get(PARAM_CLUSTER_ADRESSES) != null) { getLog().info( "configuring fastCGI handler using the following adresses : "); final PoolFactory poolFactory = PoolFactoryFactory .getOrCreatePoolFactory(); for (final String addr : config.get(PARAM_CLUSTER_ADRESSES) .replaceAll("[\n\t]", ";").replaceAll(" ", "").split(";")) { if (!addr.isEmpty()) { getLog().info(" => " + addr); poolFactory.addAddress(addr.trim()); } } handler.setConnectionFactory(new PooledConnectionFactory( poolFactory)); } else { throw new IllegalArgumentException( "Cannot create fcgi handler : did you provide any configuration ?"); } if (config.get(PARAM_ALLOWED_HEADERS) != null) { final String allowedHeaders = config.get(PARAM_ALLOWED_HEADERS); if (getLog().isInfoEnabled()) { getLog().info( "The following http headers will be transmitted (regex): [" + allowedHeaders + "]"); } Pattern allowedHeadersPattern; try { allowedHeadersPattern = Pattern.compile(allowedHeaders); } catch (PatternSyntaxException pse) { throw new IllegalArgumentException( "Invalid regular expression in configuration parameter " + PARAM_ALLOWED_HEADERS, pse); } handler.setAllowedHeaders(allowedHeadersPattern); } if (config.get(PARAM_PHP_SCRIPT_FROM_PATH) != null) { final String phpScriptFromPathPattern = config.get(PARAM_PHP_SCRIPT_FROM_PATH); Pattern phpScriptFromPath; try { phpScriptFromPath = Pattern.compile(phpScriptFromPathPattern); } catch (PatternSyntaxException pse) { throw new IllegalArgumentException( "Invalid regular expression in configuration parameter " + PARAM_PHP_SCRIPT_FROM_PATH, pse); } handler.setPhpScriptFromPathPattern(phpScriptFromPath); } if (config.get(PARAM_KEEP_ALIVE) != null) { final String value = config.get(PARAM_KEEP_ALIVE); if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) { handler.setKeepAlive(true); } } if (config.get(PARAM_START_EXECUTABLE) != null) { final String cmd = config.get(PARAM_START_EXECUTABLE); try { handler.startProcess(cmd); } catch (final IOException e) { getLog().error("while starting external process", e); throw new RuntimeException(e); } } return handler; }  static FastCGIHandler create(final Map<String, String> config); final static String PARAM_SERVER_ADDRESS; final static String PARAM_START_EXECUTABLE; final static String PARAM_CONNECTION_FACTORY; final static String PARAM_CLUSTER_ADRESSES; final static String PARAM_KEEP_ALIVE; final static String PARAM_ALLOWED_HEADERS; final static String PARAM_PHP_SCRIPT_FROM_PATH; final static String[] PARAM_NAMES; }
@Test public void testCreate_simpleClusteredAddresses() throws Exception { final Map<String, String> data = new HashMap<String, String>(); data.put(FastCGIHandlerFactory.PARAM_CLUSTER_ADRESSES, "127.0.0.1:9003"); FastCGIHandlerFactory.create(data); verify(poolFactory).addAddress("127.0.0.1:9003"); }
@Test public void testCreate_twoClusteredAddresses() throws Exception { final Map<String, String> data = new HashMap<String, String>(); data.put(FastCGIHandlerFactory.PARAM_CLUSTER_ADRESSES, "127.0.0.1:9000;127.0.0.1:9001"); FastCGIHandlerFactory.create(data); verify(poolFactory).addAddress("127.0.0.1:9000"); verify(poolFactory).addAddress("127.0.0.1:9001"); }
@Test public void testCreate_clusteredAddressesMultiLine() throws Exception { final Map<String, String> data = new HashMap<String, String>(); data.put( FastCGIHandlerFactory.PARAM_CLUSTER_ADRESSES, " 127.0.0.1:9000;127.0.0.1:9001\n\t127.0.0.1:9002;\n 127.0.0.1:9003;127.0.0.1:9004\n[::1]:9005"); FastCGIHandlerFactory.create(data); verify(poolFactory).addAddress("127.0.0.1:9000"); verify(poolFactory).addAddress("127.0.0.1:9001"); verify(poolFactory).addAddress("127.0.0.1:9002"); verify(poolFactory).addAddress("127.0.0.1:9003"); verify(poolFactory).addAddress("127.0.0.1:9004"); verify(poolFactory).addAddress("[::1]:9005"); }
@Test public void testCreate_clusteredAddressesJustMultiLine() throws Exception { final Map<String, String> data = new HashMap<String, String>(); data.put( FastCGIHandlerFactory.PARAM_CLUSTER_ADRESSES, " 127.0.0.1:9000\n127.0.0.1:9001\n127.0.0.1:9002\n127.0.0.1:9003\n127.0.0.1:9004\n[::1]:9005"); FastCGIHandlerFactory.create(data); verify(poolFactory).addAddress("127.0.0.1:9000"); verify(poolFactory).addAddress("127.0.0.1:9001"); verify(poolFactory).addAddress("127.0.0.1:9002"); verify(poolFactory).addAddress("127.0.0.1:9003"); verify(poolFactory).addAddress("127.0.0.1:9004"); verify(poolFactory).addAddress("[::1]:9005"); }
@Test public void testCreate_parseKeepAlive() throws Exception { final Map<String, String> data = new HashMap<String, String>(); data.put(FastCGIHandlerFactory.PARAM_CLUSTER_ADRESSES, "127.0.0.1:9003"); final FastCGIHandler handlerWithoutKeepAlive = FastCGIHandlerFactory .create(data); verify(poolFactory).addAddress("127.0.0.1:9003"); assertThat(handlerWithoutKeepAlive).isNotNull(); assertThat(handlerWithoutKeepAlive.isKeepAlive()).isFalse(); data.put(FastCGIHandlerFactory.PARAM_KEEP_ALIVE, "true"); final FastCGIHandler handlerWithKeepAlive = FastCGIHandlerFactory .create(data); assertThat(handlerWithKeepAlive).isNotNull(); assertThat(handlerWithKeepAlive.isKeepAlive()).isTrue(); }