/*
* Created on 27-mei-2006.
*
* This software is published under the "GNU General Public
* license", see http://www.gnu.org/copyleft/gpl.html for
* additional information.
*
*/
package net.sf.myjaut.version;
import net.sf.myjaut.cmd.HttpCommand;
import net.sf.myjaut.cmd.HttpCommandException;
import net.sf.myjaut.xml.ValueNotFoundException;
import net.sf.myjaut.xml.XmlProperties;
public class VersionChecker {
private static final String RECENT_VERSION_TAG_NAME = "recent-version";
private static final String LAST_SUPPORTED_VERSION_TAG_NAME = "last-version-supported";
public VersionChecker(String url) {
this.url = url;
}
private String url;
public void checkVersion() throws MoreRecentVersionExistsException, VersionOutOfSupportException, CannotCheckVersionException {
try {
if (Double.parseDouble(getLastSupportedVersion()) > Double.parseDouble(getCurrentVersion()))
throw new VersionOutOfSupportException();
if (Double.parseDouble(getRecentVersion()) > Double.parseDouble(getCurrentVersion()))
throw new MoreRecentVersionExistsException();
}
catch (ValueNotFoundException exc) {
throw new CannotCheckVersionException(exc);
}
catch (NumberFormatException exc) {
throw new CannotCheckVersionException(exc);
}
catch (VersionProvisionException exc) {
throw new CannotCheckVersionException(exc);
}
catch (NullPointerException exc) {
throw new CannotCheckVersionException(exc);
}
}
public String getLastSupportedVersion() throws NumberFormatException, ValueNotFoundException, CannotCheckVersionException {
return getXmlProperties().getValue(LAST_SUPPORTED_VERSION_TAG_NAME);
}
private XmlProperties getXmlProperties() throws CannotCheckVersionException {
HttpCommand hc = new HttpCommand(url);
try {
hc.addParameter("version", new VersionProvider().getVersion());
}
catch (VersionProvisionException exc) {
throw new CannotCheckVersionException(exc);
}
try {
hc.execute();
}
catch (HttpCommandException exc) {
throw new CannotCheckVersionException(exc);
}
return hc.getXmlProperties();
}
private String getCurrentVersion() throws NumberFormatException, VersionProvisionException {
return new VersionProvider().getVersion();
}
public String getRecentVersion() throws NumberFormatException, ValueNotFoundException, CannotCheckVersionException {
return getXmlProperties().getValue(RECENT_VERSION_TAG_NAME);
}
}