Search in sources :

Example 1 with Timer

use of act.metric.Timer in project actframework by actframework.

the class AppCompiler method compile.

public void compile(String className) {
    Timer timer = metric.startTimer("act:classload:compile:" + className);
    ICompilationUnit[] compilationUnits = new ICompilationUnit[1];
    compilationUnits[0] = classLoader.source(className).compilationUnit();
    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();
    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);
    org.eclipse.jdt.internal.compiler.Compiler jdtCompiler = new Compiler(nameEnv, policy, compilerOptions, requestor, problemFactory) {

        @Override
        protected void handleInternalException(Throwable e, CompilationUnitDeclaration ud, CompilationResult result) {
        }
    };
    jdtCompiler.compile(compilationUnits);
    timer.stop();
}
Also used : ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) Compiler(org.eclipse.jdt.internal.compiler.Compiler) Compiler(org.eclipse.jdt.internal.compiler.Compiler) Timer(act.metric.Timer) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) org.eclipse.jdt.internal.compiler(org.eclipse.jdt.internal.compiler)

Example 2 with Timer

use of act.metric.Timer in project actframework by actframework.

the class NetworkHandler method handle.

public void handle(final ActionContext ctx, final NetworkDispatcher dispatcher) {
    if (isDestroyed()) {
        return;
    }
    Exception refreshError = null;
    if (Act.isDev()) {
        try {
            boolean updated = app.checkUpdates(false);
            if (updated && !app.hasBlockIssue()) {
                app.jobManager().on(SysEventId.POST_START, new Runnable() {

                    @Override
                    public void run() {
                        handle(ctx, dispatcher);
                    }
                }, true);
                dispatcher.keep();
                return;
            }
        } catch (Exception e) {
            refreshError = e;
        }
    }
    final H.Request req = ctx.req();
    String url = req.url();
    H.Method method = req.method();
    url = contentSuffixProcessor.apply(req, url);
    try {
        url = urlContextProcessor.apply(req, url);
    } catch (NotFound notFound) {
        ctx.handler(AlwaysNotFound.INSTANCE);
        ctx.saveLocal();
        AlwaysNotFound.INSTANCE.apply(ctx);
        return;
    }
    Timer timer = metric.startTimer(MetricInfo.ROUTING);
    final RequestHandler requestHandler = router().getInvoker(method, url, ctx);
    ctx.handler(requestHandler);
    timer.stop();
    boolean resourceGetter = requestHandler instanceof ResourceGetter || requestHandler instanceof FileGetter;
    if (null != refreshError && !resourceGetter) {
        ctx.saveLocal();
        handleException(refreshError, ctx, "Error refreshing app");
        ActionContext.clearCurrent();
        return;
    }
    NetworkJob job = new NetworkJob() {

        @Override
        public void run() {
            Timer timer = Metric.NULL_METRIC.startTimer("null");
            if (metric != Metric.NULL_METRIC) {
                String key = S.concat(MetricInfo.HTTP_HANDLER, ":", requestHandler.toString());
                timer = metric.startTimer(key);
            }
            ctx.saveLocal();
            EventBus eventBus = app.eventBus();
            try {
                eventBus.emit(new PreHandle(ctx));
                requestHandler.handle(ctx);
            } catch (Result r) {
                if (isError(r)) {
                    ctx.handler(FastRequestHandler.DUMB);
                }
                try {
                    r = RequestHandlerProxy.GLOBAL_AFTER_INTERCEPTOR.apply(r, ctx);
                } catch (Exception e) {
                    logger.error(e, "Error calling global after interceptor");
                    r = ActErrorResult.of(e);
                }
                if (null == ctx.handler() || isError(r)) {
                    ctx.handler(FastRequestHandler.DUMB);
                }
                H.Format fmt = req.accept();
                if (H.Format.UNKNOWN == fmt) {
                    fmt = req.contentType();
                }
                ctx.prepareRespForWrite().addHeaderIfNotAdded(H.Header.Names.CONTENT_TYPE, fmt.contentType());
                r.apply(req, ctx.prepareRespForWrite());
            } catch (Exception e) {
                handleException(e, ctx, "Error handling network request");
            } finally {
                // we don't destroy ctx here in case it's been passed to
                // another thread
                eventBus.emit(new PostHandle(ctx));
                ActionContext.clearCurrent();
                timer.stop();
            }
        }
    };
    if (method.unsafe() || !requestHandler.express(ctx)) {
        dispatcher.dispatch(job);
    } else {
        job.run();
    }
}
Also used : H(org.osgl.http.H) PostHandle(act.handler.event.PostHandle) EventBus(act.event.EventBus) ResourceGetter(act.handler.builtin.ResourceGetter) NotAppliedException(org.osgl.exception.NotAppliedException) PreHandle(act.handler.event.PreHandle) FileGetter(act.handler.builtin.FileGetter) Result(org.osgl.mvc.result.Result) ErrorResult(org.osgl.mvc.result.ErrorResult) ActErrorResult(act.view.ActErrorResult) Timer(act.metric.Timer) FastRequestHandler(act.handler.builtin.controller.FastRequestHandler) RequestHandler(act.handler.RequestHandler) AlwaysNotFound(act.handler.builtin.AlwaysNotFound) NotFound(org.osgl.mvc.result.NotFound)

Example 3 with Timer

use of act.metric.Timer in project actframework by actframework.

the class AppCompiler method compile.

public void compile(Collection<Source> sources) {
    Timer timer = metric.startTimer("act:classload:compile:_all");
    int len = sources.size();
    ICompilationUnit[] compilationUnits = new ICompilationUnit[len];
    int i = 0;
    for (Source source : sources) {
        compilationUnits[i++] = source.compilationUnit();
    }
    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();
    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);
    org.eclipse.jdt.internal.compiler.Compiler jdtCompiler = new Compiler(nameEnv, policy, compilerOptions, requestor, problemFactory) {

        @Override
        protected void handleInternalException(Throwable e, CompilationUnitDeclaration ud, CompilationResult result) {
        }
    };
    jdtCompiler.compile(compilationUnits);
    timer.stop();
}
Also used : ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) Compiler(org.eclipse.jdt.internal.compiler.Compiler) Compiler(org.eclipse.jdt.internal.compiler.Compiler) Timer(act.metric.Timer) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) org.eclipse.jdt.internal.compiler(org.eclipse.jdt.internal.compiler)

Example 4 with Timer

use of act.metric.Timer in project actframework by actframework.

the class DevModeClassLoader method scanSources.

private void scanSources() {
    Timer timer = metric.startTimer("act:classload:scan:scanSources");
    try {
        logger.debug("start to scan sources...");
        List<AppSourceCodeScanner> scanners = app().scannerManager().sourceCodeScanners();
        Set<String> classesNeedByteCodeScan = C.newSet();
        if (scanners.isEmpty()) {
            // LOGGER.warn("No source code scanner found");
            for (String className : sources.keySet()) {
                classesNeedByteCodeScan.add(className);
            }
        } else {
            for (String className : sources.keySet()) {
                classesNeedByteCodeScan.add(className);
                logger.debug("scanning %s ...", className);
                List<AppSourceCodeScanner> l = new ArrayList<>();
                for (AppSourceCodeScanner scanner : scanners) {
                    if (scanner.start(className)) {
                        // LOGGER.trace("scanner %s added to the list", scanner.getClass().getName());
                        l.add(scanner);
                    }
                }
                Source source = source(className);
                String[] lines = source.code().split("[\\n\\r]+");
                for (int i = 0, j = lines.length; i < j; ++i) {
                    String line = lines[i];
                    for (AppSourceCodeScanner scanner : l) {
                        scanner.visit(i, line, className);
                    }
                }
            }
        }
        if (classesNeedByteCodeScan.isEmpty()) {
            return;
        }
        final Set<String> embeddedClassNames = C.newSet();
        scanByteCode(classesNeedByteCodeScan, new $.F1<String, byte[]>() {

            @Override
            public byte[] apply(String s) throws NotAppliedException, $.Break {
                return bytecodeFromSource(s, embeddedClassNames);
            }
        });
        while (!embeddedClassNames.isEmpty()) {
            Set<String> embeddedClassNameCopy = C.newSet(embeddedClassNames);
            scanByteCode(embeddedClassNameCopy, new $.F1<String, byte[]>() {

                @Override
                public byte[] apply(String s) throws NotAppliedException, $.Break {
                    return bytecodeFromSource(s, embeddedClassNames);
                }
            });
            embeddedClassNames.removeAll(embeddedClassNameCopy);
        }
    } finally {
        timer.stop();
    }
}
Also used : org.osgl.$(org.osgl.$) NotAppliedException(org.osgl.exception.NotAppliedException) Timer(act.metric.Timer)

Aggregations

Timer (act.metric.Timer)4 org.eclipse.jdt.internal.compiler (org.eclipse.jdt.internal.compiler)2 Compiler (org.eclipse.jdt.internal.compiler.Compiler)2 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)2 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)2 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)2 NotAppliedException (org.osgl.exception.NotAppliedException)2 EventBus (act.event.EventBus)1 RequestHandler (act.handler.RequestHandler)1 AlwaysNotFound (act.handler.builtin.AlwaysNotFound)1 FileGetter (act.handler.builtin.FileGetter)1 ResourceGetter (act.handler.builtin.ResourceGetter)1 FastRequestHandler (act.handler.builtin.controller.FastRequestHandler)1 PostHandle (act.handler.event.PostHandle)1 PreHandle (act.handler.event.PreHandle)1 ActErrorResult (act.view.ActErrorResult)1 org.osgl.$ (org.osgl.$)1 H (org.osgl.http.H)1 ErrorResult (org.osgl.mvc.result.ErrorResult)1 NotFound (org.osgl.mvc.result.NotFound)1