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

net.hasor.dataql.Finder Maven / Gradle / Ivy

/*
 * Copyright 2008-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 net.hasor.dataql;
import net.hasor.core.AppContext;
import net.hasor.core.Provider;
import net.hasor.core.TypeSupplier;
import net.hasor.utils.ExceptionUtils;
import net.hasor.utils.ResourcesUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 资源加载器
 * @author 赵永春 ([email protected])
 * @version : 2019-12-11
 */
public interface Finder {
    /** 默认实现 */
    public static final Finder                                 DEFAULT              = new Finder() {
    };
    /** 通过 TypeSupplier 委托 findBean 的类型创建 */
    public static final Function         TYPE_SUPPLIER        = typeSupplier -> {
        return new Finder() {
            public Object findBean(Class beanType) {
                return typeSupplier.get(beanType);
            }
        };
    };
    /** 与 ofAppContext 类似不同的是允许 延迟生产 AppContext */
    public static final Function, Finder> APP_CONTEXT_SUPPLIER = appContextSupplier -> {
        Supplier single = Provider.of(appContextSupplier).asSingle();
        return new Finder() {
            public Object findBean(Class beanType) {
                return single.get().getInstance(beanType);
            }
        };
    };
    /** 通过 AppContext 委托 findBean 的类型创建 */
    public static final Function           APP_CONTEXT          = appContext -> {
        return APP_CONTEXT_SUPPLIER.apply(Provider.of(appContext));
    };

    /** 负责处理 import @"/net/hasor/demo.ql" as demo;方式中 ‘/net/hasor/demo.ql’ 资源的加载 */
    public default InputStream findResource(String resourceName) throws IOException {
        // .加载资源
        InputStream inputStream = null;
        try {
            inputStream = ResourcesUtils.getResourceAsStream(resourceName);
        } catch (Exception e) {
            throw ExceptionUtils.toRuntimeException(e, throwable -> new RuntimeException("import compiler failed -> '" + resourceName + "' not found.", throwable));
        }
        return inputStream;
    }

    /** 负责处理 import 'net.hasor.dataql.sdk.CollectionUdfSource' as collect;方式的资源的加载。 */
    public default Object findBean(Class beanType) {
        try {
            return beanType.newInstance();
        } catch (Exception e) {
            throw ExceptionUtils.toRuntimeException(e, throwable -> new RuntimeException("load Bean failed -> '" + beanType.getName(), throwable));
        }
    }

    public default FragmentProcess findFragmentProcess(String fragmentType) {
        throw new RuntimeException(fragmentType + " fragment undefine.");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy