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

com.github.siwenyan.si.SiContextMonitor Maven / Gradle / Ivy

There is a newer version: 1.25.1.0
Show newest version
package com.github.siwenyan.si;

import com.github.siwenyan.common.EasyJson;
import com.github.siwenyan.common.Reporter;
import com.github.siwenyan.common.Sys;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class SiContextMonitor {

    protected final Map>> specsByEventName = new HashMap>>();

    public SiContextMonitor(List> specs) {
        for (Map spec : specs) {
            String eventName = spec.get("Event");
            if (!this.specsByEventName.containsKey(eventName)) {
                this.specsByEventName.put(eventName, new LinkedList>());
            }
            this.specsByEventName.get(eventName).add(spec);
        }
    }

    public static SiContextMonitor buildMonitor(List> monitorModel, Map handlers) {
        try {
            SiContextMonitor siContextMonitor = null;
            if (null == monitorModel) {
                return null;
            } else {
                if (null == handlers) {
                    return new SiContextMonitor(monitorModel);
                } else {
                    return new SiContextMonitor(monitorModel) {

                        @Override
                        protected ISiEventHandler getHandler(String handlerName) {
                            ISiEventHandler handler = super.getHandler(handlerName);
                            for (String name : handlers.keySet()) {
                                if (name.equals(handlerName)) {
                                    return handler;
                                }
                            }
                            return super.getHandler(handlerName);
                        }

                    };
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Unable to create monitor " + monitorModel.toString() + "\r\n" + e.getMessage());
        }
    }

    public static SiContextMonitor buildMonitor(String jsonStringOrJsonFilePath, Map handlers) {
        String jsonString = null;
        try {
            File jsonFile = Sys.findFile(jsonStringOrJsonFilePath);
            jsonString = FileUtils.readFileToString(jsonFile);
        } catch (Exception e) {
            jsonString = jsonStringOrJsonFilePath;
        }

        try {
            List> jsonMonitor = EasyJson.readMaps(jsonString);
            return buildMonitor(jsonMonitor, handlers);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected ISiEventHandler getHandler(String handlerName) {
        if ("Side".equals(handlerName)) {
            return new SiEventHandlerSide();
        } else if ("AssertEquals".equals(handlerName)) {
            return new SiEventHandlerAssertEquals();
        } else {
            return null;
        }
    }

    public void dispatch(SiContextMonitorEvent event) throws UnhandledEventException {
        Reporter reporter = event.getSiTest().getSiContext().getReporter();
        String eventName = event.getName();
        if (this.specsByEventName.containsKey(eventName)) {
            for (Map spec : this.specsByEventName.get(eventName)) {
                String handlerName = spec.get("Handler");
                ISiEventHandler handler = this.getHandler(handlerName);
                if (null == handler) {
                    try {
                        handler = (ISiEventHandler) Class.forName(handlerName).newInstance();
                    } catch (Exception e) {
                        String msg = "Unable to handle event: " + spec.toString();
                        msg += "\r\n" + e.getClass();
                        msg += "\r\n" + e.getMessage();

                        reporter.reportWarning(msg);
                        throw new UnhandledEventException(msg);
                    }
                }
                try {
                    reporter.start("handler", "name", handlerName);
                    handler.handle(event, spec);
                } finally {
                    reporter.end("handler");
                }
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy