Skip to content

Commit 36abb3c

Browse files
[#13] Add name to ConnectorCreator, use it when instantiating
the connector to choose one over the others
1 parent 00872df commit 36abb3c

File tree

13 files changed

+73
-22
lines changed

13 files changed

+73
-22
lines changed

README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,11 @@ classpath)
200200

201201
Example of configuration for the `HBaseConnector`:
202202
```
203-
hbase {
204-
"isSecure": false,
205-
"namespace": "DARWIN",
206-
"table": "REPOSITORY",
207-
"coreSite": "/etc/hadoop/conf/core-site.xml",
208-
"hbaseSite": "/etc/hadoop/conf/hbase-site.xml",
209-
}
203+
"isSecure": false,
204+
"namespace": "DARWIN",
205+
"table": "REPOSITORY",
206+
"coreSite": "/etc/hadoop/conf/core-site.xml",
207+
"hbaseSite": "/etc/hadoop/conf/hbase-site.xml",
210208
```
211209
### HBase Connector dependencies
212210
Darwin HBase Connector does not provide HBase dependencies in a transitive manner since that would lead to hard to
@@ -225,11 +223,9 @@ The configuration keys managed by the `PostgresConnector` are:
225223

226224
Example of configuration for the `PostgresConnector`:
227225
```
228-
postgres {
229-
"host": "localhost:5432"
230-
"db": "srdb"
231-
"username": "postgres"
232-
"password": "srpsql"
233-
"table": "schema_registry"
234-
}
226+
"host": "localhost:5432"
227+
"db": "srdb"
228+
"username": "postgres"
229+
"password": "srpsql"
230+
"table": "schema_registry"
235231
```

common/src/main/scala/it/agilelab/darwin/common/ConnectorCreator.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import com.typesafe.config.Config
77
*/
88
trait ConnectorCreator {
99

10+
/**
11+
* @return the name of the Connector
12+
*/
13+
def name(): String
14+
1015
/**
1116
* This method should be overridden in each connector module returning its implementation.
1217
*
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package it.agilelab.darwin.common
22

33
import java.util.ServiceLoader
4+
5+
import com.typesafe.config.Config
6+
import it.agilelab.darwin.manager.util.ConfigurationKeys
7+
48
import scala.collection.JavaConverters._
59

610
/**
711
* Used to obtain the correct implementation of [[Connector]] found on the classpath using the [[ConnectorCreator]]
812
*/
913
object ConnectorFactory extends Logging {
1014

11-
private val DEFAULT_PROVIDER: String = "TEST" //TODO set a default implementation
12-
1315
/**
1416
* Retrieves all the registered [[ConnectorCreator]] in the classpath.
1517
*
@@ -21,13 +23,29 @@ object ConnectorFactory extends Logging {
2123
creators
2224
}
2325

24-
def creator(): ConnectorCreator = creator(DEFAULT_PROVIDER)
26+
/**
27+
* @return the first ConnectorCreator, use ONLY if you are sure that just one is available in the classpath
28+
*/
29+
def creator(): Option[ConnectorCreator] = creators().headOption
30+
2531

26-
def creator(name: String): ConnectorCreator = {
27-
creators().find(_.getClass.getName == name) match {
28-
case Some(c) => c
29-
case _ => throw new ClassNotFoundException("Creator " + name + " not found")
32+
/**
33+
* @return the ConnectorCreator identified by the name given as input
34+
*/
35+
def creator(name: String): Option[ConnectorCreator] = {
36+
creators().find(_.name == name)
37+
}
38+
39+
/**
40+
* @return the ConnectorCreator identified by the name given as input
41+
*/
42+
def creator(conf: Config): Option[ConnectorCreator] = {
43+
if (conf.hasPath(ConfigurationKeys.CONNECTOR)) {
44+
creator(conf.getString(ConfigurationKeys.CONNECTOR))
45+
} else {
46+
creator()
3047
}
3148
}
3249

50+
3351
}

core/src/main/scala/it/agilelab/darwin/manager/util/ConfigurationKeys.scala renamed to common/src/main/scala/it/agilelab/darwin/manager/util/ConfigurationKeys.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ object ConfigurationKeys {
44

55
val CREATE_TABLE = "createTable"
66

7+
val CONNECTOR = "connector"
8+
79
}

core/src/main/scala/it/agilelab/darwin/manager/AvroSchemaManager.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait AvroSchemaManager extends Logging {
2222
protected def config: Config
2323

2424
protected[darwin] lazy val connector: Connector = {
25-
val cnt = ConnectorFactory.creators().headOption.map(_.create(config))
25+
val cnt = ConnectorFactory.creator(config).map(_.create(config))
2626
.getOrElse(throw new ConnectorNotFoundException(config))
2727

2828
if (config.getBoolean(ConfigurationKeys.CREATE_TABLE)) {

hbase/src/main/scala/it/agilelab/darwin/connector/hbase/HBaseConnectorCreator.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ class HBaseConnectorCreator extends ConnectorCreator with Logging {
1010
log.debug("HBase connector created")
1111
connector
1212
}
13+
14+
/**
15+
* @return the name of the Connector
16+
*/
17+
override def name(): String = "hbase"
1318
}

ignite/src/main/scala/it/agilelab/darwin/connector/ignite/IgniteConnectorCreator.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ import it.agilelab.darwin.common.{Connector, ConnectorCreator}
55

66
class IgniteConnectorCreator extends ConnectorCreator {
77
override def create(config: Config): Connector = new IgniteConnector(config)
8+
9+
/**
10+
* @return the name of the Connector
11+
*/
12+
override def name(): String = "ignite"
813
}

mock-connector/src/main/scala/it/agilelab/darwin/connector/mock/MockConnectorCreator.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ import it.agilelab.darwin.common.{Connector, ConnectorCreator}
55

66
class MockConnectorCreator extends ConnectorCreator {
77
override def create(config: Config): Connector = new MockConnector(config)
8+
9+
/**
10+
* @return the name of the Connector
11+
*/
12+
override def name(): String = "mock"
813
}

0 commit comments

Comments
 (0)