File: WorkflowPersistenceService.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (110 lines) | stat: -rw-r--r-- 5,214 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//------------------------------------------------------------------------------
// <copyright file="StatePersistenceService.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.IO;
using System.IO.Compression;
using System.Workflow.Runtime;
using System.Workflow.ComponentModel;
using System.Diagnostics;

namespace System.Workflow.Runtime.Hosting
{
    /// <summary> Service for saving engine state. </summary>
    [Obsolete("The System.Workflow.* types are deprecated.  Instead, please use the new types from System.Activities.*")]
    public abstract class WorkflowPersistenceService : WorkflowRuntimeService
    {
        /// <summary> Saves the state of a workflow instance. </summary>
        /// <param name="state"> The workflow instance state to save </param>
        internal protected abstract void SaveWorkflowInstanceState(Activity rootActivity, bool unlock);

        /// <summary></summary>
        /// <param name="state"></param>
        internal protected abstract void UnlockWorkflowInstanceState(Activity rootActivity);

        /// <summary> Loads the state of a workflow instance. </summary>
        /// <param name="instanceId"> The unique ID of the instance to load </param>
        /// <returns> The workflow instance state</returns>
        internal protected abstract Activity LoadWorkflowInstanceState(Guid instanceId);

        /// <summary> Saves the state of a completed scope. </summary>
        /// <param name="completedScopeState"> The completed scope to save </param>
        internal protected abstract void SaveCompletedContextActivity(Activity activity);

        /// <summary> Loads the state of a completed scope </summary>
        /// <param name="scopeId"> The unique identifier of the completed scope </param>
        /// <returns> The completed scope or null </returns>
        internal protected abstract Activity LoadCompletedContextActivity(Guid scopeId, Activity outerActivity);

        /// <summary></summary>
        /// <param name="activity"></param>
        /// <returns>The value of the "UnloadOnIdle" flag</returns>
        internal protected abstract bool UnloadOnIdle(Activity activity);

        static protected byte[] GetDefaultSerializedForm(Activity activity)
        {
            DateTime startTime = DateTime.Now;
            Byte[] result;

            Debug.Assert(activity != null, "Null activity");
            using (MemoryStream stream = new MemoryStream(10240))
            {
                stream.Position = 0;
                activity.Save(stream);
                using (MemoryStream compressedStream = new MemoryStream((int)stream.Length))
                {
                    using (GZipStream gzs = new GZipStream(compressedStream, CompressionMode.Compress, true))
                    {
                        gzs.Write(stream.GetBuffer(), 0, (int)stream.Length);
                    }

                    ActivityExecutionContextInfo executionContextInfo = (ActivityExecutionContextInfo)activity.GetValue(Activity.ActivityExecutionContextInfoProperty);
                    TimeSpan timeElapsed = DateTime.Now - startTime;
                    WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0,
                        "Serialized a {0} with id {1} to length {2}. Took {3}.",
                        executionContextInfo, executionContextInfo.ContextGuid, compressedStream.Length, timeElapsed);

                    result = compressedStream.GetBuffer();
                    Array.Resize<Byte>(ref result, Convert.ToInt32(compressedStream.Length));
                }
            }
            return result;
        }

        static protected Activity RestoreFromDefaultSerializedForm(Byte[] activityBytes, Activity outerActivity)
        {
            DateTime startTime = DateTime.Now;
            Activity state;

            MemoryStream stream = new MemoryStream(activityBytes);
            stream.Position = 0;

            using (GZipStream gzs = new GZipStream(stream, CompressionMode.Decompress, true))
            {
                state = Activity.Load(gzs, outerActivity);
            }
            Debug.Assert(state != null, "invalid state recovered");
            TimeSpan timeElapsed = DateTime.Now - startTime;
            WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0,
                "Deserialized a {0} to length {1}. Took {2}.",
                state, stream.Length, timeElapsed);

            return state;
        }
        static protected internal bool GetIsBlocked(Activity rootActivity)
        {
            return (bool)rootActivity.GetValue(WorkflowExecutor.IsBlockedProperty);
        }
        static protected internal string GetSuspendOrTerminateInfo(Activity rootActivity)
        {
            return (string)rootActivity.GetValue(WorkflowExecutor.SuspendOrTerminateInfoProperty);
        }
        static protected internal WorkflowStatus GetWorkflowStatus(Activity rootActivity)
        {
            return (WorkflowStatus)rootActivity.GetValue(WorkflowExecutor.WorkflowStatusProperty);
        }
    }
}