
net.customware.license.support.restriction.TimestampRestriction Maven / Gradle / Ivy
The newest version!
package net.customware.license.support.restriction;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.customware.license.support.simple.SimpleLicenseContent;
import net.customware.license.support.simple.SimpleRestriction;
import net.customware.license.support.simple.SimpleRestrictionException;
/**
* This validates that the license has a timestamp which is after the
* timestamp for the validator. This ensures that the license is more recent
* than the licensee.
*
* @author David Peterson
*/
public class TimestampRestriction extends SimpleRestriction {
private long timestamp;
public TimestampRestriction( String timestamp, String format ) {
SimpleDateFormat df = new SimpleDateFormat( format );
try {
this.timestamp = df.parse( timestamp ).getTime();
} catch ( ParseException e ) {
throw new IllegalArgumentException( "Invalid timestamp for the specified format: " + timestamp );
}
}
public TimestampRestriction( Date timestamp ) {
this.timestamp = timestamp.getTime();
}
public TimestampRestriction( long timestamp ) {
this.timestamp = timestamp;
}
@Override protected void checkRestriction( Object context, SimpleLicenseContent content, Object attributeValue )
throws SimpleRestrictionException {
// If we don't have a specific timestamp to check, just pass it through.
if ( timestamp <= 0 )
return;
// If no timestamp was set to check, continue on.
if ( attributeValue == null )
return;
long cTimestamp = getTimestamp( attributeValue );
if ( cTimestamp <= 0 )
throw new SimpleRestrictionException( "The license does not provide a timestamp." );
if ( cTimestamp < timestamp )
throw new SimpleRestrictionException( "The license timestamp is too old." );
}
public static boolean setTimestamp( SimpleLicenseContent content, long timestamp ) {
return content.setAttribute( TimestampRestriction.class, new Long( timestamp ) );
}
public static long getTimestamp( Object attributeValue ) {
Long timestamp = ( Long ) attributeValue;
return timestamp == null ? -1L : timestamp.longValue();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy