public ImportContext(SharePointSessionFactory sessionFactory, string contentLocation, string metadataLocation, string cacheLocation) { this.SessionFactory = sessionFactory; this.ContentLocation = contentLocation; this.MetadataLocation = metadataLocation; this.CacheLocation = cacheLocation; this.XmlSettings = new XmlReaderSettings(); this.XmlSettings.DtdProcessing = DtdProcessing.Parse; this.XmlSettings.MaxCharactersFromEntities = 1024; }
public static int MainLoop(string[] args) { ILog log = null; string baseDir = Directory.GetCurrentDirectory(); // Initialize log4j Configuration options = new Configuration(baseDir, args); if (options.help) { Console.Error.WriteLine(options.GetUsage()); return(1); } List <string> errors = options.ValidateConfiguration(); if (errors.Count > 0) { Console.Error.WriteLine(string.Format("{0} Configuration Errors detected:", errors.Count)); foreach (string e in errors) { Console.Error.WriteLine(string.Format("\t* {0}", e)); } return(2); } System.IO.Directory.CreateDirectory(options.caches); string logDir = string.Format("{0}\\logs", baseDir); System.IO.Directory.CreateDirectory(logDir); Environment.SetEnvironmentVariable("CMF_LOGDATE", string.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now)); Environment.SetEnvironmentVariable("CMF_LOGDIR", logDir); XmlConfigurator.Configure(new FileInfo(string.Format("{0}\\log4net.xml", baseDir))); LOG = log = LogManager.GetLogger(typeof(Launcher)); log.Info("Initializing Application"); if (options.indexOnly) { ImportContext importContext = new ImportContext(null, options.content, options.metadata, options.caches); FormatResolver formatResolver = new FormatResolver(importContext); new DocumentImporter(new FolderImporter(importContext), formatResolver, options.locationMode, options.fixExtensions).StoreLocationIndex(); return(0); } ServicePointManager.MaxServicePointIdleTime = (int)SharePointSession.TIME_OUT.TotalMilliseconds; ServicePointManager.SetTcpKeepAlive(true, (int)SharePointSession.TIME_OUT.TotalMilliseconds, 60000); ServicePointManager.DefaultConnectionLimit = options.threads * 10; using (DirectoryEntry ldapDirectory = BindToLDAP(options)) { log.Info(string.Format("Using SharePoint at [{0}]", options.siteUrl)); string userString = options.user; if (!string.IsNullOrWhiteSpace(options.domain)) { userString = string.Format("{0}@{1}", userString, options.domain); } SecureString userPassword = null; if (!string.IsNullOrWhiteSpace(options.user)) { if (options.password == null) { Console.Write(string.Format("Enter The Sharepoint Password for [{0}]: ", userString)); userPassword = Tools.ReadPassword(); } else { String pass = CRYPT.Decrypt(options.password); pass = CRYPT.Encrypt(pass); log.Info(string.Format("Using stored credentials for [{0}] = [{1}]", userString, pass)); userPassword = new SecureString(); foreach (char c in CRYPT.Decrypt(pass)) { userPassword.AppendChar(c); } } } using (SharePointSessionFactory sessionFactory = new SharePointSessionFactory(new SharePointSessionInfo(options.siteUrl, options.user, userPassword, options.domain, options.applicationId, options.certificateKey, options.certificatePass, options.library, options.reuseCount))) { ImportContext importContext = new ImportContext(sessionFactory, options.content, options.metadata, options.caches); using (ObjectPool <SharePointSession> .Ref sessionRef = sessionFactory.GetSession()) { SharePointSession session = sessionRef.Target; ClientContext clientContext = session.ClientContext; List documentLibrary = sessionRef.Target.DocumentLibrary; // We configure the document library as required documentLibrary.EnableVersioning = true; documentLibrary.EnableMinorVersions = true; documentLibrary.ForceCheckout = false; documentLibrary.ContentTypesEnabled = true; // documentLibrary.MajorVersionLimit = 50000; // documentLibrary.MajorWithMinorVersionsLimit = 50000; documentLibrary.Update(); session.ExecuteQuery(); } FormatResolver formatResolver = new FormatResolver(importContext); ContentTypeImporter contentTypeImporter = null; for (int i = 0; i <= options.retries; i++) { try { contentTypeImporter = new ContentTypeImporter(importContext, options.library, options.cleanTypes); break; } catch (Exception e) { contentTypeImporter = null; log.Warn("WARNING: ContentTypeImporter failed to initialize due to an exception", e); } } if (contentTypeImporter == null) { log.Error(string.Format("ContentTypeImporter failed to initialize after {0} attempts", options.retries + 1)); return(3); } UserGroupImporter userGroupImporter = null; for (int i = 0; i <= options.retries; i++) { try { userGroupImporter = new UserGroupImporter(importContext, ldapDirectory, options.ldapSyncDomain, options.fallbackUser, options.internalUser, options.fallbackGroup, options.internalGroup); break; } catch (Exception e) { userGroupImporter = null; log.Warn("WARNING: UserGroupImporter failed to initialize due to an exception", e); } } if (userGroupImporter == null) { log.Error(string.Format("UserGroupImporter failed to initialize after {0} attempts", options.retries + 1)); return(4); } PermissionsImporter permissionsImporter = new PermissionsImporter(userGroupImporter); FolderImporter folderImporter = new FolderImporter(contentTypeImporter, permissionsImporter); DocumentImporter documentImporter = new DocumentImporter(folderImporter, formatResolver, options.locationMode, options.fixExtensions); bool aborted = false; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { if (aborted) { return; } aborted = true; e.Cancel = true; documentImporter.Abort = true; folderImporter.Abort = true; string msg = "Program Interrupted (hit Ctrl-C again to terminate immediately)"; if (log == null) { Console.WriteLine(msg); } else { log.Warn(msg); } }; try { documentImporter.StoreDocuments(options.threads, options.simulationMode, options.locationMode, options.autoPublish, options.retries); folderImporter.FinalizeFolders(options.threads, options.retries); documentImporter.StoreLocationIndex(); } finally { log.Info(documentImporter.ProcessingReport); log.Info(folderImporter.ProcessingReport); } return(aborted ? 1 : 0); } } }