Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
AnalyzerBeans Copyright (C) 2014 Neopost - Customer Information Management This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 
 package org.eobjects.analyzer.cluster.http;
 
 import java.util.Map;
 
 
Helper method for handling servlet requests and responses according to the requests sent by the master node if it is using a HttpClusterManager.
 
 public class SlaveServletHelper {
 
     private static final Logger logger = LoggerFactory.getLogger(SlaveServletHelper.class);
 
     private final AnalyzerBeansConfiguration _configuration;
     private final SlaveJobInterceptor _jobInterceptor;
     private final ConcurrentMap<StringAnalysisResultFuture_runningJobs;

    
 
     @Deprecated
     public SlaveServletHelper(AnalyzerBeansConfiguration configuration) {
         this(configurationnew ConcurrentHashMap<StringAnalysisResultFuture>());
     }

    
Creates a SlaveServletHelper.

Parameters:
configuration
runningJobsMap a map to be used for internal book-keeping of running jobs. This parameter is required so that multiple SlaveServletHelpers can share the same running jobs state.
 
     public SlaveServletHelper(AnalyzerBeansConfiguration configuration,
             ConcurrentMap<StringAnalysisResultFuturerunningJobsMap) {
         this(configurationnullrunningJobsMap);
     }

    
 
     @Deprecated
     public SlaveServletHelper(AnalyzerBeansConfiguration configurationSlaveJobInterceptor jobInterceptor) {
        this(configurationjobInterceptornew ConcurrentHashMap<StringAnalysisResultFuture>());
    }

    
Creates a SlaveServletHelper.

Parameters:
configuration the slave's org.eobjects.analyzer.configuration.AnalyzerBeansConfiguration.
jobInterceptor an optional interceptor
runningJobsMap a map to be used for internal book-keeping of running jobs. This parameter is required so that multiple SlaveServletHelpers can share the same running jobs state.
    public SlaveServletHelper(AnalyzerBeansConfiguration configurationSlaveJobInterceptor jobInterceptor,
            ConcurrentMap<StringAnalysisResultFuturerunningJobsMap) {
        if (configuration == null) {
            throw new IllegalArgumentException("AnalyzerBeansConfiguration cannot be null");
        }
         = configuration;
         = jobInterceptor;
         = runningJobsMap;
    }
    
    
    public void handleRequest(final HttpServletRequest requestfinal HttpServletResponse responsethrows IOException {
        handleRequest(requestresponsenew AnalysisListener[0]);
    }

    
Completely handles a HTTP request and response. This method is functionally equivalent of calling these methods in sequence: readJob(javax.servlet.http.HttpServletRequest) serializeResult(org.eobjects.analyzer.job.runner.AnalysisResultFuture,java.lang.String) sendResponse(javax.servlet.http.HttpServletResponse,java.io.Serializable)

Parameters:
request
response
analysisListeners
Throws:
java.io.IOException
    public void handleRequest(final HttpServletRequest requestfinal HttpServletResponse responsefinal AnalysisListener ... analysisListenersthrows IOException {
        final String jobId = request.getParameter(.);
        final String action = request.getParameter(.);
        if (..equals(action)) {
            .info("Handling 'cancel' request: {}"jobId);
            cancelJob(jobId);
            return;
        }
        if (..equals(action)) {
            .info("Handling 'run' request: {}"jobId);
            final AnalysisJob job;
            try {
                job = readJob(request);
            } catch (IOException e) {
                .error("Failed to read job definition from HTTP request"e);
                throw e;
            }
            final Serializable resultObject;
            try {
                final AnalysisResultFuture resultFuture = runJob(jobjobIdanalysisListeners);
                resultObject = serializeResult(resultFuturejobId);
            } catch (RuntimeException e) {
                .error("Unexpected error occurred while running slave job"e);
                throw e;
            }
            try {
                sendResponse(responseresultObject);
            } catch (IOException e) {
                .error("Failed to send job result through HTTP response"e);
                throw e;
            }
            return;
        }
        .warn("Unspecified action request: {}"jobId);
    }

    

Deprecated:
use serializeResult(org.eobjects.analyzer.job.runner.AnalysisResultFuture,java.lang.String) instead.
Parameters:
resultFuture
Returns:
    @Deprecated
    public Serializable serializeResult(AnalysisResultFuture resultFuture) {
        return serializeResult(resultFuturenull);
    }
    public Serializable serializeResult(AnalysisResultFuture resultFutureString slaveJobId) {
        try {
            // wait for result to be ready
            resultFuture.await();
            final Serializable resultObject;
            if (resultFuture.isSuccessful()) {
                resultObject = new SimpleAnalysisResult(resultFuture.getResultMap());
            } else {
                resultObject = new ArrayList<Throwable>(resultFuture.getErrors());
            }
            return resultObject;
        } finally {
            if (slaveJobId != null) {
                .remove(slaveJobId);
            }
        }
    }
    public AnalysisJob readJob(HttpServletRequest requestthrows IOException {
        final JaxbJobReader reader = new JaxbJobReader();
        final String jobDefinition = request.getParameter(.);
        final InputStream inputStream;
        if (jobDefinition == null) {
            // backwards compatibility node
            inputStream = request.getInputStream();
        } else {
            inputStream = new ByteArrayInputStream(jobDefinition.getBytes());
        }
        try {
            final AnalysisJobBuilder jobBuilder = reader.create(inputStream);
            if ( != null) {
                .intercept(jobBuilder);
            }
            final AnalysisJob job = jobBuilder.toAnalysisJob();
            return job;
        } finally {
            FileHelper.safeClose(inputStream);
        }
    }

    
Runs a slave job

Deprecated:
use runJob(org.eobjects.analyzer.job.AnalysisJob,java.lang.String) instead
Parameters:
job
Returns:
    @Deprecated
    public AnalysisResultFuture runJob(AnalysisJob job) {
        return runJob(jobnull);
    }

    
Runs a slave job

Parameters:
job
slaveJobId a unique ID for the slave job.
Returns:
    public AnalysisResultFuture runJob(AnalysisJob jobString slaveJobId) {
        return runJob(jobslaveJobIdnew AnalysisListener[0]);
    }

    
Runs a slave job

Parameters:
job
slaveJobId
analysisListeners
Returns:
    public AnalysisResultFuture runJob(AnalysisJob jobString slaveJobIdAnalysisListener... analysisListeners) {
        final AnalysisRunner runner = new SlaveAnalysisRunner(analysisListeners);
        final AnalysisResultFuture resultFuture = runner.run(job);
        if (slaveJobId != null) {
            .put(slaveJobIdresultFuture);
        }
        return resultFuture;
    }

    
Cancels a slave job, referred by it's id.

Parameters:
slaveJobId
Returns:
whether or not the job was (found and) cancelled.
    public boolean cancelJob(String slaveJobId) {
        final AnalysisResultFuture resultFuture = .remove(slaveJobId);
        if (resultFuture != null) {
            resultFuture.cancel();
            return true;
        }
        return false;
    }
    public void sendResponse(HttpServletResponse responseSerializable objectthrows IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        try {
            SerializationUtils.serialize(objectoutputStream);
        } finally {
            outputStream.flush();
        }
    }
New to GrepCode? Check out our FAQ X