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

eu.fusepool.p3.transformer.LongRunningTransformerWrapper Maven / Gradle / Ivy

/*
 * Copyright 2014 reto.
 *
 * 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 eu.fusepool.p3.transformer;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.activation.MimeType;

/**
 * Wraps a SyncTransformer as an AsyncTransormer. The intended usage 
 * is for long running transformers.
 * @author reto
 */
public class LongRunningTransformerWrapper implements AsyncTransformer {
    private CallBackHandler callBackHandler;
    private final SyncTransformer wrapped;
    private final Set activeRequests = Collections.synchronizedSet(new HashSet());

    public LongRunningTransformerWrapper(SyncTransformer wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public void activate(CallBackHandler callBackHandler) {
        this.callBackHandler = callBackHandler;
    }

    @Override
    public void transform(final HttpRequestEntity requestEntity, final String requestId) throws IOException {
        activeRequests.add(requestId);
        final PreReadEntity preReadEntity = new PreReadEntity(requestEntity);
        (new Thread() {

            @Override
            public void run() {
                try {
                    callBackHandler.responseAvailable(requestId, wrapped.transform(preReadEntity));
                } catch (Exception ex) {
                    callBackHandler.reportException(requestId, ex);
                } 
                activeRequests.remove(requestId);
            }
            
        }).start();
        
    }

    @Override
    public boolean isActive(String requestId) {
        return activeRequests.contains(requestId);
    }

    @Override
    public Set getSupportedInputFormats() {
        return wrapped.getSupportedInputFormats();
    }

    @Override
    public Set getSupportedOutputFormats() {
        return wrapped.getSupportedOutputFormats();
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy