⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 relativevalidityperiod.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
* Copyright (c) 2000-2005, University of Salford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this 
* list of conditions and the following disclaimer.
* 
* Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation 
* and/or other materials provided with the distribution. 
*
* Neither the name of the University of Salford nor the names of its 
* contributors may be used to endorse or promote products derived from this 
* software without specific prior written permission. 
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
* POSSIBILITY OF SUCH DAMAGE.
*/

package issrg.pba.rbac;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * This is a class representing a ValidityPeriod relative to the current time.
 * Its notBefore and notAfter time depend on the time of evaluation and are 
 * specified as an offset from the current time.
 *
 * <p>A positive offset to the notBefore end of the period means it is in the
 * past from now (and a negative means it is in the future from now). A 
 * positive offset to the notAfter end of the period means it is in the future
 * from now (and a negative means it is in the past from now).
 */
public class RelativeValidityPeriod extends DefaultValidityPeriodBehaviour {

  /**
   * This is the actual clock used for calculating relative periods. At construction time it is
   * copied from the CustomisePERMIS.systemClock variable.
   */
  protected Clock theClock = CustomisePERMIS.getSystemClock();

  private GregorianCalendar gc = new GregorianCalendar();

  protected boolean unlimited=false;   // i.e. only one side of the period is defined (see constructors with notAfter boolean)
  protected boolean notAfter;         // in that case this variable decides the limited edge
  protected RelativeDate nb;
  protected RelativeDate na;

  protected RelativeValidityPeriod() {
  }

  /**
   * The integers specify how many years, months, etc to add (you can use 
   * negative values as well) to
   * Now to get notBefore or notAfter date. If notAfter flag is true, the 
   * integers will specify a notAfter;
   * otherwise it specifies notBefore. The other end of the period is infinite.
   *
   * @param years - the offset in years
   * @param months - the offset in months
   * @param days - the offset in days
   * @param hours - the offset in hours
   * @param minutes - the offset in minutes
   * @param seconds - the offset in seconds
   * @param notAfter - the flag specifying whether this is the offset for 
   *   notAfter or notBefore; if true, the specified offset is for notAfter,
   *   otherwise for notBefore; the other end of the period is infinite.
   */
  public RelativeValidityPeriod(int years, int months, int days, int hours, int minutes, int seconds, boolean notAfter){
    unlimited=true;
    this.notAfter=notAfter;

    // note that the values are assigned to both notBefore and notAfter, but they will be used only
    // as notAfter flag specifies. It is done this way to avoid redundant ifs
    nb = na = new RelativeDate(years, months, days, hours, minutes, seconds);
  }

  /**
   * This constructor builds the object using a reference to a ready to use 
   * RelativeDate object.
   * The meaning of the notAfter flag is the same, as in 
   * RelativeValidityPeriod(int,int,int,int,int,int,boolean)
   * constructor.
   * 
   * @param rd - the RelativeDate
   * @param notAfter - the flag telling if it is the RelativeDate of the 
   *   notAfter or notBefore
   */
  public RelativeValidityPeriod(RelativeDate rd, boolean notAfter){
    unlimited=true;
    this.notAfter=notAfter;

    nb=na=rd;
  }

  /**
   * This constructor lets build a RelativeValidityPeriod by specifying offsets
   * for both the notBefore and notAfter ends explicitly. All nb* parameters
   * specify the notBefore offset, all na* parameters specify the notAfter 
   * offset.
   * 
   */
  public RelativeValidityPeriod(int nbYears, int nbMonths, int nbDays, int nbHours, int nbMinutes, int nbSeconds,
                      int naYears, int naMonths, int naDays, int naHours, int naMinutes, int naSeconds){
    nb = new RelativeDate(nbYears, nbMonths, nbDays, nbHours, nbMinutes, nbSeconds);
    na = new RelativeDate(naYears, naMonths, naDays, naHours, naMinutes, naSeconds);
  }

  /**
   * This constructor specifies a RelativeValidityPeriod with the given 
   * RelativeDates of the notBefore and notAfter ends.
   *
   * @param notBefore - the RelativeDate of the notBefore end of the period
   * @param notAfter - the RelativeDate of the notAfter end of the period
   */
  public RelativeValidityPeriod(RelativeDate notBefore, RelativeDate notAfter){
    nb=notBefore;
    na=notAfter;
  }

  /**
   * This method returns the notBefore time offset from current time (as 
   * provided by the Clock). The offset is subtracted from now, i.e. the 
   * positive offset moves the end into the past.
   *
   * @return the absolute Date computed from now, or null, if the validity 
   *   period is unlimited on this end (lasts since the time immemorial)
   */
  public Date getNotBefore(){
    if (unlimited && notAfter){
      return null;
    }

    synchronized(gc){
      gc.setTime(getNow());
      gc.add(gc.YEAR, -nb.years);
      gc.add(gc.MONTH, -nb.months);
      gc.add(gc.DATE, -nb.days);
      gc.add(gc.HOUR, -nb.hours);
      gc.add(gc.MINUTE, -nb.minutes);
      gc.add(gc.SECOND, -nb.seconds);

      return gc.getTime();
    }
  }

  /**
   * This method returns the notAfter time offset from current time (as 
   * provided by the Clock). The offset is added to now, i.e. the 
   * positive offset moves the end into the future.
   *
   * @return the absolute Date computed from now, or null, if the validity 
   *   period is unlimited on this end (lasts forever)
   */
  public Date getNotAfter(){
    if (unlimited && !notAfter){
      return null;
    }

    synchronized(gc){
      gc.setTime(getNow());
      gc.add(gc.YEAR, na.years);
      gc.add(gc.MONTH, na.months);
      gc.add(gc.DATE, na.days);
      gc.add(gc.HOUR, na.hours);
      gc.add(gc.MINUTE, na.minutes);
      gc.add(gc.SECOND, na.seconds);

      return gc.getTime();
    }
  }

  /**
   * This is a utility method that returns the Date corresponding to the 
   * current time returned by the Clock.
   */
  private java.util.Date getNow(){
    java.util.Date tm = theClock.getTime();
    long l = tm.getTime();
    tm.setTime(l - (l % 1000));   // removing milliseconds; we are not that precise
    return tm;
  }

  public Object clone(){
    RelativeValidityPeriod rp = new RelativeValidityPeriod(nb, na);
    rp.unlimited=unlimited;
    rp.notAfter=notAfter;

    return rp;
  }

  public String toString(){
    return "relative ("
            +((unlimited && notAfter)?"-oo":nb.toString())
            +"; "
            +((unlimited && !notAfter)?"+oo":na.toString())
            +")";
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -