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

com.jianggujin.http.support.JResponseCreatorCache Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2018 jianggujin (www.jianggujin.com).
 * 
 * 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.jianggujin.http.support;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * 响应对象创建器缓存
 * 
 * @author jianggujin
 *
 */
public class JResponseCreatorCache {
    private volatile static Map, JResponseCreator> creators;
    static {
        ServiceLoader loader = ServiceLoader.load(JResponseCreator.class);
        if (loader != null) {
            Iterator iterator = loader.iterator();
            if (iterator.hasNext()) {
                creators = new HashMap, JResponseCreator>();
            }
            while (iterator.hasNext()) {
                JResponseCreator creator = iterator.next();
                creators.put(creator.getClass(), creator);
            }
        }
    }

    /**
     * 获得响应创建器
     * 
     * @param clazz
     * @return
     */
    public static JResponseCreator getCreator(Class clazz) {
        if (clazz == null || creators == null) {
            return null;
        }
        return creators.get(clazz);
    }

    /**
     * 注册响应创建器
     * 
     * @param creator
     */
    public synchronized static void register(JResponseCreator creator) {
        if (creator != null) {
            if (creators == null) {
                creators = new HashMap, JResponseCreator>();
            }
            creators.put(creator.getClass(), creator);
        }
    }

    /**
     * 清空
     */
    public synchronized static void clear() {
        if (creators != null) {
            creators.clear();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy