source: launchers/browserbundle/src/main/scala/net/i2p/RouterLauncherApp.scala @ 345e741

Last change on this file since 345e741 was a3a8ee1, checked in by meeh <meeh@…>, 2 years ago

Major update for the OSX Launcher code. Now it will bundle
installer resources with the fat jar (we add an exclusion list later)
which the base directory will be built(or updated if lacking files) upon
startup of the I2P router. This is done by the OSXDeployment class which
is an extension for the DeployProfile? class written for Mac OS X.

Since the app bundle itself should be R/O, we use ~/Library/I2P as base path,
and continue using ~/Library/Application? Support/i2p as config path. The BB
code will have other paths.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1package net.i2p
2
3import java.io.{File, InputStream}
4
5import net.i2p.launchers.DeployProfile
6import net.i2p.router.Router
7import org.json4s._
8import org.json4s.native.JsonMethods._
9
10/**
11  *
12  * For java developers:
13  * A scala object is like an instance of a class.
14  * If you define a method inside an object, it's equals to
15  * java's static methods.
16  *
17  * Also, in scala, the body of a class/object is executed as it's
18  * constructor.
19  *
20  * Also noteworthy;
21  * val is immutable
22  * var is mutable
23  *
24  *
25  * @author Meeh
26  * @version 0.0.1
27  * @since 0.9.35
28  */
29object RouterLauncherApp extends App {
30
31  def toBytes(xs: Int*) = xs.map(_.toByte).toArray
32
33  def getInt(bytes: Array[Byte]): Int = (bytes(3) << 24) & 0xff000000 | (bytes(2) << 16) & 0x00ff0000 | (bytes(1) << 8) & 0x0000ff00 | (bytes(0) << 0) & 0x000000ff
34
35  /**
36    * Encodes bytes in "binary form" as mentioned in Native Messaging in the WebExtension API.
37    * @param length
38    * @return
39    */
40  def getBytes(length: Int): Array[Byte] = {
41    val bytes = new Array[Byte](4)
42    bytes(0) = (length & 0xFF).toByte
43    bytes(1) = ((length >> 8) & 0xFF).toByte
44    bytes(2) = ((length >> 16) & 0xFF).toByte
45    bytes(3) = ((length >> 24) & 0xFF).toByte
46    bytes
47  }
48
49  def readMessage(in: InputStream): String = {
50    val arr = new Array[Byte](4)
51    in.read(arr)
52    val bytes = new Array[Byte](getInt(arr))
53    in.read(bytes)
54    new String(bytes, "UTF-8")
55  }
56
57  def sendMessage(message: String): Unit = {
58    System.out.write(getBytes(message.length))
59    System.out.write(message.getBytes("UTF-8"))
60  }
61
62  // Executed at launch
63  val basePath = Option(System.getProperty("i2p.dir.base")).getOrElse(System.getenv("I2PBASE"))
64  val configPath = Option(System.getProperty("i2p.dir.config")).getOrElse(System.getenv("I2PCONFIG"))
65
66  println(s"basePath => ${basePath}\nconfigPath => ${configPath}")
67
68  object ErrorUtils {
69    def errorMessageInJson(message: String, solution: String) : JObject = JObject(
70      List(
71        ("error",
72          JObject(
73            ("message", JString(message)),
74            ("solution", JString(solution))
75          )
76        )
77      )
78    )
79
80    def printError(message: String, solution: String): Unit = {
81      println(compact(render( errorMessageInJson(message,solution) )))
82    }
83
84    def printErrorAndExit(message: String, solution: String, exitCode: Int = 1): Unit = {
85      printError(message, solution)
86      System.exit(exitCode)
87    }
88  }
89
90  // Path related error checking
91  if (basePath == null || basePath.isEmpty) ErrorUtils.printErrorAndExit("I2P Base path is missing", "set property i2p.dir.base or environment variable I2PBASE")
92  if (configPath  == null || configPath.isEmpty) ErrorUtils.printErrorAndExit("I2P Config path is missing", "set property i2p.dir.config or environment variable I2PCONFIG")
93  if (!new File(basePath).exists()) ErrorUtils.printErrorAndExit("I2P Base path don't exist", "Reinstall the Browser Bundle")
94  if (!new File(configPath).exists()) ErrorUtils.printErrorAndExit("I2P Config path don't exist", "Delete your config directory for the Browser Bundle")
95
96
97  val deployer = new DeployProfile(configPath,basePath)
98  deployer.verifyExistenceOfConfig()
99
100  // Required/mocked properties
101  System.setProperty("wrapper.version", "portable-1")
102  System.setProperty("i2p.dir.portableMode", "true")
103  System.setProperty("loggerFilenameOverride", "router-@.log")
104
105  //ErrorUtils.printError(s"Starting up with arguments ${(args mkString ", ")}",":)")
106
107  Router.main(args)
108}
109
110
Note: See TracBrowser for help on using the repository browser.