Search in sources :

Example 6 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)

Example 7 with XmlFile

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

the class User method load.

/**
     * Loads the other data from disk if it's available.
     */
private synchronized void load() {
    properties.clear();
    XmlFile config = getConfigFile();
    try {
        if (config.exists())
            config.unmarshal(this);
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Failed to load " + config, e);
    }
    // remove nulls that have failed to load
    for (Iterator<UserProperty> itr = properties.iterator(); itr.hasNext(); ) {
        if (itr.next() == null)
            itr.remove();
    }
    // doing so after load makes sure that newly added user properties do get reflected
    for (UserPropertyDescriptor d : UserProperty.all()) {
        if (getProperty(d.clazz) == null) {
            UserProperty up = d.newInstance(this);
            if (up != null)
                properties.add(up);
        }
    }
    for (UserProperty p : properties) p.setUser(this);
}
Also used : XmlFile(hudson.XmlFile) IOException(java.io.IOException)

Example 8 with XmlFile

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

the class SystemServiceImpl method getConfigFile.

public XmlFile getConfigFile() {
    securityService.checkPermission(Hudson.ADMINISTER);
    // Hudson.getConfigFile() is not public, so we have to duplicate some muck here
    File f = new File(getWorkingDirectory(), "config.xml");
    return new XmlFile(Hudson.XSTREAM, f);
}
Also used : XmlFile(hudson.XmlFile) XmlFile(hudson.XmlFile) File(java.io.File)

Example 9 with XmlFile

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

the class AbstractItem method doConfigDotXml.

/**
     * Accepts <tt>config.xml</tt> submission, as well as serve it.
     */
@WebMethod(name = "config.xml")
public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp) throws IOException {
    if (req.getMethod().equals("GET")) {
        // read
        checkPermission(EXTENDED_READ);
        rsp.setContentType("application/xml");
        getConfigFile().writeRawTo(rsp.getOutputStream());
        return;
    }
    if (req.getMethod().equals("POST")) {
        // submission
        checkPermission(CONFIGURE);
        XmlFile configXmlFile = getConfigFile();
        AtomicFileWriter out = new AtomicFileWriter(configXmlFile.getFile());
        try {
            try {
                // this allows us to use UTF-8 for storing data,
                // plus it checks any well-formedness issue in the submitted
                // data
                Transformer t = TransformerFactory.newInstance().newTransformer();
                t.transform(new StreamSource(req.getReader()), new StreamResult(out));
                out.close();
            } catch (TransformerException e) {
                throw new IOException2("Failed to persist configuration.xml", e);
            }
            // try to reflect the changes by reloading
            new XmlFile(Items.XSTREAM, out.getTemporaryFile()).unmarshal(this);
            onLoad(getParent(), getRootDir().getName());
            // if everything went well, commit this new version
            out.commit();
        } finally {
            // don't leave anything behind
            out.abort();
        }
        return;
    }
    // huh?
    rsp.sendError(SC_BAD_REQUEST);
}
Also used : Transformer(javax.xml.transform.Transformer) XmlFile(hudson.XmlFile) StreamResult(javax.xml.transform.stream.StreamResult) AtomicFileWriter(hudson.util.AtomicFileWriter) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException) IOException2(hudson.util.IOException2) WebMethod(org.kohsuke.stapler.WebMethod)

Example 10 with XmlFile

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

the class Hudson method loadTasks.

private synchronized TaskBuilder loadTasks() throws IOException {
    File projectsDir = new File(root, "jobs");
    if (!projectsDir.isDirectory() && !projectsDir.mkdirs()) {
        if (projectsDir.exists()) {
            throw new IOException(projectsDir + " is not a directory");
        }
        throw new IOException("Unable to create " + projectsDir + "\nPermission issue? Please create this directory manually.");
    }
    File[] subdirs = projectsDir.listFiles(new FileFilter() {

        public boolean accept(File child) {
            return child.isDirectory() && Items.getConfigFile(child).exists();
        }
    });
    TaskGraphBuilder g = new TaskGraphBuilder();
    Handle loadHudson = g.requires(InitMilestone.EXTENSIONS_AUGMENTED).attains(InitMilestone.JOB_LOADED).add("Loading global config", new Executable() {

        public void run(Reactor session) throws Exception {
            XmlFile cfg = getConfigFile();
            if (cfg.exists()) {
                // reset some data that may not exist in the disk file
                // so that we can take a proper compensation action later.
                primaryView = null;
                views.clear();
                // load from disk
                cfg.unmarshal(Hudson.this);
            }
            // if we are loading old data that doesn't have this field
            if (slaves == null) {
                slaves = new NodeList();
            }
            clouds.setOwner(Hudson.this);
            items.clear();
        }
    });
    for (final File subdir : subdirs) {
        g.requires(loadHudson).attains(InitMilestone.JOB_LOADED).notFatal().add("Loading job " + subdir.getName(), new Executable() {

            public void run(Reactor session) throws Exception {
                TopLevelItem item = (TopLevelItem) Items.load(Hudson.this, subdir);
                items.put(item.getName(), item);
            }
        });
    }
    g.requires(InitMilestone.JOB_LOADED).add("Finalizing set up", new Executable() {

        public void run(Reactor session) throws Exception {
            rebuildDependencyGraph();
            {
                // recompute label objects - populates the labels mapping.
                for (// Note that not all labels are visible until the slaves have connected.
                Node slave : // Note that not all labels are visible until the slaves have connected.
                slaves) {
                    slave.getAssignedLabels();
                }
                getAssignedLabels();
            }
            // this is both for clean Hudson and for backward compatibility.
            if (views.size() == 0 || primaryView == null) {
                View v = new AllView(Messages.Hudson_ViewName());
                v.owner = Hudson.this;
                views.add(0, v);
                primaryView = v.getViewName();
            }
            // read in old data that doesn't have the security field set
            if (authorizationStrategy == null) {
                if (useSecurity == null || !useSecurity) {
                    authorizationStrategy = AuthorizationStrategy.UNSECURED;
                } else {
                    authorizationStrategy = new FullControlOnceLoggedInAuthorizationStrategy();
                }
            }
            if (securityRealm == null) {
                if (useSecurity == null || !useSecurity) {
                    setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
                } else {
                    setSecurityRealm(new LegacySecurityRealm());
                }
            } else {
                // force the set to proxy
                setSecurityRealm(securityRealm);
            }
            if (useSecurity != null && !useSecurity) {
                // forced reset to the unsecure mode.
                // this works as an escape hatch for people who locked themselves out.
                authorizationStrategy = AuthorizationStrategy.UNSECURED;
                setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
            }
            // Initialize the filter with the crumb issuer
            setCrumbIssuer(crumbIssuer);
            // auto register root actions
            for (Action a : getExtensionList(RootAction.class)) {
                if (!actions.contains(a)) {
                    actions.add(a);
                }
            }
        }
    });
    return g;
}
Also used : FullControlOnceLoggedInAuthorizationStrategy(hudson.security.FullControlOnceLoggedInAuthorizationStrategy) XmlFile(hudson.XmlFile) NodeList(hudson.slaves.NodeList) IOException(java.io.IOException) ExtensionListView(hudson.ExtensionListView) FormException(hudson.model.Descriptor.FormException) RestartNotSupportedException(hudson.lifecycle.RestartNotSupportedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) ReactorException(org.jvnet.hudson.reactor.ReactorException) TimeoutException(java.util.concurrent.TimeoutException) ParseException(java.text.ParseException) JellyException(org.apache.commons.jelly.JellyException) ANTLRException(antlr.ANTLRException) AcegiSecurityException(org.acegisecurity.AcegiSecurityException) BindException(java.net.BindException) AccessDeniedException(org.acegisecurity.AccessDeniedException) TaskGraphBuilder(org.jvnet.hudson.reactor.TaskGraphBuilder) Handle(org.jvnet.hudson.reactor.TaskGraphBuilder.Handle) LegacySecurityRealm(hudson.security.LegacySecurityRealm) FileFilter(java.io.FileFilter) Executable(org.jvnet.hudson.reactor.Executable) Reactor(org.jvnet.hudson.reactor.Reactor) TextFile(hudson.util.TextFile) XmlFile(hudson.XmlFile) File(java.io.File)

Aggregations

XmlFile (hudson.XmlFile)14 IOException (java.io.IOException)8 File (java.io.File)6 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 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 TextFile (hudson.util.TextFile)1