Monday, June 3, 2013

SCALA - Menyediakan Konfigurasi bagi Aplikasi


Asumsikan bahwa kita mempunyai struktur folder sebagai berikut:
PROJECT_NAME
+  conf
    +  datamover
        +  config.properties
+  libs
+  bin
    +  run.sh

Di file run.sh, pastikan bahwa semua folder (libraries, configurations) telah diinclude dalam CLASSPATH, sehingga CLASSPATH=conf;conf/datamover;libs;bin

Ada beberapa cara setting variables

1. Menggunakan config file
 
Untuk mengakses

import java.util.Properties
import java.io._ 

val DEFAULT_CONFIG : String = "config.properties"
val fileProperties : Properties 
def getFileProperties(propertiesFileName: String = DEFAULT_CONFIG): Properties = {
    if (fileProperties == null) {
      fileProperties = new Properties
      fileProperties.load(new FileReader(
          new File(AppDocExtractor.getClass.getResource(propertiesFileName).toURI)))
    }
    fileProperties
}
fileProperties = getFileProperties(DEFAULT_CONFIG)
if (fileProperties.getProperty("sleep.second") != null) {
    val sleepSecond : Int =  fileProperties.getProperty("sleep.second").toInt

2. Mengambil parameter dari pemanggilan aplikasi
    Misalnya ada aplikasi dipanggil dengan: java -Dusethread=1
USE_THREAD_PARAM = "usethread"
val isUseThread = if (System.getProperty(USE_THREAD_PARAM) != null)
     if ("1".equals(System.getProperty(USE_THREAD_PARAM))) true else false
  else false

No comments: