forked from efpiva/dover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicroCore.cs
153 lines (136 loc) · 5.27 KB
/
MicroCore.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Dover Framework - OpenSource Development framework for SAP Business One
* Copyright (C) 2014 Eduardo Piva
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact me at <[email protected]>
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using SAPbobsCOM;
using System.IO;
using Dover.Framework.Service;
using Castle.Core.Logging;
using Dover.Framework.Model;
using Dover.Framework.Factory;
using Dover.Framework.Log;
using System.Reflection;
using Dover.Framework.Attribute;
using System.Threading;
namespace Dover.Framework
{
internal class MicroCore
{
private DatabaseConfiguration dbConf;
private AssemblyManager assemblyLoader;
private FileUpdate fileUpdate;
private MicroCoreEventDispatcher dispatcher;
private MicroBoot microBoot;
private I18NService i18nService;
internal static bool reboot = true; // Used to signal a reboot by AppEvent.
private int rebootCount = 0;
public ILogger Logger { get; set; }
public MicroCore(DatabaseConfiguration dbConf, AssemblyManager assemblyLoader,
MicroCoreEventDispatcher dispatcher, MicroBoot microBoot, I18NService i18nService,
FileUpdate fileUpdate)
{
this.microBoot = microBoot;
this.dbConf = dbConf;
this.assemblyLoader = assemblyLoader;
this.dispatcher = dispatcher;
this.i18nService = i18nService;
this.fileUpdate = fileUpdate;
i18nService.ConfigureThreadI18n(System.Threading.Thread.CurrentThread);
microBoot.coreShutdownEvent = new ManualResetEvent(false);
}
internal void PrepareFramework()
{
try
{
while (reboot)
{
microBoot.coreShutdownEvent.Reset();
reboot = false;
Logger.Debug(Messages.PreparingFramework);
dbConf.PrepareDatabase();
if (InsideInception())
return;
string appFolder = CheckAppFolder();
Logger.Debug(DebugString.Format(Messages.CreatedAppFolder, appFolder));
assemblyLoader.UpdateFrameworkAssemblies(appFolder);
assemblyLoader.UpdateAddinsDBAssembly();
if (rebootCount == 0)
dispatcher.RegisterEvents();
microBoot.AppFolder = appFolder;
microBoot.StartInception();
microBoot.Boot();
microBoot.coreShutdownEvent.WaitOne();
rebootCount++;
}
reboot = true; // in case we need to start it again (i.e. unit test).
ContainerManager.Container.Dispose();
SAPServiceFactory.LogOff();
}
catch (Exception e)
{
Logger.Fatal(String.Format(Messages.GeneralError, e.Message), e);
Environment.Exit(10);
}
}
private void CopyResource(string destination, string resource)
{
using (var doverConfig = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
if (!File.Exists(destination) && doverConfig != null)
{
using(var destinationStream = new FileStream(destination, FileMode.CreateNew))
{
byte[] buffer = new byte[1000];
int buffLen;
while ((buffLen = doverConfig.Read(buffer, 0, 1000)) > 0)
{
destinationStream.Write(buffer, 0, buffLen);
}
}
}
}
}
private string CheckAppFolder()
{
string appFolder = fileUpdate.GetDoverDirectory();
string frameworkFolder = Path.Combine(appFolder, "Framework");
CreateIfNotExists(frameworkFolder);
string cacheFolder = Path.Combine(appFolder, "..", "Cache");
CreateIfNotExists(cacheFolder);
return frameworkFolder;
}
private void CreateIfNotExists(string appFolder)
{
if (System.IO.Directory.Exists(appFolder) == false)
{
System.IO.Directory.CreateDirectory(appFolder);
}
}
private bool InsideInception()
{
return AppDomain.CurrentDomain.FriendlyName == "Dover.Inception"
|| AppDomain.CurrentDomain.FriendlyName == "Dover.AddIn";
}
}
}