Skip to content

Commit

Permalink
fix issue when loading config
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed Mar 18, 2012
1 parent ad1e807 commit dd31672
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 180 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-core</artifactId>
<version>1.424</version>
<version>1.453-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,25 @@ of this software and associated documentation files (the "Software"), to deal

import hudson.BulkChange;
import hudson.XmlFile;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import jenkins.model.Jenkins;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ConfigDescription;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import jenkins.model.Jenkins;

/**
* Backward compatibility layer for old subtypes of {@link ConfigProvider}
*
* @deprecated as of 1.2.
* Extend {@link AbstractConfigProviderImpl} directly.
* @deprecated as of 1.2. Extend {@link AbstractConfigProviderImpl} directly.
*/
public abstract class AbstractConfigProvider extends AbstractConfigProviderImpl {

protected final String ID_PREFIX = this.getClass().getSimpleName() + ".";
protected final String ID_PREFIX = this.getClass().getSimpleName() + ".";

public AbstractConfigProvider() {
load();
}
public AbstractConfigProvider() {
load();
}

@Override
public String getProviderId() {
Expand All @@ -65,38 +58,39 @@ public String getDisplayName() {

/**
* Overridden for backward compatibility to let subtype customize the file name.
*/
*/
@Override
public void save() {
if (BulkChange.contains(this))
return;
try {
getConfigXml().write(this);
SaveableListener.fireOnChange(this, getConfigXml());
} catch (IOException e) {
e.printStackTrace();
}
}
public void save() {
if (BulkChange.contains(this))
return;
try {
getConfigXml().write(this);
SaveableListener.fireOnChange(this, getConfigXml());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Overridden for backward compatibility to let subtype customize the file name.
*/
public void load() {
XmlFile xml = getConfigXml();
if (xml.exists()) {
try {
xml.unmarshal(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}

protected XmlFile getConfigXml() {
return new XmlFile(Jenkins.XSTREAM, new File(Jenkins.getInstance().getRootDir(), this.getXmlFileName()));
}

protected String getXmlFileName() {
return getClass().getName()+".xml";
*/
@Override
public void load() {
XmlFile xml = getConfigXml();
if (xml.exists()) {
try {
xml.unmarshal(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}

protected XmlFile getConfigXml() {
return new XmlFile(Jenkins.XSTREAM, new File(Jenkins.getInstance().getRootDir(), this.getXmlFileName()));
}

protected String getXmlFileName() {
return getClass().getName() + ".xml";
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package org.jenkinsci.lib.configprovider;

import org.jenkinsci.lib.configprovider.model.Config;
import hudson.BulkChange;
import hudson.XmlFile;
import hudson.model.listeners.SaveableListener;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;

import org.jenkinsci.lib.configprovider.model.Config;

/**
* Partial default implementation of {@link ConfigProvider}.
Expand All @@ -16,46 +26,84 @@
*/
public abstract class AbstractConfigProviderImpl extends ConfigProvider {

protected Map<String, Config> configs = new HashMap<String, Config>();

public AbstractConfigProviderImpl() {
}

@Override
public Collection<Config> getAllConfigs() {
return Collections.unmodifiableCollection(configs.values());
}

@Override
public Config getConfigById(String configId) {
return configs.get(configId);
}

@Override
public String getProviderId() {
return getId();
}

@Override
public boolean isResponsibleFor(String configId) {
return configId != null && configId.startsWith(getProviderId());
}

@Override
public Config newConfig() {
String id = this.getProviderId() + "." + System.currentTimeMillis();
return new Config(id, null, null, null);
}

@Override
public void remove(String configId) {
configs.remove(configId);
this.save();
}

@Override
public void save(Config config) {
configs.put(config.id, config);
this.save();
}
private static final Logger LOGGER = Logger.getLogger(AbstractConfigProviderImpl.class.getName());

protected Map<String, Config> configs = new HashMap<String, Config>();

public AbstractConfigProviderImpl() {
}

@Override
public Collection<Config> getAllConfigs() {
return Collections.unmodifiableCollection(configs.values());
}

@Override
public Config getConfigById(String configId) {
return configs.get(configId);
}

@Override
public String getProviderId() {
return getId();
}

@Override
public boolean isResponsibleFor(String configId) {
return configId != null && configId.startsWith(getProviderId());
}

@Override
public Config newConfig() {
String id = this.getProviderId() + "." + System.currentTimeMillis();
return new Config(id, null, null, null);
}

@Override
public void remove(String configId) {
configs.remove(configId);
this.save();
}

@Override
public void save(Config config) {
configs.put(config.id, config);
this.save();
}

/**
* Saves the configuration info to the disk.
*/
public synchronized void save() {
if (BulkChange.contains(this))
return;
try {
getConfigXml().write(this);
SaveableListener.fireOnChange(this, getConfigXml());
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to save " + getConfigXml(), e);
}
}

/**
* Overridden for backward compatibility to let subtype customize the file name.
*/
public void load() {
XmlFile xml = getConfigXml();
if (xml.exists()) {
try {
xml.unmarshal(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}

protected XmlFile getConfigXml() {
return new XmlFile(Jenkins.XSTREAM, new File(Jenkins.getInstance().getRootDir(), this.getXmlFileName()));
}

protected String getXmlFileName() {
return getId() + ".xml";
}
}
Loading

0 comments on commit dd31672

Please sign in to comment.