Repository: incubator-reef Updated Branches: refs/heads/master ce017df22 -> 92e50d8f7
[REEF-808] Add documents and fix typos in HelloREEFHttp example This issue address the followings. * Add document for package `hellohttp` * Fix typo by renaming class `HttpServerShellCmdtHandler` to `HttpServerShellCmdHandler` * Fix documentation typos JIRA: [REEF-808](https://issues.apache.org/jira/browse/REEF-808) Pull Request: This closes #541 Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/92e50d8f Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/92e50d8f Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/92e50d8f Branch: refs/heads/master Commit: 92e50d8f7dbdacb1ad24d02eeaf9e2686cde48c5 Parents: ce017df Author: Dongjoon Hyun <[email protected]> Authored: Mon Oct 5 14:56:17 2015 +0900 Committer: Mariia Mykhailova <[email protected]> Committed: Mon Oct 5 12:11:59 2015 -0700 ---------------------------------------------------------------------- .../reef/examples/hellohttp/HelloREEFHttp.java | 9 +- .../hellohttp/HttpServerShellCmdHandler.java | 173 +++++++++++++++++++ .../hellohttp/HttpServerShellCmdtHandler.java | 173 ------------------- .../examples/hellohttp/HttpShellJobDriver.java | 10 +- .../reef/examples/hellohttp/package-info.java | 2 +- 5 files changed, 182 insertions(+), 185 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/92e50d8f/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HelloREEFHttp.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HelloREEFHttp.java b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HelloREEFHttp.java index 3804de6..b285fd5 100644 --- a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HelloREEFHttp.java +++ b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HelloREEFHttp.java @@ -32,10 +32,8 @@ import org.apache.reef.webserver.HttpHandlerConfiguration; import org.apache.reef.webserver.HttpServerReefEventHandler; import org.apache.reef.webserver.ReefEventStateManager; -import java.util.logging.Logger; - /** - * Example to run HelloREEF with a webserver. + * Distributed shell example based on REEF HTTP Server component. */ public final class HelloREEFHttp { /** @@ -47,7 +45,6 @@ public final class HelloREEFHttp { * Number of milliseconds to wait for the job to complete. */ public static final int JOB_TIMEOUT = 60 * 1000; // 60 sec. - private static final Logger LOG = Logger.getLogger(HelloREEFHttp.class.getName()); /** * @return the driver-side configuration to be merged into the DriverConfiguration to enable the HTTP server. @@ -55,7 +52,7 @@ public final class HelloREEFHttp { public static Configuration getHTTPConfiguration() { final Configuration httpHandlerConfiguration = HttpHandlerConfiguration.CONF .set(HttpHandlerConfiguration.HTTP_HANDLERS, HttpServerReefEventHandler.class) - .set(HttpHandlerConfiguration.HTTP_HANDLERS, HttpServerShellCmdtHandler.class) + .set(HttpHandlerConfiguration.HTTP_HANDLERS, HttpServerShellCmdHandler.class) .build(); final Configuration driverConfigurationForHttpServer = DriverServiceConfiguration.CONF .set(DriverServiceConfiguration.ON_EVALUATOR_ALLOCATED, @@ -74,7 +71,7 @@ public final class HelloREEFHttp { public static Configuration getDriverConfiguration() { return DriverConfiguration.CONF .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(HttpShellJobDriver.class)) - .set(DriverConfiguration.DRIVER_IDENTIFIER, "HelloHTTP") + .set(DriverConfiguration.DRIVER_IDENTIFIER, "HelloREEF") .set(DriverConfiguration.ON_DRIVER_STARTED, HttpShellJobDriver.StartHandler.class) .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, HttpShellJobDriver.AllocatedEvaluatorHandler.class) .set(DriverConfiguration.ON_EVALUATOR_FAILED, HttpShellJobDriver.FailedEvaluatorHandler.class) http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/92e50d8f/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdHandler.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdHandler.java b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdHandler.java new file mode 100644 index 0000000..f5c233d --- /dev/null +++ b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdHandler.java @@ -0,0 +1,173 @@ +/* + * 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.reef.examples.hellohttp; + +import org.apache.reef.tang.InjectionFuture; +import org.apache.reef.tang.annotations.Unit; +import org.apache.reef.util.CommandUtils; +import org.apache.reef.wake.EventHandler; +import org.apache.reef.webserver.HttpHandler; +import org.apache.reef.webserver.ParsedHttpRequest; + +import javax.inject.Inject; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Http Event handler for Shell Command. + */ +@Unit +class HttpServerShellCmdHandler implements HttpHandler { + /** + * Standard Java logger. + */ + private static final Logger LOG = Logger.getLogger(HttpServerShellCmdHandler.class.getName()); + + private static final int WAIT_TIMEOUT = 10 * 1000; + + private static final int WAIT_TIME = 50; + + /** + * ClientMessageHandler. + */ + private final InjectionFuture<HttpShellJobDriver.ClientMessageHandler> messageHandler; + + /** + * uri specification. + */ + private String uriSpecification = "Command"; + + /** + * output for command. + */ + private String cmdOutput = null; + + /** + * HttpServerShellEventHandler constructor. + */ + @Inject + public HttpServerShellCmdHandler(final InjectionFuture<HttpShellJobDriver.ClientMessageHandler> messageHandler) { + this.messageHandler = messageHandler; + } + + /** + * returns URI specification for the handler. + * + * @return + */ + @Override + public String getUriSpecification() { + return uriSpecification; + } + + /** + * set URI specification. + * + * @param s + */ + public void setUriSpecification(final String s) { + uriSpecification = s; + } + + /** + * it is called when receiving a http request. + * + * @param parsedHttpRequest + * @param response + */ + @Override + public final synchronized void onHttpRequest(final ParsedHttpRequest parsedHttpRequest, + final HttpServletResponse response) + throws IOException, ServletException { + LOG.log(Level.INFO, "HttpServeShellCmdHandler in webserver onHttpRequest is called: {0}", + parsedHttpRequest.getRequestUri()); + final Map<String, List<String>> queries = parsedHttpRequest.getQueryMap(); + final String queryStr = parsedHttpRequest.getQueryString(); + + if (parsedHttpRequest.getTargetEntity().equalsIgnoreCase("Evaluators")) { + final byte[] b = HttpShellJobDriver.CODEC.encode(queryStr); + LOG.log(Level.INFO, "HttpServeShellCmdHandler call HelloDriver onCommand(): {0}", queryStr); + messageHandler.get().onNext(b); + + notify(); + + final long endTime = System.currentTimeMillis() + WAIT_TIMEOUT; + while (cmdOutput == null) { + final long waitTime = endTime - System.currentTimeMillis(); + if (waitTime <= 0) { + break; + } + + try { + wait(WAIT_TIME); + } catch (final InterruptedException e) { + LOG.log(Level.WARNING, "HttpServeShellCmdHandler onHttpRequest InterruptedException: {0}", e); + } + } + if (cmdOutput != null) { + response.getOutputStream().write(cmdOutput.getBytes(StandardCharsets.UTF_8)); + cmdOutput = null; + } + } else if (parsedHttpRequest.getTargetEntity().equalsIgnoreCase("Driver")) { + final String commandOutput = CommandUtils.runCommand(queryStr); + response.getOutputStream().write(commandOutput.getBytes(StandardCharsets.UTF_8)); + } + } + + /** + * called after shell command is completed. + * + * @param message + */ + public final synchronized void onHttpCallback(final byte[] message) { + final long endTime = System.currentTimeMillis() + WAIT_TIMEOUT; + while (cmdOutput != null) { + final long waitTime = endTime - System.currentTimeMillis(); + if (waitTime <= 0) { + break; + } + + try { + wait(WAIT_TIME); + } catch (final InterruptedException e) { + LOG.log(Level.WARNING, "HttpServeShellCmdHandler onHttpCallback InterruptedException: {0}", e); + } + } + LOG.log(Level.INFO, "HttpServeShellCmdHandler OnCallback: {0}", HttpShellJobDriver.CODEC.decode(message)); + cmdOutput = HttpShellJobDriver.CODEC.decode(message); + + notify(); + } + + /** + * Handler for client to call back. + */ + final class ClientCallBackHandler implements EventHandler<byte[]> { + @Override + public void onNext(final byte[] message) { + HttpServerShellCmdHandler.this.onHttpCallback(message); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/92e50d8f/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdtHandler.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdtHandler.java b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdtHandler.java deleted file mode 100644 index 88ff154..0000000 --- a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpServerShellCmdtHandler.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * 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.reef.examples.hellohttp; - -import org.apache.reef.tang.InjectionFuture; -import org.apache.reef.tang.annotations.Unit; -import org.apache.reef.util.CommandUtils; -import org.apache.reef.wake.EventHandler; -import org.apache.reef.webserver.HttpHandler; -import org.apache.reef.webserver.ParsedHttpRequest; - -import javax.inject.Inject; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Http Event handler for Shell Command. - */ -@Unit -class HttpServerShellCmdtHandler implements HttpHandler { - /** - * Standard Java logger. - */ - private static final Logger LOG = Logger.getLogger(HttpServerShellCmdtHandler.class.getName()); - - private static final int WAIT_TIMEOUT = 10 * 1000; - - private static final int WAIT_TIME = 50; - - /** - * ClientMessageHandler. - */ - private final InjectionFuture<HttpShellJobDriver.ClientMessageHandler> messageHandler; - - /** - * uri specification. - */ - private String uriSpecification = "Command"; - - /** - * output for command. - */ - private String cmdOutput = null; - - /** - * HttpServerDistributedShellEventHandler constructor. - */ - @Inject - public HttpServerShellCmdtHandler(final InjectionFuture<HttpShellJobDriver.ClientMessageHandler> messageHandler) { - this.messageHandler = messageHandler; - } - - /** - * returns URI specification for the handler. - * - * @return - */ - @Override - public String getUriSpecification() { - return uriSpecification; - } - - /** - * set URI specification. - * - * @param s - */ - public void setUriSpecification(final String s) { - uriSpecification = s; - } - - /** - * it is called when receiving a http request. - * - * @param parsedHttpRequest - * @param response - */ - @Override - public final synchronized void onHttpRequest(final ParsedHttpRequest parsedHttpRequest, - final HttpServletResponse response) - throws IOException, ServletException { - LOG.log(Level.INFO, "HttpServeShellCmdtHandler in webserver onHttpRequest is called: {0}", - parsedHttpRequest.getRequestUri()); - final Map<String, List<String>> queries = parsedHttpRequest.getQueryMap(); - final String queryStr = parsedHttpRequest.getQueryString(); - - if (parsedHttpRequest.getTargetEntity().equalsIgnoreCase("Evaluators")) { - final byte[] b = HttpShellJobDriver.CODEC.encode(queryStr); - LOG.log(Level.INFO, "HttpServeShellCmdtHandler call HelloDriver onCommand(): {0}", queryStr); - messageHandler.get().onNext(b); - - notify(); - - final long endTime = System.currentTimeMillis() + WAIT_TIMEOUT; - while (cmdOutput == null) { - final long waitTime = endTime - System.currentTimeMillis(); - if (waitTime <= 0) { - break; - } - - try { - wait(WAIT_TIME); - } catch (final InterruptedException e) { - LOG.log(Level.WARNING, "HttpServeShellCmdtHandler onHttpRequest InterruptedException: {0}", e); - } - } - if (cmdOutput != null) { - response.getOutputStream().write(cmdOutput.getBytes(StandardCharsets.UTF_8)); - cmdOutput = null; - } - } else if (parsedHttpRequest.getTargetEntity().equalsIgnoreCase("Driver")) { - final String commandOutput = CommandUtils.runCommand(queryStr); - response.getOutputStream().write(commandOutput.getBytes(StandardCharsets.UTF_8)); - } - } - - /** - * called after shell command is completed. - * - * @param message - */ - public final synchronized void onHttpCallback(final byte[] message) { - final long endTime = System.currentTimeMillis() + WAIT_TIMEOUT; - while (cmdOutput != null) { - final long waitTime = endTime - System.currentTimeMillis(); - if (waitTime <= 0) { - break; - } - - try { - wait(WAIT_TIME); - } catch (final InterruptedException e) { - LOG.log(Level.WARNING, "HttpServeShellCmdtHandler onHttpCallback InterruptedException: {0}", e); - } - } - LOG.log(Level.INFO, "HttpServeShellCmdtHandler OnCallback: {0}", HttpShellJobDriver.CODEC.decode(message)); - cmdOutput = HttpShellJobDriver.CODEC.decode(message); - - notify(); - } - - /** - * Handler for client to call back. - */ - final class ClientCallBackHandler implements EventHandler<byte[]> { - @Override - public void onNext(final byte[] message) { - HttpServerShellCmdtHandler.this.onHttpCallback(message); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/92e50d8f/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpShellJobDriver.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpShellJobDriver.java b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpShellJobDriver.java index 55692ab..d91e4db 100644 --- a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpShellJobDriver.java +++ b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/HttpShellJobDriver.java @@ -53,19 +53,19 @@ import java.util.logging.Logger; @SuppressWarnings("checkstyle:hideutilityclassconstructor") @Unit public final class HttpShellJobDriver { + private static final Logger LOG = Logger.getLogger(HttpShellJobDriver.class.getName()); /** * String codec is used to encode the results * before passing them back to the client. */ public static final ObjectSerializableCodec<String> CODEC = new ObjectSerializableCodec<>(); - private static final Logger LOG = Logger.getLogger(HttpShellJobDriver.class.getName()); /** * Evaluator Requester. */ private final EvaluatorRequestor evaluatorRequestor; /** - * Number of Evalutors to request (default is 1). + * Number of Evaluators to request (default is 2). */ private final int numEvaluators = 2; /** @@ -92,7 +92,7 @@ public final class HttpShellJobDriver { /** * Callback handler for http return message. */ - private HttpServerShellCmdtHandler.ClientCallBackHandler httpCallbackHandler; + private HttpServerShellCmdHandler.ClientCallBackHandler httpCallbackHandler; /** * Job Driver Constructor. @@ -102,10 +102,10 @@ public final class HttpShellJobDriver { */ @Inject public HttpShellJobDriver(final EvaluatorRequestor requestor, - final HttpServerShellCmdtHandler.ClientCallBackHandler clientCallBackHandler) { + final HttpServerShellCmdHandler.ClientCallBackHandler clientCallBackHandler) { this.evaluatorRequestor = requestor; this.httpCallbackHandler = clientCallBackHandler; - LOG.log(Level.FINE, "Instantiated 'HelloDriver'"); + LOG.log(Level.FINE, "Instantiated 'HttpShellJobDriver'"); } /** http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/92e50d8f/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/package-info.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/package-info.java b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/package-info.java index 6917d8e..d4db8d3 100644 --- a/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/package-info.java +++ b/lang/java/reef-examples/src/main/java/org/apache/reef/examples/hellohttp/package-info.java @@ -17,6 +17,6 @@ * under the License. */ /** - * TODO: Document. + * Distributed shell example based on REEF HTTP Server component. */ package org.apache.reef.examples.hellohttp;
