forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tronprotocol#45 from Rovak/app-di
Add dependency injection, refactor static classes
- Loading branch information
Showing
27 changed files
with
361 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.tron.application; | ||
|
||
import com.google.inject.Injector; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Application { | ||
|
||
private Injector injector; | ||
private ServiceContainer services; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger("Application"); | ||
|
||
public Application(Injector injector) { | ||
this.injector = injector; | ||
this.services = new ServiceContainer(); | ||
} | ||
|
||
public Injector getInjector() { | ||
return injector; | ||
} | ||
|
||
public void addService(Service service) { | ||
this.services.add(service); | ||
} | ||
|
||
public void run() { | ||
this.services.start(); | ||
} | ||
|
||
public void shutdown() { | ||
logger.info("shutting down"); | ||
this.services.stop(); | ||
System.exit(0); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/org/tron/application/ApplicationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.tron.application; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
|
||
public class ApplicationFactory { | ||
|
||
/** | ||
* Build a Guice instance | ||
* | ||
* @return Guice | ||
*/ | ||
public Injector buildGuice() { | ||
return Guice.createInjector( | ||
new Module()); | ||
} | ||
|
||
/** | ||
* Build a new application | ||
* | ||
* @return | ||
*/ | ||
public Application build() { | ||
return new Application(buildGuice()); | ||
} | ||
|
||
/** | ||
* Build a new cli application | ||
* | ||
* @return | ||
*/ | ||
public CliApplication buildCli() { | ||
return new CliApplication(buildGuice()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.tron.application; | ||
|
||
import com.google.inject.Injector; | ||
import org.tron.peer.Peer; | ||
|
||
public class CliApplication extends Application { | ||
|
||
private Peer peer; | ||
|
||
public CliApplication(Injector injector) { | ||
super(injector); | ||
} | ||
|
||
public Peer getPeer() { | ||
return peer; | ||
} | ||
|
||
public void setPeer(Peer peer) { | ||
this.peer = peer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.tron.application; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Provides; | ||
import com.google.inject.Singleton; | ||
import org.tron.consensus.client.Client; | ||
import org.tron.consensus.server.Server; | ||
import org.tron.storage.leveldb.LevelDbDataSourceImpl; | ||
|
||
import javax.inject.Named; | ||
|
||
import static org.tron.core.Constant.BLOCK_DB_NAME; | ||
import static org.tron.core.Constant.TRANSACTION_DB_NAME; | ||
|
||
public class Module extends AbstractModule { | ||
|
||
@Override | ||
protected void configure() { | ||
|
||
} | ||
|
||
@Provides | ||
@Singleton | ||
public Client buildClient() { | ||
return new Client(); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public Server buildServer() { | ||
return new Server(); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
@Named("transaction") | ||
public LevelDbDataSourceImpl buildTransactionDb() { | ||
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(TRANSACTION_DB_NAME); | ||
db.initDB(); | ||
return db; | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
@Named("block") | ||
public LevelDbDataSourceImpl buildBlockDb() { | ||
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(BLOCK_DB_NAME); | ||
db.initDB(); | ||
return db; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.tron.application; | ||
|
||
public interface Service { | ||
|
||
void start(); | ||
void stop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.tron.application; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class ServiceContainer implements Service { | ||
|
||
private ArrayList<Service> services; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger("Services"); | ||
|
||
public ServiceContainer() { | ||
this.services = new ArrayList<>(); | ||
} | ||
|
||
public void add(Service service) { | ||
this.services.add(service); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
logger.debug("Starting services"); | ||
for (Service service : this.services) { | ||
logger.debug("Starting " + service.getClass().getSimpleName()); | ||
service.start(); | ||
} | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
for (Service service : this.services) { | ||
logger.debug("Stopping " + service.getClass().getSimpleName()); | ||
service.stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.