Skip to content
Permalink
Browse files

[JENKINS-40094] Fix global config.xml content

  • Loading branch information...
amuniz committed Dec 7, 2016
1 parent 5f2f23d commit c8df4a8d28a68ed89f144276c2c9756537d86976
@@ -0,0 +1,61 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.support.api;

import hudson.Util;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Temporal file content, auto-deleted after {@link #writeTo(OutputStream)}.
*/
public class TemporaryFileContent extends FileContent {

private File f;

public TemporaryFileContent(String name, File file) {
super(name, file);
f = file;
}

@Override
public void writeTo(OutputStream os) throws IOException {
super.writeTo(os);
delete();
}

private void delete() {
try {
Util.deleteFile(f);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to delete tmp file {0}", f.getAbsolutePath());
}
}

private static final Logger LOGGER = Logger.getLogger(TemporaryFileContent.class.getName());
}
@@ -2,7 +2,7 @@

import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.FileContent;
import com.cloudbees.jenkins.support.api.TemporaryFileContent;
import com.cloudbees.jenkins.support.util.Helper;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
@@ -44,16 +44,9 @@ public void addContents(@NonNull Container container) {
File patchedXmlFile = null;
try {
patchedXmlFile = SecretHandler.findSecrets(configFile);
container.add(new FileContent("jenkins-root-configuration-files/" + configFile.getName(), patchedXmlFile));
container.add(new TemporaryFileContent("jenkins-root-configuration-files/" + configFile.getName(), patchedXmlFile));
} catch (IOException | SAXException | TransformerException e) {
LOGGER.log(Level.WARNING, "could not add the {0} configuration file to the support bundle because of: {1}", new Object[]{configFile.getName(), e});
} finally {
//delete temporary file - we have copied it in the support bundle already so we do not need it anymore
if (patchedXmlFile != null) {
if(!patchedXmlFile.delete()) {
LOGGER.log(Level.WARNING, "Failed to delete tmp file {0}", new Object[]{ patchedXmlFile.getPath() });
}
}
}
} else {
//this should never happen..
@@ -2,7 +2,7 @@

import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.FileContent;
import com.cloudbees.jenkins.support.api.TemporaryFileContent;
import com.cloudbees.jenkins.support.util.Helper;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
@@ -52,7 +52,7 @@ public boolean accept(File dir, String name) {
if (configFile.exists()) {
try {
File patchedXmlFile = SecretHandler.findSecrets(configFile);
container.add(new FileContent("jenkins-root-configuration-files/" + configFile.getName(), patchedXmlFile));
container.add(new TemporaryFileContent("jenkins-root-configuration-files/" + configFile.getName(), patchedXmlFile));
} catch (IOException | SAXException | TransformerException e) {
LOGGER.log(Level.WARNING, "could not add the {0} configuration file to the support bundle because of: {1}", new Object[]{configFile.getName(), e});
}

0 comments on commit c8df4a8

Please sign in to comment.
You can’t perform that action at this time.