Search in sources :

Example 51 with PrintWriter

use of java.io.PrintWriter in project bazel by bazelbuild.

the class RemoteWorker method main.

public static void main(String[] args) throws Exception {
    OptionsParser parser = OptionsParser.newOptionsParser(RemoteOptions.class, RemoteWorkerOptions.class);
    parser.parseAndExitUponError(args);
    RemoteOptions remoteOptions = parser.getOptions(RemoteOptions.class);
    RemoteWorkerOptions remoteWorkerOptions = parser.getOptions(RemoteWorkerOptions.class);
    if (remoteWorkerOptions.workPath == null) {
        printUsage(parser);
        return;
    }
    System.out.println("*** Initializing in-memory cache server.");
    ConcurrentMap<String, byte[]> cache = ConcurrentMapFactory.isRemoteCacheOptions(remoteOptions) ? ConcurrentMapFactory.create(remoteOptions) : new ConcurrentHashMap<String, byte[]>();
    System.out.println("*** Starting grpc server on all locally bound IPs on port " + remoteWorkerOptions.listenPort + ".");
    Path workPath = getFileSystem().getPath(remoteWorkerOptions.workPath);
    FileSystemUtils.createDirectoryAndParents(workPath);
    RemoteWorker worker = new RemoteWorker(workPath, remoteWorkerOptions, new ConcurrentMapActionCache(cache));
    final Server server = ServerBuilder.forPort(remoteWorkerOptions.listenPort).addService(worker.getCasServer()).addService(worker.getExecutionServer()).addService(worker.getExecCacheServer()).build();
    server.start();
    final Path pidFile;
    if (remoteWorkerOptions.pidFile != null) {
        pidFile = getFileSystem().getPath(remoteWorkerOptions.pidFile);
        PrintWriter writer = new PrintWriter(pidFile.getOutputStream());
        writer.append(Integer.toString(ProcessUtils.getpid()));
        writer.append("\n");
        writer.close();
    } else {
        pidFile = null;
    }
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            System.err.println("*** Shutting down grpc server.");
            server.shutdown();
            if (pidFile != null) {
                try {
                    pidFile.delete();
                } catch (IOException e) {
                    System.err.println("Cannot remove pid file: " + pidFile.toString());
                }
            }
            System.err.println("*** Server shut down.");
        }
    });
    server.awaitTermination();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Server(io.grpc.Server) ConcurrentMapActionCache(com.google.devtools.build.lib.remote.ConcurrentMapActionCache) RemoteOptions(com.google.devtools.build.lib.remote.RemoteOptions) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) OptionsParser(com.google.devtools.common.options.OptionsParser) PrintWriter(java.io.PrintWriter)

Example 52 with PrintWriter

use of java.io.PrintWriter in project solo by b3log.

the class ArticleProcessorTestCase method getRandomArticles.

/**
     * getRandomArticles.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void getRandomArticles() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/get-random-articles.do");
    when(request.getMethod()).thenReturn("POST");
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    dispatcherServlet.service(request, response);
    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.contains(content, "{\"randomArticles"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 53 with PrintWriter

use of java.io.PrintWriter in project solo by b3log.

the class ArticleProcessorTestCase method getTagArticlesByPage.

/**
     * getTagArticlesByPage.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void getTagArticlesByPage() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/articles/tags/Solo/1");
    when(request.getMethod()).thenReturn("GET");
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    dispatcherServlet.service(request, response);
    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.contains(content, "{\"sc\":true"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 54 with PrintWriter

use of java.io.PrintWriter in project solo by b3log.

the class ArticleProcessorTestCase method showArticle.

/**
     * showArticle.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void showArticle() throws Exception {
    final JSONObject article = getArticleRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/pagepermalink");
    when(request.getMethod()).thenReturn("GET");
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
    when(request.getAttribute(Article.ARTICLE)).thenReturn(article);
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    final HTTPRequestContext httpRequestContext = new HTTPRequestContext();
    httpRequestContext.setRequest(request);
    httpRequestContext.setResponse(response);
    final ArticleProcessor articleProcessor = Lifecycle.getBeanManager().getReference(ArticleProcessor.class);
    articleProcessor.showArticle(httpRequestContext, request, response);
    final Map<String, Object> dataModel = httpRequestContext.getRenderer().getRenderDataModel();
    final JSONObject handledArticle = (JSONObject) dataModel.get(Article.ARTICLE);
    Assert.assertTrue(StringUtils.contains(handledArticle.optString(Keys.OBJECT_ID), article.optString(Keys.OBJECT_ID)));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) StringWriter(java.io.StringWriter) HTTPRequestContext(org.b3log.latke.servlet.HTTPRequestContext) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) JSONObject(org.json.JSONObject) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 55 with PrintWriter

use of java.io.PrintWriter in project solo by b3log.

the class ArticleProcessorTestCase method showArticlePwdForm.

/**
     * showArticlePwdForm.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void showArticlePwdForm() throws Exception {
    final JSONObject article = getArticleRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    final String articleId = article.optString(Keys.OBJECT_ID);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/console/article-pwd");
    when(request.getMethod()).thenReturn("GET");
    when(request.getParameter("articleId")).thenReturn(articleId);
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    dispatcherServlet.service(request, response);
    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.contains(content, "<title>访问密码</title>"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) StringWriter(java.io.StringWriter) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Aggregations

PrintWriter (java.io.PrintWriter)4039 StringWriter (java.io.StringWriter)1201 IOException (java.io.IOException)788 File (java.io.File)643 Test (org.junit.Test)512 FileWriter (java.io.FileWriter)318 FileOutputStream (java.io.FileOutputStream)313 OutputStreamWriter (java.io.OutputStreamWriter)278 BufferedReader (java.io.BufferedReader)202 ArrayList (java.util.ArrayList)196 ByteArrayOutputStream (java.io.ByteArrayOutputStream)162 HttpServletResponse (javax.servlet.http.HttpServletResponse)145 InputStreamReader (java.io.InputStreamReader)140 Date (java.util.Date)131 HashMap (java.util.HashMap)130 ServletException (javax.servlet.ServletException)126 BufferedWriter (java.io.BufferedWriter)125 HttpServletRequest (javax.servlet.http.HttpServletRequest)125 FastPrintWriter (com.android.internal.util.FastPrintWriter)124 Map (java.util.Map)118