PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/kontalk/Kontalk.java

https://gitlab.com/sgk/kontalk
Java | 179 lines | 118 code | 28 blank | 33 comment | 9 complexity | 94cc4f172915248ab33bc07643b71492 MD5 | raw file
  1. /*
  2. * Kontalk Java client
  3. * Copyright (C) 2014 Kontalk Devteam <devteam@kontalk.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package org.kontalk;
  19. import org.kontalk.misc.KonException;
  20. import org.kontalk.system.Database;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.net.InetAddress;
  24. import java.net.ServerSocket;
  25. import java.nio.file.Paths;
  26. import java.util.Optional;
  27. import java.util.logging.FileHandler;
  28. import java.util.logging.Handler;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import java.util.logging.SimpleFormatter;
  32. import org.apache.commons.lang.SystemUtils;
  33. import org.kontalk.crypto.PGPUtils;
  34. import org.kontalk.model.ThreadList;
  35. import org.kontalk.model.UserList;
  36. import org.kontalk.system.Config;
  37. import org.kontalk.system.Control;
  38. import org.kontalk.util.CryptoUtils;
  39. import org.kontalk.util.Tr;
  40. import org.kontalk.view.View;
  41. /**
  42. * @author Alexander Bikadorov {@literal <bikaejkb@mail.tu-berlin.de>}
  43. */
  44. public final class Kontalk {
  45. private static final Logger LOGGER = Logger.getLogger(Kontalk.class.getName());
  46. public static final String VERSION = "3.0.1";
  47. public static final String RES_PATH = "res/";
  48. private static final String CONFIG_DIR;
  49. private static ServerSocket RUN_LOCK;
  50. static {
  51. // initialize translation
  52. Tr.init();
  53. // check java version
  54. String jVersion = System.getProperty("java.version");
  55. if (jVersion.startsWith("1.7")) {
  56. View.showWrongJavaVersionDialog();
  57. LOGGER.severe("java too old: "+jVersion);
  58. System.exit(-3);
  59. }
  60. // use platform dependent configuration directory
  61. String homeDir = System.getProperty("user.home");
  62. if (SystemUtils.IS_OS_WINDOWS) {
  63. CONFIG_DIR = Paths.get(homeDir,"Kontalk").toString();
  64. } else {
  65. CONFIG_DIR = Paths.get(homeDir, ".kontalk").toString();
  66. }
  67. // create app directory
  68. boolean created = new File(CONFIG_DIR).mkdirs();
  69. if (created)
  70. LOGGER.info("created configuration directory");
  71. // log to file
  72. String logPath = Paths.get(CONFIG_DIR, "debug.log").toString();
  73. Handler fileHandler = null;
  74. try {
  75. fileHandler = new FileHandler(logPath, 1024*1000, 1, true);
  76. } catch (IOException | SecurityException ex) {
  77. LOGGER.log(Level.WARNING, "can't log to file", ex);
  78. }
  79. if (fileHandler != null) {
  80. fileHandler.setFormatter(new SimpleFormatter());
  81. Logger.getLogger("").addHandler(fileHandler);
  82. }
  83. LOGGER.info("--START, version: "+VERSION+"--");
  84. // fix crypto restriction
  85. CryptoUtils.removeCryptographyRestrictions();
  86. // register provider
  87. PGPUtils.registerProvider();
  88. }
  89. private Kontalk(String[] args) {
  90. // check if already running
  91. try {
  92. InetAddress addr = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
  93. RUN_LOCK = new ServerSocket(9871, 10, addr);
  94. } catch(java.net.BindException ex) {
  95. LOGGER.severe("already running");
  96. System.exit(2);
  97. } catch(IOException ex) {
  98. LOGGER.log(Level.WARNING, "can't create socket", ex);
  99. }
  100. this.parseArgs(args);
  101. }
  102. public void start() {
  103. Config.initialize(CONFIG_DIR + "/" + Config.CONF_NAME);
  104. Control control = new Control();
  105. Optional<View> optView = View.create(control);
  106. if (!optView.isPresent()) {
  107. control.shutDown();
  108. return; // never reached
  109. }
  110. View view = optView.get();
  111. try {
  112. Database.initialize(CONFIG_DIR + "/" + Database.DB_NAME);
  113. } catch (KonException ex) {
  114. LOGGER.log(Level.SEVERE, "can't initialize database", ex);
  115. control.shutDown();
  116. return; // never reached
  117. }
  118. // order matters!
  119. UserList.getInstance().load();
  120. ThreadList.getInstance().load();
  121. view.init();
  122. control.launch();
  123. }
  124. // parse optional arguments
  125. private void parseArgs(String[] args) {
  126. if (args.length != 0) {
  127. String className = this.getClass().getEnclosingClass().getName();
  128. LOGGER.log(Level.WARNING, "Usage: java {0} ", className);
  129. }
  130. }
  131. public static String getConfigDir() {
  132. return CONFIG_DIR;
  133. }
  134. public static void exit() {
  135. try {
  136. RUN_LOCK.close();
  137. } catch (IOException ex) {
  138. LOGGER.log(Level.WARNING, "can't close run socket", ex);
  139. }
  140. LOGGER.info("exit");
  141. System.exit(0);
  142. }
  143. /**
  144. * @param args the command line arguments
  145. */
  146. public static void main(String[] args) {
  147. LOGGER.setLevel(Level.ALL);
  148. Kontalk model = new Kontalk(args);
  149. model.start();
  150. }
  151. }