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

jetbrick.web.mvc.action.ArgumentGetterResolver Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/**
 * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved.
 *
 * Email: [email protected]
 * URL: http://subchen.github.io/
 *
 * 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 jetbrick.web.mvc.action;

import java.lang.annotation.Annotation;
import java.util.IdentityHashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.*;
import jetbrick.ioc.Ioc;
import jetbrick.ioc.annotations.*;
import jetbrick.lang.ExceptionUtils;
import jetbrick.lang.Validate;
import jetbrick.web.mvc.Model;
import jetbrick.web.mvc.RequestContext;
import jetbrick.web.mvc.action.annotations.*;
import jetbrick.web.mvc.multipart.FilePart;
import jetbrick.web.servlet.map.*;

/**
 * 全局 ArgumentGetter 管理器.
 */
public final class ArgumentGetterResolver {
    private final Map, TypedArgumentGetter> typedMaps = new IdentityHashMap, TypedArgumentGetter>();
    private final Map, Class>> annotatedMaps = new IdentityHashMap, Class>>();

    @Inject
    private Ioc ioc;

    @IocInit
    public void initialize() {
        // typed
        register(RequestContext.class, RequestContextArgumentGetter.class);
        register(Model.class, ModelArgumentGetter.class);
        register(HttpServletRequest.class, HttpServletRequestArgumentGetter.class);
        register(HttpServletResponse.class, HttpServletResponseArgumentGetter.class);
        register(HttpSession.class, HttpSessionArgumentGetter.class);
        register(ServletContext.class, ServletContextArgumentGetter.class);
        register(FilePart.class, FilePartArgumentGetter.class);
        register(FilePart[].class, FilePartsArgumentGetter.class);
        register(RequestAttributeMap.class, RequestAttributeMapArgumentGetter.class);
        register(SessionAttributeMap.class, SessionAttributeMapArgumentGetter.class);
        register(ServletContextAttributeMap.class, ServletContextAttributeMapArgumentGetter.class);
        register(RequestParameterMap.class, RequestParameterMapArgumentGetter.class);
        register(RequestParameterValuesMap.class, RequestParameterValuesMapArgumentGetter.class);
        register(RequestHeaderMap.class, RequestHeaderMapArgumentGetter.class);
        register(RequestHeaderValuesMap.class, RequestHeaderValuesMapArgumentGetter.class);
        register(RequestCookieMap.class, RequestCookieMapArgumentGetter.class);
        register(ServletContextInitParameterMap.class, ServletContextInitParameterMapArgumentGetter.class);

        // annotated
        register(PathVariable.class, PathVariableArgumentGetter.class);
        register(RequestParam.class, RequestParamArgumentGetter.class);
        register(RequestForm.class, RequestFormArgumentGetter.class);
        register(RequestHeader.class, RequestHeaderArgumentGetter.class);
        register(RequestCookie.class, RequestCookieArgumentGetter.class);
        register(RequestAttribute.class, RequestAttributeArgumentGetter.class);
        register(SessionAttribute.class, SessionAttributeArgumentGetter.class);
        register(ServletContextAttribute.class, ServletContextAttributeArgumentGetter.class);
        register(InitParameter.class, InitParameterArgumentGetter.class);
    }

    @SuppressWarnings("unchecked")
    public void register(Class type, Class argumentGetterClass) {
        if (TypedArgumentGetter.class.isAssignableFrom(argumentGetterClass)) {
            // singleton
            try {
                TypedArgumentGetter getter = (TypedArgumentGetter) ioc.injectClass(argumentGetterClass);
                typedMaps.put(type, getter);
            } catch (Exception e) {
                throw ExceptionUtils.unchecked(e);
            }
        } else if (AnnotatedArgumentGetter.class.isAssignableFrom(argumentGetterClass)) {
            annotatedMaps.put(type, (Class>) argumentGetterClass);
        } else {
            throw new IllegalStateException("Invalid class " + argumentGetterClass);
        }
    }

    @SuppressWarnings("unchecked")
    public  TypedArgumentGetter lookup(Class type) {
        return (TypedArgumentGetter) typedMaps.get(type);
    }

    @SuppressWarnings("rawtypes")
    public  Class lookup(A annotation) {
        Validate.notNull(annotation);

        // Special code
        if (annotation.annotationType().isAnnotationPresent(InjectParameterWith.class)) {
            return IocBeanArgumentGetter.class;
        }
        return annotatedMaps.get(annotation.annotationType());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy