<commit_before>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.wso2.carbon.uuf.internal.core.deployment;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.SetMultimap;
import org.wso2.carbon.uuf.api.Placeholder;
import org.wso2.carbon.uuf.api.config.ComponentManifest;
import org.wso2.carbon.uuf.api.config.Configuration;
import org.wso2.carbon.uuf.api.config.DependencyNode;
import org.wso2.carbon.uuf.api.reference.AppReference;
import org.wso2.carbon.uuf.api.reference.ComponentReference;
import org.wso2.carbon.uuf.api.reference.FileReference;
import org.wso2.carbon.uuf.api.reference.FragmentReference;
import org.wso2.carbon.uuf.api.reference.LayoutReference;
import org.wso2.carbon.uuf.api.reference.PageReference;
import org.wso2.carbon.uuf.api.reference.ThemeReference;
import org.wso2.carbon.uuf.core.App;
import org.wso2.carbon.uuf.core.Component;
import org.wso2.carbon.uuf.core.Fragment;
import org.wso2.carbon.uuf.core.Layout;
import org.wso2.carbon.uuf.core.Lookup;
import org.wso2.carbon.uuf.core.Page;
import org.wso2.carbon.uuf.core.Theme;
import org.wso2.carbon.uuf.core.UriPatten;
import org.wso2.carbon.uuf.exception.InvalidTypeException;
import org.wso2.carbon.uuf.exception.MalformedConfigurationException;
import org.wso2.carbon.uuf.exception.UUFException;
import org.wso2.carbon.uuf.internal.core.auth.SessionRegistry;
import org.wso2.carbon.uuf.internal.core.deployment.parser.ComponentManifestParser;
import org.wso2.carbon.uuf.internal.core.deployment.parser.ConfigurationParser;
import org.wso2.carbon.uuf.internal.core.deployment.parser.DependencyTreeParser;
import org.wso2.carbon.uuf.internal.util.NameUtils;
import org.wso2.carbon.uuf.spi.RenderableCreator;
import org.yaml.snakeyaml.Yaml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static org.wso2.carbon.uuf.internal.util.NameUtils.getFullyQualifiedName;

public class AppCreator {

    private final Map<String, RenderableCreator> renderableCreators;
    private final Set<String> supportedExtensions;
    private final ClassLoaderProvider classLoaderProvider;

    public AppCreator(Set<RenderableCreator> renderableCreators, ClassLoaderProvider classLoaderProvider) {
        this.renderableCreators = new HashMap<>();
        this.supportedExtensions = new HashSet<>();
        for (RenderableCreator renderableCreator : renderableCreators) {
            for (String extension : renderableCreator.getSupportedFileExtensions()) {
                this.renderableCreators.put(extension, renderableCreator);
            }
            supportedExtensions.addAll(renderableCreator.getSupportedFileExtensions());
        }
        this.classLoaderProvider = classLoaderProvider;
    }

    public App createApp(AppReference appReference, String contextPath) {
        // Parse dependency tree.
        DependencyNode rootNode = DependencyTreeParser.parse(appReference.getDependencyTree());
        // Parse configurations.
        Map<?, ?> rawConfiguration = ConfigurationParser.parse(appReference.getConfiguration());
        // Create Lookup.
        final Lookup lookup = new Lookup(getFlattenedDependencies(rootNode), new Configuration(rawConfiguration));

        // Created Components.
        final Map<String, Component> createdComponents = new HashMap<>();
        rootNode.traverse(dependencyNode -> {
            if (createdComponents.containsKey(dependencyNode.getArtifactId())) {
                return; // Component for this dependency node is already created.
            }

            String componentName = dependencyNode.getArtifactId();
            String componentVersion = dependencyNode.getVersion();
            String componentContextPath;
            if (dependencyNode == rootNode) {
                // This is the root component.
                componentContextPath = Component.ROOT_COMPONENT_CONTEXT_PATH;
            } else {
                componentContextPath = dependencyNode.getContextPath();
            }
            ComponentReference componentReference = appReference.getComponentReference(componentContextPath);
            ClassLoader classLoader = classLoaderProvider.getClassLoader(componentName, componentVersion,
                                                                         componentReference);
            Component component = createComponent(componentName, componentVersion, componentContextPath,
                                                  componentReference, classLoader, lookup);
            lookup.add(component);
            createdComponents.put(componentName, component);
        });
        // Create Themes.
        Set<Theme> themes = appReference.getThemeReferences().map(this::createTheme).collect(Collectors.toSet());
        // Create App.
        String appName = rootNode.getArtifactId();
        String appContextPath = (contextPath == null) ? rootNode.getContextPath() : contextPath;<commit_msg>replaced if with ternary operator in AppCreator class as per https://github.com/wso2/carbon-uuf/pull/86#discussion_r91059085 comment<commit_after>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.wso2.carbon.uuf.internal.core.deployment;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.SetMultimap;
import org.wso2.carbon.uuf.api.Placeholder;
import org.wso2.carbon.uuf.api.config.ComponentManifest;
import org.wso2.carbon.uuf.api.config.Configuration;
import org.wso2.carbon.uuf.api.config.DependencyNode;
import org.wso2.carbon.uuf.api.reference.AppReference;
import org.wso2.carbon.uuf.api.reference.ComponentReference;
import org.wso2.carbon.uuf.api.reference.FileReference;
import org.wso2.carbon.uuf.api.reference.FragmentReference;
import org.wso2.carbon.uuf.api.reference.LayoutReference;
import org.wso2.carbon.uuf.api.reference.PageReference;
import org.wso2.carbon.uuf.api.reference.ThemeReference;
import org.wso2.carbon.uuf.core.App;
import org.wso2.carbon.uuf.core.Component;
import org.wso2.carbon.uuf.core.Fragment;
import org.wso2.carbon.uuf.core.Layout;
import org.wso2.carbon.uuf.core.Lookup;
import org.wso2.carbon.uuf.core.Page;
import org.wso2.carbon.uuf.core.Theme;
import org.wso2.carbon.uuf.core.UriPatten;
import org.wso2.carbon.uuf.exception.InvalidTypeException;
import org.wso2.carbon.uuf.exception.MalformedConfigurationException;
import org.wso2.carbon.uuf.exception.UUFException;
import org.wso2.carbon.uuf.internal.core.auth.SessionRegistry;
import org.wso2.carbon.uuf.internal.core.deployment.parser.ComponentManifestParser;
import org.wso2.carbon.uuf.internal.core.deployment.parser.ConfigurationParser;
import org.wso2.carbon.uuf.internal.core.deployment.parser.DependencyTreeParser;
import org.wso2.carbon.uuf.internal.util.NameUtils;
import org.wso2.carbon.uuf.spi.RenderableCreator;
import org.yaml.snakeyaml.Yaml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static org.wso2.carbon.uuf.internal.util.NameUtils.getFullyQualifiedName;

public class AppCreator {

    private final Map<String, RenderableCreator> renderableCreators;
    private final Set<String> supportedExtensions;
    private final ClassLoaderProvider classLoaderProvider;

    public AppCreator(Set<RenderableCreator> renderableCreators, ClassLoaderProvider classLoaderProvider) {
        this.renderableCreators = new HashMap<>();
        this.supportedExtensions = new HashSet<>();
        for (RenderableCreator renderableCreator : renderableCreators) {
            for (String extension : renderableCreator.getSupportedFileExtensions()) {
                this.renderableCreators.put(extension, renderableCreator);
            }
            supportedExtensions.addAll(renderableCreator.getSupportedFileExtensions());
        }
        this.classLoaderProvider = classLoaderProvider;
    }

    public App createApp(AppReference appReference, String contextPath) {
        // Parse dependency tree.
        DependencyNode rootNode = DependencyTreeParser.parse(appReference.getDependencyTree());
        // Parse configurations.
        Map<?, ?> rawConfiguration = ConfigurationParser.parse(appReference.getConfiguration());
        // Create Lookup.
        final Lookup lookup = new Lookup(getFlattenedDependencies(rootNode), new Configuration(rawConfiguration));

        // Created Components.
        final Map<String, Component> createdComponents = new HashMap<>();
        rootNode.traverse(dependencyNode -> {
            if (createdComponents.containsKey(dependencyNode.getArtifactId())) {
                return; // Component for this dependency node is already created.
            }

            String componentName = dependencyNode.getArtifactId();
            String componentVersion = dependencyNode.getVersion();
            String componentContextPath = (dependencyNode == rootNode) ? Component.ROOT_COMPONENT_CONTEXT_PATH :
                    dependencyNode.getContextPath();
            ComponentReference componentReference = appReference.getComponentReference(componentContextPath);
            ClassLoader classLoader = classLoaderProvider.getClassLoader(componentName, componentVersion,
                                                                         componentReference);
            Component component = createComponent(componentName, componentVersion, componentContextPath,
                                                  componentReference, classLoader, lookup);
            lookup.add(component);
            createdComponents.put(componentName, component);
        });
        // Create Themes.
        Set<Theme> themes = appReference.getThemeReferences().map(this::createTheme).collect(Collectors.toSet());
        // Create App.
        String appName = rootNode.getArtifactId();
        String appContextPath = (contextPath == null) ? rootNode.getContextPath() : contextPath;
{'fraction_non_alphanumeric': 0.09497779296207721, 'fraction_numerical': 0.007943286641612573, 'mean_word_length': 3.6134751773049647, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4337635', 'n_tokens_mistral': 3304, 'n_tokens_neox': 3205, 'n_words': 655}