-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySpring.scala
More file actions
executable file
·169 lines (138 loc) · 5.43 KB
/
Copy pathMySpring.scala
File metadata and controls
executable file
·169 lines (138 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
*
*
* <p>Copyright: Copyright (c) 2011</p>
* <p>Company: BigSense</p>
* @author Sumit Khanna <sumit@penguindreams.org>
*/
package io.bigsense.spring
import java.io.FileInputStream
import java.util.Properties
import com.jolbox.bonecp.BoneCPDataSource
import io.bigsense.action._
import io.bigsense.conversion.{ConverterTrait, KeyPemConverter, TimezoneConverter, UnitsConverter}
import io.bigsense.db.ServiceDataHandler
import io.bigsense.format._
import io.bigsense.security.{DisabledSecurityManager, SignatureSecurityManager}
import io.bigsense.util.{CommandLineSignatureManager, SQLProperties}
import io.bigsense.validation._
import io.bigsense.server.BigSenseServer
import org.slf4j.LoggerFactory
object MySpring {
private val config = BigSenseServer.config.options
// Database
lazy val jdbcURL = dbConnectionString(
config("dbms"),
config("dbHostname"),
config("dbDatabase"),
config("dbPort")
)
private def dataSource(username: String, password: String) = {
val ds = new BoneCPDataSource()
ds.setDriverClass(dbDriver(config("dbms")))
ds.setJdbcUrl(jdbcURL)
ds.setMaxConnectionsPerPartition(config("dbPoolMaxPerPart").toInt)
ds.setMinConnectionsPerPartition(config("dbPoolMinPerPart").toInt)
ds.setPartitionCount(config("dbPoolPartitions").toInt)
ds.setUsername(username)
ds.setPassword(password)
ds
}
private lazy val sqlCommandProps = new SQLProperties(s"/io/bigsense/db/${config("dbms")}.commands")
private lazy val converterMap: Map[String, ConverterTrait] = Map("Units" -> new UnitsConverter, "Timezone" -> new TimezoneConverter, "Keys" -> new KeyPemConverter)
lazy val commandLineSignatureManager = new CommandLineSignatureManager(serviceDataHandler)
lazy val serviceDataHandler = new ServiceDataHandler {
ds = dataSource(config("dbUser"), config("dbPass"))
sqlCommands = sqlCommandProps
dbDialect = config("dbms")
converters = converterMap
}
// Actions
lazy val aggregateAction = new AggregateAction {validator = new AggregateActionValidator}
lazy val imageAction = new ImageAction {validator = new ImageActionValidator}
lazy val queryAction = new QueryAction {validator = new QueryActionValidator}
lazy val sensorAction = new SensorAction {validator = new SensorActionValidator}
lazy val statusAction = new StatusAction {validator = new StatusActionValidator}
def getAction(action: String): Option[ActionTrait] = {
action match {
case "Aggregate" => Some(aggregateAction)
case "Image" => Some(imageAction)
case "Query" => Some(queryAction)
case "Sensor" => Some(sensorAction)
case "Status" => Some(statusAction)
case _ => None
}
}
private def dbDriver(dbms: String) = {
dbms match {
case "pgsql" => "org.postgis.DriverWrapper"
case "mssql" => "net.sourceforge.jtds.jdbc.Driver"
case "mysql" => "com.mysql.jdbc.Driver"
}
}
private def dbConnectionString(dbms: String, hostname: String, database: String, port: String) = {
dbms match {
case "pgsql" => s"jdbc:postgresql://$hostname:$port/$database"
case "mssql" => s"jdbc:jtds:sqlserver://$hostname:$port/$database"
case "mysql" => s"jdbc:mysql://$hostname:$port/$database?useLegacyDatetimeCode=false&serverTimezone=UTC"
}
}
// Formats
lazy val senseXMLFormat = new SenseDataXMLFormat
lazy val txtFormat = new TabDelimitedFormat
lazy val csvFormat = new CSVFormat
lazy val tableHTMLFormat = new TableHTMLFormat
lazy val senseJsonFormat = new SenseJsonFormat
def getFormat(format: String): Option[FormatTrait] = {
format match {
case "sense.xml" => Some(senseXMLFormat)
case "txt" => Some(txtFormat)
case "csv" => Some(csvFormat)
case "table.html" => Some(tableHTMLFormat)
case "sense.json" => Some(senseJsonFormat)
case _ => None
}
}
// Security
lazy val securityManager = config("securityManager") match {
case "Signature" => new SignatureSecurityManager {dbHandler = serviceDataHandler}
case "Disabled" => new DisabledSecurityManager
}
}
object BigSensePropertyLocation {
private val log = LoggerFactory.getLogger(this.getClass)
def printProperties() = properties.list(System.out)
val environmentVarMap = Map("DBMS" -> "dbms",
"DB_HOSTNAME" -> "dbHostname",
"DB_DATABASE" -> "dbDatabase",
"DB_PORT" -> "dbPort",
"DB_USER" -> "dbUser",
"DB_PASS" -> "dbPass",
"DBO_USER" -> "dboUser",
"DBO_PASS" -> "dboPass",
"SECURITY_MANAGER" -> "securityManager",
"SERVER" -> "server",
"HTTP_PORT" -> "httpPort")
lazy val properties = {
val p = new Properties()
p.load(getClass.getResourceAsStream("/io/bigsense/spring/defaults.properties"))
if(!BigSenseServer.config.params.configFile.isSupplied && !BigSenseServer.config.params.useEnvironmentVars.isSupplied) {
log.warn("Neither configuration file (-c) or environment variable settings (-e) was defined. Using default settings.")
}
if(BigSenseServer.config.params.configFile.isSupplied) {
p.load(new FileInputStream(BigSenseServer.config.params.configFile()))
}
if(BigSenseServer.config.params.useEnvironmentVars.isSupplied) {
environmentVarMap.foreach( e =>
sys.env.get(e._1) match {
case Some(value : String) => {
log.info(s"Loading environment variable ${e._1}. Setting ${e._2} to $value")
p.setProperty(e._2, value)
}
case None => Unit
}
)
}
p
}
}