public static void UnregisterLibraryAssemblies(Program program, Schema.LoadedLibrary loadedLibrary) { while (loadedLibrary.Assemblies.Count > 0) { program.CatalogDeviceSession.UnregisterAssembly(loadedLibrary, loadedLibrary.Assemblies[loadedLibrary.Assemblies.Count - 1] as Assembly); } }
public static void EnsureLibraryRegistered(Program program, Schema.LibraryReference libraryReference, bool withReconciliation) { Schema.LoadedLibrary loadedLibrary = program.CatalogDeviceSession.ResolveLoadedLibrary(libraryReference.Name, false); if (loadedLibrary == null) { Schema.LoadedLibrary currentLibrary = program.ServerProcess.ServerSession.CurrentLibrary; try { Schema.Library library = program.Catalog.Libraries[libraryReference.Name]; if (!VersionNumber.Compatible(libraryReference.Version, library.Version)) { throw new Schema.SchemaException(Schema.SchemaException.Codes.LibraryVersionMismatch, libraryReference.Name, libraryReference.Version.ToString(), library.Version.ToString()); } RegisterLibrary(program, library.Name, withReconciliation); } finally { program.ServerProcess.ServerSession.CurrentLibrary = currentLibrary; } } else { Schema.Library library = program.Catalog.Libraries[libraryReference.Name]; if (!VersionNumber.Compatible(libraryReference.Version, library.Version)) { throw new Schema.SchemaException(Schema.SchemaException.Codes.LibraryVersionMismatch, libraryReference.Name, libraryReference.Version.ToString(), library.Version.ToString()); } } }
public void CheckClassDependency(Schema.LoadedLibrary library, ClassDefinition classDefinition) { Schema.RegisteredClass classValue = Catalog.ClassLoader.GetClass(CatalogDeviceSession, classDefinition); if (!library.IsRequiredLibrary(classValue.Library)) { throw new Schema.SchemaException(Schema.SchemaException.Codes.NonRequiredClassDependency, classValue.Name, library.Name, classValue.Library.Name); // TODO: Better exception } }
private static void EnsureLibraryUnregistered(Program program, Schema.Library library, bool withReconciliation) { Schema.LoadedLibrary loadedLibrary = program.CatalogDeviceSession.ResolveLoadedLibrary(library.Name, false); if (loadedLibrary != null) { while (loadedLibrary.RequiredByLibraries.Count > 0) { EnsureLibraryUnregistered(program, program.Catalog.Libraries[loadedLibrary.RequiredByLibraries[0].Name], withReconciliation); } UnregisterLibrary(program, library.Name, withReconciliation); } }
protected bool _firstRun; // Indicates whether or not this is the first time this server has run on the configured store /* * Catalog Startup -> * Catalog startup occurs in 5 phases * Bootstrap -> * Creates the SystemUser and CatalogDevice, then connects the SystemSession and opens a device session with the catalog device * Core -> * Creates the core catalog objects required to compile D4 statements. These objects are programmatically created by the * server and include the Admin user, User role, Temp device, ApplicationTransaction device and the Server-level rights. * Base -> * These are the basic objects required to facilitate caching and D4 compilation. These objects are created by running * the DataTypes.d4 script, and include the majority of the system data types. All the objects created up to this phase * constitute the base objects that will always be present in any given instance of a Dataphor Server, and are used as * the default set of cached objects. * System -> * The system objects are all the remaining objects in the System library, and are created by running the SystemCatalog.d4 * script. These objects are only created on a first-time run for a given catalog store. * Load -> * The load phase finishes preparing the server to compile and run D4 statements by restoring server state. */ private void InitializeCatalog() { LogMessage("Initializing Catalog..."); // Create the Catalog device // Note that this must be the first object created to avoid the ID being different on subsequent loads Schema.Object.SetNextObjectID(0); _catalogDevice = CreateCatalogDevice(); // Create the system user _systemUser = new Schema.User(SystemUserID, "System User", String.Empty); // Create the system library _systemLibrary = new Schema.LoadedLibrary(SystemLibraryName); _systemLibrary.Owner = _systemUser; LoadSystemAssemblies(); _catalog.LoadedLibraries.Add(_systemLibrary); // Load available libraries LoadAvailableLibraries(); // Connect the System Session if (_systemSession != null) { _systemSession.Dispose(); _systemProcess = null; } _systemSession = (ServerSession)InternalConnect(SystemSessionID, new SessionInfo(_systemUser.ID, _systemUser.Password, SystemLibraryName)); _systemSession.SessionInfo.UsePlanCache = false; _systemProcess = (ServerProcess)((IServerSession)_systemSession).StartProcess(new ProcessInfo(_systemSession.SessionInfo)); _systemProcess.SuppressWarnings = true; // Register the Catalog device _catalogDevice.Owner = _systemUser; _catalogDevice.Library = _systemLibrary; _catalogDevice.ClassDefinition = new ClassDefinition("System.CatalogDevice"); _catalogDevice.Start(_systemProcess); _catalogDevice.Register(_systemProcess); _firstRun = DetermineFirstRun(); // If this is a repository or there are no objects in the catalog, register, else resolve InternalInitializeCatalog(); // Bind the native type references to the system data types BindNativeTypes(); LogMessage("Catalog Initialized."); }
protected void GetNameResolutionPath(NameResolutionPath path, int level, Schema.LoadedLibrary library) { while (path.Count <= level) { path.Add(new Schema.LoadedLibraries()); } if (!path.Contains(library)) { path[level].Add(library); } foreach (LoadedLibrary localLibrary in library.RequiredLibraries) { GetNameResolutionPath(path, level + 1, localLibrary); } }
private ServerSession InternalConnect(int sessionID, SessionInfo sessionInfo) { Schema.User user = ValidateLogin(sessionID, sessionInfo); ServerSession session = new ServerSession(this, sessionID, sessionInfo, user); try { Schema.LoadedLibrary currentLibrary = null; if (sessionInfo.DefaultLibraryName != String.Empty) { if (_systemProcess == null) { currentLibrary = _catalog.LoadedLibraries[sessionInfo.DefaultLibraryName]; } else { currentLibrary = _systemProcess.CatalogDeviceSession.ResolveLoadedLibrary(sessionInfo.DefaultLibraryName, false); } } if (currentLibrary == null) { currentLibrary = _catalog.LoadedLibraries[GeneralLibraryName]; } session.CurrentLibrary = currentLibrary; _sessions.Add(session); return(session); } catch { session.Dispose(); throw; } }
private static void LoadLibrary(Program program, string libraryName, bool isKnown) { lock (program.Catalog.Libraries) { try { Schema.Library library = program.Catalog.Libraries[libraryName]; VersionNumber currentVersion = ((ServerCatalogDeviceSession)program.CatalogDeviceSession).GetCurrentLibraryVersion(libraryName); if (program.Catalog.LoadedLibraries.Contains(library.Name)) { throw new Schema.SchemaException(Schema.SchemaException.Codes.LibraryAlreadyLoaded, libraryName); } bool isLoaded = false; bool areAssembliesRegistered = false; Schema.LoadedLibrary loadedLibrary = null; try { loadedLibrary = new Schema.LoadedLibrary(libraryName); loadedLibrary.Owner = program.CatalogDeviceSession.ResolveUser(((ServerCatalogDeviceSession)program.CatalogDeviceSession).GetLibraryOwner(libraryName)); // Ensure that each required library is loaded foreach (Schema.LibraryReference reference in library.Libraries) { Schema.Library requiredLibrary = program.Catalog.Libraries[reference.Name]; if (!VersionNumber.Compatible(reference.Version, requiredLibrary.Version)) { throw new Schema.SchemaException(Schema.SchemaException.Codes.LibraryVersionMismatch, reference.Name, reference.Version.ToString(), requiredLibrary.Version.ToString()); } if (!program.Catalog.LoadedLibraries.Contains(reference.Name)) { if (!requiredLibrary.IsSuspect) { LoadLibrary(program, reference.Name, isKnown); } else { throw new Schema.SchemaException(Schema.SchemaException.Codes.RequiredLibraryNotLoaded, libraryName, reference.Name); } } loadedLibrary.RequiredLibraries.Add(program.CatalogDeviceSession.ResolveLoadedLibrary(reference.Name)); program.Catalog.OperatorResolutionCache.Clear(loadedLibrary.GetNameResolutionPath(program.ServerProcess.ServerSession.Server.SystemLibrary)); loadedLibrary.ClearNameResolutionPath(); } program.ServerProcess.ServerSession.Server.DoLibraryLoading(library.Name); try { // RegisterAssemblies RegisterLibraryFiles(program, library, loadedLibrary); areAssembliesRegistered = true; program.CatalogDeviceSession.InsertLoadedLibrary(loadedLibrary); loadedLibrary.AttachLibrary(); try { ((ServerCatalogDeviceSession)program.CatalogDeviceSession).SetLibraryOwner(loadedLibrary.Name, loadedLibrary.Owner.ID); } catch (Exception registerException) { loadedLibrary.DetachLibrary(); throw registerException; } isLoaded = true; // If we reach this point, a subsequent exception must unload the library if (library.IsSuspect) { library.IsSuspect = false; library.SaveInfoToFile(Path.Combine(library.GetInstanceLibraryDirectory(((Server.Server)program.ServerProcess.ServerSession.Server).InstanceDirectory), Schema.LibraryUtility.GetInfoFileName(library.Name))); } } finally { program.ServerProcess.ServerSession.Server.DoLibraryLoaded(library.Name); } } catch (Exception exception) { program.ServerProcess.ServerSession.Server.LogError(exception); library.IsSuspect = true; library.SuspectReason = ExceptionUtility.DetailedDescription(exception); library.SaveInfoToFile(Path.Combine(library.GetInstanceLibraryDirectory(((Server.Server)program.ServerProcess.ServerSession.Server).InstanceDirectory), Schema.LibraryUtility.GetInfoFileName(library.Name))); if (isLoaded) { UnregisterLibrary(program, libraryName, false); } else if (areAssembliesRegistered) { UnregisterLibraryAssemblies(program, loadedLibrary); } throw; } ((ServerCatalogDeviceSession)program.CatalogDeviceSession).SetCurrentLibraryVersion(library.Name, currentVersion); // Once a library has loaded, record the version number program.Catalog.Libraries.DoLibraryLoaded(program, library.Name); } catch { if (program.ServerProcess.ServerSession.Server.State == ServerState.Started) { throw; } } } }
public static void RegisterLibrary(Program program, string libraryName, bool withReconciliation) { int saveReconciliationState = program.ServerProcess.SuspendReconciliationState(); try { if (!withReconciliation) { program.ServerProcess.DisableReconciliation(); } try { lock (program.Catalog.Libraries) { Schema.Library library = program.Catalog.Libraries[libraryName]; Schema.LoadedLibrary loadedLibrary = program.CatalogDeviceSession.ResolveLoadedLibrary(library.Name, false); if (loadedLibrary != null) { throw new Schema.SchemaException(Schema.SchemaException.Codes.LibraryAlreadyRegistered, libraryName); } loadedLibrary = new Schema.LoadedLibrary(libraryName); loadedLibrary.Owner = program.Plan.User; // Ensure that each required library is registered foreach (Schema.LibraryReference reference in library.Libraries) { CheckCircularLibraryReference(program, library, reference.Name); EnsureLibraryRegistered(program, reference, withReconciliation); loadedLibrary.RequiredLibraries.Add(program.CatalogDeviceSession.ResolveLoadedLibrary(reference.Name)); program.Catalog.OperatorResolutionCache.Clear(loadedLibrary.GetNameResolutionPath(program.ServerProcess.ServerSession.Server.SystemLibrary)); loadedLibrary.ClearNameResolutionPath(); } ((Server.Server)program.ServerProcess.ServerSession.Server).DoLibraryLoading(library.Name); try { // Register the assemblies RegisterLibraryFiles(program, library, loadedLibrary); program.CatalogDeviceSession.InsertLoadedLibrary(loadedLibrary); loadedLibrary.AttachLibrary(); try { // Set the current library to the newly registered library Schema.LoadedLibrary currentLibrary = program.Plan.CurrentLibrary; program.ServerProcess.ServerSession.CurrentLibrary = loadedLibrary; try { // run the register.d4 script if it exists in the library // catalog objects created in this script are part of this library string registerFileName = Path.Combine(library.GetLibraryDirectory(((Server.Server)program.ServerProcess.ServerSession.Server).LibraryDirectory), RegisterFileName); if (File.Exists(registerFileName)) { try { using (StreamReader reader = new StreamReader(registerFileName)) { program.ServerProcess.ServerSession.Server.RunScript ( program.ServerProcess, reader.ReadToEnd(), libraryName, new DAE.Debug.DebugLocator(String.Format(RegisterDocumentLocator, libraryName), 1, 1) ); } } catch (Exception exception) { throw new RuntimeException(RuntimeException.Codes.LibraryRegistrationFailed, exception, libraryName); } } ((ServerCatalogDeviceSession)program.CatalogDeviceSession).SetCurrentLibraryVersion(library.Name, library.Version); ((ServerCatalogDeviceSession)program.CatalogDeviceSession).SetLibraryOwner(loadedLibrary.Name, loadedLibrary.Owner.ID); if (library.IsSuspect) { library.IsSuspect = false; library.SaveInfoToFile(Path.Combine(library.GetInstanceLibraryDirectory(((Server.Server)program.ServerProcess.ServerSession.Server).InstanceDirectory), GetInfoFileName(library.Name))); } } catch { program.ServerProcess.ServerSession.CurrentLibrary = currentLibrary; throw; } } catch { loadedLibrary.DetachLibrary(); throw; } program.Catalog.Libraries.DoLibraryLoaded(program, library.Name); } finally { ((Server.Server)program.ServerProcess.ServerSession.Server).DoLibraryLoaded(libraryName); } } } finally { if (!withReconciliation) { program.ServerProcess.EnableReconciliation(); } } } finally { program.ServerProcess.ResumeReconciliationState(saveReconciliationState); } }
public static void RegisterLibraryFiles(Program program, Schema.Library library, Schema.LoadedLibrary loadedLibrary) { // Register each assembly with the DAE #if !LOADFROMLIBRARIES foreach (Schema.FileReference file in ALibrary.Files) { if ((file.Environments.Count == 0) || file.Environments.Contains(Environments.WindowsServer)) { string sourceFile = Path.IsPathRooted(file.FileName) ? file.FileName : Path.Combine(ALibrary.GetLibraryDirectory(((Server.Server)AProgram.ServerProcess.ServerSession.Server).LibraryDirectory), file.FileName); string targetFile = Path.Combine(PathUtility.GetBinDirectory(), Path.GetFileName(file.FileName)); if (!File.Exists(sourceFile)) { throw new System.IO.IOException(String.Format("File \"{0}\" not found.", sourceFile)); } try { #if !RESPECTREADONLY FileUtility.EnsureWriteable(targetFile); #endif if ((File.GetLastWriteTimeUtc(sourceFile) > File.GetLastWriteTimeUtc(targetFile))) // source newer than target { File.Copy(sourceFile, targetFile, true); } } catch (IOException) { // Ignore this exception so that assembly copying does not fail if the assembly is already loaded } } } #endif // Load assemblies after all files are copied in so that multi-file assemblies and other dependencies are certain to be present foreach (Schema.FileReference file in library.Files) { if ((file.Environments.Count == 0) || file.Environments.Contains(Environments.WindowsServer)) { #if LOADFROMLIBRARIES string sourceFile = Path.IsPathRooted(file.FileName) ? file.FileName : Path.Combine(library.GetLibraryDirectory(((Server.Server)program.ServerProcess.ServerSession.Server).LibraryDirectory), file.FileName); if (FileUtility.IsAssembly(sourceFile)) { Assembly assembly = Assembly.LoadFrom(sourceFile); #else string targetFile = Path.Combine(PathUtility.GetBinDirectory(), Path.GetFileName(file.FileName)); if (FileUtility.IsAssembly(targetFile)) { Assembly assembly = Assembly.LoadFrom(targetFile); #endif if (file.IsAssembly) { program.CatalogDeviceSession.RegisterAssembly(loadedLibrary, assembly); } } } } }
public static void UnregisterLibrary(Program program, string libraryName, bool withReconciliation) { int saveReconciliationState = program.ServerProcess.SuspendReconciliationState(); try { if (!withReconciliation) { program.ServerProcess.DisableReconciliation(); } try { lock (program.Catalog.Libraries) { Schema.LoadedLibrary loadedLibrary = program.CatalogDeviceSession.ResolveLoadedLibrary(libraryName); if (Schema.Object.NamesEqual(loadedLibrary.Name, Engine.SystemLibraryName)) { throw new Schema.SchemaException(Schema.SchemaException.Codes.CannotUnregisterSystemLibrary); } if (Schema.Object.NamesEqual(loadedLibrary.Name, Engine.GeneralLibraryName)) { throw new Schema.SchemaException(Schema.SchemaException.Codes.CannotUnregisterGeneralLibrary); } if (loadedLibrary.RequiredByLibraries.Count > 0) { throw new Schema.SchemaException(Schema.SchemaException.Codes.LibraryIsRequired, loadedLibrary.Name); } // Drop all the objects in the library program.ServerProcess.ServerSession.Server.RunScript ( program.ServerProcess, program.ServerProcess.ServerSession.Server.ScriptDropLibrary(program.CatalogDeviceSession, libraryName), loadedLibrary.Name, null ); // Any session with current library set to the unregistered library will be set to General program.ServerProcess.ServerSession.Server.LibraryUnloaded(loadedLibrary.Name); // Remove the library from the catalog program.CatalogDeviceSession.DeleteLoadedLibrary(loadedLibrary); loadedLibrary.DetachLibrary(); // Unregister each assembly that was loaded with this library UnregisterLibraryAssemblies(program, loadedLibrary); // TODO: Unregister assemblies when the .NET framework supports it program.Catalog.Libraries.DoLibraryUnloaded(program, loadedLibrary.Name); } } finally { if (!withReconciliation) { program.ServerProcess.EnableReconciliation(); } } } finally { program.ServerProcess.ResumeReconciliationState(saveReconciliationState); } }
public void AttachDependency(Schema.Object objectValue) { if (_creationObjects == null) { _creationObjects = new List <Schema.Object>(); } if (_creationObjects.Count > 0) { // If this is a generated object, attach the dependency to the generator, rather than the object directly. // Unless it is a device object, in which case the dependency should be attached to the generated object. if (objectValue.IsGenerated && !(objectValue is Schema.DeviceObject) && (objectValue.GeneratorID >= 0)) { objectValue = objectValue.ResolveGenerator(CatalogDeviceSession); } if (objectValue is Schema.Property) { objectValue = ((Schema.Property)objectValue).Representation; } Schema.Object localObjectValue = (Schema.Object)_creationObjects[_creationObjects.Count - 1]; if ((localObjectValue != objectValue) && (!(objectValue is Schema.Reference) || (localObjectValue is Schema.TableVar)) && (!localObjectValue.HasDependencies() || !localObjectValue.Dependencies.Contains(objectValue.ID))) { if (!_serverProcess.ServerSession.Server.IsEngine) { if ( (localObjectValue.Library != null) && !localObjectValue.IsSessionObject && !localObjectValue.IsATObject ) { Schema.LoadedLibrary library = localObjectValue.Library; Schema.LoadedLibrary dependentLibrary = objectValue.Library; if (dependentLibrary == null) { throw new Schema.SchemaException(Schema.SchemaException.Codes.NonLibraryDependency, localObjectValue.Name, library.Name, objectValue.Name); } if (!library.IsRequiredLibrary(dependentLibrary) && (!String.Equals(dependentLibrary.Name, Engine.SystemLibraryName, StringComparison.OrdinalIgnoreCase))) // Ignore dependencies to the system library, these are implicitly available { throw new Schema.SchemaException(Schema.SchemaException.Codes.NonRequiredLibraryDependency, localObjectValue.Name, library.Name, objectValue.Name, dependentLibrary.Name); } } // if LObject is not a session object or an AT object, AObject cannot be a session object if ((localObjectValue is Schema.CatalogObject) && !localObjectValue.IsSessionObject && !localObjectValue.IsATObject && objectValue.IsSessionObject) { throw new Schema.SchemaException(Schema.SchemaException.Codes.SessionObjectDependency, localObjectValue.Name, ((Schema.CatalogObject)objectValue).SessionObjectName); } // if LObject is not a generated or an AT object or a plan object (Name == SessionObjectName), AObject cannot be an AT object if (((localObjectValue is Schema.CatalogObject) && (((Schema.CatalogObject)localObjectValue).SessionObjectName != localObjectValue.Name)) && !localObjectValue.IsGenerated && !localObjectValue.IsATObject && objectValue.IsATObject) { if (objectValue is Schema.TableVar) { throw new Schema.SchemaException(Schema.SchemaException.Codes.ApplicationTransactionObjectDependency, localObjectValue.Name, ((Schema.TableVar)objectValue).SourceTableName); } else { throw new Schema.SchemaException(Schema.SchemaException.Codes.ApplicationTransactionObjectDependency, localObjectValue.Name, ((Schema.Operator)objectValue).SourceOperatorName); } } } Error.AssertFail(objectValue.ID > 0, "Object {0} does not have an object id and cannot be tracked as a dependency.", objectValue.Name); localObjectValue.AddDependency(objectValue); } } }