Search in sources :

Example 1 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class MatrixProject method loadConfigurations.

/**
     * Recursively search for configuration and put them to the map
     *
     * <p>
     * The directory structure would be <tt>axis-a/b/axis-c/d/axis-e/f</tt> for
     * combination [a=b,c=d,e=f]. Note that two combinations [a=b,c=d] and [a=b,c=d,e=f]
     * can both co-exist (where one is an archived record and the other is live, for example)
     * so search needs to be thorough.
     *
     * @param dir
     *      Directory to be searched.
     * @param result
     *      Receives the loaded {@link MatrixConfiguration}s.
     * @param combination
     *      Combination of key/values discovered so far while traversing the directories.
     *      Read-only.
     */
private void loadConfigurations(File dir, CopyOnWriteMap.Tree<Combination, MatrixConfiguration> result, Map<String, String> combination) {
    File[] axisDirs = dir.listFiles(new FileFilter() {

        public boolean accept(File child) {
            return child.isDirectory() && child.getName().startsWith("axis-");
        }
    });
    if (axisDirs == null)
        return;
    for (File subdir : axisDirs) {
        // axis name
        String axis = subdir.getName().substring(5);
        File[] valuesDir = subdir.listFiles(new FileFilter() {

            public boolean accept(File child) {
                return child.isDirectory();
            }
        });
        // no values here
        if (valuesDir == null)
            continue;
        for (File v : valuesDir) {
            Map<String, String> c = new HashMap<String, String>(combination);
            c.put(axis, TokenList.decode(v.getName()));
            try {
                XmlFile config = Items.getConfigFile(v);
                if (config.exists()) {
                    Combination comb = new Combination(c);
                    // if we already have this in memory, just use it.
                    // otherwise load it
                    MatrixConfiguration item = null;
                    if (this.configurations != null)
                        item = this.configurations.get(comb);
                    if (item == null) {
                        item = (MatrixConfiguration) config.read();
                        item.setCombination(comb);
                        item.onLoad(this, v.getName());
                    }
                    result.put(item.getCombination(), item);
                }
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Failed to load matrix configuration " + v, e);
            }
            loadConfigurations(v, result, c);
        }
    }
}
Also used : XmlFile(hudson.XmlFile) HashMap(java.util.HashMap) IOException(java.io.IOException) FileFilter(java.io.FileFilter) XmlFile(hudson.XmlFile) File(java.io.File)

Example 2 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class LogRecorder method doConfigSubmit.

/**
     * Accepts submission from the configuration page.
     */
public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
    JSONObject src = req.getSubmittedForm();
    String newName = src.getString("name"), redirect = ".";
    XmlFile oldFile = null;
    if (!name.equals(newName)) {
        Hudson.checkGoodName(newName);
        oldFile = getConfigFile();
        // rename
        getParent().logRecorders.remove(name);
        this.name = newName;
        getParent().logRecorders.put(name, this);
        redirect = "../" + Util.rawEncode(newName) + '/';
    }
    List<Target> newTargets = req.bindJSONToList(Target.class, src.get("targets"));
    for (Target t : newTargets) t.enable();
    targets.replaceBy(newTargets);
    save();
    if (oldFile != null)
        oldFile.delete();
    rsp.sendRedirect2(redirect);
}
Also used : JSONObject(net.sf.json.JSONObject) XmlFile(hudson.XmlFile)

Example 3 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class Fingerprint method load.

/*package*/
static Fingerprint load(File file) throws IOException {
    XmlFile configFile = getConfigFile(file);
    if (!configFile.exists())
        return null;
    long start = 0;
    if (logger.isLoggable(Level.FINE))
        start = System.currentTimeMillis();
    try {
        Fingerprint f = (Fingerprint) configFile.read();
        if (logger.isLoggable(Level.FINE))
            logger.fine("Loading fingerprint " + file + " took " + (System.currentTimeMillis() - start) + "ms");
        return f;
    } catch (IOException e) {
        if (file.exists() && file.length() == 0) {
            // Despite the use of AtomicFile, there are reports indicating that people often see
            // empty XML file, presumably either due to file system corruption (perhaps by sudden
            // power loss, etc.) or abnormal program termination.
            // generally we don't want to wipe out user data just because we can't load it,
            // but if the file size is 0, which is what's reported in HUDSON-2012, then it seems
            // like recovering it silently by deleting the file is not a bad idea.
            logger.log(Level.WARNING, "Size zero fingerprint. Disk corruption? " + configFile, e);
            file.delete();
            return null;
        }
        logger.log(Level.WARNING, "Failed to load " + configFile, e);
        throw e;
    }
}
Also used : XmlFile(hudson.XmlFile) IOException(java.io.IOException)

Example 4 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class Queue method load.

/**
     * Loads the queue contents that was {@link #save() saved}.
     */
