|
| 1 | +namespace UIShell.OSGi |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Threading; |
| 6 | + using Core; |
| 7 | + using Core.Service; |
| 8 | + using Utility; |
| 9 | + |
| 10 | + public class BundleRuntime : IDisposable |
| 11 | + { |
| 12 | + private bool _isDisposed; |
| 13 | + private bool _started; |
| 14 | + private volatile BundleRuntimeState _state; |
| 15 | + |
| 16 | + public int BundleClassLoaderCacheSize { get; set; } |
| 17 | + |
| 18 | + public bool EnableBundleClassLoaderCache { get; set; } |
| 19 | + |
| 20 | + public bool EnableGlobalAssemblyFeature { get; set; } |
| 21 | + |
| 22 | + public IFramework Framework { get; private set; } |
| 23 | + |
| 24 | + public static BundleRuntime Instance { get; internal set; } |
| 25 | + |
| 26 | + internal IServiceManager ServiceManager => Framework.ServiceContainer; |
| 27 | + |
| 28 | + public string[] StartArgs { get; private set; } |
| 29 | + |
| 30 | + public BundleRuntimeState State |
| 31 | + { |
| 32 | + get { return _state; } |
| 33 | + private set { _state = value; } |
| 34 | + } |
| 35 | + |
| 36 | + public BundleRuntime() |
| 37 | + : this(new string[] { "Plugins" }) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + public BundleRuntime(string pluginsPath) |
| 42 | + : this(new string[] { pluginsPath }) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + public BundleRuntime(string[] pluginsPathList) |
| 47 | + { |
| 48 | + BundleClassLoaderCacheSize = 50; |
| 49 | + if (Instance != null) |
| 50 | + { |
| 51 | + throw new Exception(Messages.SingletonOSGiAllowed); |
| 52 | + } |
| 53 | + if ((pluginsPathList == null) || (pluginsPathList.Length == 0)) |
| 54 | + { |
| 55 | + throw new Exception(Messages.AtLeastOnePluginsPathMustBeSpecified); |
| 56 | + } |
| 57 | + var framework = new Framework |
| 58 | + { |
| 59 | + Options = new FrameworkOptions(pluginsPathList) |
| 60 | + }; |
| 61 | + Framework = framework; |
| 62 | + Instance = this; |
| 63 | + } |
| 64 | + |
| 65 | + public void AddService<ServiceInterface>(object serviceInstance) |
| 66 | + { |
| 67 | + Framework.AddSystemService(typeof(ServiceInterface), new object[] { serviceInstance }); |
| 68 | + } |
| 69 | + |
| 70 | + public void AddService(Type serviceInterface, object serviceInstance) |
| 71 | + { |
| 72 | + Framework.AddSystemService(serviceInterface, new object[] { serviceInstance }); |
| 73 | + } |
| 74 | + |
| 75 | + public static void Dispose() |
| 76 | + { |
| 77 | + if (Instance != null) |
| 78 | + { |
| 79 | + if (Instance._started) |
| 80 | + { |
| 81 | + Instance.Stop(); |
| 82 | + } |
| 83 | + Instance = null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public T GetFirstOrDefaultService<T>() |
| 88 | + { |
| 89 | + if (ServiceManager != null) |
| 90 | + { |
| 91 | + return ServiceManager.GetFirstOrDefaultService<T>(); |
| 92 | + } |
| 93 | + return default(T); |
| 94 | + } |
| 95 | + |
| 96 | + public object GetFirstOrDefaultService(string serviceTypeName) |
| 97 | + { |
| 98 | + if (ServiceManager != null) |
| 99 | + { |
| 100 | + return ServiceManager.GetFirstOrDefaultService(serviceTypeName); |
| 101 | + } |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + public object GetFirstOrDefaultService(Type serviceType) |
| 106 | + { |
| 107 | + if (ServiceManager != null) |
| 108 | + { |
| 109 | + return ServiceManager.GetFirstOrDefaultService(serviceType); |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + public List<T> GetService<T>() |
| 115 | + { |
| 116 | + if (ServiceManager != null) |
| 117 | + { |
| 118 | + return ServiceManager.GetService<T>(); |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + public List<object> GetService(string serviceTypeName) |
| 124 | + { |
| 125 | + if (ServiceManager != null) |
| 126 | + { |
| 127 | + return ServiceManager.GetService(serviceTypeName); |
| 128 | + } |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + public List<object> GetService(Type serviceType) |
| 133 | + { |
| 134 | + if (ServiceManager != null) |
| 135 | + { |
| 136 | + return ServiceManager.GetService(serviceType); |
| 137 | + } |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + public void RemoveService<ServiceInterface>(object serviceInstance) |
| 142 | + { |
| 143 | + Framework.RemoveSystemService(typeof(ServiceInterface), serviceInstance); |
| 144 | + } |
| 145 | + |
| 146 | + public void RemoveService(Type serviceInterface, object serviceInstance) |
| 147 | + { |
| 148 | + Framework.RemoveSystemService(serviceInterface, serviceInstance); |
| 149 | + } |
| 150 | + |
| 151 | + public void Start() |
| 152 | + { |
| 153 | + if (Thread.CurrentThread.Name == null) |
| 154 | + { |
| 155 | + Thread.CurrentThread.Name = "FrameworkThread"; |
| 156 | + } |
| 157 | + Start(null); |
| 158 | + } |
| 159 | + |
| 160 | + public void Start(string[] args) |
| 161 | + { |
| 162 | + if (!_started) |
| 163 | + { |
| 164 | + StartArgs = args; |
| 165 | + State = BundleRuntimeState.Starting; |
| 166 | + try |
| 167 | + { |
| 168 | + Framework.Start(); |
| 169 | + State = BundleRuntimeState.Started; |
| 170 | + _started = true; |
| 171 | + } |
| 172 | + catch (Exception exception) |
| 173 | + { |
| 174 | + State = BundleRuntimeState.Stopped; |
| 175 | + _started = false; |
| 176 | + FileLogUtility.Error(Messages.StartTheFrameworkFailed); |
| 177 | + FileLogUtility.Error(exception); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public void Stop() |
| 183 | + { |
| 184 | + if (_started) |
| 185 | + { |
| 186 | + State = BundleRuntimeState.Stopping; |
| 187 | + try |
| 188 | + { |
| 189 | + Framework.Stop(); |
| 190 | + } |
| 191 | + catch (Exception exception) |
| 192 | + { |
| 193 | + FileLogUtility.Error(Messages.StopTheFrameworkFailed); |
| 194 | + FileLogUtility.Error(exception); |
| 195 | + } |
| 196 | + State = BundleRuntimeState.Stopped; |
| 197 | + _started = false; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + void IDisposable.Dispose() |
| 202 | + { |
| 203 | + if (!_isDisposed) |
| 204 | + { |
| 205 | + Dispose(); |
| 206 | + GC.SuppressFinalize(this); |
| 207 | + State = BundleRuntimeState.Disposed; |
| 208 | + _isDisposed = true; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
| 213 | + |
0 commit comments