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