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

org.sysfoundry.kiln.base.ss.evt.Subscriber Maven / Gradle / Ivy

There is a newer version: 0.1.3
Show newest version
/*
 * Copyright 2019 Sysfoundry (www.sysfoundry.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.sysfoundry.kiln.base.ss.evt;

import com.google.common.eventbus.Subscribe;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.sysfoundry.kiln.base.evt.Event;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

@Slf4j
class Subscriber {

    private Object target;
    private SubscriberMeta meta;


    Subscriber(@NonNull Object targetObject,@NonNull SubscriberMeta meta){
        this.target = targetObject;
        this.meta = meta;
    }


    @Subscribe
    public void subscribeForEvent(Event event){

        log.trace("Received event {} ",event.getName());

        Optional> interestedEventsListOptional = meta.getInterestedEventsList();
        Optional targetMethodOptional = meta.getTargetMethod();

        log.trace("InterestedEventsListOptional {}",interestedEventsListOptional);

        interestedEventsListOptional.ifPresent(eventList->{
            log.trace("Event List {}",eventList);
            if(eventList.isEmpty() || eventList.contains(event.getName())){
                //now invoke the target method with the event information
                targetMethodOptional.ifPresent(method -> {
                    int paramCount = method.getParameterCount();
                    Object[] args = new Object[]{};
                    if(paramCount > 0){
                        //invoke with the event argument
                        args = new Object[]{event};
                    }
                    try {
                        method.invoke(target, args);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                });
            }
        });
    }


    public Object getTarget(){
        return this.target;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy