/jEdit/tags/jedit-4-5-pre1/installer/ServerKiller.java

# · Java · 133 lines · 66 code · 15 blank · 52 comment · 3 complexity · f69dfc46ab799a4416fce08618828bfc MD5 · raw file

  1. /*
  2. * ServerKiller.java - Utility class for the installer
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2009 Eric Le Lay
  7. *
  8. * this code is freely adapted from org/gjt/sp/jedit/jEdit.java
  9. * Copyright (C) 1998, 2005 Slava Pestov
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or any later version.
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. package installer;
  25. import java.io.*;
  26. import java.net.*;
  27. /**
  28. * Utility class to check for a running jEdit server,
  29. * and stop it.
  30. * Useful on windows platform, where the jedit.jar archive
  31. * is locked and can't be overwritten by the installer.
  32. *
  33. * NB: The server file must be in the standard location (i.e. $HOME/.jedit/server)
  34. * for the server to be found.
  35. * @version $Id$
  36. */
  37. public class ServerKiller
  38. {
  39. /**
  40. * try to contact a running instance of jEdit Server
  41. * and ask it to close.
  42. * @return true either if no server was detected, or the server was shut-down,
  43. * false otherwise
  44. */
  45. public static boolean quitjEditServer()
  46. {
  47. /* {{{ default server file location */
  48. String settingsDirectory = System.getProperty("user.home");
  49. File portFile;
  50. File f = new File(settingsDirectory);
  51. portFile = new File(f,".jedit/server");
  52. /* }}} */
  53. if(portFile.exists())
  54. {
  55. try
  56. {
  57. BufferedReader in = new BufferedReader(new FileReader(portFile));
  58. String check = in.readLine();
  59. if(!check.equals("b"))
  60. {
  61. System.out.println("Wrong port file format");
  62. return false;
  63. }
  64. int port = Integer.parseInt(in.readLine());
  65. int key = Integer.parseInt(in.readLine());
  66. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),port);
  67. DataOutputStream out = new DataOutputStream(
  68. socket.getOutputStream());
  69. out.writeInt(key);
  70. // we can't close the socket cleanly, because we want
  71. // to wait for complete exit, and then it's too late.
  72. // so the socket is closed when the JVM is shut down.
  73. String script;
  74. script = "jEdit.exit(null,true);\n";
  75. out.writeUTF(script);
  76. // block until its closed
  77. try
  78. {
  79. socket.getInputStream().read();
  80. }
  81. catch(Exception e)
  82. {
  83. //should get an exception !
  84. }
  85. in.close();
  86. out.close();
  87. }
  88. catch(FileNotFoundException fnfe)
  89. {
  90. //it exists : we checked that earlier !
  91. }
  92. catch(UnknownHostException uhe)
  93. {
  94. //localhost doesn't exist ?
  95. }
  96. catch(IOException ioe)
  97. {
  98. System.out.println("Exception while trying to connect to existing server:");
  99. System.out.println(ioe);
  100. System.out.println("Don't worry too much !");
  101. return false; //warn the user
  102. }
  103. }
  104. return true;
  105. }
  106. /**
  107. * try to connect to any running server instance and close it.
  108. * exit with an error code on failure, but not if no server was found.
  109. */
  110. public static void main(String[] args)
  111. {
  112. boolean success = quitjEditServer();
  113. if(!success)
  114. {
  115. System.exit(-1);
  116. }
  117. }
  118. }