Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- public class ServiceFactory {
- @Autowired
- private List<PlatformService> services;
- private static final Map<Class<? extends PlatformService>, PlatformService> singletonCache = new HashMap<>();
- private static final Map<String, PlatformService> instanceCache = new HashMap<>();
- @PostConstruct
- public void initializeSingletonCache() {
- services.forEach(service -> {
- singletonCache.put(service.getClass(), service);
- service.initialize();
- });
- }
- public static <T extends PlatformService> T registerInstanceService(String serviceName, Class<T> service) throws RuntimeException, InstantiationException, IllegalAccessException {
- if (instanceServices.containsKey(serviceName)) {
- throw new RuntimeException("An instance with the name " + serviceName + " has already been created");
- }
- T serviceObject = service.newInstance();
- instanceServices.put(serviceName, serviceObject);
- return serviceObject;
- }
- public static <T extends PlatformService> void registerInstanceService(String serviceName, T service) throws Exception {
- service.initialize();
- instanceServices.put(serviceName, service);
- }
- public static <T extends PlatformService> T getService(Class<T> service) {
- return service.cast(singletonServices.get(service));
- }
- public static Map<Class<? extends PlatformService>, PlatformService> getSingletonServices() {
- return singletonServices;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement