/carrot-jdk6-jnlp-unix/src/common/share/classes/com/sun/deploy/si/SingleInstanceManager.java

https://github.com/carrot-garden/carrot-jnlper · Java · 189 lines · 136 code · 24 blank · 29 comment · 33 complexity · 6947fdc481c2dd5443e0ab6bdac70c5a MD5 · raw file

  1. /*
  2. * @(#)SingleInstanceManager.java 1.18 10/03/24
  3. *
  4. * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
  5. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.deploy.si;
  8. import java.net.*;
  9. import java.io.*;
  10. import java.util.StringTokenizer;
  11. import java.lang.NumberFormatException;
  12. import com.sun.deploy.util.Trace;
  13. import com.sun.deploy.util.TraceLevel;
  14. import com.sun.deploy.config.Config;
  15. // This is the Java Implementation of the Single Instance Service.
  16. // This code will only be used if someone tries to use Single Instance
  17. // Service by invoking "javaws http://jnlp.url" - one example of it
  18. // is invoking from the Java Web Start application manager.
  19. public class SingleInstanceManager {
  20. static private final boolean DEBUG = false;
  21. static private String _idString;
  22. static private int _currPort;
  23. static private String _randomNumberString = null;
  24. static private String _openPrintFilePath = null;
  25. static private String _actionName = null;
  26. static public void setActionName(String s) {
  27. if (s == null || s.equals("-open") || s.equals("-print")) {
  28. _actionName = s;
  29. }
  30. }
  31. static public String getActionName() { return _actionName; }
  32. static public void setOpenPrintFilePath(String s) { _openPrintFilePath = s; }
  33. static public String getOpenPrintFilePath() { return _openPrintFilePath; }
  34. // returns true if single instance server is running for the idString
  35. public static boolean isServerRunning(String idString) {
  36. if(DEBUG)
  37. System.out.println("isServerRunning ? : "+idString);
  38. File siDir = new File(SingleInstanceImpl.SI_FILEDIR);
  39. String[] fList = siDir.list();
  40. if (fList != null) {
  41. for (int i = 0; i < fList.length; i++) {
  42. if(DEBUG) {
  43. System.out.println("isServerRunning: "+i+": "+fList[i]);
  44. System.out.println("\t sessionString: "+Config.getInstance().getSessionSpecificString());
  45. System.out.println("\t SingleInstanceFilePrefix: "+SingleInstanceImpl.getSingleInstanceFilePrefix(idString +
  46. Config.getInstance().getSessionSpecificString()));
  47. }
  48. // if file with the same prefix already exist, server is
  49. // running
  50. if (fList[i].startsWith(
  51. SingleInstanceImpl.getSingleInstanceFilePrefix(idString +
  52. Config.getInstance().getSessionSpecificString()))) {
  53. try {
  54. _currPort = Integer.parseInt(
  55. fList[i].substring(fList[i].lastIndexOf('_') + 1));
  56. if(DEBUG)
  57. System.out.println("isServerRunning: "+i+": port: "+_currPort);
  58. } catch (NumberFormatException nfe) {
  59. if(DEBUG)
  60. System.out.println("isServerRunning: "+i+": port parsing failed");
  61. Trace.ignoredException(nfe);
  62. return false;
  63. }
  64. Trace.println("server running at port: " + _currPort);
  65. File siFile = new File(SingleInstanceImpl.SI_FILEDIR,
  66. fList[i]);
  67. BufferedReader br = null;
  68. // get random number from single instance file
  69. try {
  70. br = new BufferedReader(new FileReader(siFile));
  71. _randomNumberString = br.readLine();
  72. if(DEBUG)
  73. System.out.println("isServerRunning: "+i+": magic: "+_randomNumberString);
  74. } catch (IOException ioe ) {
  75. if(DEBUG)
  76. System.out.println("isServerRunning: "+i+": reading magic failed");
  77. Trace.ignoredException(ioe);
  78. } finally {
  79. try {
  80. if (br != null) {
  81. br.close();
  82. }
  83. } catch (IOException ioe) {
  84. Trace.ignoredException(ioe);
  85. }
  86. }
  87. if(DEBUG)
  88. System.out.println("isServerRunning: "+i+": setting id - OK");
  89. _idString = idString;
  90. return true;
  91. } else {
  92. if(DEBUG)
  93. System.out.println("isServerRunning: "+i+": prefix NOK");
  94. }
  95. }
  96. } else {
  97. if(DEBUG)
  98. System.out.println("isServerRunning: empty file list");
  99. }
  100. if(DEBUG)
  101. System.out.println("isServerRunning: false");
  102. return false;
  103. }
  104. // returns true if we connect successfully to the server for the idString
  105. public static boolean connectToServer(String outputString) {
  106. Trace.println("connect to: " + _idString + " " + _currPort, TraceLevel.TEMP);
  107. if (_randomNumberString == null) {
  108. // should not happen
  109. Trace.println("MAGIC number is null, bail out", TraceLevel.TEMP);
  110. return false;
  111. }
  112. //Now we open the tcpSocket and the stream
  113. try {
  114. Socket s_socket = new Socket("127.0.0.1",_currPort);
  115. PrintStream out = new PrintStream(s_socket.getOutputStream());
  116. BufferedReader br = new BufferedReader(
  117. new InputStreamReader(s_socket.getInputStream()));
  118. // send random number
  119. out.println(_randomNumberString);
  120. // check and see if javaws command line contains open/print action
  121. String openPrintFilePath = getOpenPrintFilePath();
  122. String actionName = getActionName();
  123. if (openPrintFilePath != null && actionName != null) {
  124. // send open/print command to single instance server
  125. out.println(SingleInstanceImpl.SI_MAGICWORD_OPENPRINT);
  126. out.println(actionName);
  127. out.println(openPrintFilePath);
  128. } else {
  129. // send MAGICWORD
  130. out.println(SingleInstanceImpl.SI_MAGICWORD);
  131. // send over the jnlp file
  132. out.println(outputString);
  133. }
  134. // indicate end of file transmission
  135. out.println(SingleInstanceImpl.SI_EOF);
  136. out.flush();
  137. // wait for ACK (OK) response
  138. Trace.println("waiting for ack", TraceLevel.TEMP);
  139. int tries = 5;
  140. // try to listen for ACK
  141. for (int i=0; i<tries; i++) {
  142. String str = br.readLine();
  143. if (str != null && str.equals(SingleInstanceImpl.SI_ACK)) {
  144. Trace.println("GOT ACK", TraceLevel.TEMP);
  145. s_socket.close();
  146. return true;
  147. }
  148. }
  149. s_socket.close();
  150. } catch (java.net.ConnectException ce) {
  151. // for solaris
  152. // no server is running - continue launch
  153. Trace.println("no server is running - continue launch!", TraceLevel.TEMP);
  154. return false;
  155. } catch (java.net.SocketException se) {
  156. // for windows
  157. // no server is running - continue launch
  158. Trace.println("no server is running - continue launch!", TraceLevel.TEMP);
  159. } catch (Exception ioe) {
  160. ioe.printStackTrace();
  161. }
  162. Trace.println("no ACK from server, bail out", TraceLevel.TEMP);
  163. return false;
  164. }
  165. }