From 828e1991a5a4c60b9eeca68142479f4a22e1be18 Mon Sep 17 00:00:00 2001 From: Gelin Luo Date: Tue, 18 Feb 2020 07:46:30 +1100 Subject: [PATCH] Allow inject StorageService by name optionally #6 --- CHANGELOG.md | 5 ++++ pom.xml | 2 +- .../act/storage/StorageServiceManager.java | 30 ++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c92e779..76e8d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # act-storage CHANGE LOG +0.16.0 +* Allow inject StorageService by name optionally #6 +* update to act-1.8.32 +* update osgl-storage to 1.10.1 + 0.15.0 - 03/Nov/2019 * update to act-1.8.29 diff --git a/pom.xml b/pom.xml index ae050ab..d743773 100755 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,7 @@ org.actframework parent - 1.8.29 + 1.8.32-SNAPSHOT diff --git a/src/main/java/act/storage/StorageServiceManager.java b/src/main/java/act/storage/StorageServiceManager.java index 6d62eb9..3a11f0d 100644 --- a/src/main/java/act/storage/StorageServiceManager.java +++ b/src/main/java/act/storage/StorageServiceManager.java @@ -20,6 +20,7 @@ * #L% */ +import act.Act; import act.Destroyable; import act.app.App; import act.app.AppService; @@ -31,6 +32,7 @@ import act.storage.db.util.Setter; import org.osgl.$; import org.osgl.exception.ConfigurationException; +import org.osgl.inject.NamedProvider; import org.osgl.logging.LogManager; import org.osgl.logging.Logger; import org.osgl.storage.ISObject; @@ -44,6 +46,7 @@ import java.util.Map; import java.util.Set; import javax.enterprise.context.ApplicationScoped; +import javax.inject.Provider; import javax.inject.Singleton; /** @@ -104,11 +107,29 @@ public class StorageServiceManager extends AppServicePlugin implements AppServic private boolean isDestroyed; + private final Provider SS_PROVIDER = new Provider() { + @Override + public IStorageService get() { + return storageService(DEFAULT); + } + }; + + private final NamedProvider SS_NAMED_PROVIDER = new NamedProvider() { + @Override + public IStorageService get(String name) { + IStorageService ss = storageService(name); + if (null == ss && ("ss".equalsIgnoreCase(name) || "storageService".equalsIgnoreCase(name) || "svc".equalsIgnoreCase(name) || "service".equalsIgnoreCase(name))) { + return storageService(DEFAULT); + } + return ss; + } + }; + public StorageServiceManager() { } @Override - protected void applyTo(App app) { + protected void applyTo(final App app) { this.reset(); this.app = app; initServices(app.config()); @@ -124,6 +145,13 @@ public StorageServiceManager resolve(App app) { }); } }); + app.jobManager().on(SysEventId.PRE_START, new Runnable() { + @Override + public void run() { + app.injector().registerProvider(IStorageService.class, SS_PROVIDER); + app.injector().registerNamedProvider(IStorageService.class, SS_NAMED_PROVIDER); + } + }); app.eventBus().emit(new StorageServiceManagerInitialized(this)); }