All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rhq.plugins.apache.parser.ApacheConfigWriter Maven / Gradle / Ivy

There is a newer version: 4.13.0
Show newest version
package org.rhq.plugins.apache.parser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@SuppressWarnings({ "UnusedDeclaration" })
public class ApacheConfigWriter {

    private static final Log log = LogFactory.getLog(ApacheConfigReader.class);

    public ApacheConfigWriter(ApacheDirectiveTree tree) {
    }

    private void findUpdated(ApacheDirective dir, List updatedNodes) {
        if (dir.isUpdated()) {
            updatedNodes.add(dir);
        }
        for (ApacheDirective directive : dir.getChildDirectives()) {
            findUpdated(directive, updatedNodes);
        }
    }

    private Set findUpdatedFiles(List updatedNodes) {
        Set updatedFiles = new HashSet();
        for (ApacheDirective dir : updatedNodes) {
            updatedFiles.contains(dir.getFile());
        }
        return updatedFiles;
    }

    public void saveFile(String path) {

    }

    private ApacheDirective findFirstFileDirective(ApacheDirective node, String file) {
        if (node.getFile().equals(file)) {
            return node.getParentNode();
        }
        for (ApacheDirective dir : node.getChildDirectives()) {
            ApacheDirective dd = findFirstFileDirective(dir, file);
            if (dd != null)
                return dd.getParentNode();
        }
        return null;
    }

    public void saveFile(String file, ApacheDirective dir) throws Exception {
        File fl = new File(file);
        if (!fl.exists()) {
            if (!fl.createNewFile()) {
                log.error("Failed to create apache config file: " + fl.getAbsolutePath());
                throw new IOException("Failed to create apache config file: " + fl.getAbsolutePath());
            }
        }

        OutputStream str = new FileOutputStream(fl);
        try {
            for (ApacheDirective d : dir.getChildDirectives()) {
                if (dir.getFile().equals(file)) {
                    writeToFile(str, dir, file);
                }
            }
        } finally {
            str.close();
        }
    }

    private void writeToFile(OutputStream str, ApacheDirective dir, String file) throws Exception {
        if (dir.getFile().equals(file)) {
            str.write(dir.getText().getBytes());
            if (dir.isNested()) {
                for (ApacheDirective tempDir : dir.getChildDirectives()) {
                    writeToFile(str, tempDir, file);
                }
                str.write(("").getBytes());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy