com.jpattern.core.SystemProvider Maven / Gradle / Ivy
package com.jpattern.core;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.jpattern.service.cache.ACacheServiceBuilder;
import com.jpattern.service.cache.ICacheService;
import com.jpattern.service.cache.NullCacheService;
import com.jpattern.service.log.ALoggerServiceBuilder;
import com.jpattern.service.log.ILoggerService;
import com.jpattern.service.log.NullLoggerService;
import com.jpattern.service.log.slf4j.Slf4jLoggerService;
import com.jpattern.service.mail.AMailServiceBuilder;
import com.jpattern.service.mail.IMailService;
import com.jpattern.service.mail.NullMailService;
/**
*
* @author Francesco Cina'
*
* 29/gen/2011
*/
public class SystemProvider implements ISystemProvider {
private Map serviceMap = new HashMap();
@Override
public final IService getService(String aServiceName) {
if (!contains(aServiceName)) {
return new NullService();
}
return getServiceMap().get(aServiceName);
}
@Override
public final ILoggerService getLoggerService() {
IService service = getService(IServicesName.LOGGER_SERVICE);
if (service instanceof ILoggerService) {
return (ILoggerService) service;
}
return new Slf4jLoggerService(new NullLoggerService());
}
@Override
public final IMailService getMailService() {
IService service = getService(IServicesName.MAIL_SENDER_SERVICE);
if (service instanceof IMailService) {
return (IMailService) service;
}
return new NullMailService();
}
@Override
public final ICacheService getCacheService() {
IService service = getService(IServicesName.CACHE_SERVICE);
if (service instanceof ICacheService) {
return (ICacheService) service;
}
return new NullCacheService();
}
@Override
public final void buildService(AServiceBuilder aServiceBuilder) {
serviceMap.put(aServiceBuilder.getName(), aServiceBuilder.build());
}
@Override
public final void buildService(IService service) {
serviceMap.put(service.getName(), service);
}
@Override
public final void buildLoggerService(ALoggerServiceBuilder aLoggerServiceBuilder) {
buildService(aLoggerServiceBuilder);
}
@Override
public final void buildMailService(AMailServiceBuilder aMailServiceBuilder) {
buildService(aMailServiceBuilder);
}
@Override
public final void remove(String aServiceName) {
if (serviceMap.containsKey(aServiceName)){
serviceMap.get(aServiceName).stopService();
serviceMap.remove(aServiceName);
}
}
@Override
public boolean contains(String aServiceName) {
return serviceMap.containsKey(aServiceName);
}
@Override
public void removeAllServices() {
for (Entry entry : serviceMap.entrySet()) {
entry.getValue().stopService();
}
serviceMap.clear();
}
@Override
public Map getServiceMap() {
return serviceMap;
}
@Override
public void buildCacheService(ACacheServiceBuilder cacheServiceBuilder) {
buildService(cacheServiceBuilder);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy