public FreeMarkerServiceResponse call()

in src/main/java/org/apache/freemarker/onlinetester/services/FreeMarkerService.java [323:404]


        public FreeMarkerServiceResponse call() throws Exception {
            try {
                Template template;
                try {
                    TemplateConfiguration tCfg = new TemplateConfiguration();
                    tCfg.setParentConfiguration(freeMarkerConfig);
                    
                    if (args.outputFormat != null) {
                        tCfg.setOutputFormat(args.outputFormat);
                    }
                    if (args.locale != null) {
                        tCfg.setLocale(args.locale);
                    }
                    if (args.timeZone != null) {
                        tCfg.setTimeZone(args.timeZone);
                    }
                    if (args.tagSyntax != null) {
                    	tCfg.setTagSyntax(args.tagSyntax);
                    }
                    if (args.interpolationSyntax != null) {
                    	tCfg.setInterpolationSyntax(args.interpolationSyntax);
                    }
                    
                    template = new Template(null, null,
                            new StringReader(args.templateSourceCode), freeMarkerConfig, tCfg, null);
                    tCfg.apply(template);
                } catch (ParseException e) {
                    // Expected (part of normal operation)
                    return createFailureResponse(e);
                } catch (Exception e) {
                    // Not expected
                    throw new FreeMarkerServiceException("Unexpected exception during template parsing", e);
                }
                
                FreeMarkerInternalsAccessor.makeTemplateInterruptable(template);
                
                boolean resultTruncated;
                StringWriter writer = new StringWriter();
                try {
                    synchronized (this) {
                        templateExecutorThread = Thread.currentThread(); 
                        templateExecutionStarted = true;
                        notifyAll();
                    }
                    try {
                        template.process(args.dataModel, new LengthLimitedWriter(writer, maxOutputLength));
                    } finally {
                        synchronized (this) {
                            templateExecutorThread = null;
                            FreeMarkerInternalsAccessor.clearAnyPendingTemplateProcessingInterruption();
                        }
                    }
                    resultTruncated = false;
                } catch (LengthLimitExceededException e) {
                    // Not really an error, we just cut the output here.
                    resultTruncated = true;
                    writer.write(new MessageFormat(MAX_OUTPUT_LENGTH_EXCEEDED_TERMINATION, AllowedSettingValues.DEFAULT_LOCALE)
                            .format(new Object[] { maxOutputLength }));
                    // Falls through
                } catch (TemplateException e) {
                    // Expected (part of normal operation)
                    return createFailureResponse(e);
                } catch (Throwable e) {
                    if (FreeMarkerInternalsAccessor.isTemplateProcessingInterruptedException(e)
                    		|| e instanceof ThreadDeath /* due to Thread.stop() */) {
                        return new FreeMarkerServiceResponse.Builder().buildForFailure(new TimeoutException(
                                "Template processing was aborted for exceeding the " + getMaxTemplateExecutionTime()
                                + " ms time limit set for this online service. This is usually because you have "
                                + "a very long running #list (or other kind of loop) in your template.")); 
                    }
                    // Not expected
                    throw new FreeMarkerServiceException("Unexpected exception during template evaluation", e);
                }
                
                return new FreeMarkerServiceResponse.Builder().buildForSuccess(writer.toString(), resultTruncated);
            } finally {
                synchronized (this) {
                    taskEnded = true;
                    notifyAll();
                }
            }
        }