001// Copyright 2024 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.corelib.pages;
016
017import java.util.ArrayList;
018import java.util.Collections;
019import java.util.Comparator;
020import java.util.List;
021
022import org.apache.tapestry5.MarkupWriter;
023import org.apache.tapestry5.annotations.UnknownActivationContextCheck;
024import org.apache.tapestry5.annotations.WhitelistAccessOnly;
025import org.apache.tapestry5.internal.plastic.PlasticClassLoader;
026import org.apache.tapestry5.ioc.annotations.Inject;
027import org.apache.tapestry5.services.pageload.PageClassLoaderContext;
028import org.apache.tapestry5.services.pageload.PageClassLoaderContextManager;
029
030/**
031 * Shows information about the page classloader contexts.
032 */
033@UnknownActivationContextCheck(false)
034@WhitelistAccessOnly
035public class PageClassLoaderContexts
036{
037
038    @Inject
039    private PageClassLoaderContextManager pageClassLoaderContextManager;
040    
041    void onRender(MarkupWriter writer)
042    {
043        final PageClassLoaderContext root = pageClassLoaderContextManager.getRoot();
044        render(root, writer);
045    }
046
047    private void render(PageClassLoaderContext context, MarkupWriter writer) 
048    {
049        
050        final int classes = context.getClassNames().size();
051        writer.element("li");
052        writer.element("details");
053        writer.element("summary");
054        writer.element("span", "class", "glyphicon glyphicon-zoom-in");
055        writer.end(); // span
056        writer.write(context.getName());
057        writer.write(" (");
058        writer.write(((PlasticClassLoader) context.getClassLoader()).getClassLoaderId());
059        writer.write(", ");
060        writer.write(String.valueOf(classes));
061        if (classes > 1)
062        {
063            writer.write(" classes)");
064        }
065        else
066        {
067            writer.write(" class)");
068        }
069        writer.end(); // summary
070
071        if (!context.isRoot() && !context.getClassNames().isEmpty())
072        {
073            final List<String> classNames = new ArrayList<>(context.getClassNames());
074            Collections.sort(classNames);
075            writer.element("ul");
076            for (String className : classNames) 
077            {
078                writer.element("li").text(className);
079                writer.end(); // li
080            }
081            writer.end(); // ul
082        }
083        
084        writer.end(); // details
085        
086        final List<PageClassLoaderContext> children = new ArrayList<>(context.getChildren());
087        
088        if (!children.isEmpty())
089        {
090            children.sort(Comparator.comparing(PageClassLoaderContext::getName));
091            writer.element("ul");
092            for (PageClassLoaderContext child : children) 
093            {
094                writer.element("li");
095                render(child, writer);
096                writer.end(); // li
097            }
098            writer.end(); // ul
099        }
100        
101        writer.end(); //li
102    }
103    
104}