Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expired checker #312

Merged
merged 11 commits into from Feb 16, 2015
30 changes: 29 additions & 1 deletion izpack-api/src/main/java/com/izforge/izpack/api/data/Info.java
Expand Up @@ -21,7 +21,10 @@
package com.izforge.izpack.api.data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -142,6 +145,15 @@ public class Info implements Serializable
*/
private Set<TempDir> tempdirs;

/**
* The date on which the installer expires, if not null
*/
private Date expiresDate = null;
/**
* The format of the expiration date
*/
public static final String EXPIRE_DATE_FORMAT = "yyyy-MM-dd";

public boolean isPrivilegedExecutionRequired()
{
return requirePrivilegedExecution;
Expand Down Expand Up @@ -193,7 +205,7 @@ public void setRebootActionConditionID(String rebootActionConditionID)
}

/**
* The constructor, deliberatly void.
* The constructor, deliberately void.
*/
public Info()
{
Expand Down Expand Up @@ -639,4 +651,20 @@ public Set<TempDir> getTempDirs()
{
return tempdirs;
}

public Date getExpiresDate()
{
return expiresDate;
}

public void setExpiresDate(Date value)
{
expiresDate = value;
}

public void setExpiresDate(String value) throws ParseException
{
SimpleDateFormat dateFormat = new SimpleDateFormat(EXPIRE_DATE_FORMAT);
expiresDate = dateFormat.parse(value);
}
}
Expand Up @@ -72,6 +72,7 @@
import com.izforge.izpack.api.data.DynamicVariable;
import com.izforge.izpack.api.data.GUIPrefs;
import com.izforge.izpack.api.data.Info;
import static com.izforge.izpack.api.data.Info.EXPIRE_DATE_FORMAT;
import com.izforge.izpack.api.data.Info.TempDir;
import com.izforge.izpack.api.data.InstallerRequirement;
import com.izforge.izpack.api.data.LookAndFeels;
Expand Down Expand Up @@ -134,6 +135,7 @@
import com.izforge.izpack.util.PlatformModelMatcher;
import com.izforge.izpack.util.file.DirectoryScanner;
import com.izforge.izpack.util.file.FileUtils;
import java.text.ParseException;

/**
* A parser for the installer xml configuration. This parses a document conforming to the
Expand Down Expand Up @@ -1947,6 +1949,22 @@ protected void addInfo(IXMLElement data) throws Exception
info.setJdkRequired("yes".equals(jdkRequired.getContent()));
}

// Does the installer expire?
IXMLElement expiresDate = root.getFirstChildNamed("expiresdate");
if (expiresDate != null)
{
try
{
info.setExpiresDate(expiresDate.getContent());
}
catch (ParseException e)
{
throw new CompilerException(
"expiresdate must be in format '" + EXPIRE_DATE_FORMAT + "'",
e);
}
}

// validate and insert (and require if -web kind) web dir
IXMLElement webDirURL = root.getFirstChildNamed("webdir");
if (webDirURL != null)
Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.izforge.izpack.installer.data.UninstallDataWriter;
import com.izforge.izpack.installer.event.InstallerListeners;
import com.izforge.izpack.installer.event.ProgressNotifiersImpl;
import com.izforge.izpack.installer.requirement.ExpiredChecker;
import com.izforge.izpack.installer.requirement.InstallerRequirementChecker;
import com.izforge.izpack.installer.requirement.JDKChecker;
import com.izforge.izpack.installer.requirement.JavaVersionChecker;
Expand Down Expand Up @@ -97,6 +98,7 @@ protected void registerComponents(MutablePicoContainer pico)
addComponent(JavaVersionChecker.class);
addComponent(JDKChecker.class);
addComponent(LangPackChecker.class);
addComponent(ExpiredChecker.class);
addComponent(RequirementsChecker.class);
addComponent(LockFileChecker.class);
addComponent(MergeManagerImpl.class);
Expand Down
@@ -0,0 +1,125 @@
/*
* IzPack - Copyright 2001-2012 Julien Ponge, All Rights Reserved.
*
* http://izpack.org/
* http://izpack.codehaus.org/
*
* Copyright 2013 Bill Root
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.izforge.izpack.installer.requirement;

import com.izforge.izpack.api.data.InstallData;
import com.izforge.izpack.api.handler.Prompt;
import com.izforge.izpack.api.installer.RequirementChecker;
import java.util.Date;

/**
* Verifies that the installer has not expired.
*
* @author Bill Root
*/
public class ExpiredChecker implements RequirementChecker
{
/**
* The installation data.
*/
private final InstallData installData;

/**
* The prompt.
*/
private final Prompt prompt;


/**
* Constructs a <tt>ExpiredChecker</tt>.
*
* @param installData the installation data
* @param prompt the prompt
*/
public ExpiredChecker(InstallData installData, Prompt prompt)
{
this.installData = installData;
this.prompt = prompt;
}

/**
* Determines whether the installer expires, and if so, whether it has.
*
* @return <tt>true</tt> if installer has NOT expired, otherwise
* <tt>false</tt>
*/
@Override
public boolean check()
{
if (!expires())
return true;

try
{
if (expired())
{
showExpired();
return false;
}
else
return true;
}
catch (Exception ex)
{
prompt.error(String.format(
"Could not check expiration date because: %s. Please correct the installer.",
ex.toString()));
// we return true so user can workaround installer problem
return true;
}
}


private boolean expired()
{
Date expiresDate = installData.getInfo().getExpiresDate();
return new Date().after(expiresDate);
}


/**
* Determines whether the installer expires.
*
* @return <tt>true</tt> if installer expires, otherwise <tt>false</tt>
*/
private boolean expires()
{
return installData.getInfo().getExpiresDate() != null;
}


/**
* Invoked when the installer has expired.
* <p/>
* This tells the user why we're canceling.
*/
protected void showExpired()
{
String message = "This installer has expired.";
if (installData.getInfo().getAppURL() != null)
{
String urlText = installData.getInfo().getAppURL();
message += "\n\n"
+ "Please download a new one from " + urlText;
}
prompt.error(message);
}
}
Expand Up @@ -57,6 +57,11 @@ public class RequirementsChecker implements RequirementChecker
*/
private final LockFileChecker lockChecker;

/**
* The expired installer checker.
*/
private final ExpiredChecker expiredChecker;

/**
* The installer requirement checker.
*/
Expand All @@ -70,17 +75,20 @@ public class RequirementsChecker implements RequirementChecker
* @param versionChecker the java version checker
* @param jdkChecker the JDK checker
* @param lockChecker the lock file checker
* @param expiredChecker the expiration checker
* @param installerRequirementChecker the installer requirement checker
*/
public RequirementsChecker(Variables variables, LangPackChecker langChecker, JavaVersionChecker versionChecker,
JDKChecker jdkChecker, LockFileChecker lockChecker,
ExpiredChecker expiredChecker,
InstallerRequirementChecker installerRequirementChecker)
{
this.variables = variables;
this.versionChecker = versionChecker;
this.jdkChecker = jdkChecker;
this.lockChecker = lockChecker;
this.langChecker = langChecker;
this.expiredChecker = expiredChecker;
this.installerRequirementChecker = installerRequirementChecker;
}

Expand All @@ -94,6 +102,7 @@ public boolean check()
{
variables.refresh();
return langChecker.check() && versionChecker.check() && jdkChecker.check() && lockChecker.check() &&
expiredChecker.check() &&
installerRequirementChecker.check();
}
}
Expand Up @@ -31,6 +31,7 @@
import com.izforge.izpack.api.handler.Prompt;
import com.izforge.izpack.api.installer.RequirementChecker;
import com.izforge.izpack.api.resource.Locales;
import com.izforge.izpack.core.data.DefaultVariables;
import com.izforge.izpack.core.handler.ConsolePrompt;
import com.izforge.izpack.installer.data.InstallData;
import com.izforge.izpack.test.util.TestConsole;
Expand Down Expand Up @@ -64,7 +65,7 @@ public abstract class AbstractRequirementCheckerTest
*/
public AbstractRequirementCheckerTest()
{
installData = new InstallData(null, Platforms.FEDORA_LINUX);
installData = new InstallData(new DefaultVariables(), Platforms.FEDORA_LINUX);
Info info = new Info();
installData.setInfo(info);

Expand Down