| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Net.Security; |
| | | 4 | | |
| | | 5 | | namespace Ice.Internal; |
| | | 6 | | |
| | | 7 | | public sealed class ObjectAdapterFactory |
| | | 8 | | { |
| | | 9 | | public void shutdown() |
| | | 10 | | { |
| | | 11 | | List<Ice.ObjectAdapter> adapters; |
| | 1 | 12 | | lock (_mutex) |
| | | 13 | | { |
| | | 14 | | // |
| | | 15 | | // Ignore shutdown requests if the object adapter factory has |
| | | 16 | | // already been shut down. |
| | | 17 | | // |
| | 1 | 18 | | if (_instance == null) |
| | | 19 | | { |
| | 1 | 20 | | return; |
| | | 21 | | } |
| | | 22 | | |
| | 1 | 23 | | adapters = new List<Ice.ObjectAdapter>(_adapters); |
| | | 24 | | |
| | 1 | 25 | | _instance = null; |
| | 1 | 26 | | _communicator = null; |
| | | 27 | | |
| | 1 | 28 | | System.Threading.Monitor.PulseAll(_mutex); |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | // |
| | | 32 | | // Deactivate outside the thread synchronization, to avoid |
| | | 33 | | // deadlocks. |
| | | 34 | | // |
| | 1 | 35 | | foreach (Ice.ObjectAdapter adapter in adapters) |
| | | 36 | | { |
| | 1 | 37 | | adapter.deactivate(); |
| | | 38 | | } |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | public void waitForShutdown() |
| | | 42 | | { |
| | | 43 | | List<Ice.ObjectAdapter> adapters; |
| | 1 | 44 | | lock (_mutex) |
| | | 45 | | { |
| | | 46 | | // |
| | | 47 | | // First we wait for the shutdown of the factory itself. |
| | | 48 | | // |
| | 1 | 49 | | while (_instance != null) |
| | | 50 | | { |
| | 1 | 51 | | System.Threading.Monitor.Wait(_mutex); |
| | | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | adapters = new List<Ice.ObjectAdapter>(_adapters); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | // |
| | | 58 | | // Now we wait for deactivation of each object adapter. |
| | | 59 | | // |
| | 1 | 60 | | foreach (Ice.ObjectAdapter adapter in adapters) |
| | | 61 | | { |
| | 1 | 62 | | adapter.waitForDeactivate(); |
| | | 63 | | } |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | public bool isShutdown() |
| | | 67 | | { |
| | 1 | 68 | | lock (_mutex) |
| | | 69 | | { |
| | 1 | 70 | | return _instance == null; |
| | | 71 | | } |
| | 1 | 72 | | } |
| | | 73 | | |
| | | 74 | | public void destroy() |
| | | 75 | | { |
| | | 76 | | // |
| | | 77 | | // First wait for shutdown to finish. |
| | | 78 | | // |
| | 1 | 79 | | waitForShutdown(); |
| | | 80 | | |
| | | 81 | | List<Ice.ObjectAdapter> adapters; |
| | 1 | 82 | | lock (_mutex) |
| | | 83 | | { |
| | 1 | 84 | | adapters = new List<Ice.ObjectAdapter>(_adapters); |
| | 1 | 85 | | } |
| | | 86 | | |
| | 1 | 87 | | foreach (Ice.ObjectAdapter adapter in adapters) |
| | | 88 | | { |
| | 1 | 89 | | adapter.destroy(); |
| | | 90 | | } |
| | | 91 | | |
| | 1 | 92 | | lock (_mutex) |
| | | 93 | | { |
| | 1 | 94 | | _adapters.Clear(); |
| | 1 | 95 | | } |
| | 1 | 96 | | } |
| | | 97 | | |
| | | 98 | | public void |
| | | 99 | | updateConnectionObservers() |
| | | 100 | | { |
| | | 101 | | List<Ice.ObjectAdapter> adapters; |
| | 1 | 102 | | lock (_mutex) |
| | | 103 | | { |
| | 1 | 104 | | adapters = new List<Ice.ObjectAdapter>(_adapters); |
| | 1 | 105 | | } |
| | | 106 | | |
| | 1 | 107 | | foreach (Ice.ObjectAdapter adapter in adapters) |
| | | 108 | | { |
| | 1 | 109 | | adapter.updateConnectionObservers(); |
| | | 110 | | } |
| | 1 | 111 | | } |
| | | 112 | | |
| | | 113 | | public void |
| | | 114 | | updateThreadObservers() |
| | | 115 | | { |
| | | 116 | | List<Ice.ObjectAdapter> adapters; |
| | 1 | 117 | | lock (_mutex) |
| | | 118 | | { |
| | 1 | 119 | | adapters = new List<Ice.ObjectAdapter>(_adapters); |
| | 1 | 120 | | } |
| | | 121 | | |
| | 1 | 122 | | foreach (Ice.ObjectAdapter adapter in adapters) |
| | | 123 | | { |
| | 1 | 124 | | adapter.updateThreadObservers(); |
| | | 125 | | } |
| | 1 | 126 | | } |
| | | 127 | | |
| | | 128 | | public Ice.ObjectAdapter createObjectAdapter( |
| | | 129 | | string name, |
| | | 130 | | Ice.RouterPrx router, |
| | | 131 | | SslServerAuthenticationOptions serverAuthenticationOptions) |
| | | 132 | | { |
| | 1 | 133 | | lock (_mutex) |
| | | 134 | | { |
| | 1 | 135 | | if (_instance == null) |
| | | 136 | | { |
| | 0 | 137 | | throw new Ice.CommunicatorDestroyedException(); |
| | | 138 | | } |
| | | 139 | | |
| | 1 | 140 | | if (name.Length > 0) |
| | | 141 | | { |
| | 1 | 142 | | if (_adapterNamesInUse.Contains(name)) |
| | | 143 | | { |
| | 1 | 144 | | throw new AlreadyRegisteredException("object adapter", name); |
| | | 145 | | } |
| | 1 | 146 | | _adapterNamesInUse.Add(name); |
| | | 147 | | } |
| | 1 | 148 | | } |
| | | 149 | | |
| | | 150 | | // |
| | | 151 | | // Must be called outside the synchronization since initialize can make client invocations |
| | | 152 | | // on the router if it's set. |
| | | 153 | | // |
| | 1 | 154 | | Ice.ObjectAdapter adapter = null; |
| | | 155 | | try |
| | | 156 | | { |
| | 1 | 157 | | if (name.Length == 0) |
| | | 158 | | { |
| | 1 | 159 | | adapter = new Ice.ObjectAdapter( |
| | 1 | 160 | | _instance, |
| | 1 | 161 | | _communicator, |
| | 1 | 162 | | this, |
| | 1 | 163 | | System.Guid.NewGuid().ToString(), |
| | 1 | 164 | | null, |
| | 1 | 165 | | true, |
| | 1 | 166 | | serverAuthenticationOptions); |
| | | 167 | | } |
| | | 168 | | else |
| | | 169 | | { |
| | 1 | 170 | | adapter = new Ice.ObjectAdapter( |
| | 1 | 171 | | _instance, |
| | 1 | 172 | | _communicator, |
| | 1 | 173 | | this, |
| | 1 | 174 | | name, |
| | 1 | 175 | | router, |
| | 1 | 176 | | false, |
| | 1 | 177 | | serverAuthenticationOptions); |
| | | 178 | | } |
| | | 179 | | |
| | 1 | 180 | | lock (_mutex) |
| | | 181 | | { |
| | 1 | 182 | | if (_instance == null) |
| | | 183 | | { |
| | 0 | 184 | | throw new Ice.CommunicatorDestroyedException(); |
| | | 185 | | } |
| | 1 | 186 | | _adapters.Add(adapter); |
| | 1 | 187 | | } |
| | 1 | 188 | | } |
| | 0 | 189 | | catch (Ice.CommunicatorDestroyedException) |
| | | 190 | | { |
| | 0 | 191 | | adapter?.destroy(); |
| | 0 | 192 | | throw; |
| | | 193 | | } |
| | 1 | 194 | | catch (Ice.LocalException) |
| | | 195 | | { |
| | 1 | 196 | | if (name.Length > 0) |
| | | 197 | | { |
| | 1 | 198 | | lock (_mutex) |
| | | 199 | | { |
| | 1 | 200 | | _adapterNamesInUse.Remove(name); |
| | 1 | 201 | | } |
| | | 202 | | } |
| | 1 | 203 | | throw; |
| | | 204 | | } |
| | | 205 | | |
| | 1 | 206 | | return adapter; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | public Ice.ObjectAdapter findObjectAdapter(Reference reference) |
| | | 210 | | { |
| | | 211 | | List<Ice.ObjectAdapter> adapters; |
| | 1 | 212 | | lock (_mutex) |
| | | 213 | | { |
| | 1 | 214 | | if (_instance == null) |
| | | 215 | | { |
| | 1 | 216 | | return null; |
| | | 217 | | } |
| | | 218 | | |
| | 1 | 219 | | adapters = new List<Ice.ObjectAdapter>(_adapters); |
| | 1 | 220 | | } |
| | | 221 | | |
| | 1 | 222 | | foreach (Ice.ObjectAdapter adapter in adapters) |
| | | 223 | | { |
| | | 224 | | try |
| | | 225 | | { |
| | 1 | 226 | | if (adapter.isLocal(reference)) |
| | | 227 | | { |
| | 1 | 228 | | return adapter; |
| | | 229 | | } |
| | 1 | 230 | | } |
| | 0 | 231 | | catch (Ice.ObjectAdapterDestroyedException) |
| | | 232 | | { |
| | | 233 | | // Ignore. |
| | 0 | 234 | | } |
| | | 235 | | } |
| | | 236 | | |
| | 1 | 237 | | return null; |
| | 1 | 238 | | } |
| | | 239 | | |
| | | 240 | | public void removeObjectAdapter(Ice.ObjectAdapter adapter) |
| | | 241 | | { |
| | 1 | 242 | | lock (_mutex) |
| | | 243 | | { |
| | 1 | 244 | | if (_instance == null) |
| | | 245 | | { |
| | 1 | 246 | | return; |
| | | 247 | | } |
| | | 248 | | |
| | 1 | 249 | | _adapters.Remove(adapter); |
| | 1 | 250 | | _adapterNamesInUse.Remove(adapter.getName()); |
| | 1 | 251 | | } |
| | 1 | 252 | | } |
| | | 253 | | |
| | | 254 | | public void flushAsyncBatchRequests(Ice.CompressBatch compressBatch, CommunicatorFlushBatchAsync outAsync) |
| | | 255 | | { |
| | | 256 | | List<Ice.ObjectAdapter> adapters; |
| | 1 | 257 | | lock (_mutex) |
| | | 258 | | { |
| | 1 | 259 | | adapters = new List<Ice.ObjectAdapter>(_adapters); |
| | 1 | 260 | | } |
| | | 261 | | |
| | 1 | 262 | | foreach (Ice.ObjectAdapter adapter in adapters) |
| | | 263 | | { |
| | 1 | 264 | | adapter.flushAsyncBatchRequests(compressBatch, outAsync); |
| | | 265 | | } |
| | 1 | 266 | | } |
| | | 267 | | |
| | | 268 | | // |
| | | 269 | | // Only for use by Instance. |
| | | 270 | | // |
| | 1 | 271 | | internal ObjectAdapterFactory(Instance instance, Ice.Communicator communicator) |
| | | 272 | | { |
| | 1 | 273 | | _instance = instance; |
| | 1 | 274 | | _communicator = communicator; |
| | 1 | 275 | | _adapterNamesInUse = new HashSet<string>(); |
| | 1 | 276 | | _adapters = new List<Ice.ObjectAdapter>(); |
| | 1 | 277 | | } |
| | | 278 | | |
| | | 279 | | private Instance _instance; |
| | | 280 | | private Ice.Communicator _communicator; |
| | | 281 | | private readonly HashSet<string> _adapterNamesInUse; |
| | | 282 | | private readonly List<Ice.ObjectAdapter> _adapters; |
| | 1 | 283 | | private readonly object _mutex = new(); |
| | | 284 | | } |