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

org.apache.batchee.extras.chain.Chain Maven / Gradle / Ivy

Go to download

This module implements readers/writers/... using no more than JavaEE APIs. The mani constraint is to not use more than JavaEE but not depend on it for features (= be usable in J2SE).

There is a newer version: 2.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.batchee.extras.chain;

import org.apache.batchee.doc.api.Documentation;
import org.apache.batchee.extras.locator.BeanLocator;

import javax.batch.api.BatchProperty;
import javax.inject.Inject;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collection;

public abstract class Chain {
    @Inject
    @BatchProperty
    @Documentation("The locator to use to create chain components")
    protected String locator;

    @Inject
    @BatchProperty
    @Documentation("The list of components of the chain")
    protected String chain;

    @Inject
    @BatchProperty
    @Documentation("The separator to use in chain string (default to ',')")
    protected String separator;

    @Inject
    @BatchProperty
    @Documentation("Should release phase of the lifecycle be called (default to false), use true for @Dependent beans.")
    protected String forceRelease;

    private Collection> runtimeChain = new ArrayList>();

    public Object runChain(final Object item) throws Exception {
        if (chain == null) {
            return item;
        }
        if (separator == null) {
            separator = ",";
        }

        if (runtimeChain.isEmpty()) {
            final BeanLocator beanLocator = getBeanLocator();
            final Class type = findType();
            for (final String next : chain.split(separator)) {
                runtimeChain.add(beanLocator.newInstance(type, next));
            }
        }

        Object current = item;
        for (final BeanLocator.LocatorInstance next : runtimeChain) {
            current = invoke(next, current);
        }

        if (Boolean.parseBoolean(forceRelease)) {
            for (final BeanLocator.LocatorInstance next : runtimeChain) {
                next.release();
            }
            runtimeChain.clear();
        }

        return current;
    }

    protected BeanLocator getBeanLocator() {
        return BeanLocator.Finder.get(locator);
    }

    protected Class findType() {
        return Class.class.cast(ParameterizedType.class.cast(getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
    }

    protected abstract Object invoke(BeanLocator.LocatorInstance next, Object current)  throws Exception;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy