src/jdk.jpackage.runtime/share/classes/jdk/jpackage/runtime/singleton/SingleInstanceService.java
branchJDK-8200758-branch
changeset 57099 9a85a7a076ad
parent 57098 fd4868c5fca1
child 57100 489b1afd0ec8
equal deleted inserted replaced
57098:fd4868c5fca1 57099:9a85a7a076ad
     1 /*
       
     2  * Copyright (c) 2017, 2018, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jpackage.runtime.singleton;
       
    27 
       
    28 import java.io.BufferedReader;
       
    29 import java.io.File;
       
    30 import java.io.FileReader;
       
    31 import java.io.IOException;
       
    32 import java.io.InputStreamReader;
       
    33 import java.io.OutputStream;
       
    34 import java.io.PrintStream;
       
    35 import java.net.Socket;
       
    36 import java.nio.charset.Charset;
       
    37 
       
    38 /**
       
    39  * The {@code SingleInstanceService} class provides public methods for using
       
    40  * Single Instance functionality in applications packaged by jpackage.
       
    41  * To use these methods, the option named "--singleton" must
       
    42  * be specified on the jpackage command line.
       
    43  *
       
    44  * @since 13
       
    45  */
       
    46 public class SingleInstanceService {
       
    47 
       
    48     static private boolean DEBUG = false;
       
    49     static private PrintStream DEBUG_STREAM = null;
       
    50 
       
    51     static private int currPort;
       
    52     static private String stringId = null;
       
    53     static private String randomNumberString = null;
       
    54 
       
    55     static private SingleInstanceImpl instance = null;
       
    56 
       
    57     static final int ENCODING_PLATFORM = 1;
       
    58     static final int ENCODING_UNICODE = 2;
       
    59 
       
    60     static final String ENCODING_PLATFORM_NAME = "UTF-8";
       
    61     static final String ENCODING_UNICODE_NAME = "UTF-16LE";
       
    62 
       
    63     static final String APP_ID_PREFIX = "jpackage.si.";
       
    64 
       
    65     private SingleInstanceService() {}
       
    66 
       
    67     static void enableDebug(boolean enable, PrintStream stream) {
       
    68         DEBUG = enable;
       
    69         DEBUG_STREAM = stream;
       
    70     }
       
    71 
       
    72     static void trace(String message) {
       
    73         if (DEBUG && DEBUG_STREAM != null) {
       
    74             DEBUG_STREAM.println(message);
       
    75         }
       
    76     }
       
    77 
       
    78     static void trace(Throwable t) {
       
    79         if (DEBUG && DEBUG_STREAM != null) {
       
    80             t.printStackTrace(DEBUG_STREAM);
       
    81         }
       
    82     }
       
    83 
       
    84     /**
       
    85      * Adds the given slistener for the current process.
       
    86      *
       
    87      * If the given slistener object is null or
       
    88      * has already been added, then nothing is added.
       
    89      *
       
    90      * @param slistener the {@code SingleInstanceListener} to add
       
    91      */
       
    92     public static void addSingleInstanceListener(
       
    93             SingleInstanceListener slistener) {
       
    94         if (instance == null) {
       
    95             synchronized(SingleInstanceService.class) {
       
    96                 if (instance == null) {
       
    97                     instance = new SingleInstanceImpl();
       
    98                 }
       
    99             }
       
   100         }
       
   101         instance.addSingleInstanceListener(slistener,
       
   102                 APP_ID_PREFIX + ProcessHandle.current().pid());
       
   103     }
       
   104 
       
   105     /**
       
   106      * removes the given slistener for the current process.
       
   107      *
       
   108      * If the given slistener object is null or
       
   109      * has not already been added, nothing is removed.
       
   110      *
       
   111      * @param slistener the {@code SingleInstanceListener} to remove.
       
   112      */
       
   113     public static void removeSingleInstanceListener(
       
   114             SingleInstanceListener slistener) {
       
   115         if (instance != null) {
       
   116             instance.removeSingleInstanceListener(slistener);
       
   117         }
       
   118     }
       
   119 
       
   120     /**
       
   121      * Returns true if single instance server is running for the id
       
   122      */
       
   123     static boolean isServerRunning(String id) {
       
   124         trace("isServerRunning ? : "+ id);
       
   125         File siDir = new File(SingleInstanceImpl.SI_FILEDIR);
       
   126         String[] fList = siDir.list();
       
   127         if (fList != null) {
       
   128             String prefix = SingleInstanceImpl.getSingleInstanceFilePrefix(id);
       
   129             for (String file : fList) {
       
   130                 trace("isServerRunning: " + file);
       
   131                 trace("\t String id: " + id);
       
   132                 trace("\t SingleInstanceFilePrefix: " + prefix);
       
   133                 // if file with the same prefix already exist, server is running
       
   134                 if (file.startsWith(prefix)) {
       
   135                     try {
       
   136                         currPort = Integer.parseInt(
       
   137                                     file.substring(file.lastIndexOf('_') + 1));
       
   138                         trace("isServerRunning: " + file
       
   139                                 + ": port: " + currPort);
       
   140                     } catch (NumberFormatException nfe) {
       
   141                         trace("isServerRunning: " + file
       
   142                                 + ": port parsing failed");
       
   143                         trace(nfe);
       
   144                         return false;
       
   145                     }
       
   146 
       
   147                     trace("Server running at port: " + currPort);
       
   148                     File siFile = new File(SingleInstanceImpl.SI_FILEDIR, file);
       
   149 
       
   150                     // get random number from single instance file
       
   151                     try (BufferedReader br = new BufferedReader(
       
   152                             new FileReader(siFile))) {
       
   153                         randomNumberString = br.readLine();
       
   154                         trace("isServerRunning: " + file + ": magic: "
       
   155                                 + randomNumberString);
       
   156                     } catch (IOException ioe ) {
       
   157                         trace("isServerRunning: " + file
       
   158                                 + ": reading magic failed");
       
   159                         trace(ioe);
       
   160                     }
       
   161                     trace("isServerRunning: " + file + ": setting id - OK");
       
   162                     stringId = id;
       
   163                     return true;
       
   164                 } else {
       
   165                     trace("isServerRunning: " + file + ": prefix NOK");
       
   166                 }
       
   167             }
       
   168         } else {
       
   169             trace("isServerRunning: empty file list");
       
   170         }
       
   171         trace("isServerRunning: false");
       
   172         return false;
       
   173     }
       
   174 
       
   175     /**
       
   176      * Returns true if we connect successfully to the server for the stringId
       
   177      */
       
   178     static boolean connectToServer(String[] args) {
       
   179         trace("Connect to: " + stringId + " " + currPort);
       
   180 
       
   181         if (randomNumberString == null) {
       
   182             // should not happen
       
   183             trace("MAGIC number is null, bail out.");
       
   184             return false;
       
   185         }
       
   186 
       
   187         // Now we open the tcpSocket and the stream
       
   188         Socket socket = null;
       
   189         OutputStream os = null;
       
   190         PrintStream out = null;
       
   191         InputStreamReader isr = null;
       
   192         BufferedReader br = null;
       
   193         try {
       
   194             socket = new Socket("127.0.0.1", currPort);
       
   195             os = socket.getOutputStream();
       
   196             byte[] encoding = new byte[1];
       
   197             encoding[0] = ENCODING_PLATFORM;
       
   198             os.write(encoding);
       
   199             String encodingName = Charset.defaultCharset().name();
       
   200 
       
   201             out = new PrintStream(os, true, encodingName);
       
   202             isr = new InputStreamReader(socket.getInputStream(), encodingName);
       
   203             br = new BufferedReader(isr);
       
   204 
       
   205             // send random number
       
   206             out.println(randomNumberString);
       
   207             // send MAGICWORD
       
   208             out.println(SingleInstanceImpl.SI_MAGICWORD);
       
   209 
       
   210             for (String arg : args) {
       
   211                 out.println(arg);
       
   212             }
       
   213 
       
   214             // indicate end of file transmission
       
   215             out.println(SingleInstanceImpl.SI_EOF);
       
   216             out.flush();
       
   217 
       
   218             // wait for ACK (OK) response
       
   219             trace("Waiting for ack");
       
   220             final int tries = 5;
       
   221 
       
   222             // try to listen for ACK
       
   223             for (int i=0; i < tries; i++) {
       
   224                 String str = br.readLine();
       
   225                 if (str != null && str.equals(SingleInstanceImpl.SI_ACK)) {
       
   226                     trace("Got ACK");
       
   227                     return true;
       
   228                 }
       
   229             }
       
   230         } catch (java.net.SocketException se) {
       
   231             // no server is running - continue launch
       
   232             trace("No server is running - continue launch.");
       
   233             trace(se);
       
   234         } catch (Exception ioe) {
       
   235             trace(ioe);
       
   236         }
       
   237         finally {
       
   238             try {
       
   239                 if (br != null) {
       
   240                     br.close();
       
   241                 }
       
   242                 if (isr != null) {
       
   243                     isr.close();
       
   244                 }
       
   245                 if (out != null) {
       
   246                     out.close();
       
   247                 }
       
   248                 if (os != null) {
       
   249                     os.close();
       
   250                 }
       
   251                 if (socket != null) {
       
   252                     socket.close();
       
   253                 }
       
   254             } catch (IOException ioe) {
       
   255                 trace(ioe);
       
   256             }
       
   257         }
       
   258         trace("No ACK from server, bail out.");
       
   259         return false;
       
   260     }
       
   261 }