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

com.github.yoojia.halo.actions.HaloAction Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.github.yoojia.halo.actions;

import com.github.yoojia.halo.HaloChain;
import com.github.yoojia.halo.HaloRequest;
import com.github.yoojia.halo.HaloResponse;
import com.github.yoojia.halo.supports.*;
import com.github.yoojia.halo.utils.SystemTime;

/**
 * @author YOOJIA.CHEN ([email protected])
 */
public final class HaloAction extends HaloModule {

    private final static int DEFAULT_PRIORITY = 0;

    private final ActionManager mManager = new ActionManager();

    private boolean mForwardEnabled;

    @Override
    public void init(Context context, Config config) {
        mLogger.debug("Init...");
        mForwardEnabled = config == null ? false : config.getValue("defaultForward", false);
        mLogger.trace("Using default forward: {}", mForwardEnabled);
        final long start = System.nanoTime();
        context.getClassScanner().walkClasses(new FilterScanner<>(Action.class, Handle.class, mManager));
        SystemTime.log(start, "HaloAction.init");
    }

    @Override
    public void onService(HaloRequest request, HaloResponse response, KernelChain kernelChain) throws Exception {
        final long findStart = System.nanoTime();
        final ActionProcessor processor = mManager.findMatched(request.method, request.resources);
        if(processor != null){
            SystemTime.log(findStart, String.format("HALOAction.find(:%s)", request.uri));
            final long start = System.nanoTime();
            try{
                final HttpChain chain = new HttpChain(mForwardEnabled);
                request.putAllParams(processor.parseParams(request.resources));
                processor.perform(request, response, chain);
                if( ! chain.isForwardEnabled()) {
                    return;
                }
            } finally {
                SystemTime.log(start, String.format("HaloAction.processed(:%s)", request.uri));
            }
        }
        kernelChain.onService(request, response, kernelChain);
    }

    @Override
    public void onStartup(Context context) {
        mManager.rebuild();
    }

    @Override
    public void onShutdown(Context context) {
        mManager.clear();
    }

    private class HttpChain extends HaloChain {

        public HttpChain(boolean defaultNextDisabled) {
            super(defaultNextDisabled);
        }

        @Override
        public boolean isForwardEnabled() {
            return super.isForwardEnabled();
        }
    }

    @Override
    public int getPriority() {
        return DEFAULT_PRIORITY;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy