Search in sources :

Example 1 with ComponentVisitor

use of org.olat.core.util.component.ComponentVisitor in project OpenOLAT by OpenOLAT.

the class ValidatingVisitor method handleDirties.

/**
 * to be called by Window.java or the AjaxController only!
 * this method is synchronized on the Window instance
 *
 * @return a updateUI-Command or null if there are no dirty components (normally not the case for sync (user-click) request, but often the case
 * for pull request, since nothing has changed yet on the screen.
 */
public Command handleDirties() throws CannotReplaceDOMFragmentException {
    // more accurately, the synchronized is needed when other classes than window call this method.
    synchronized (this) {
        Command com = null;
        boolean isDebugLog = log.isDebug();
        StringBuilder debugMsg = null;
        long start = 0;
        if (isDebugLog) {
            log.debug("Perf-Test: Window.handleDirties started...");
            start = System.currentTimeMillis();
        }
        final List<Component> dirties = new ArrayList<Component>();
        ComponentVisitor dirtyV = new ComponentVisitor() {

            public boolean visit(Component comp, UserRequest ureq) {
                boolean visitChildren = false;
                if (comp == null) {
                    log.warn("Ooops, a component is null");
                } else if (!comp.isVisible()) {
                    // a component just made -visible- still needs to be collected (detected by checking dirty flag)
                    if (comp.isDirty()) {
                        dirties.add(comp);
                        // clear manually here since this component will not be rendered
                        comp.setDirty(false);
                    }
                } else if (comp.isDirty()) {
                    dirties.add(comp);
                } else {
                    // visible and not dirty -> visit children
                    visitChildren = true;
                }
                return visitChildren;
            }
        };
        ComponentTraverser ct = new ComponentTraverser(dirtyV, getContentPane(), false);
        ct.visitAll(null);
        int dCnt = dirties.size();
        if (isDebugLog) {
            long durationVisitAll = System.currentTimeMillis() - start;
            log.debug("Perf-Test: Window.handleDirties after ct.visitAll durationVisitAll=" + durationVisitAll);
            log.debug("Perf-Test: Window.handleDirties dirties.size()=" + dirties.size());
        }
        if (dCnt > 0) {
            // collect the redraw dirties command
            try {
                JSONObject root = new JSONObject();
                root.put("cc", dirties.size());
                root.put("wts", timestamp);
                JSONArray ja = new JSONArray();
                root.put("cps", ja);
                GlobalSettings gsettings = wbackofficeImpl.getGlobalSettings();
                synchronized (render_mutex) {
                    // o_clusterOK by:fj
                    // we let all dirty components render themselves.
                    // not offered (since not usability-useful) is the include of new js-libraries and css-libraries here, since this may invoke a screen reload
                    // which disturbes the user and lets him/her loose the focus and the cursor.
                    AsyncMediaResponsible amr = null;
                    long rstart = 0;
                    if (isDebugLog) {
                        rstart = System.currentTimeMillis();
                        debugMsg = new StringBuilder("update:").append(String.valueOf(dCnt)).append(";");
                    }
                    for (int i = 0; i < dCnt; i++) {
                        Component toRender = dirties.get(i);
                        if (isDebugLog) {
                            log.debug("Perf-Test: Window.handleDirties toRender.getComponentName()=" + toRender.getComponentName());
                            log.debug("Perf-Test: Window.handleDirties toRender=" + toRender);
                        }
                        boolean wasDomR = toRender.isDomReplaceable();
                        if (!wasDomR) {
                            throw new CannotReplaceDOMFragmentException("cannot replace as dom fragment:" + toRender.getComponentName() + " (" + toRender.getClass().getName() + ")," + toRender.getExtendedDebugInfo());
                        }
                        Panel wrapper = new Panel("renderpanel");
                        // to omit <div> around the render helper panel
                        wrapper.setDomReplaceable(false);
                        RenderResult renderResult = null;
                        StringOutput jsol = null;
                        StringOutput hdr = null;
                        StringOutput result = null;
                        try {
                            toRender.setDomReplaceable(false);
                            wrapper.setContent(toRender);
                            String newTimestamp = String.valueOf(timestamp);
                            URLBuilder ubu = new URLBuilder(uriPrefix, getInstanceId(), newTimestamp);
                            renderResult = new RenderResult();
                            // if we have an around-component-interception
                            // set the handler for this render cycle
                            InterceptHandler interceptHandler = wbackofficeImpl.getInterceptHandler();
                            if (interceptHandler != null) {
                                InterceptHandlerInstance dhri = interceptHandler.createInterceptHandlerInstance();
                                renderResult.setInterceptHandlerRenderInstance(dhri);
                            }
                            Renderer fr = Renderer.getInstance(wrapper, null, ubu, renderResult, gsettings);
                            jsol = StringOutputPool.allocStringBuilder(2048);
                            fr.renderBodyOnLoadJSFunctionCall(jsol, toRender);
                            hdr = StringOutputPool.allocStringBuilder(2048);
                            fr.renderHeaderIncludes(hdr, toRender);
                            long pstart = 0;
                            if (isDebugLog) {
                                pstart = System.currentTimeMillis();
                            }
                            result = StringOutputPool.allocStringBuilder(100000);
                            fr.render(toRender, result, null);
                            if (isDebugLog) {
                                long pstop = System.currentTimeMillis();
                                debugMsg.append(toRender.getComponentName()).append(":").append((pstop - pstart));
                                if (i < dCnt - 1)
                                    debugMsg.append(",");
                            }
                        } catch (Exception e) {
                            throw new OLATRuntimeException("Unexpected error ", e);
                        } finally {
                            toRender.setDomReplaceable(true);
                        }
                        if (renderResult.getRenderException() != null) {
                            throw new OLATRuntimeException(Window.class, renderResult.getLogMsg(), renderResult.getRenderException());
                        }
                        AsyncMediaResponsible curAmr = renderResult.getAsyncMediaResponsible();
                        if (curAmr != null) {
                            if (amr != null) {
                                throw new AssertException("can set amr only once in a screen!");
                            } else {
                                amr = curAmr;
                            }
                        }
                        JSONObject jo = new JSONObject();
                        String cid = toRender.getDispatchID();
                        if (Settings.isDebuging()) {
                            // for debugging only
                            jo.put("cname", toRender.getComponentName());
                            jo.put("clisteners", toRender.getListenerInfo());
                            jo.put("hfragsize", result.length());
                        }
                        jo.put("cid", cid);
                        jo.put("cw", toRender.isDomReplacementWrapperRequired());
                        jo.put("cidvis", toRender.isVisible());
                        jo.put("hfrag", StringOutputPool.freePop(result));
                        jo.put("jsol", StringOutputPool.freePop(jsol));
                        jo.put("hdr", StringOutputPool.freePop(hdr));
                        ja.put(jo);
                    }
                    // to null otherwise it possible that e.g. pdf served as following click within a CP component
                    if (amr != null) {
                        setAsyncMediaResponsible(amr);
                    }
                    if (isDebugLog) {
                        long rstop = System.currentTimeMillis();
                        debugMsg.append(";inl_part_render:").append((rstop - rstart));
                        log.debug(debugMsg.toString());
                    }
                }
                com = CommandFactory.createDirtyComponentsCommand();
                com.setSubJSON(root);
                if (isDebugLog) {
                    long durationHandleDirties = System.currentTimeMillis() - start;
                    log.debug("Perf-Test:" + durationHandleDirties);
                }
                return com;
            } catch (JSONException e) {
                throw new AssertException("wrong data put into json object", e);
            }
        }
        if (isDebugLog) {
            long durationHandleDirties = System.currentTimeMillis() - start;
            log.debug("Perf-Test: Window.handleDirties finished 2  durationHandleDirties=" + durationHandleDirties);
        }
        return com;
    }
}
Also used : ComponentVisitor(org.olat.core.util.component.ComponentVisitor) ComponentTraverser(org.olat.core.util.component.ComponentTraverser) ArrayList(java.util.ArrayList) RenderResult(org.olat.core.gui.render.RenderResult) StringOutput(org.olat.core.gui.render.StringOutput) InterceptHandlerInstance(org.olat.core.gui.render.intercept.InterceptHandlerInstance) InterceptHandler(org.olat.core.gui.render.intercept.InterceptHandler) UserRequest(org.olat.core.gui.UserRequest) AssertException(org.olat.core.logging.AssertException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) GlobalSettings(org.olat.core.gui.GlobalSettings) HistoryPoint(org.olat.core.id.context.HistoryPoint) AssertException(org.olat.core.logging.AssertException) JSONException(org.json.JSONException) InvalidRequestParameterException(org.olat.core.gui.components.form.flexible.impl.InvalidRequestParameterException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException) URLBuilder(org.olat.core.gui.render.URLBuilder) Panel(org.olat.core.gui.components.panel.Panel) AsyncMediaResponsible(org.olat.core.gui.media.AsyncMediaResponsible) JSONObject(org.json.JSONObject) JSCommand(org.olat.core.gui.control.winmgr.JSCommand) Command(org.olat.core.gui.control.winmgr.Command) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Renderer(org.olat.core.gui.render.Renderer)

Example 2 with ComponentVisitor

use of org.olat.core.util.component.ComponentVisitor in project openolat by klemens.

the class ValidatingVisitor method handleDirties.

/**
 * to be called by Window.java or the AjaxController only!
 * this method is synchronized on the Window instance
 *
 * @return a updateUI-Command or null if there are no dirty components (normally not the case for sync (user-click) request, but often the case
 * for pull request, since nothing has changed yet on the screen.
 */
public Command handleDirties() throws CannotReplaceDOMFragmentException {
    // more accurately, the synchronized is needed when other classes than window call this method.
    synchronized (this) {
        Command com = null;
        boolean isDebugLog = log.isDebug();
        StringBuilder debugMsg = null;
        long start = 0;
        if (isDebugLog) {
            log.debug("Perf-Test: Window.handleDirties started...");
            start = System.currentTimeMillis();
        }
        final List<Component> dirties = new ArrayList<Component>();
        ComponentVisitor dirtyV = new ComponentVisitor() {

            public boolean visit(Component comp, UserRequest ureq) {
                boolean visitChildren = false;
                if (comp == null) {
                    log.warn("Ooops, a component is null");
                } else if (!comp.isVisible()) {
                    // a component just made -visible- still needs to be collected (detected by checking dirty flag)
                    if (comp.isDirty()) {
                        dirties.add(comp);
                        // clear manually here since this component will not be rendered
                        comp.setDirty(false);
                    }
                } else if (comp.isDirty()) {
                    dirties.add(comp);
                } else {
                    // visible and not dirty -> visit children
                    visitChildren = true;
                }
                return visitChildren;
            }
        };
        ComponentTraverser ct = new ComponentTraverser(dirtyV, getContentPane(), false);
        ct.visitAll(null);
        int dCnt = dirties.size();
        if (isDebugLog) {
            long durationVisitAll = System.currentTimeMillis() - start;
            log.debug("Perf-Test: Window.handleDirties after ct.visitAll durationVisitAll=" + durationVisitAll);
            log.debug("Perf-Test: Window.handleDirties dirties.size()=" + dirties.size());
        }
        if (dCnt > 0) {
            // collect the redraw dirties command
            try {
                JSONObject root = new JSONObject();
                root.put("cc", dirties.size());
                root.put("wts", timestamp);
                JSONArray ja = new JSONArray();
                root.put("cps", ja);
                GlobalSettings gsettings = wbackofficeImpl.getGlobalSettings();
                synchronized (render_mutex) {
                    // o_clusterOK by:fj
                    // we let all dirty components render themselves.
                    // not offered (since not usability-useful) is the include of new js-libraries and css-libraries here, since this may invoke a screen reload
                    // which disturbes the user and lets him/her loose the focus and the cursor.
                    AsyncMediaResponsible amr = null;
                    long rstart = 0;
                    if (isDebugLog) {
                        rstart = System.currentTimeMillis();
                        debugMsg = new StringBuilder("update:").append(String.valueOf(dCnt)).append(";");
                    }
                    for (int i = 0; i < dCnt; i++) {
                        Component toRender = dirties.get(i);
                        if (isDebugLog) {
                            log.debug("Perf-Test: Window.handleDirties toRender.getComponentName()=" + toRender.getComponentName());
                            log.debug("Perf-Test: Window.handleDirties toRender=" + toRender);
                        }
                        boolean wasDomR = toRender.isDomReplaceable();
                        if (!wasDomR) {
                            throw new CannotReplaceDOMFragmentException("cannot replace as dom fragment:" + toRender.getComponentName() + " (" + toRender.getClass().getName() + ")," + toRender.getExtendedDebugInfo());
                        }
                        Panel wrapper = new Panel("renderpanel");
                        // to omit <div> around the render helper panel
                        wrapper.setDomReplaceable(false);
                        RenderResult renderResult = null;
                        StringOutput jsol = null;
                        StringOutput hdr = null;
                        StringOutput result = null;
                        try {
                            toRender.setDomReplaceable(false);
                            wrapper.setContent(toRender);
                            String newTimestamp = String.valueOf(timestamp);
                            URLBuilder ubu = new URLBuilder(uriPrefix, getInstanceId(), newTimestamp);
                            renderResult = new RenderResult();
                            // if we have an around-component-interception
                            // set the handler for this render cycle
                            InterceptHandler interceptHandler = wbackofficeImpl.getInterceptHandler();
                            if (interceptHandler != null) {
                                InterceptHandlerInstance dhri = interceptHandler.createInterceptHandlerInstance();
                                renderResult.setInterceptHandlerRenderInstance(dhri);
                            }
                            Renderer fr = Renderer.getInstance(wrapper, null, ubu, renderResult, gsettings);
                            jsol = StringOutputPool.allocStringBuilder(2048);
                            fr.renderBodyOnLoadJSFunctionCall(jsol, toRender);
                            hdr = StringOutputPool.allocStringBuilder(2048);
                            fr.renderHeaderIncludes(hdr, toRender);
                            long pstart = 0;
                            if (isDebugLog) {
                                pstart = System.currentTimeMillis();
                            }
                            result = StringOutputPool.allocStringBuilder(100000);
                            fr.render(toRender, result, null);
                            if (isDebugLog) {
                                long pstop = System.currentTimeMillis();
                                debugMsg.append(toRender.getComponentName()).append(":").append((pstop - pstart));
                                if (i < dCnt - 1)
                                    debugMsg.append(",");
                            }
                        } catch (Exception e) {
                            throw new OLATRuntimeException("Unexpected error ", e);
                        } finally {
                            toRender.setDomReplaceable(true);
                        }
                        if (renderResult.getRenderException() != null) {
                            throw new OLATRuntimeException(Window.class, renderResult.getLogMsg(), renderResult.getRenderException());
                        }
                        AsyncMediaResponsible curAmr = renderResult.getAsyncMediaResponsible();
                        if (curAmr != null) {
                            if (amr != null) {
                                throw new AssertException("can set amr only once in a screen!");
                            } else {
                                amr = curAmr;
                            }
                        }
                        JSONObject jo = new JSONObject();
                        String cid = toRender.getDispatchID();
                        if (Settings.isDebuging()) {
                            // for debugging only
                            jo.put("cname", toRender.getComponentName());
                            jo.put("clisteners", toRender.getListenerInfo());
                            jo.put("hfragsize", result.length());
                        }
                        jo.put("cid", cid);
                        jo.put("cw", toRender.isDomReplacementWrapperRequired());
                        jo.put("cidvis", toRender.isVisible());
                        jo.put("hfrag", StringOutputPool.freePop(result));
                        jo.put("jsol", StringOutputPool.freePop(jsol));
                        jo.put("hdr", StringOutputPool.freePop(hdr));
                        ja.put(jo);
                    }
                    // to null otherwise it possible that e.g. pdf served as following click within a CP component
                    if (amr != null) {
                        setAsyncMediaResponsible(amr);
                    }
                    if (isDebugLog) {
                        long rstop = System.currentTimeMillis();
                        debugMsg.append(";inl_part_render:").append((rstop - rstart));
                        log.debug(debugMsg.toString());
                    }
                }
                com = CommandFactory.createDirtyComponentsCommand();
                com.setSubJSON(root);
                if (isDebugLog) {
                    long durationHandleDirties = System.currentTimeMillis() - start;
                    log.debug("Perf-Test:" + durationHandleDirties);
                }
                return com;
            } catch (JSONException e) {
                throw new AssertException("wrong data put into json object", e);
            }
        }
        if (isDebugLog) {
            long durationHandleDirties = System.currentTimeMillis() - start;
            log.debug("Perf-Test: Window.handleDirties finished 2  durationHandleDirties=" + durationHandleDirties);
        }
        return com;
    }
}
Also used : ComponentVisitor(org.olat.core.util.component.ComponentVisitor) ComponentTraverser(org.olat.core.util.component.ComponentTraverser) ArrayList(java.util.ArrayList) RenderResult(org.olat.core.gui.render.RenderResult) StringOutput(org.olat.core.gui.render.StringOutput) InterceptHandlerInstance(org.olat.core.gui.render.intercept.InterceptHandlerInstance) InterceptHandler(org.olat.core.gui.render.intercept.InterceptHandler) UserRequest(org.olat.core.gui.UserRequest) AssertException(org.olat.core.logging.AssertException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) GlobalSettings(org.olat.core.gui.GlobalSettings) HistoryPoint(org.olat.core.id.context.HistoryPoint) AssertException(org.olat.core.logging.AssertException) JSONException(org.json.JSONException) InvalidRequestParameterException(org.olat.core.gui.components.form.flexible.impl.InvalidRequestParameterException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException) URLBuilder(org.olat.core.gui.render.URLBuilder) Panel(org.olat.core.gui.components.panel.Panel) AsyncMediaResponsible(org.olat.core.gui.media.AsyncMediaResponsible) JSONObject(org.json.JSONObject) JSCommand(org.olat.core.gui.control.winmgr.JSCommand) Command(org.olat.core.gui.control.winmgr.Command) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Renderer(org.olat.core.gui.render.Renderer)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 GlobalSettings (org.olat.core.gui.GlobalSettings)2 UserRequest (org.olat.core.gui.UserRequest)2 InvalidRequestParameterException (org.olat.core.gui.components.form.flexible.impl.InvalidRequestParameterException)2 Panel (org.olat.core.gui.components.panel.Panel)2 Command (org.olat.core.gui.control.winmgr.Command)2 JSCommand (org.olat.core.gui.control.winmgr.JSCommand)2 AsyncMediaResponsible (org.olat.core.gui.media.AsyncMediaResponsible)2 RenderResult (org.olat.core.gui.render.RenderResult)2 Renderer (org.olat.core.gui.render.Renderer)2 StringOutput (org.olat.core.gui.render.StringOutput)2 URLBuilder (org.olat.core.gui.render.URLBuilder)2 InterceptHandler (org.olat.core.gui.render.intercept.InterceptHandler)2 InterceptHandlerInstance (org.olat.core.gui.render.intercept.InterceptHandlerInstance)2 HistoryPoint (org.olat.core.id.context.HistoryPoint)2 AssertException (org.olat.core.logging.AssertException)2