EntLibHelper.cs :  » RSS-RDF » RSS-Feeder » RSSFeeder » Helpers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » RSS RDF » RSS Feeder 
RSS Feeder » RSSFeeder » Helpers » EntLibHelper.cs
// Copyright  2005 by Omar Al Zabir. All rights are reserved.
// 
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
// 
// website http://www.oazabir.com, email OmarAlZabir@gmail.com, msn oazabir@hotmail.com

using System;

using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;

namespace RSSFeeder.Helpers{

  /// <summary>
  /// Helper class for dealing with enterprise library
  /// </summary>
  public class EntLibHelper : RSSCommon.PropertyEditor.IPasswordProvider
  {
    private const string CONFIGURATION_SECTION_NAME = "globalConfiguration";
    private const string INFO_CATEGORY = "Info";
    private const string WARN_CATEGORY = "Warn";
    private const string ERROR_CATEGORY = "Error";
    private const string DEBUG_CATEGORY = "Debug";

    private const int INFO_PRIORITY = 4;
    private const int INFO_EVENTID = 0;

    private const int ERROR_PRIORITY = 3;
    private const int ERROR_EVENTID = 0;

    private const int WARN_PRIORITY = 2;
    private const int WARN_EVENTID = 0;

    private const int DEBUG_PRIORITY = 1;
    private const int DEBUG_EVENTID = 0;

    private const string EXCEPTION_POLICY_NAME = "General Policy";
    private const string SYMMETRIC_INSTANCE = "DPAPI";
    private const string HASH_INSTANCE = "MD5CryptoServiceProvider";

    public const string XSL_CACHE_MANAGER = "XSL Cache";

    /*
    Category - used to determine the routing of the LogEntry; 
    Priority - used to filter Log Entries, only those above the Minimum Priority are processed (defaults to -1 which indicates that the Minimum Priority should be used); 
    EventId - a value you can use to further categorise Log Entries (defaults to 0 for a LogEntry and to 1 for a LogEntry implicitly created by Logger.Write); 
    Severity - indicates the severity of the Log Entry, e.g., Information, Warning, Error etc. 
    Title - a summary of the LogEntry.Message. 
    */
    
    private static CacheManager manager = CacheFactory.GetCacheManager(XSL_CACHE_MANAGER);

    public static void Info( string title, object message )
    {
      try
      {
        Logger.Write( "Log message", "Debug" );
        Logger.Write( message, INFO_CATEGORY, INFO_PRIORITY, INFO_EVENTID, Microsoft.Practices.EnterpriseLibrary.Logging.Severity.Information, title );
      }
      catch (Exception ex)
      {
      }
    }
    public static void Error( string title, object message )
    {
      try
      {
        Logger.Write( message, ERROR_CATEGORY, ERROR_PRIORITY, ERROR_EVENTID, Microsoft.Practices.EnterpriseLibrary.Logging.Severity.Error, title );
      }
      catch (Exception ex)
      {
      }
    }
    public static void Warn( string title, object message )
    {
      try
      {
        Logger.Write( message, WARN_CATEGORY, WARN_PRIORITY, WARN_EVENTID, Microsoft.Practices.EnterpriseLibrary.Logging.Severity.Warning, title );
      }
      catch (Exception ex)
      {
      }
    }
    public static void Debug( string title, object message )
    {
      try
      {
        Logger.Write( message, DEBUG_CATEGORY, DEBUG_PRIORITY, DEBUG_EVENTID, Microsoft.Practices.EnterpriseLibrary.Logging.Severity.Unspecified, title );
      }
      catch (Exception ex)
      {
      }
    }

    public static void Exception( string title, Exception x )
    {
      ApplicationException wrapped = new ApplicationException( "title", x );
      bool rethrow = ExceptionPolicy.HandleException( wrapped, EXCEPTION_POLICY_NAME );
      if( rethrow )
      {
        throw x;
      }
    }

    internal static void UnhandledException(Exception x)
    {
      // An unhandled exception occured somewhere in our application. Let
      // the 'Global Policy' handler have a try at handling it.
      try
      {
        bool rethrow = ExceptionPolicy.HandleException(x, "Unhandled Exception");
        if (rethrow)
        {
          // Something has gone very wrong - exit the application.
          System.Windows.Forms.Application.Exit();
        }
        else
        {
          ErrorReportForm.QueueErrorReport( new ErrorReportItem( x.Message, x.ToString() ) );
        }
      }
      catch
      {
        // Something has gone wrong during HandleException (e.g. incorrect configuration of the block).
        // Exit the application
        string errorMsg = "An unexpected exception occured while calling HandleException with policy 'Global Policy'. "; 
        errorMsg += "Please check the event log for details about the exception." + Environment.NewLine + Environment.NewLine;

        System.Windows.Forms.MessageBox.Show(errorMsg, "Application Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);        
        
        System.Windows.Forms.Application.Exit();
      }
    }

    /// <summary>
    /// Load configuration from configuration Xml file. If the configuration does not exist, it returns false.
    /// </summary>
    /// <returns>True if configuration exists and loaded</returns>
    public static bool LoadConfiguration()
    {
      try
      {
        RSSCommon.Configuration existing = ConfigurationManager.GetConfiguration( CONFIGURATION_SECTION_NAME ) as RSSCommon.Configuration;
        if( null != existing )
        {
          RSSCommon.Configuration.Instance = existing;
          return true;
        }
        else
        {
          return false;
        }
      }
      catch
      {
        return false;
      }
      
    }

    public static void SaveConfiguration()
    {
      ConfigurationManager.WriteConfiguration( CONFIGURATION_SECTION_NAME, RSSCommon.Configuration.Instance );
    }

    public static void StoreInCache( string key, object data )
    {
      manager.Add( key, data );      
    }

    public static object GetCachedObject( string key )
    {

      if( manager.Contains( key ) )
        return manager.GetData( key );
      else 
        return null;      
    }

    string RSSCommon.PropertyEditor.IPasswordProvider.Encrypt( string data )
    {
      if( 0 == data.Length ) return string.Empty;
      return Encrypt( data );
    }

    string RSSCommon.PropertyEditor.IPasswordProvider.Decrypt( string data )
    {
      return Decrypt( data );
    }
    string RSSCommon.PropertyEditor.IPasswordProvider.Hash( string data )
    {
      return Hash( data );
    }

    public static string Encrypt( string data )
    {
      if( 0 == data.Trim().Length ) return string.Empty;
      return Cryptographer.EncryptSymmetric( SYMMETRIC_INSTANCE, data );
    }

    public static string Decrypt( string data )
    {
      if( 0 == data.Trim().Length ) return string.Empty;
      try
      {
        return Cryptographer.DecryptSymmetric( SYMMETRIC_INSTANCE, data );
      }
      catch
      {
        throw new ApplicationException( "Cannot decrypt password. Make sure you are not manually typing the password. Click the '...' button and set password from the dialog." );
      }
    }

    public static string Hash( string data )
    {
      if( 0 == data.Length ) return string.Empty;
      
      return Cryptographer.CreateHash( HASH_INSTANCE, data );
    }
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.