/** * Initialize this JolokiaServer with the given HttpServer. The calle is responsible for managing * (starting/stopping) the HttpServer. * * @param pServer server to use * @param pConfig configuration * @param pLazy whether the initialization should be done lazy or not */ protected final void init(HttpServer pServer, JolokiaServerConfig pConfig, boolean pLazy) { config = pConfig; lazy = pLazy; // Create proper context along with handler final String contextPath = pConfig.getContextPath(); jolokiaHttpHandler = new JolokiaHttpHandler(pConfig.getJolokiaConfig()); HttpContext context = pServer.createContext(contextPath, jolokiaHttpHandler); // Add authentication if configured final Authenticator authenticator = pConfig.getAuthenticator(); if (authenticator != null) { context.setAuthenticator(authenticator); } url = detectAgentUrl(pServer, pConfig, contextPath); }
private static HttpServer createHttpServer(ExecutorService execs) throws Exception { InetSocketAddress inetAddress = new InetSocketAddress(0); HttpServer testServer = HttpServer.create(inetAddress, 5); testServer.setExecutor(execs); HttpContext context = testServer.createContext("/test"); context.setHandler( new HttpHandler() { public void handle(HttpExchange msg) { try { synchronized (lock) { ++s_received; if ((s_received % 1000) == 0) { System.out.println("Received=" + s_received); } } String method = msg.getRequestMethod(); if (method.equals("POST")) { InputStream is = msg.getRequestBody(); byte[] buf = readFully(is); is.close(); writePostReply(msg, buf); } else { System.out.println("****** METHOD not handled ***** " + method); System.out.println("Received=" + s_received); } synchronized (lock) { ++sent; if ((sent % 1000) == 0) { System.out.println("sent=" + sent); } } } catch (Exception e) { e.printStackTrace(); } finally { msg.close(); } } }); return testServer; }