Package com.astamuse.asta4d

Examples of com.astamuse.asta4d.Context


import com.astamuse.asta4d.Context;

public abstract class ParallelRowConvertor<S, T> implements RowConvertor<S, T> {

    public Future<T> invoke(ExecutorService es, final int rowIndex, final S data) {
        final Context context = Context.getCurrentThreadContext();
        return es.submit(new Callable<T>() {
            @Override
            public T call() throws Exception {
                return Context.with(context.clone(), new Callable<T>() {
                    @Override
                    public T call() throws Exception {
                        return convert(rowIndex, data);
                    }
                });
View Full Code Here


        }
        readySnippetCount = readySnippetCount - blockedSnippetCount;

        String renderDeclaration;
        Renderer renderer;
        Context context = Context.getCurrentThreadContext();
        Configuration conf = Configuration.getConfiguration();
        final SnippetInvoker invoker = conf.getSnippetInvoker();

        String refId;
        Element renderTarget;
        for (Element element : snippetList) {
            if (!conf.isSkipSnippetExecution()) {
                // for a faked snippet node which is created by template
                // analyzing process, the render target element should be its
                // child.
                if (element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE).equals(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE_FAKE)) {
                    renderTarget = element.children().first();
                } else {
                    renderTarget = element;
                }
                context.setCurrentRenderingElement(renderTarget);
                renderDeclaration = element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_RENDER);
                refId = element.attr(ExtNodeConstants.ATTR_SNIPPET_REF);
                try {
                    if (element.hasAttr(ExtNodeConstants.SNIPPET_NODE_ATTR_PARALLEL)) {
                        ConcurrentRenderHelper crHelper = ConcurrentRenderHelper.getInstance(context, doc);
                        final Context newContext = context.clone();
                        final String declaration = renderDeclaration;
                        crHelper.submitWithContext(newContext, declaration, refId, new Callable<Renderer>() {
                            @Override
                            public Renderer call() throws Exception {
                                return invoker.invoke(declaration);
View Full Code Here

        }
        return newList;
    }

    public final static <S, T> List<Future<T>> transformToFuture(final Iterable<S> sourceList, final ParallelRowConvertor<S, T> convertor) {
        final Context context = Context.getCurrentThreadContext();
        final Configuration conf = Configuration.getConfiguration();
        Boolean isInParallelConverting = context.getData(ParallelListConversionMark);

        if (isInParallelConverting != null) {// recursive converting
            switch (conf.getParallelRecursivePolicyForListRendering()) {
            case EXCEPTION:
                throw new RuntimeException(
                        "Parallel list converting is forbidden (by default) to avoid deadlock. You can change this policy by Configuration.setParallelRecursivePolicyForListRendering().");
            case CURRENT_THREAD:
                List<T> list = transform(sourceList, (RowConvertor<S, T>) convertor);
                return transform(list, new RowConvertor<T, Future<T>>() {
                    @Override
                    public Future<T> convert(int rowIndex, final T obj) {
                        return new Future<T>() {
                            @Override
                            public boolean cancel(boolean mayInterruptIfRunning) {
                                return false;
                            }

                            @Override
                            public boolean isCancelled() {
                                return false;
                            }

                            @Override
                            public boolean isDone() {
                                return true;
                            }

                            @Override
                            public T get() throws InterruptedException, ExecutionException {
                                return obj;
                            }

                            @Override
                            public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                                return obj;
                            }
                        };
                    }
                });
            case NEW_THREAD:
                ExecutorService executor = ParallelFallbackExecutor;
                List<Future<T>> futureList = new LinkedList<>();
                int index = 0;
                for (S obj : sourceList) {
                    futureList.add(convertor.invoke(executor, index, obj));
                    index++;
                }
                return futureList;
            default:
                return Collections.emptyList();
            }
        } else {// not in recursive converting
            Context newContext = context.clone();
            newContext.setData(ParallelListConversionMark, Boolean.TRUE);
            try {
                return Context.with(newContext, new Callable<List<Future<T>>>() {
                    @Override
                    public List<Future<T>> call() throws Exception {
                        ExecutorService executor = ListExecutorServiceUtil.getExecutorService();
View Full Code Here

        return "";
    }

    @Override
    public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Context context = Context.getCurrentThreadContext();
        for (Entry<String, ?> entry : model.entrySet()) {
            context.setData(entry.getKey(), entry.getValue());
        }
        // templateProvider.produce(response);
    }
View Full Code Here

    private ServletContext servletContext;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Context templateContext = Context.getCurrentThreadContext();
        if (templateContext == null) {
            templateContext = new WebApplicationContext();
            Context.setCurrentThreadContext(templateContext);
        }
        templateContext.init();
        WebApplicationContext webContext = (WebApplicationContext) templateContext;
        webContext.setRequest(request);
        webContext.setResponse(response);
        webContext.setServletContext(servletContext);
        return true;
View Full Code Here

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        Context templateContext = Context.getCurrentThreadContext();
        if (templateContext != null) {
            templateContext.clear();
        }
        super.afterCompletion(request, response, handler, ex);
    }
View Full Code Here

        }
        return map;
    }

    public Context clone() {
        Context newCtx = new WebApplicationContext();
        copyScopesTo(newCtx);
        return newCtx;
    }
View Full Code Here

     * @param request
     * @return ContentProvider
     * @throws Exception
     */
    private List<ContentProvider> handleRequest(UrlMappingRule currentRule) throws Exception {
        Context context = Context.getCurrentThreadContext();
        RequestHandlerInvokerFactory factory = WebApplicationConfiguration.getWebApplicationConfiguration()
                .getRequestHandlerInvokerFactory();
        RequestHandlerInvoker invoker = factory.getInvoker();

        List<ContentProvider> cpList = invoker.invoke(currentRule);
        context.setData(KEY_REQUEST_HANDLER_RESULT, cpList);

        return cpList;

    }
View Full Code Here

        }
        readySnippetCount = readySnippetCount - blockedSnippetCount;

        String renderDeclaration;
        Renderer renderer;
        Context context = Context.getCurrentThreadContext();
        Configuration conf = Configuration.getConfiguration();
        final SnippetInvoker invoker = conf.getSnippetInvoker();

        String refId;
        Element renderTarget;
        for (Element element : snippetList) {
            if (!conf.isSkipSnippetExecution()) {
                // for a faked snippet node which is created by template
                // analyzing process, the render target element should be its
                // child.
                if (element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE).equals(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE_FAKE)) {
                    renderTarget = element.children().first();
                } else {
                    renderTarget = element;
                }
                context.setCurrentRenderingElement(renderTarget);
                renderDeclaration = element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_RENDER);
                refId = element.attr(ExtNodeConstants.ATTR_SNIPPET_REF);
                if (element.hasAttr(ExtNodeConstants.SNIPPET_NODE_ATTR_PARALLEL)) {
                    ConcurrentRenderHelper crHelper = ConcurrentRenderHelper.getInstance(context, doc);
                    final Context newContext = context.clone();
                    final String declaration = renderDeclaration;
                    crHelper.submitWithContext(newContext, refId, new Callable<Renderer>() {
                        @Override
                        public Renderer call() throws Exception {
                            return invoker.invoke(declaration);
View Full Code Here

     * @return ContentProvider
     * @throws Exception
     */
    private List<ContentProvider<?>> handleRequest(UrlMappingRule currentRule) throws Exception {
        // TODO should we handle the exceptions?
        Context context = Context.getCurrentThreadContext();
        RequestHandlerInvokerFactory factory = WebApplicationConfiguration.getWebApplicationConfiguration()
                .getRequestHandlerInvokerFactory();
        RequestHandlerInvoker invoker = factory.getInvoker();

        Object requestHandlerResult;
        try {
            requestHandlerResult = invoker.invoke(currentRule);
        } catch (InvocationTargetException ex) {
            logger.error(currentRule.toString(), ex);
            requestHandlerResult = ex.getTargetException();
        } catch (Exception ex) {
            logger.error(currentRule.toString(), ex);
            requestHandlerResult = ex;
        }

        context.setData(KEY_REQUEST_HANDLER_RESULT, requestHandlerResult);

        List<ContentProvider<?>> cpList = new ArrayList<>();

        if (requestHandlerResult instanceof List) {
            List<?> resultList = (List<?>) requestHandlerResult;
View Full Code Here

TOP

Related Classes of com.astamuse.asta4d.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.