All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudimpl.outstack.spring.component.SpringService Maven / Gradle / Ivy

There is a newer version: 4.0.551
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cloudimpl.outstack.spring.component;

import com.cloudimpl.outstack.common.CloudMessage;
import com.cloudimpl.outstack.core.Inject;
import com.cloudimpl.outstack.core.Injector;
import com.cloudimpl.outstack.core.Named;
import com.cloudimpl.outstack.runtime.CommandHandler;
import com.cloudimpl.outstack.runtime.EntityCommandHandler;
import com.cloudimpl.outstack.runtime.EntityEventHandler;
import com.cloudimpl.outstack.runtime.EventRepositoryFactory;
import com.cloudimpl.outstack.runtime.ServiceProvider;
import com.cloudimpl.outstack.runtime.domainspec.RootEntity;
import com.cloudimpl.outstack.runtime.util.Util;
import java.util.HashSet;
import java.util.Set;
import org.reactivestreams.Publisher;
import com.cloudimpl.outstack.runtime.Handler;
import com.cloudimpl.outstack.runtime.domainspec.ChildEntity;
import com.cloudimpl.outstack.runtime.domainspec.Entity;
import static com.cloudimpl.outstack.spring.component.SpringQueryService.filterEntity;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import reactor.core.publisher.Mono;

/**
 *
 * @author nuwan
 * @param 
 */
public class SpringService implements Function{
    
    private static final Set>> HANDLERS = new HashSet<>();
    private static final Set> CMD_ENTITIES = new HashSet<>();
    private final ServiceProvider serviceProvider;
    
    @Inject
    @Named("RRHnd")
    private BiFunction  requestHandler;
    
    public SpringService(EventRepositoryFactory factory) {
        System.out.println("service :"+this.getClass().getName() + " factory : "+factory);
        Class root = Util.extractGenericParameter(this.getClass(), SpringService.class, 0);
        serviceProvider = new ServiceProvider<>(root,factory.createOrGetRepository(root),factory::createOrGetRepository,()->requestHandler,()->Cluster.autoWireInstance());
        HANDLERS.stream()
                .filter(h->SpringService.filter(root, h))
                .filter(h->EntityCommandHandler.class.isAssignableFrom(h))
                .forEach(e->serviceProvider.registerCommandHandler((Class) e));
        HANDLERS.stream()
                .filter(h->SpringService.filter(root, h))
                .filter(h->EntityEventHandler.class.isAssignableFrom(h))
                .forEach(e->serviceProvider.registerEventHandler((Class) e));
        
        CMD_ENTITIES.stream().filter(e -> SpringQueryService.filterEntity(root, e))
                .forEach(e -> serviceProvider.registerDefaultCmdHandlersForEntity(e));
    }
    
    public static void $(Class> handler)
    {
        HANDLERS.add(handler);
    }
    
     public static void $$(Class entityType) {
        CMD_ENTITIES.add(entityType);
    }
     
    @Override
    public Publisher apply(CloudMessage msg)
    {
        return serviceProvider.apply(msg.data());
    }
    
    public static boolean filter(Class rootType,Class> handlerType)
    {
        Class entityType = Util.extractGenericParameter(handlerType, handlerType.getSuperclass(), 0);
        Class root = RootEntity.isMyType(entityType)? entityType: Util.extractGenericParameter(entityType,ChildEntity.class, 0);
        return rootType == root;
    }
    
    public static Collection> queryEntities(Class rootType) {
        return CMD_ENTITIES.stream().filter(h -> filterEntity(rootType, h)).collect(Collectors.toList());
    }
    
    public static Collection>> handlers(Class rootType)
    {
        return HANDLERS.stream().filter(h->filter(rootType, h)).collect(Collectors.toList());
    }
   
    public static Collection> cmdEntities(Class rootType) {
        return CMD_ENTITIES.stream().filter(h -> filterEntity(rootType, h)).collect(Collectors.toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy