提交 4ec7fe3f 编写于 作者: wu-sheng's avatar wu-sheng

Finish more codes about modulization.

上级 ef496e0d
...@@ -41,6 +41,13 @@ public abstract class Module { ...@@ -41,6 +41,13 @@ public abstract class Module {
*/ */
public abstract Class<? extends Service>[] services(); public abstract Class<? extends Service>[] services();
/**
* Run the prepare stage for the module, including finding all potential providers, and asking them to prepare.
*
* @param moduleManager of this module
* @param configuration of this module
* @throws ProviderNotFoundException when even don't find a single one providers.
*/
void prepare(ModuleManager moduleManager, void prepare(ModuleManager moduleManager,
ApplicationConfiguration.ModuleConfiguration configuration) throws ProviderNotFoundException { ApplicationConfiguration.ModuleConfiguration configuration) throws ProviderNotFoundException {
ServiceLoader<ModuleProvider> moduleProviderLoader = ServiceLoader.load(ModuleProvider.class); ServiceLoader<ModuleProvider> moduleProviderLoader = ServiceLoader.load(ModuleProvider.class);
...@@ -50,16 +57,14 @@ public abstract class Module { ...@@ -50,16 +57,14 @@ public abstract class Module {
if (provider.module().equals(getClass())) { if (provider.module().equals(getClass())) {
ModuleProvider newProvider; ModuleProvider newProvider;
try { try {
newProvider = provider.getClass().getConstructor(ModuleManager.class, Module.class).newInstance(moduleManager, this); newProvider = provider.getClass().newInstance();
} catch (InstantiationException e) { } catch (InstantiationException e) {
throw new ProviderNotFoundException(e); throw new ProviderNotFoundException(e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new ProviderNotFoundException(e); throw new ProviderNotFoundException(e);
} catch (InvocationTargetException e) {
throw new ProviderNotFoundException(e);
} catch (NoSuchMethodException e) {
throw new ProviderNotFoundException(e);
} }
newProvider.setManager(moduleManager);
newProvider.setModule(this);
loadedProviders.add(newProvider); loadedProviders.add(newProvider);
} }
} }
......
...@@ -32,13 +32,16 @@ import java.util.Properties; ...@@ -32,13 +32,16 @@ import java.util.Properties;
public abstract class ModuleProvider { public abstract class ModuleProvider {
protected ModuleManager manager; protected ModuleManager manager;
protected Module module; protected Module module;
protected Map<Class<? extends Service>, Service> services = new HashMap<>(); private Map<Class<? extends Service>, Service> services = new HashMap<>();
public ModuleProvider() { public ModuleProvider() {
} }
ModuleProvider(ModuleManager manager, Module module) { void setManager(ModuleManager manager) {
this.manager = manager; this.manager = manager;
}
void setModule(Module module) {
this.module = module; this.module = module;
} }
...@@ -71,10 +74,23 @@ public abstract class ModuleProvider { ...@@ -71,10 +74,23 @@ public abstract class ModuleProvider {
*/ */
public abstract String[] requiredModules(); public abstract String[] requiredModules();
/**
* Register a implementation for the service of this module provider.
* @param serviceType
* @param service
*/
protected void registerServiceImplementation(Class<? extends Service> serviceType, Service service) {
this.services.put(serviceType, service);
}
void requiredCheck(Class<? extends Service>[] requiredServices) throws ServiceNotProvidedException { void requiredCheck(Class<? extends Service>[] requiredServices) throws ServiceNotProvidedException {
if (requiredServices == null) if (requiredServices == null)
return; return;
if (requiredServices.length != services.size()) {
throw new ServiceNotProvidedException("Haven't provided enough plugins.");
}
for (Class<? extends Service> service : requiredServices) { for (Class<? extends Service> service : requiredServices) {
if (!services.containsKey(service)) { if (!services.containsKey(service)) {
throw new ServiceNotProvidedException("Service:" + service.getName() + " not provided"); throw new ServiceNotProvidedException("Service:" + service.getName() + " not provided");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册