Package com.astamuse.asta4d

Examples of com.astamuse.asta4d.Context


    private static void transferDataHolder(ContextDataHolder from, ContextDataHolder to) {
        to.setData(from.getName(), from.getScope(), from.getFoundOriginalData(), from.getValue());
    }

    private static ContextDataHolder findValueForTarget(TargetInfo targetInfo, Class overrideSearchType) throws DataOperationException {
        Context context = Context.getCurrentThreadContext();
        ContextDataFinder dataFinder = Configuration.getConfiguration().getContextDataFinder();

        Class searchType = overrideSearchType == null ? targetInfo.type : overrideSearchType;
        ContextDataHolder valueHolder = dataFinder.findDataInContext(context, targetInfo.scope, targetInfo.name, searchType);
        if (valueHolder == null && targetInfo.contextDataSetFactory != null) {
            Object value;
            if (targetInfo.isContextDataSetSingletonInContext) {
                // this map was initialized when the context was initialized
                HashMap<String, Object> cdSetSingletonMap = context.getData(ContextDataSetSingletonMapKey);
                // we must synchronize it to avoid concurrent access on the map
                synchronized (cdSetSingletonMap) {
                    String clsName = targetInfo.type.getName();
                    value = cdSetSingletonMap.get(clsName);
                    if (value == null) {
View Full Code Here


     * @param instance
     * @throws DataOperationException
     */
    public final static void setContextDataFromInstance(Object instance) throws DataOperationException {
        try {
            Context context = Context.getCurrentThreadContext();
            InstanceWireTarget target = getInstanceTarget(instance);
            Object value;
            for (FieldInfo fi : target.getFieldList) {
                value = FieldUtils.readField(fi.field, instance, true);
                context.setData(fi.scope, fi.name, value);
            }

            for (MethodInfo mi : target.getMethodList) {
                value = mi.method.invoke(instance);
                context.setData(mi.scope, mi.name, value);
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
            String msg = String.format("Exception when inject value from instance of [%s] to Context.", instance.getClass().toString());
            throw new DataOperationException(msg, e);
        }
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

        if (locale != null || LocaleUtils.isAvailableLocale(locale)) {
            return factory.retrieveResourceBundle(resourceName, locale);
        }

        Context context = Context.getCurrentThreadContext();
        Locale currentLocale = context.getCurrentLocale();
        if (currentLocale != null || LocaleUtils.isAvailableLocale(currentLocale)) {
            return factory.retrieveResourceBundle(resourceName, currentLocale);
        } else {
            return factory.retrieveResourceBundle(resourceName, Locale.getDefault());
        }
View Full Code Here

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().clone();
        return es.submit(new Callable<T>() {
            @Override
            public T call() throws Exception {
                return Context.with(context, new Callable<T>() {
                    @Override
View Full Code Here

    @Override
    public boolean beforeProcess(SnippetExecutionHolder execution) throws Exception {

        try {
            Context context = Context.getCurrentThreadContext();
            Object lastSnippetInstance = context.getData(InstanceCacheKey);
            // it means that we need an instance injection and a reverse
            // injection
            if (lastSnippetInstance != execution.getInstance()) {
                if (lastSnippetInstance != null) {
                    InjectUtil.setContextDataFromInstance(lastSnippetInstance);
                }
                InjectUtil.injectToInstance(execution.getInstance());
                context.setData(InstanceCacheKey, execution.getInstance());
            }

            Object[] params = InjectUtil.getMethodInjectParams(execution.getMethod());
            execution.setParams(params);
            return true;
View Full Code Here

    private final static String InstanceListCacheKey = SnippetInitializeInterceptor.class + "##InstanceListCacheKey##";

    @Override
    public boolean beforeProcess(SnippetExecutionHolder execution) throws Exception {

        Context context = Context.getCurrentThreadContext();
        InitializedListHolder listHolder = context.getData(InstanceListCacheKey);
        if (listHolder == null) {
            listHolder = new InitializedListHolder();
            context.setData(InstanceListCacheKey, listHolder);
        }

        Object target = execution.getInstance();
        boolean inialized = false;
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();
                    // the hosting element of this faked snippet has been removed by outer a snippet
                    if (renderTarget == null) {
                        element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS, ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS_FINISHED);
                        continue;
                    }
                } 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 null;
    }

    private Map<String, Object> getCacheMap(String key) {
        Context context = Context.getCurrentThreadContext();
        Map<String, Object> map = context.getData(key);
        if (map == null) {
            map = new HashMap<>();
            context.setData(key, map);
        }
        return map;
    }
View Full Code Here

    private static Template NotFoundHolder;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private Map<String, Template> getCachedTemplateMap() {
        Context context = Context.getCurrentThreadContext();
        Configuration conf = Configuration.getConfiguration();
        if (conf.isCacheEnable()) {
            return defaultCachedTemplateMap;
        } else {
            // for debug, if we do not cache it in context, some templates will
            // be probably initialized for hundreds times
            // thus there will be hundreds logs of template initializing in info
            // level.
            String key = TemplateResolver.class.getName() + "##template-cache-map";
            Map<String, Template> contextCachedMap = context.getData(key);
            if (contextCachedMap == null) {
                contextCachedMap = new ConcurrentHashMap<>();
                context.setData(key, contextCachedMap);
            }
            return contextCachedMap;
        }
    }
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.