public synchronized void load() {
    try {
        // first try the old format
        File queueFile = getQueueFile();
        if (queueFile.exists()) {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(queueFile)));
            String line;
            while ((line = in.readLine()) != null) {
                AbstractProject j = Hudson.getInstance().getItemByFullName(line, AbstractProject.class);
                if (j != null)
                    j.scheduleBuild();
            }
            in.close();
            // discard the queue file now that we are done
            queueFile.delete();
        } else {
            queueFile = getXMLQueueFile();
            if (queueFile.exists()) {
                List list = (List) new XmlFile(XSTREAM, queueFile).read();
                int maxId = 0;
                for (Object o : list) {
                    if (o instanceof Task) {
                        // backward compatibility
                        schedule((Task) o, 0);
                    } else if (o instanceof Item) {
                        Item item = (Item) o;
                        if (item.task == null)
                            // botched persistence. throw this one away
                            continue;
                        maxId = Math.max(maxId, item.id);
                        if (item instanceof WaitingItem) {
                            waitingList.add((WaitingItem) item);
                        } else if (item instanceof BlockedItem) {
                            blockedProjects.put(item.task, (BlockedItem) item);
                        } else if (item instanceof BuildableItem) {
                            buildables.add((BuildableItem) item);
                        } else {
                            throw new IllegalStateException("Unknown item type! " + item);
                        }
                    }
                // this conveniently ignores null
                }
                WaitingItem.COUNTER.set(maxId);
                // I just had an incident where all the executors are dead at AbstractProject._getRuns()
                // because runs is null. Debugger revealed that this is caused by a MatrixConfiguration
                // object that doesn't appear to be de-serialized properly.
                // I don't know how this problem happened, but to diagnose this problem better
                // when it happens again, save the old queue file for introspection.
                File bk = new File(queueFile.getPath() + ".bak");
                bk.delete();
                queueFile.renameTo(bk);
                queueFile.delete();
            }
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "Failed to load the queue file " + getXMLQueueFile(), e);
    }
}
Also used : AbstractQueueTask(hudson.model.queue.AbstractQueueTask) SafeTimerTask(hudson.triggers.SafeTimerTask) SubTask(hudson.model.queue.SubTask) InputStreamReader(java.io.InputStreamReader) XmlFile(hudson.XmlFile) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ExtensionPoint(hudson.ExtensionPoint) BufferedReader(java.io.BufferedReader) List(java.util.List) ArrayList(java.util.ArrayList) ExtensionList(hudson.ExtensionList) XmlFile(hudson.XmlFile) File(java.io.File)

Example 5 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class UpdateCenter method load.

/**
     * Loads the data from the disk into this object.
     */
public synchronized void load() throws IOException {
    UpdateSite defaultSite = new UpdateSite("default", config.getUpdateCenterUrl() + "update-center.json");
    XmlFile file = getConfigFile();
    if (file.exists()) {
        try {
            sites.replaceBy(((PersistedList) file.unmarshal(sites)).toList());
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Failed to load " + file, e);
        }
        for (UpdateSite site : sites) {
            // replace the legacy site with the new site
            if (site.isLegacyDefault()) {
                sites.remove(site);
                sites.add(defaultSite);
                break;
            }
        }
    } else {
        if (sites.isEmpty()) {
            // If there aren't already any UpdateSources, add the default one.
            // to maintain compatibility with existing UpdateCenterConfiguration, create the default one as specified by UpdateCenterConfiguration
            sites.add(defaultSite);
        }
    }
}
Also used : XmlFile(hudson.XmlFile) IOException(java.io.IOException)

Aggregations

XmlFile (hudson.XmlFile)18 File (java.io.File)8 IOException (java.io.IOException)8 FileFilter (java.io.FileFilter)2 ArrayList (java.util.ArrayList)2 ANTLRException (antlr.ANTLRException)1 ExtensionList (hudson.ExtensionList)1 ExtensionListView (hudson.ExtensionListView)1 ExtensionPoint (hudson.ExtensionPoint)1 RestartNotSupportedException (hudson.lifecycle.RestartNotSupportedException)1 Action (hudson.model.Action)1 FormException (hudson.model.Descriptor.FormException)1 AbstractQueueTask (hudson.model.queue.AbstractQueueTask)1 SubTask (hudson.model.queue.SubTask)1 FullControlOnceLoggedInAuthorizationStrategy (hudson.security.FullControlOnceLoggedInAuthorizationStrategy)1 LegacySecurityRealm (hudson.security.LegacySecurityRealm)1 NodeList (hudson.slaves.NodeList)1 SafeTimerTask (hudson.triggers.SafeTimerTask)1 AtomicFileWriter (hudson.util.AtomicFileWriter)1 IOException2 (hudson.util.IOException2)1