1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.salver.handler;
  24 
  25 import static java.nio.file.StandardOpenOption.CREATE;
  26 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  27 import static java.nio.file.StandardOpenOption.WRITE;
  28 
  29 import java.io.IOException;
  30 import java.net.SocketAddress;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.channels.SocketChannel;
  33 import java.nio.channels.WritableByteChannel;
  34 import java.nio.file.OpenOption;
  35 import java.nio.file.Path;
  36 
  37 import org.graalvm.compiler.salver.dumper.Dumper;
  38 import org.graalvm.compiler.salver.writer.ChannelDumpWriter;
  39 import org.graalvm.compiler.salver.writer.DumpWriter;
  40 
  41 public abstract class AbstractDumpHandler<D extends Dumper> implements DumpHandler {
  42 
  43     protected String label;
  44 
  45     protected DumpWriter writer;
  46     protected D dumper;
  47 
  48     public AbstractDumpHandler() {
  49         setLabel(getClass().getSimpleName() + ":" + Thread.currentThread().getName());
  50     }
  51 
  52     public String getLabel() {
  53         return label;
  54     }
  55 
  56     protected void setLabel(String label) {
  57         this.label = label;
  58     }
  59 
  60     public DumpWriter getWriter() {
  61         return writer;
  62     }
  63 
  64     protected void setWriter(DumpWriter writer) {
  65         this.writer = writer;
  66     }
  67 
  68     protected void setWriter(WritableByteChannel channel) {
  69         setWriter(new ChannelDumpWriter(channel));
  70     }
  71 
  72     protected void setWriter(SocketAddress remote) throws IOException {
  73         setWriter(SocketChannel.open(remote));
  74     }
  75 
  76     protected void setWriter(Path path) throws IOException {
  77         setWriter(path, WRITE, TRUNCATE_EXISTING, CREATE);
  78     }
  79 
  80     protected void setWriter(Path path, OpenOption... options) throws IOException {
  81         setWriter(FileChannel.open(path, options));
  82     }
  83 
  84     public D getDumper() {
  85         return dumper;
  86     }
  87 
  88     protected void setDumper(D dumper) {
  89         this.dumper = dumper;
  90     }
  91 
  92     @Override
  93     public void close() throws IOException {
  94         if (dumper != null) {
  95             try {
  96                 dumper.close();
  97             } finally {
  98                 dumper = null;
  99             }
 100         }
 101         if (writer != null) {
 102             try {
 103                 writer.close();
 104             } finally {
 105                 writer = null;
 106             }
 107         }
 108     }
 109 }