@@ -0,0 +1,3111 @@
+package org.jfrog.hudson;%0A%0Aimport hudson.Extension;%0Aimport hudson.model.Computer;%0Aimport hudson.model.TaskListener;%0Aimport hudson.slaves.ComputerListener;%0Aimport jenkins.model.Jenkins;%0Aimport org.apache.commons.io.FileUtils;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.io.Serializable;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * Adds an Artifactory server %60ARTIFACTORY_SERVER%60 to servers list on startup if %60artifactoryUrl%60, %60artifactoryUsername%60 and %60artifactoryPassword%60 files exist on %60/var/artifactory/%60.%0A * When the Jenkins instance is created by Jenkins X, a default Artifactory server configuration should be created. This class creates this server.%0A */%0A@Extension%0Apublic class ArtifactoryBuilderListener extends ComputerListener implements Serializable %7B%0A    private static final String STARTUP_ARTIFACTORY_SERVER_ID = %22ARTIFACTORY_SERVER%22;%0A%0A    @Override%0A    public void onOnline(final Computer c, TaskListener listener) throws IOException, InterruptedException %7B%0A        ArtifactoryBuilder.DescriptorImpl descriptor = (ArtifactoryBuilder.DescriptorImpl) Jenkins.getInstance().getDescriptor(ArtifactoryBuilder.class);%0A        List%3CArtifactoryServer%3E artifactoryServers = descriptor.getArtifactoryServers();%0A        if (artifactoryServers == null) %7B%0A            artifactoryServers = new ArrayList%3C%3E();%0A        %7D%0A        if (!isServerExists(artifactoryServers)) %7B%0A            Path secretsDir = Paths.get(%22/var%22, %22artifactory%22);%0A            File artifactoryUrl = secretsDir.resolve(%22artifactoryUrl%22).toFile();%0A            File artifactoryUsername = secretsDir.resolve(%22artifactoryUsername%22).toFile();%0A            File artifactoryPassword = secretsDir.resolve(%22artifactoryPassword%22).toFile();%0A            if (artifactoryUrl.isFile() && artifactoryUsername.isFile() && artifactoryPassword.isFile()) %7B%0A                String artifactoryUrlStr = FileUtils.readFileToString(artifactoryUrl);%0A                String artifactoryUsernameStr = FileUtils.readFileToString(artifactoryUsername);%0A                String artifactoryPasswordStr = FileUtils.readFileToString(artifactoryPassword);%0A                artifactoryServers.add(createArtifactoryServer(artifactoryUrlStr, artifactoryUsernameStr, artifactoryPasswordStr));%0A                descriptor.setArtifactoryServers(artifactoryServers);%0A                descriptor.save();%0A            %7D%0A        %7D%0A%0A        super.onOnline(c, listener);%0A    %7D%0A%0A    private ArtifactoryServer createArtifactoryServer(String url, String username, String password) %7B%0A        CredentialsConfig credentialsConfig = new CredentialsConfig(username, password, %22%22);%0A        return new ArtifactoryServer(STARTUP_ARTIFACTORY_SERVER_ID, url, credentialsConfig, credentialsConfig, 0, false, null);%0A    %7D%0A%0A    private boolean isServerExists(List%3CArtifactoryServer%3E artifactoryServers) %7B%0A        for (ArtifactoryServer server : artifactoryServers) %7B%0A            if (STARTUP_ARTIFACTORY_SERVER_ID.equals(server.getName())) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%7D%0A

Create Artifactory server on startup