dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/System/ComponentModel LicFileLicenseP


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/System/ComponentModel LicFileLicenseProvider.cs, 1.1, 1.2 LicenseManager.cs, 1.2, 1.3 LicenseProvider.cs, 1.1, 1.2
Date: Tue, 16 Sep 2003 20:27:04 -0400

Update of /cvsroot/dotgnu-pnet/pnetlib/System/ComponentModel
In directory subversions:/tmp/cvs-serv21921/System/ComponentModel

Modified Files:
        LicFileLicenseProvider.cs LicenseManager.cs LicenseProvider.cs 
Log Message:


Finish off the implementation of component license management.


Index: LicFileLicenseProvider.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/System/ComponentModel/LicFileLicenseProvider.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** LicFileLicenseProvider.cs   12 Sep 2003 06:08:58 -0000      1.1
--- LicFileLicenseProvider.cs   17 Sep 2003 00:27:02 -0000      1.2
***************
*** 25,29 ****
  #if CONFIG_COMPONENT_MODEL
  
! using System;
  
  public class LicFileLicenseProvider : LicenseProvider
--- 25,30 ----
  #if CONFIG_COMPONENT_MODEL
  
! using System.IO;
! using System.ComponentModel.Design;
  
  public class LicFileLicenseProvider : LicenseProvider
***************
*** 33,43 ****
  
        // Get the license for a type.
-       [TODO]
        public override License GetLicense
                                (LicenseContext context, Type type, object 
instance,
                                 bool allowExceptions)
                        {
!                               // TODO
!                               return null;
                        }
  
--- 34,107 ----
  
        // Get the license for a type.
        public override License GetLicense
                                (LicenseContext context, Type type, object 
instance,
                                 bool allowExceptions)
                        {
!                               String key, path;
!                               StreamReader reader;
! 
!                               // Bail out if we don't have a license context.
!                               if(context == null)
!                               {
!                                       return null;
!                               }
! 
!                               // Use the saved key if we saw this type 
previously.
!                               if(context.UsageMode == 
LicenseUsageMode.Runtime)
!                               {
!                                       key = context.GetSavedLicenseKey(type, 
null);
!                                       if(key != null && IsKeyValid(key, type))
!                                       {
!                                               return new FileLicense(key);
!                                       }
!                               }
! 
!                               // Find the pathname of the assembly containing 
the type.
!                       #if CONFIG_COMPONENT_MODEL_DESIGN
!                               ITypeResolutionService trs;
!                               trs = (ITypeResolutionService)
!                                       
context.GetService(typeof(ITypeResolutionService));
!                               if(trs != null)
!                               {
!                                       path = 
trs.GetPathOfAssembly(type.Assembly.GetName());
!                                       if(path == null)
!                                       {
!                                               path = type.Assembly.Location;
!                                       }
!                               }
!                               else
!                       #endif
!                               {
!                                       path = type.Assembly.Location;
!                               }
! 
!                               // Look for a "*.lic" file for the type.
!                               path = Path.Combine(Path.GetDirectoryName(path),
!                                                                       
type.FullName + ".lic");
!                               try
!                               {
!                                       reader = new StreamReader(path);
!                               }
!                               catch(Exception)
!                               {
!                                       // Could not open the file, so assume 
unlicensed.
!                                       return null;
!                               }
! 
!                               // Read the key from the first line of the 
license file.
!                               key = reader.ReadLine();
!                               reader.Close();
! 
!                               // Bail out if the key is invalid.
!                               if(key == null || !IsKeyValid(key, type))
!                               {
!                                       return null;
!                               }
! 
!                               // Cache the key within the context.
!                               context.SetSavedLicenseKey(type, key);
! 
!                               // Return a new file license to the caller.
!                               return new FileLicense(key);
                        }
  
***************
*** 61,64 ****
--- 125,157 ----
                                }
                        }
+ 
+       // License class for files.
+       private sealed class FileLicense : License
+       {
+               // Internal state.
+               private String key;
+ 
+               // Constructor.
+               public FileLicense(String key)
+                               {
+                                       this.key = key;
+                               }
+ 
+               // Get the license key.
+               public override String LicenseKey
+                               {
+                                       get
+                                       {
+                                               return key;
+                                       }
+                               }
+ 
+               // Dispose of this license.
+               public override void Dispose()
+                               {
+                                       // Nothing to do here.
+                               }
+ 
+       }; // class FileLicense
  
  }; // class LicFileLicenseProvider

Index: LicenseManager.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System/ComponentModel/LicenseManager.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** LicenseManager.cs   16 Sep 2003 05:25:31 -0000      1.2
--- LicenseManager.cs   17 Sep 2003 00:27:02 -0000      1.3
***************
*** 26,29 ****
--- 26,31 ----
  
  using System;
+ using System.Collections;
+ using System.Reflection;
  
  public sealed class LicenseManager
***************
*** 31,34 ****
--- 33,40 ----
        // Internal state.
        private static LicenseContext currentContext;
+       private static Object contextLockedBy;
+       private static Object ourLock;
+       private static Hashtable providers;
+       private static DefaultLicenseProvider defaultProvider;
  
        // Cannot instantiate this class.
***************
*** 78,123 ****
                                return CreateWithContext(type, creationContext, 
new Object [0]);
                        }
-       [TODO]
        public static Object CreateWithContext
                                (Type type, LicenseContext creationContext, 
Object[] args)
                        {
!                               // TODO
!                               return null;
                        }
  
        // Determine if a type has a valid license.
-       [TODO]
        public static bool IsLicensed(Type type)
                        {
!                               // TODO
!                               return false;
                        }
  
        // Determine if a valid license can be granted for a type.
-       [TODO]
        public static bool IsValid(Type type)
                        {
!                               // TODO
!                               return false;
                        }
-       [TODO]
        public static bool IsValid(Type type, Object instance, out License 
license)
                        {
!                               // TODO
!                               return false;
                        }
  
        // Lock the license context associated with an object.
-       [TODO]
        public static void LockContext(Object contextUser)
                        {
!                               // TODO
                        }
  
        // Unlock the license context associated with an object.
-       [TODO]
        public static void UnlockContext(Object contextUser)
                        {
!                               // TODO
                        }
  
