DeployLX Software Protection System

Limit..::..Peek Method

Allows a limit to communicate it's status without interacting with the user. Used with child limits to pick limits that are valid before attempting to interact with the user.

Syntax

Public Overridable Function Peek ( _
	context As SecureLicenseContext _
) As PeekResult
public virtual PeekResult Peek(
	SecureLicenseContext context
)

Parameters

context
The SecureLicenseContext used to determine the environment and settings of the licensing.

Return Value

Returns one of the PeekResult values indicating the status of the limit.

Remarks

When the SecureLicenseManager is attempting to find a license it will peek each license to find the license that is likely to require the least amount of interaction with the user. The Peek status is also used by the OrLimit to find a child limit that is valid without attempting to fully validate each one.

The result obtained from peeking the status of a limit is used only as a hint and the limit will still have an opportunity to perform full validation before a license is considered valid.

Caution Custom limits that override the Peek method must never show a form to the user.

Examples

This sample demonstrates a basic custom limit. See the Custom Limits topic for details on creating a custom limit.
Visual Basic
Imports Microsoft.VisualBasic
Imports System
Imports DeployLX.Licensing.v5

Namespace VBProtectedDll
    Public Class EnvironmentCheckLimit
        Inherits Limit
        Public Sub New()
        End Sub

        Public Overrides ReadOnly Property Description() As String
            Get
                Return "Checks that an environment variable has been set to a specific value."
            End Get
        End Property

        Public Overrides ReadOnly Property Name() As String
            Get
                Return "Environment Check"
            End Get
        End Property

        Public Property VariableName() As String
            Get
                Return _variableName
            End Get
            Set
                AssertNotReadOnly()
                If _variableName <> Value Then
                    _variableName = Value
                    OnChanged("VariableName")
                End If
            End Set
        End Property
        Private _variableName As String

        Public Property VariableValue() As String
            Get
                Return _variableValue
            End Get
            Set
                AssertNotReadOnly()
                If _variableValue <> Value Then
                    _variableValue = Value
                    OnChanged("VariableValue")
                End If
            End Set
        End Property
        Private _variableValue As String

        Public Overrides Function Validate(ByVal context As SecureLicenseContext) As ValidationResult
            If (Not IsVariableSet(context, True)) Then
                Return ValidationResult.Invalid
            End If

            Return MyBase.Validate(context)
        End Function


        Public Overrides Function Granted(ByVal context As SecureLicenseContext) As ValidationResult
            If (Not IsVariableSet(context, True)) Then
                Return ValidationResult.Invalid
            End If
            Return MyBase.Granted(context)
        End Function

        Public Overrides Function Peek(ByVal context As SecureLicenseContext) As PeekResult
            If (Not IsVariableSet(context, False)) Then
                Return PeekResult.Invalid
            End If

            Return MyBase.Peek(context)
        End Function

        Protected Overrides Function WriteToXml(ByVal writer As System.Xml.XmlWriter, ByVal signing As LicenseSaveType) As Boolean
            If _variableName Is Nothing Then
                Return False
            End If

            If _variableValue Is Nothing Then
                Return False
            End If

            writer.WriteAttributeString("variableName", _variableName)
            writer.WriteAttributeString("variableValue", _variableValue)

            ' Any child limits will automatically be written to the XML.
            Return True
        End Function

        Protected Overrides Function ReadFromXml(ByVal reader As System.Xml.XmlReader) As Boolean
            _variableName = reader.GetAttribute("variableName")
            _variableValue = reader.GetAttribute("variableValue")

            Return ReadChildLimits(reader)
        End Function

        Protected Function IsVariableSet(ByVal context As SecureLicenseContext, ByVal reportError As Boolean) As Boolean
            Try
                Dim value As String = Environment.GetEnvironmentVariable(_variableName)
                If value Is Nothing OrElse value <> _variableValue Then
                    If reportError Then
                        context.ReportError("Environment variable ({0}) missing or not set.", Me, Nothing, ErrorSeverity.Normal, _variableName)
                    End If
                    Return False
                End If
                Return True
            Catch ex As Exception
                If reportError Then
                    context.ReportError("Could not check environment.", Me, ex, ErrorSeverity.High)
                End If
                Return False
            End Try
        End Function

    End Class
End Namespace
C#
using System;
using DeployLX.Licensing.v5;

namespace CSharpProtectedDll
{
    public class EnvironmentCheckLimit : Limit
    {
        public EnvironmentCheckLimit()
        {
        }

        public override string Description
        {
            get { return "Checks that an environment variable has been set to a specific value."; }
        }

        public override string Name
        {
            get { return "Environment Check"; }
        }

        public string VariableName
        {
            get { return _variableName; }
            set {
                AssertNotReadOnly();
                if( _variableName != value )
                {
                    _variableName = value;
                    OnChanged( "VariableName" );
                }
            }
        }
        private string _variableName;

        public string VariableValue
        {
            get { return _variableValue; }
            set {
                AssertNotReadOnly();
                if( _variableValue != value )
                {
                    _variableValue = value;
                    OnChanged( "VariableValue" );
                }
            }
        }
        private string _variableValue;

        public override ValidationResult Validate( SecureLicenseContext context )
        {
            if( !IsVariableSet( context, true ) )
                return ValidationResult.Invalid;

            return base.Validate( context );
        }


        public override ValidationResult Granted( SecureLicenseContext context )
        {
            if( !IsVariableSet( context, true ) )
                return ValidationResult.Invalid;
            return base.Granted( context );
        }

        public override PeekResult Peek( SecureLicenseContext context )
        {
            if( !IsVariableSet( context, false ) )
                return PeekResult.Invalid;

            return base.Peek( context );
        }

        protected override bool WriteToXml( System.Xml.XmlWriter writer, LicenseSaveType signing )
        {
            if( _variableName == null )
                return false;

            if( _variableValue == null )
                return false;

            writer.WriteAttributeString( "variableName", _variableName );
            writer.WriteAttributeString( "variableValue", _variableValue );

            // Any child limits will automatically be written to the XML.
            return true;
        }

        protected override bool ReadFromXml( System.Xml.XmlReader reader )
        {
            _variableName = reader.GetAttribute( "variableName" );
            _variableValue = reader.GetAttribute( "variableValue" );

            return ReadChildLimits( reader );
        }

        protected bool IsVariableSet( SecureLicenseContext context, bool reportError )
        {
            try
            {
                string value = Environment.GetEnvironmentVariable( _variableName );
                if( value == null || value != _variableValue )
                {
                    if( reportError )
                        context.ReportError( "Environment variable ({0}) missing or not set.", this, null, ErrorSeverity.Normal, _variableName );
                    return false;
                }
                return true;
            }
            catch( Exception ex )
            {
                if( reportError )
                    context.ReportError( "Could not check environment.", this, ex, ErrorSeverity.High );
                return false;
            }
        }

    }
}

Assembly:  DeployLX.Licensing.v5 (in DeployLX.Licensing.v5.dll)

See Also