<commit_before>/*
 * Copyright 2006-2017 the original author or authors.
 *
 * Licensed 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 com.consol.citrus.admin.service;

import com.consol.citrus.admin.Application;
import com.consol.citrus.admin.connector.WebSocketPushEventsListener;
import com.consol.citrus.admin.exception.ApplicationRuntimeException;
import com.consol.citrus.admin.marshal.NamespacePrefixMapper;
import com.consol.citrus.admin.model.*;
import com.consol.citrus.admin.service.command.maven.MavenBuildContext;
import com.consol.citrus.admin.model.git.Repository;
import com.consol.citrus.admin.model.maven.MavenArchetype;
import com.consol.citrus.admin.model.spring.Property;
import com.consol.citrus.admin.model.spring.SpringBean;
import com.consol.citrus.admin.service.command.filesystem.DeleteCommand;
import com.consol.citrus.admin.service.command.filesystem.MoveCommand;
import com.consol.citrus.admin.service.command.git.GitCommand;
import com.consol.citrus.admin.service.command.maven.MavenArchetypeCommand;
import com.consol.citrus.admin.service.command.util.CurlCommand;
import com.consol.citrus.admin.service.command.util.UnzipCommand;
import com.consol.citrus.admin.service.spring.SpringBeanService;
import com.consol.citrus.admin.service.spring.SpringJavaConfigService;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.util.FileUtils;
import com.consol.citrus.util.XMLUtils;
import com.consol.citrus.xml.xpath.XPathUtils;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.springframework.util.xml.SimpleNamespaceContext;
import org.w3c.dom.*;

import javax.annotation.PostConstruct;
import javax.xml.xpath.XPathConstants;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <NAME>
 */
@Service
public class ProjectService {

    @Autowired
    private FileBrowserService fileBrowserService;

    /** Current project actively opened in Citrus admin */
    private Project project;

    /** Holds in memory list of recently opened projects */
    private List<String> recentlyOpened = new ArrayList<>();

    @Autowired
    private SpringBeanService springBeanService;

    @Autowired
    private SpringJavaConfigService springJavaConfigService;

    @Autowired
    private TerminalService terminalService;

    @Autowired
    private Environment environment;

    /** Logger */
    private static Logger log = LoggerFactory.getLogger(ProjectService.class);

    @PostConstruct
    public void loadDefaultProject() throws Exception {
        String defaultProjectHome = System.getProperty(Application.PROJECT_HOME, System.getenv(Application.PROJECT_HOME_ENV));
        if (project == null && StringUtils.hasText(defaultProjectHome)) {
            load(defaultProjectHome);
        }

        String repositoryUrl = System.getProperty(Application.PROJECT_REPOSITORY, System.getenv(Application.PROJECT_REPOSITORY_ENV));
        if (project == null && StringUtils.hasText(repositoryUrl)) {
            Repository repository = new Repository();
            repository.setUrl(repositoryUrl);

            repository.setBranch(System.getProperty(Application.PROJECT_REPOSITORY_BRANCH,
                    System.getenv(Application.PROJECT_REPOSITORY_BRANCH_ENV) != null ? System.getenv(Application.PROJECT_REPOSITORY_BRANCH_ENV) : repository.getBranch()));
            repository.setModule(System.getProperty(Application.PROJECT_REPOSITORY_MODULE,
                    System.getenv(Application.PROJECT_REPOSITORY_MODULE_ENV) != null ? System.getenv(Application.PROJECT_REPOSITORY_MODULE_ENV) : repository.getModule()));

            create(repository);
        }

        String mavenArchetype = System.getProperty(Application.MAVEN_ARCHETYPE_COORDINATES, System.getenv(Application.MAVEN_ARCHETYPE_COORDINATES_ENV));
        if (project == null && StringUtils.hasText(mavenArchetype)) {
            MavenArchetype archetype = new MavenArchetype();

            MavenCoordinate archetypeCoordinates = MavenCoordinates.createCoordinate(mavenArchetype);
            archetype.setArchetypeGroupId(archetypeCoordinates.getGroupId());
            archetype.setArchetypeArtifactId(archetypeCoordinates.getArtifactId());
            archetype.setArchetypeVersion(archetypeCoordinates.getVersion());

            String mavenProject = System.getProperty(Application.MAVEN_PROJECT_COORDINATES,
                    System.getenv(Application.MAVEN_PROJECT_COORDINATES_ENV) != null ? System.getenv(Application.MAVEN_PROJECT_COORDINATES_ENV) : "com.consol.citrus:citrus-project:1.0.0");

            MavenCoordinate projectCoordinates = MavenCoordinates.createCoordinate(mavenProject);
            archetype.setGroupId(projectCoordinates.getGroupId());
            archetype.setArtifactId(projectCoordinates.getArtifactId());
            archetype.setVersion(projectCoordinates.getVersion());

            archetype.setPackageName(System.getProperty(Application.MAVEN_PROJECT_PACKAGE,
                    System.getenv(Application.MAVEN_PROJECT_PACKAGE_ENV) != null ? System.getenv(Application.MAVEN_PROJECT_PACKAGE_ENV) : projectCoordinates.getGroupId()));

            create(archetype);
        }
    }

    /**
     * Loads Citrus project from project home.
     * @param projectHomeDir
     * @return
     */
    public void load(String projectHomeDir) {
        Project project = new Project(projectHomeDir);
        if (project.getProjectInfoFile().exists()) {
            project.loadSettings();
        } else if (!validateProject(project)) {
            throw new ApplicationRuntimeException(String.format("Invalid project home '%s' - not a proper Citrus project", projectHomeDir));
        }

        log.info("Loading project: " + projectHomeDir);

        if (project.isMavenProject()) {
            try {
                String pomXml = FileUtils.readToString(new FileSystemResource(project.getMavenPomFile()));
                SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
                nsContext.bindNamespaceUri("mvn", "http://maven.apache.org/POM/4.0.0");

                Document pomDoc = XMLUtils.parseMessagePayload(pomXml);
                project.setName(evaluate(pomDoc, "/mvn:project/mvn:artifactId", nsContext));

                String version = evaluate(pomDoc, "/mvn:project/mvn:version", nsContext);
                String parentVersion = evaluate(pomDoc, "/mvn:project/mvn:parent/mvn:version", nsContext);
                if (StringUtils.hasText(version)) {
                    project.setVersion(version);
                } else if (StringUtils.hasText(parentVersion)) {
                    project.setVersion(parentVersion);<commit_msg>Automatically read citrus-application.properties file when loading project settings<commit_after>/*
 * Copyright 2006-2017 the original author or authors.
 *
 * Licensed 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 com.consol.citrus.admin.service;

import com.consol.citrus.Citrus;
import com.consol.citrus.admin.Application;
import com.consol.citrus.admin.connector.WebSocketPushEventsListener;
import com.consol.citrus.admin.exception.ApplicationRuntimeException;
import com.consol.citrus.admin.marshal.NamespacePrefixMapper;
import com.consol.citrus.admin.model.Module;
import com.consol.citrus.admin.model.Project;
import com.consol.citrus.admin.model.git.Repository;
import com.consol.citrus.admin.model.maven.MavenArchetype;
import com.consol.citrus.admin.model.spring.Property;
import com.consol.citrus.admin.model.spring.SpringBean;
import com.consol.citrus.admin.service.command.filesystem.DeleteCommand;
import com.consol.citrus.admin.service.command.filesystem.MoveCommand;
import com.consol.citrus.admin.service.command.git.GitCommand;
import com.consol.citrus.admin.service.command.maven.MavenArchetypeCommand;
import com.consol.citrus.admin.service.command.maven.MavenBuildContext;
import com.consol.citrus.admin.service.command.util.CurlCommand;
import com.consol.citrus.admin.service.command.util.UnzipCommand;
import com.consol.citrus.admin.service.spring.SpringBeanService;
import com.consol.citrus.admin.service.spring.SpringJavaConfigService;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.util.FileUtils;
import com.consol.citrus.util.XMLUtils;
import com.consol.citrus.xml.xpath.XPathUtils;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.springframework.util.xml.SimpleNamespaceContext;
import org.w3c.dom.*;

import javax.annotation.PostConstruct;
import javax.xml.xpath.XPathConstants;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <NAME>
 */
@Service
public class ProjectService {

    @Autowired
    private FileBrowserService fileBrowserService;

    /** Current project actively opened in Citrus admin */
    private Project project;

    /** Holds in memory list of recently opened projects */
    private List<String> recentlyOpened = new ArrayList<>();

    @Autowired
    private SpringBeanService springBeanService;

    @Autowired
    private SpringJavaConfigService springJavaConfigService;

    @Autowired
    private TerminalService terminalService;

    @Autowired
    private Environment environment;

    /** Logger */
    private static Logger log = LoggerFactory.getLogger(ProjectService.class);

    @PostConstruct
    public void loadDefaultProject() throws Exception {
        String defaultProjectHome = System.getProperty(Application.PROJECT_HOME, System.getenv(Application.PROJECT_HOME_ENV));
        if (project == null && StringUtils.hasText(defaultProjectHome)) {
            load(defaultProjectHome);
        }

        String repositoryUrl = System.getProperty(Application.PROJECT_REPOSITORY, System.getenv(Application.PROJECT_REPOSITORY_ENV));
        if (project == null && StringUtils.hasText(repositoryUrl)) {
            Repository repository = new Repository();
            repository.setUrl(repositoryUrl);

            repository.setBranch(System.getProperty(Application.PROJECT_REPOSITORY_BRANCH,
                    System.getenv(Application.PROJECT_REPOSITORY_BRANCH_ENV) != null ? System.getenv(Application.PROJECT_REPOSITORY_BRANCH_ENV) : repository.getBranch()));
            repository.setModule(System.getProperty(Application.PROJECT_REPOSITORY_MODULE,
                    System.getenv(Application.PROJECT_REPOSITORY_MODULE_ENV) != null ? System.getenv(Application.PROJECT_REPOSITORY_MODULE_ENV) : repository.getModule()));

            create(repository);
        }

        String mavenArchetype = System.getProperty(Application.MAVEN_ARCHETYPE_COORDINATES, System.getenv(Application.MAVEN_ARCHETYPE_COORDINATES_ENV));
        if (project == null && StringUtils.hasText(mavenArchetype)) {
            MavenArchetype archetype = new MavenArchetype();

            MavenCoordinate archetypeCoordinates = MavenCoordinates.createCoordinate(mavenArchetype);
            archetype.setArchetypeGroupId(archetypeCoordinates.getGroupId());
            archetype.setArchetypeArtifactId(archetypeCoordinates.getArtifactId());
            archetype.setArchetypeVersion(archetypeCoordinates.getVersion());

            String mavenProject = System.getProperty(Application.MAVEN_PROJECT_COORDINATES,
                    System.getenv(Application.MAVEN_PROJECT_COORDINATES_ENV) != null ? System.getenv(Application.MAVEN_PROJECT_COORDINATES_ENV) : "com.consol.citrus:citrus-project:1.0.0");

            MavenCoordinate projectCoordinates = MavenCoordinates.createCoordinate(mavenProject);
            archetype.setGroupId(projectCoordinates.getGroupId());
            archetype.setArtifactId(projectCoordinates.getArtifactId());
            archetype.setVersion(projectCoordinates.getVersion());

            archetype.setPackageName(System.getProperty(Application.MAVEN_PROJECT_PACKAGE,
                    System.getenv(Application.MAVEN_PROJECT_PACKAGE_ENV) != null ? System.getenv(Application.MAVEN_PROJECT_PACKAGE_ENV) : projectCoordinates.getGroupId()));

            create(archetype);
        }
    }

    /**
     * Loads Citrus project from project home.
     * @param projectHomeDir
     * @return
     */
    public void load(String projectHomeDir) {
        Project project = new Project(projectHomeDir);
        if (project.getProjectInfoFile().exists()) {
            project.loadSettings();
        } else if (!validateProject(project)) {
            throw new ApplicationRuntimeException(String.format("Invalid project home '%s' - not a proper Citrus project", projectHomeDir));
        }

        log.info("Loading project: " + projectHomeDir);

        Optional<File> applicationProperties = FileUtils.findFiles(projectHomeDir + File.separator + project.getSettings().getXmlSrcDirectory(), Collections.singleton("citrus-application.properties")).stream().findAny();
        if (applicationProperties.isPresent()) {
            try (FileInputStream fileInputStream = new FileInputStream(applicationProperties.get())) {
                Properties properties = new Properties();
                properties.load(fileInputStream);

                Optional.ofNullable(properties.get(Citrus.DEFAULT_APPLICATION_CONTEXT_PROPERTY)).ifPresent(value -> project.getSettings().setSpringApplicationContext(value.toString()));
                Optional.ofNullable(properties.get(Citrus.DEFAULT_APPLICATION_CONTEXT_CLASS_PROPERTY)).ifPresent(value -> project.getSettings().setSpringJavaConfig(value.toString()));
            } catch (IOException e) {
                log.warn("Failed to read application properties file in project", e);
            }
        }

        if (project.isMavenProject()) {
            try {
                String pomXml = FileUtils.readToString(new FileSystemResource(project.getMavenPomFile()));
                SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
                nsContext.bindNamespaceUri("mvn", "http://maven.apache.org/POM/4.0.0");

                Document pomDoc = XMLUtils.parseMessagePayload(pomXml);
                project.setName(evaluate(pomDoc, "/mvn:project/mvn:artifactId", nsContext));

                String version = evaluate(pomDoc, "/mvn:project/mvn:version", nsContext);
                String parentVersion = evaluate(pomDoc, "/mvn:project/mvn:parent/mvn:version", nsContext);
                if (StringUtils.hasText(version)) {
                    project.setVersion(version);
                } else if (StringUtils.hasText(parentVersion)) {
                    project.setVersion(parentVersion);
{'fraction_non_alphanumeric': 0.09720670391061452, 'fraction_numerical': 0.002587474272272861, 'mean_word_length': 3.978337236533958, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5220798', 'n_tokens_mistral': 4762, 'n_tokens_neox': 4619, 'n_words': 810}