--- 84,245 ----
                                return CreateWithContext(type, creationContext, 
new Object [0]);
                        }
        public static Object CreateWithContext
                                (Type type, LicenseContext creationContext, 
Object[] args)
                        {
!                               lock(typeof(LicenseManager))
!                               {
!                                       // Temporarily switch to the new 
context during creation.
!                                       LicenseContext savedContext = 
currentContext;
!                                       currentContext = creationContext;
!                                       try
!                                       {
!                                               // Make sure that we are the 
only context user.
!                                               if(ourLock == null)
!                                               {
!                                                       ourLock = new Object();
!                                               }
!                                               LockContext(ourLock);
!                                               try
!                                               {
!                                                       try
!                                                       {
!                                                               return 
Activator.CreateInstance(type, args);
!                                                       }
!                                                       
catch(TargetInvocationException e)
!                                                       {
!                                                               // Re-throw the 
inner exception, if present.
!                                                               
if(e.InnerException != null)
!                                                               {
!                                                                       throw 
e.InnerException;
!                                                               }
!                                                               else
!                                                               {
!                                                                       throw;
!                                                               }
!                                                       }
!                                               }
!                                               finally
!                                               {
!                                                       UnlockContext(ourLock);
!                                               }
!                                       }
!                                       finally
!                                       {
!                                               currentContext = savedContext;
!                                       }
!                               }
                        }
  
        // Determine if a type has a valid license.
        public static bool IsLicensed(Type type)
                        {
!                               return IsValid(type);
!                       }
! 
!       // Get the license provider for a specific type.
!       private static LicenseProvider GetProvider(Type type)
!                       {
!                               Type providerType;
!                               LicenseProvider provider;
!                               Object[] attrs;
!                               lock(typeof(LicenseManager))
!                               {
!                                       // Get the cached license provider.
!                                       if(providers == null)
!                                       {
!                                               providers = new Hashtable();
!                                       }
!                                       provider = (providers[type] as 
LicenseProvider);
!                                       if(provider != null)
!                                       {
!                                               return provider;
!                                       }
! 
!                                       // Check the type's "LicenseProvider" 
attribute.
!                                       attrs = type.GetCustomAttributes
!                                               
(typeof(LicenseProviderAttribute), true);
!                                       if(attrs != null && attrs.Length > 0)
!                                       {
!                                               providerType = 
((LicenseProviderAttribute)(attrs[0]))
!                                                               
.LicenseProvider;
!                                               if(providerType != null)
!                                               {
!                                                       provider = 
(LicenseProvider)
!                                                               
(Activator.CreateInstance(providerType));
!                                                       providers[type] = 
provider;
!                                                       return provider;
!                                               }
!                                       }
! 
!                                       // No declared provider, so use the 
default provider.
!                                       if(defaultProvider == null)
!                                       {
!                                               defaultProvider = new 
DefaultLicenseProvider();
!                                       }
!                                       providers[type] = defaultProvider;
!                                       return defaultProvider;
!                               }
!                       }
! 
!       // Perform license validation for a type.
!       private static License PerformValidation(Type type, Object instance)
!                       {
!                               LicenseProvider provider = GetProvider(type);
!                               return provider.GetLicense
!                                       (CurrentContext, type, instance, false);
                        }
  
        // Determine if a valid license can be granted for a type.
        public static bool IsValid(Type type)
                        {
!                               License license = PerformValidation(type, null);
!                               if(license != null)
!                               {
!                                       license.Dispose();
!                                       return true;
!                               }
!                               else
!                               {
!                                       return false;
!                               }
                        }
        public static bool IsValid(Type type, Object instance, out License 
license)
                        {
!                               license = PerformValidation(type, instance);
!                               return (license != null);
                        }
  
        // Lock the license context associated with an object.
        public static void LockContext(Object contextUser)
                        {
!                               lock(typeof(LicenseManager))
!                               {
!                                       if(contextLockedBy == null)
!                                       {
!                                               contextLockedBy = contextUser;
!                                       }
!                                       else
!                                       {
!                                               throw new 
InvalidOperationException
!                                                       
(S._("Invalid_LicenseContextLocked"));
!                                       }
!                               }
                        }
  
        // Unlock the license context associated with an object.
        public static void UnlockContext(Object contextUser)
                        {
!                               lock(typeof(LicenseManager))
!                               {
!                                       if(contextLockedBy == contextUser)
!                                       {
!                                               contextLockedBy = null;
!                                       }
!                                       else
!                                       {
!                                               throw new 
InvalidOperationException
!                                                       
(S._("Invalid_LicenseContextNotLocked"));
!                                       }
!                               }
                        }
  
***************
*** 139,142 ****
--- 261,306 ----
                                return license;
                        }
+ 
+       // Default license provider for types that don't have their own.
+       private sealed class DefaultLicenseProvider : LicenseProvider
+       {
+               // Get the license for a type.
+               public override License GetLicense
+                                       (LicenseContext context, Type type, 
Object instance,
+                                        bool allowExceptions)
+                               {
+                                       return new 
DefaultLicense(type.FullName);
+                               }
+ 
+       }; // class DefaultLicenseProvider
+ 
+       // The default license class.
+       private sealed class DefaultLicense : License
+       {
+               // Internal state.
+               private String key;
+ 
+               // Constructor.
+               public DefaultLicense(String key)
+                               {
+                                       this.key = key;
+                               }
+ 
+               // Get the license key.
+               public override String LicenseKey
+                               {
+                                       get
+                                       {
+                                               return key;
+                                       }
+                               }
+ 
+               // Dispose of this license.
+               public override void Dispose()
+                               {
+                                       // Nothing to do here.
+                               }
+ 
+       }; // class DefaultLicense
  
  }; // class LicenseManager

Index: LicenseProvider.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/System/ComponentModel/LicenseProvider.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** LicenseProvider.cs  12 Sep 2003 06:08:58 -0000      1.1
--- LicenseProvider.cs  17 Sep 2003 00:27:02 -0000      1.2
***************
*** 34,38 ****
        // Get the license for a type.
        public abstract License GetLicense
!                       (LicenseContext context, Type type, object instance,
                         bool allowExceptions);
  
--- 34,38 ----
        // Get the license for a type.
        public abstract License GetLicense
!                       (LicenseContext context, Type type, Object instance,
                         bool allowExceptions);
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]