com.flagwind.services.base.ServiceProviderBase Maven / Gradle / Ivy
package com.flagwind.services.base;
import java.util.ArrayList;
import com.flagwind.commons.StringUtils;
import com.flagwind.events.CancelEventArgs;
import com.flagwind.events.EventProvider;
import com.flagwind.services.InvalidOperationException;
import com.flagwind.services.ServiceBuilder;
import com.flagwind.services.ServiceEntry;
import com.flagwind.services.ServiceProvider;
import com.flagwind.services.ServiceStorage;
public abstract class ServiceProviderBase extends EventProvider implements ServiceProvider {
//region 事件声明
public static final String REGISTERED = "registered";
public static final String UNREGISTERED = "unregistered";
public static final String RESOLVING = "resolving";
public static final String RESOLVED = "resolved";
//endregion
//region 成员字段
private ServiceStorage storage;
private ServiceBuilder builder;
//endregion
//region 构造函数
protected ServiceProviderBase() {
super(null);
}
protected ServiceProviderBase(ServiceStorage storage, ServiceBuilder builder) {
super(null);
if (storage == null)
throw new IllegalArgumentException("storage");
this.storage = storage;
this.builder = builder;
}
//endregion
//region 公共属性
@Override
public ServiceStorage getStorage() {
return storage;
}
public void setStorage(ServiceStorage storage) {
this.storage = storage;
}
public ServiceBuilder getBuilder() {
return builder;
}
public void setBuilder(ServiceBuilder builder) {
this.builder = builder;
}
//endregion
//region 注册方法
public void register(String name, Class> serviceType) {
this.register(name, serviceType, (Class>[]) null);
}
public void register(String name, Class> serviceType, Class>[] contractTypes) {
//创建一个服务描述项对象
com.flagwind.services.ServiceEntry entry = this.createEntry(name, serviceType, contractTypes);
//执行具体的注册操作
this.register(entry);
}
public void register(String name, Object service) {
this.register(name, service, (Class>[]) null);
}
public void register(String name, Object service, Class>[] contractTypes) {
//创建一个服务描述项对象
com.flagwind.services.ServiceEntry entry = this.createEntry(name, service, contractTypes);
//执行具体的注册操作
this.register(entry);
}
public void register(Class> serviceType, Class>[] contractTypes) {
//创建一个服务描述项对象
com.flagwind.services.ServiceEntry entry = this.createEntry(serviceType, contractTypes);
//执行具体的注册操作
this.register(entry);
}
public void register(Object service, Class>[] contractTypes) {
//创建一个服务描述项对象
com.flagwind.services.ServiceEntry entry = this.createEntry(service, contractTypes);
//执行具体的注册操作
this.register(entry);
}
public void Unregister(String name) {
com.flagwind.services.ServiceEntry entry = storage.remove(name);
if (entry != null) {
this.dispatchEvent(UNREGISTERED, entry);
}
}
//endregion
//region 解析方法
public Class> GetServiceType(String name) {
if (StringUtils.isBlank(name))
throw new IllegalArgumentException("name");
com.flagwind.services.ServiceEntry entry = this.storage.get(name);
return entry == null ? null : entry.getServiceType();
}
public Object resolve(String name) {
if (StringUtils.isBlank(name))
throw new IllegalArgumentException("name");
CancelEventArgs args = new CancelEventArgs(RESOLVING, name);
//激发“Resolving”事件
this.dispatchEvent(args);
if (args.getCancel())
return args.getData();
Object result = null;
com.flagwind.services.ServiceEntry entry = storage.get(name);
if (entry != null)
result = this.getService(entry);
//激发“Resolved”事件
this.dispatchEvent(REGISTERED, result);
//返回解析的结果
return result;
}
@Override
public T resolve(Class> type) {
return (T) this.resolve(type, null);
}
public Object resolve(Class> type, Object parameter) {
if (type == null)
throw new IllegalArgumentException("type");
CancelEventArgs args = new CancelEventArgs(type.getSimpleName(), parameter);
//激发“Resolving”事件
this.dispatchEvent(args);
if (args.getCancel())
return args.getData();
Object result = null;
com.flagwind.services.ServiceEntry entry = this.storage.get(type);
if (entry != null)
result = this.getService(entry);
//激发“Resolved”事件
this.dispatchEvent(REGISTERED, result);
//返回解析的结果
return result;
}
public Iterable resolveAll(Class> type) {
return (Iterable) this.resolveAll(type, null);
}
public Iterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy