/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; namespace Opc.Ua.Server { /// /// A generic implementation for ISystemContext interface. /// public class ServerSystemContext : SessionSystemContext { /// /// Initializes a new instance of the class. /// /// The server. public ServerSystemContext(IServerInternal server) : base((server ?? throw new ArgumentNullException(nameof(server))).Telemetry) { Server = server; OperationContext = null!; NamespaceUris = server.NamespaceUris; ServerUris = server.ServerUris; TypeTable = server.TypeTree; EncodeableFactory = server.Factory; } /// /// Initializes a new instance of the class. /// /// The server. /// The context. public ServerSystemContext(IServerInternal server, OperationContext context) : base((server ?? throw new ArgumentNullException(nameof(server))).Telemetry) { Server = server; OperationContext = context; NamespaceUris = server.NamespaceUris; ServerUris = server.ServerUris; TypeTable = server.TypeTree; EncodeableFactory = server.Factory; } /// /// Initializes a new instance of the class. /// /// The server. /// The session. public ServerSystemContext(IServerInternal server, ISession session) : base((server ?? throw new ArgumentNullException(nameof(server))).Telemetry) { Server = server; OperationContext = null!; SessionId = session.Id; UserIdentity = session.Identity; PreferredLocales = session.PreferredLocales; NamespaceUris = server.NamespaceUris; ServerUris = server.ServerUris; TypeTable = server.TypeTree; EncodeableFactory = server.Factory; } /// /// The backing this system context. /// Provides access to server-wide services (AggregateManager, /// SessionManager, audit reporting, etc.) needed by infrastructure /// components such as the historian dispatcher. /// public IServerInternal Server { get; } /// /// The operation context associated with system context. /// /// The operation context. /// /// May return null if the underlying base context's operation context /// has not been set or is not an instance /// (e.g. during NodeManager startup before any operation has begun). /// public new OperationContext? OperationContext { get => base.OperationContext as OperationContext; set => base.OperationContext = value; } /// /// Creates a copy of the context that can be used with the specified operation context. /// /// A copy of the system context. public ServerSystemContext Copy() { return (ServerSystemContext)MemberwiseClone(); } /// /// Creates a copy of the context that can be used with the specified operation context. /// /// The operation context to use. /// /// A copy of the system context that references the new operation context. /// public ServerSystemContext Copy(OperationContext context) { var copy = (ServerSystemContext)MemberwiseClone(); if (context != null) { copy.OperationContext = context; } return copy; } /// /// Creates a copy of the context that can be used with the specified session. /// /// The session to use. /// /// A copy of the system context that references the new session. /// public ServerSystemContext Copy(ISession session) { var copy = (ServerSystemContext)MemberwiseClone(); copy.OperationContext = null!; if (session != null) { copy.SessionId = session.Id; copy.UserIdentity = session.Identity; copy.PreferredLocales = session.PreferredLocales; } else { copy.SessionId = default; copy.UserIdentity = null; copy.PreferredLocales = default; } return copy; } /// /// Creates a copy of the context that can be used with the specified server context. /// /// The server context to use. /// /// A copy of the system context that references the new server context. /// public ServerSystemContext Copy(ServerSystemContext context) { var copy = (ServerSystemContext)MemberwiseClone(); if (context != null) { copy.OperationContext = context.OperationContext; copy.SessionId = context.SessionId; copy.UserIdentity = context.UserIdentity; copy.PreferredLocales = context.PreferredLocales; copy.NamespaceUris = context.NamespaceUris; copy.ServerUris = context.ServerUris; copy.TypeTable = context.TypeTable; copy.EncodeableFactory = context.EncodeableFactory; copy.NodeIdFactory = context.NodeIdFactory; } return copy; } } }