@@ -12,19 +12,31 @@ object Bootstrap {
1212
1313 def main (args : Array [String ]): Unit = {
1414
15+ println(" ChatOverflow Bootstrap Launcher." )
16+
1517 if (testValidity()) {
18+ println(" Valid ChatOverflow installation. Checking libraries..." )
19+
1620 if (checkLibraries(args)) {
1721 val javaPath = createJavaPath()
1822 if (javaPath.isDefined) {
23+ println(" Found java installation. Starting ChatOverflow..." )
1924
2025 // Start chat overflow!
2126 val process = new java.lang.ProcessBuilder (javaPath.get, " -cp" , " bin/*:lib/*" , chatOverflowMainClass)
2227 .inheritIO().start()
2328
2429 val exitCode = process.waitFor()
25- println(s " ChatOverflow stopped with code: $exitCode" )
30+ println(s " ChatOverflow stopped with exit code: $exitCode" )
31+ } else {
32+ println(" Unable to find java installation. Unable to start." )
2633 }
34+ } else {
35+ // TODO: Proper management of download problems
36+ println(" Error: Problem with libraries. Unable to start." )
2737 }
38+ } else {
39+ println(" Error: Invalid ChatOverflow installation. Please extract all provided files properly. Unable to start." )
2840 }
2941 }
3042
@@ -60,17 +72,27 @@ object Bootstrap {
6072 // Create or clean directory
6173 if (libFolder.exists()) {
6274 for (libFile <- libFolder.listFiles()) {
63- libFile.delete()
75+ try {
76+ libFile.delete()
77+ } catch {
78+ case e : Exception => println(s " Unable to delete file ' ${libFile.getName}'. Message: ${e.getMessage}" )
79+ }
6480 }
6581 } else {
66- libFolder.mkdir()
82+ try {
83+ libFolder.mkdir()
84+ } catch {
85+ case e : Exception => println(s " Unable to create library directory. Message: ${e.getMessage}" )
86+ }
6787 }
6888
6989 // Download all libraries
7090 // TODO: Check validity if everything is downloaded
91+ println(" Downloading libraries..." )
7192 downloadLibraries()
7293
7394 } else {
95+ println(" Found libraries folder. Assuming all dependencies are available properly." )
7496 true
7597 }
7698 }
@@ -83,44 +105,47 @@ object Bootstrap {
83105 val dependencies = for (dependency <- dependencyXML \\ " dependency" )
84106 yield ((dependency \ " name" ).text.trim, (dependency \ " url" ).text.trim)
85107
86- for ((name, url) <- dependencies) {
87- downloadLibrary(name, url)
108+ for (i <- dependencies.indices) {
109+ val dependency = dependencies(i)
110+ println(s " [ ${i + 1 }/ ${dependencies.length}] ${dependency._1} ( ${dependency._2}) " )
111+ if (! downloadLibrary(dependency._1, dependency._2)) {
112+ // Second try, just in case
113+ downloadLibrary(dependency._1, dependency._2)
114+ }
88115 }
89-
90116 true
91117 }
92118
93- private def downloadLibrary (libraryName : String , libraryURL : String ): Unit = {
119+ private def downloadLibrary (libraryName : String , libraryURL : String ): Boolean = {
94120 val url = new URL (libraryURL)
95121
96122 val connection = url.openConnection().asInstanceOf [HttpURLConnection ]
97- connection.setConnectTimeout(5000 )
98- connection.setReadTimeout(5000 )
123+ connection.setConnectTimeout(1000 )
124+ connection.setReadTimeout(1000 )
99125 connection.connect()
100126
101127 if (connection.getResponseCode >= 400 ) {
102-
128+ println(" Error: Unable to download library." )
129+ false
103130 }
104- // TODO: Retry 3 times
105- // TODO: Feedback on download progress
106131 else {
107-
108- // TODO: try
109- url #> new File (s " $currentFolderPath/lib/ ${libraryURL.substring(libraryURL.lastIndexOf(" /" ))}" ) !!
132+ // Save file in the lib folder (keeping the name and type)
133+ try {
134+ url #> new File (s " $currentFolderPath/lib/ ${libraryURL.substring(libraryURL.lastIndexOf(" /" ))}" ) !!
135+
136+ true
137+ } catch {
138+ case e : Exception =>
139+ println(s " Error: Unable to save library. Message: ${e.getMessage}" )
140+ false
141+ } finally {
142+ connection.disconnect()
143+ }
110144 }
111145 }
112146
113147 private def testValidity (): Boolean = {
114148 // The only validity check for now is the existence of a bin folder
115149 new File (currentFolderPath + " /bin" ).exists()
116150 }
117-
118- /*
119- TODO: Code deploy task (copying files, used after sbt clean and assembly)
120- TODO: Code bootstrap launcher
121- 1. Bootstrap launcher checks integrity (bin folder existing)
122- 2. Bootstrap launcher checks libraries (no lib folder or flag -> Download everything
123- 3. Bootstrap launcher checks java path launched with and starts java -cp "..." chat overflow main class
124- */
125-
126151}
0 commit comments