com.sinosoft.one.mvc.web.impl.mapping.EngineGroupImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sinosoft.one.mvc.web.impl.mapping.EngineGroupImpl.java

Source

/*
 * Copyright 2007-2009 the original author or authors.
 *
 * 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 com.sinosoft.one.mvc.web.impl.mapping;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.sinosoft.one.mvc.adapter.ArraysEx;
import com.sinosoft.one.mvc.web.annotation.ReqMethod;
import com.sinosoft.one.mvc.web.impl.thread.Engine;
import com.sinosoft.one.mvc.web.impl.thread.LinkedEngine;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * 
 *
 * 
 */
public class EngineGroupImpl implements EngineGroup {

    private static final Log logger = LogFactory.getLog(EngineGroup.class);

    /** ARRAY_SIZE  Engine ? */
    private static final int ARRAY_SIZE = ReqMethod.ALL.parse().size();

    private static final LinkedEngine[] emptyEngines = new LinkedEngine[0];

    /**
     * ?????????0engines
     * <p>
     * ?http method??? {@link ReqMethod#ordinal()}
     * 
     */
    private final LinkedEngine[][] engines;

    private int engineCount;

    private transient String toStringCache;

    private transient List<ReqMethod> allowedMethodsCache;

    //-----------------------------------

    public EngineGroupImpl() {
        LinkedEngine[][] engines = new LinkedEngine[ARRAY_SIZE][];
        Arrays.fill(engines, emptyEngines);
        this.engines = engines;
    }

    /**
     *  {@link Engine}  method  {@link ReqMethod#ALL}
     * 
     * @param method
     * @param engine
     */
    public void addEngine(ReqMethod method, LinkedEngine engine) {
        for (ReqMethod md : method.parse()) {
            LinkedEngine[] methodEngines = engines[md.ordinal()];
            if (methodEngines.length == 0) {
                methodEngines = new LinkedEngine[] { engine };
            } else {
                methodEngines = ArraysEx.copyOf(methodEngines, methodEngines.length + 1);
                methodEngines[methodEngines.length - 1] = engine;
            }
            engines[md.ordinal()] = methodEngines;
            engineCount++;
        }
        clearCache();
    }

    public int size() {
        return engineCount;
    }

    /**
     * ???????0
     * 
     * @param method  {@link ReqMethod#ALL}  {@link ReqMethod}
     *        ?null
     * @return
     */
    public LinkedEngine[] getEngines(ReqMethod method) {
        if (method == null) {
            return emptyEngines;
        }
        if (method == ReqMethod.ALL) {
            throw new IllegalArgumentException("method");
        }
        return engines[method.ordinal()];
    }

    /**
     * ?????
     * 
     * @param method
     * @return
     */
    public boolean isMethodAllowed(ReqMethod method) {
        return method != null && engines[method.ordinal()].length > 0;
    }

    public List<ReqMethod> getAllowedMethods() {
        if (allowedMethodsCache == null) {
            List<ReqMethod> allowedMethods = new ArrayList<ReqMethod>();
            for (ReqMethod method : ReqMethod.ALL.parse()) {
                Engine[] methodEngines = this.engines[method.ordinal()];
                if (methodEngines.length > 0) {
                    allowedMethods.add(method);
                }
            }
            allowedMethodsCache = Collections.unmodifiableList(allowedMethods);
        }
        return allowedMethodsCache;
    }

    private boolean destroyed = false;

    /**
     * ??
     */
    public void destroy() {
        if (!destroyed) {
            return;
        }
        destroyed = true;
        for (Engine[] methodEngines : engines) {
            for (Engine engine : methodEngines) {
                try {
                    engine.destroy();
                } catch (Throwable e) {
                    logger.error("", e);
                }
            }
        }
    }

    @Override
    public String toString() {
        if (this.toStringCache == null) {
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            int oriLen = sb.length();
            for (ReqMethod method : getAllowedMethods()) {
                sb.append(method.toString()).append(", ");
            }
            if (sb.length() > oriLen) {
                sb.setLength(sb.length() - 2);
            }
            sb.append("]");

            this.toStringCache = sb.toString();
        }
        return this.toStringCache;
    }

    private void clearCache() {
        allowedMethodsCache = null;
        toStringCache = null;
    }

}