src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceService.java
branchJDK-8200758-branch
changeset 57017 1b08af362a30
parent 56982 e094d5483bd6
child 57028 51cc1a1f91f3
equal deleted inserted replaced
57016:f63f13da91c0 57017:1b08af362a30
       
     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.jpackager.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 for Java Packager. To use these methods,
       
    41  * the option named "-singleton" must be specified on jpackager command line.
       
    42  *
       
    43  * @since 12
       
    44  */
       
    45 public class SingleInstanceService {
       
    46 
       
    47     static private boolean DEBUG = false;
       
    48     static private PrintStream DEBUG_STREAM = null;
       
    49 
       
    50     static private int currPort;
       
    51     static private String stringId = null;
       
    52     static private String randomNumberString = null;
       
    53 
       
    54     static private SingleInstanceImpl instance = null;
       
    55 
       
    56     static final int ENCODING_PLATFORM = 1;
       
    57     static final int ENCODING_UNICODE = 2;
       
    58 
       
    59     static final String ENCODING_PLATFORM_NAME = "UTF-8";
       
    60     static final String ENCODING_UNICODE_NAME = "UTF-16LE";
       
    61 
       
    62     static final String APP_ID_PREFIX = "jpackager.si.";
       
    63 
       
    64     private SingleInstanceService() {}
       
    65 
       
    66     static void enableDebug(boolean enable, PrintStream stream) {
       
    67         DEBUG = enable;
       
    68         DEBUG_STREAM = stream;
       
    69     }
       
    70 
       
    71     static void trace(String message) {
       
    72         if (DEBUG && DEBUG_STREAM != null) {
       
    73             DEBUG_STREAM.println(message);
       
    74         }
       
    75     }
       
    76 
       
    77     static void trace(Throwable t) {
       
    78         if (DEBUG && DEBUG_STREAM != null) {
       
    79             t.printStackTrace(DEBUG_STREAM);
       
    80         }
       
    81     }
       
    82 
       
    83     /**
       
    84      * Registers {@code SingleInstanceListener} for current process.
       
    85      * If the {@code SingleInstanceListener} object is already registered, or
       
    86      * {@code slistener} is {@code null}, then the registration is skipped.
       
    87      *
       
    88      * @param slistener the listener to handle the single instance behaviour.
       
    89      */
       
    90     public static void registerSingleInstance(
       
    91             SingleInstanceListener slistener) {
       
    92         registerSingleInstance(slistener, false);
       
    93     }
       
    94 
       
    95     /**
       
    96      * Registers {@code SingleInstanceListener} for current process.
       
    97      * If the {@code SingleInstanceListener} object is already registered, or
       
    98      * {@code slistener} is {@code null}, then the registration is skipped.
       
    99      *
       
   100      * @param slistener the listener to handle the single instance behaviour.
       
   101      * @param setFileHandler if {@code true}, the listener is notified when the
       
   102      *         application is asked to open a list of files. If OS is not MacOS,
       
   103      *         the parameter is ignored.
       
   104      */
       
   105     public static void registerSingleInstance(SingleInstanceListener slistener,
       
   106                                               boolean setFileHandler) {
       
   107         String appId = APP_ID_PREFIX + ProcessHandle.current().pid();
       
   108         registerSingleInstanceForId(slistener, appId, setFileHandler);
       
   109     }
       
   110 
       
   111     static void registerSingleInstanceForId(SingleInstanceListener slistener,
       
   112             String stringId, boolean setFileHandler) {
       
   113         // register SingleInstanceListener for given Id
       
   114         instance = new SingleInstanceImpl();
       
   115         instance.addSingleInstanceListener(slistener, stringId);
       
   116         if (setFileHandler) {
       
   117             instance.setOpenFileHandler();
       
   118         }
       
   119     }
       
   120 
       
   121     /**
       
   122      * Unregisters {@code SingleInstanceListener} for current process.
       
   123      * If the {@code SingleInstanceListener} object is not registered, or
       
   124      * {@code slistener} is {@code null}, then the unregistration is skipped.
       
   125      *
       
   126      * @param slistener the listener for unregistering.
       
   127      */
       
   128     public static void unregisterSingleInstance(
       
   129             SingleInstanceListener slistener) {
       
   130         instance.removeSingleInstanceListener(slistener);
       
   131     }
       
   132 
       
   133     /**
       
   134      * Returns true if single instance server is running for the id
       
   135      */
       
   136     static boolean isServerRunning(String id) {
       
   137         trace("isServerRunning ? : "+ id);
       
   138         File siDir = new File(SingleInstanceImpl.SI_FILEDIR);
       
   139         String[] fList = siDir.list();
       
   140         if (fList != null) {
       
   141             String prefix = SingleInstanceImpl.getSingleInstanceFilePrefix(id);
       
   142             for (String file : fList) {
       
   143                 trace("isServerRunning: " + file);
       
   144                 trace("\t String id: " + id);
       
   145                 trace("\t SingleInstanceFilePrefix: " + prefix);
       
   146                 // if file with the same prefix already exist, server is running
       
   147                 if (file.startsWith(prefix)) {
       
   148                     try {
       
   149                         currPort = Integer.parseInt(
       
   150                                     file.substring(file.lastIndexOf('_') + 1));
       
   151                         trace("isServerRunning: " + file
       
   152                                 + ": port: " + currPort);
       
   153                     } catch (NumberFormatException nfe) {
       
   154                         trace("isServerRunning: " + file
       
   155                                 + ": port parsing failed");
       
   156                         trace(nfe);
       
   157                         return false;
       
   158                     }
       
   159 
       
   160                     trace("Server running at port: " + currPort);
       
   161                     File siFile = new File(SingleInstanceImpl.SI_FILEDIR, file);
       
   162 
       
   163                     // get random number from single instance file
       
   164                     try (BufferedReader br = new BufferedReader(
       
   165                             new FileReader(siFile))) {
       
   166                         randomNumberString = br.readLine();
       
   167                         trace("isServerRunning: " + file + ": magic: "
       
   168                                 + randomNumberString);
       
   169                     } catch (IOException ioe ) {
       
   170                         trace("isServerRunning: " + file
       
   171                                 + ": reading magic failed");
       
   172                         trace(ioe);
       
   173                     }
       
   174                     trace("isServerRunning: " + file + ": setting id - OK");
       
   175                     stringId = id;
       
   176                     return true;
       
   177                 } else {
       
   178                     trace("isServerRunning: " + file + ": prefix NOK");
       
   179                 }
       
   180             }
       
   181         } else {
       
   182             trace("isServerRunning: empty file list");
       
   183         }
       
   184         trace("isServerRunning: false");
       
   185         return false;
       
   186     }
       
   187 
       
   188     /**
       
   189      * Returns true if we connect successfully to the server for the stringId
       
   190      */
       
   191     static boolean connectToServer(String[] args) {
       
   192         trace("Connect to: " + stringId + " " + currPort);
       
   193 
       
   194         if (randomNumberString == null) {
       
   195             // should not happen
       
   196             trace("MAGIC number is null, bail out.");
       
   197             return false;
       
   198         }
       
   199 
       
   200         // Now we open the tcpSocket and the stream
       
   201         Socket socket = null;
       
   202         OutputStream os = null;
       
   203         PrintStream out = null;
       
   204         InputStreamReader isr = null;
       
   205         BufferedReader br = null;
       
   206         try {
       
   207             socket = new Socket("127.0.0.1", currPort);
       
   208             os = socket.getOutputStream();
       
   209             byte[] encoding = new byte[1];
       
   210             encoding[0] = ENCODING_PLATFORM;
       
   211             os.write(encoding);
       
   212             String encodingName = Charset.defaultCharset().name();
       
   213 
       
   214             out = new PrintStream(os, true, encodingName);
       
   215             isr = new InputStreamReader(socket.getInputStream(), encodingName);
       
   216             br = new BufferedReader(isr);
       
   217 
       
   218             // send random number
       
   219             out.println(randomNumberString);
       
   220             // send MAGICWORD
       
   221             out.println(SingleInstanceImpl.SI_MAGICWORD);
       
   222 
       
   223             for (String arg : args) {
       
   224                 out.println(arg);
       
   225             }
       
   226 
       
   227             // indicate end of file transmission
       
   228             out.println(SingleInstanceImpl.SI_EOF);
       
   229             out.flush();
       
   230 
       
   231             // wait for ACK (OK) response
       
   232             trace("Waiting for ack");
       
   233             final int tries = 5;
       
   234 
       
   235             // try to listen for ACK
       
   236             for (int i=0; i < tries; i++) {
       
   237                 String str = br.readLine();
       
   238                 if (str != null && str.equals(SingleInstanceImpl.SI_ACK)) {
       
   239                     trace("Got ACK");
       
   240                     return true;
       
   241                 }
       
   242             }
       
   243         } catch (java.net.SocketException se) {
       
   244             // no server is running - continue launch
       
   245             trace("No server is running - continue launch.");
       
   246             trace(se);
       
   247         } catch (Exception ioe) {
       
   248             trace(ioe);
       
   249         }
       
   250         finally {
       
   251             try {
       
   252                 if (br != null) {
       
   253                     br.close();
       
   254                 }
       
   255                 if (isr != null) {
       
   256                     isr.close();
       
   257                 }
       
   258                 if (out != null) {
       
   259                     out.close();
       
   260                 }
       
   261                 if (os != null) {
       
   262                     os.close();
       
   263                 }
       
   264                 if (socket != null) {
       
   265                     socket.close();
       
   266                 }
       
   267             } catch (IOException ioe) {
       
   268                 trace(ioe);
       
   269             }
       
   270         }
       
   271         trace("No ACK from server, bail out.");
       
   272         return false;
       
   273     }
       
   274 }