Exemple #1
0
        void GenerateSerializersAsync(GenerationBatch batch)
        {
            if (batch.Maps.Length != batch.Datas.Length)
            {
                throw new ArgumentException("batch");
            }

            lock (batch)
            {
                if (batch.Done)
                {
                    return;
                }
                batch.Done = true;
            }

            if (backgroundGeneration)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(RunSerializerGeneration), batch);
            }
            else
            {
                RunSerializerGeneration(batch);
            }
        }
Exemple #2
0
        void CheckGeneratedTypes(XmlMapping typeMapping)
        {
            lock (this)
            {
                if (serializerData == null)
                {
                    lock (serializerTypes)
                    {
                        serializerData = (SerializerData)serializerTypes [typeMapping.Source];
                        if (serializerData == null)
                        {
                            serializerData = new SerializerData();
                            serializerTypes [typeMapping.Source] = serializerData;
                        }
                    }
                }
            }

            /*
             * Brubbel:
             * we have problems using the generator.
             * if we do not generate it no problems where there any more.
             * The problems are that the compiled generator does not work or
             * the compiler returns an error.
             * Since we do not generate any more, we never had problems with
             * serialization and deserialization of objects.
             *
             * Since we have libjit the generator is no longer needed.
             *
             * Please be careful if U want to enable this.
             */

#if USE_GENERATOR
            bool generate = false;
            lock (serializerData)
            {
                generate = (++serializerData.UsageCount == generationThreshold);
            }


            if (generate)
            {
                if (serializerData.Batch != null)
                {
                    GenerateSerializers(serializerData.Batch);
                }
                else
                {
                    GenerationBatch batch = new GenerationBatch();
                    batch.Maps  = new XmlMapping[] { typeMapping };
                    batch.Datas = new SerializerData[] { serializerData };
                    GenerateSerializers(batch);
                }
            }
#endif
        }
Exemple #3
0
        public static Assembly GenerateSerializer(Type[] types, XmlMapping[] mappings, CompilerParameters parameters)
        {
            GenerationBatch batch = new GenerationBatch();

            batch.Maps  = mappings;
            batch.Datas = new SerializerData [mappings.Length];

            for (int n = 0; n < mappings.Length; n++)
            {
                SerializerData data = new SerializerData();
                data.Batch      = batch;
                batch.Datas [n] = data;
            }

            return(GenerateSerializers(batch, parameters));
        }
Exemple #4
0
        void RunSerializerGeneration(object obj)
        {
            try
            {
                GenerationBatch batch = (GenerationBatch)obj;
                batch = LoadFromSatelliteAssembly(batch);

                if (batch != null)
                {
                    GenerateSerializers(batch, null);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Exemple #5
0
        void CheckGeneratedTypes(XmlMapping typeMapping)
        {
            lock (this)
            {
                if (serializerData == null)
                {
                    lock (serializerTypes)
                    {
                        serializerData = (SerializerData)serializerTypes [typeMapping.Source];
                        if (serializerData == null)
                        {
                            serializerData = new SerializerData();
                            serializerTypes [typeMapping.Source] = serializerData;
                        }
                    }
                }
            }

            bool generate = false;

            lock (serializerData)
            {
                if (serializerData.UsageCount >= generationThreshold && !serializerData.Generated)
                {
                    serializerData.Generated = generate = true;
                }

                serializerData.UsageCount++;
            }

            if (generate)
            {
                if (serializerData.Batch != null)
                {
                    GenerateSerializersAsync(serializerData.Batch);
                }
                else
                {
                    GenerationBatch batch = new GenerationBatch();
                    batch.Maps  = new XmlMapping[] { typeMapping };
                    batch.Datas = new SerializerData[] { serializerData };
                    GenerateSerializersAsync(batch);
                }
            }
        }
Exemple #6
0
        public static XmlSerializer [] FromMappings(XmlMapping [] mappings)
        {
            XmlSerializer[]  sers  = new XmlSerializer [mappings.Length];
            SerializerData[] datas = new SerializerData [mappings.Length];
            GenerationBatch  batch = new GenerationBatch();

            batch.Maps  = mappings;
            batch.Datas = datas;

            for (int n = 0; n < mappings.Length; n++)
            {
                if (mappings[n] != null)
                {
                    SerializerData data = new SerializerData();
                    data.Batch = batch;
                    sers[n]    = new XmlSerializer(mappings[n], data);
                    datas[n]   = data;
                }
            }

            return(sers);
        }
Exemple #7
0
		void GenerateSerializersAsync (GenerationBatch batch)
		{
			if (batch.Maps.Length != batch.Datas.Length)
				throw new ArgumentException ("batch");

			lock (batch)
			{
				if (batch.Done) return;
				batch.Done = true;
			}
			
			if (backgroundGeneration)
				ThreadPool.QueueUserWorkItem (new WaitCallback (RunSerializerGeneration), batch);
			else
				RunSerializerGeneration (batch);
		}
Exemple #8
0
		void CheckGeneratedTypes (XmlMapping typeMapping)
		{
			lock (this)
			{
				if (serializerData == null) 
				{
					lock (serializerTypes)
					{
						serializerData = (SerializerData) serializerTypes [typeMapping.Source];
						if (serializerData == null) {
							serializerData = new SerializerData();
							serializerTypes [typeMapping.Source] = serializerData;
						}
					}
				}
			}
			
			bool generate = false;
			lock (serializerData)
			{
				if (serializerData.UsageCount >= generationThreshold && !serializerData.Generated)
					serializerData.Generated = generate = true;

				serializerData.UsageCount++;
			}
			
			if (generate)
			{
				if (serializerData.Batch != null)
					GenerateSerializersAsync (serializerData.Batch);
				else
				{
					GenerationBatch batch = new GenerationBatch ();
					batch.Maps = new XmlMapping[] {typeMapping};
					batch.Datas = new SerializerData[] {serializerData};
					GenerateSerializersAsync (batch);
				}
			}
		}
Exemple #9
0
		void GenerateSerializersAsync (GenerationBatch batch)
		{
			throw new NotImplementedException();
		}
Exemple #10
0
		public static Assembly GenerateSerializer (Type[] types, XmlMapping[] mappings, CompilerParameters parameters)
		{
			GenerationBatch batch = new GenerationBatch ();
			batch.Maps = mappings;
			batch.Datas = new SerializerData [mappings.Length];
			
			for (int n=0; n<mappings.Length; n++) {
				SerializerData data = new SerializerData ();
				data.Batch = batch;
				batch.Datas [n] = data;
			}
			
			return GenerateSerializers (batch, parameters);
		}
Exemple #11
0
		public static XmlSerializer [] FromMappings (XmlMapping	[] mappings)
		{
			XmlSerializer[] sers = new XmlSerializer [mappings.Length];
			SerializerData[] datas = new SerializerData [mappings.Length];
			GenerationBatch batch = new GenerationBatch ();
			batch.Maps = mappings;
			batch.Datas = datas;
			
			for (int n=0; n<mappings.Length; n++)
			{
				if (mappings[n] != null)
				{
					SerializerData data = new SerializerData ();
					data.Batch = batch;
					sers[n] = new XmlSerializer (mappings[n], data);
					datas[n] = data;
				}
			}
			
			return sers;
		}
		void CheckGeneratedTypes (XmlMapping typeMapping)
		{
			lock (this)
			{
				if (serializerData == null) 
				{
					lock (serializerTypes)
					{
						serializerData = (SerializerData) serializerTypes [typeMapping.Source];
						if (serializerData == null) {
							serializerData = new SerializerData();
							serializerTypes [typeMapping.Source] = serializerData;
						}
					}
				}
			}
			
			/*
			Brubbel:
			we have problems using the generator.
			if we do not generate it no problems where there any more.
			The problems are that the compiled generator does not work or 
			the compiler returns an error.
			Since we do not generate any more, we never had problems with 
			serialization and deserialization of objects.
			 
			Since we have libjit the generator is no longer needed.
			
			Please be careful if U want to enable this.
			*/
			
#if USE_GENERATOR
			bool generate = false;
			lock (serializerData)
			{
				generate = (++serializerData.UsageCount == generationThreshold);
			}
			
			
			if (generate)
			{
				if (serializerData.Batch != null)
					GenerateSerializers (serializerData.Batch);
				else
				{
					GenerationBatch batch = new GenerationBatch ();
					batch.Maps = new XmlMapping[] {typeMapping};
					batch.Datas = new SerializerData[] {serializerData};
					GenerateSerializers (batch);
				}
			}
#endif
		}
Exemple #13
0
        static Assembly GenerateSerializers(GenerationBatch batch, CompilerParameters cp)
        {
            DateTime tim = DateTime.Now;

            XmlMapping[] maps = batch.Maps;

            if (cp == null)
            {
                cp = new CompilerParameters();
                cp.IncludeDebugInformation = false;
                cp.GenerateInMemory        = true;
                cp.TempFiles.KeepFiles     = !deleteTempFiles;
            }

            string       file = cp.TempFiles.AddExtension("cs");
            StreamWriter sw   = new StreamWriter(file);

            if (!deleteTempFiles)
            {
                Console.WriteLine("Generating " + file);
            }

            SerializationCodeGenerator gen = new SerializationCodeGenerator(maps);

            try
            {
                gen.GenerateSerializers(sw);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Serializer could not be generated");
                Console.WriteLine(ex);
                cp.TempFiles.Delete();
                return(null);
            }
            sw.Close();

            CSharpCodeProvider provider = new CSharpCodeProvider();
            ICodeCompiler      comp     = provider.CreateCompiler();

            cp.GenerateExecutable = false;

            foreach (Type rtype in gen.ReferencedTypes)
            {
                string path = new Uri(rtype.Assembly.CodeBase).LocalPath;
                if (!cp.ReferencedAssemblies.Contains(path))
                {
                    cp.ReferencedAssemblies.Add(path);
                }
            }

            if (!cp.ReferencedAssemblies.Contains("System.dll"))
            {
                cp.ReferencedAssemblies.Add("System.dll");
            }
            if (!cp.ReferencedAssemblies.Contains("System.Xml"))
            {
                cp.ReferencedAssemblies.Add("System.Xml");
            }
            if (!cp.ReferencedAssemblies.Contains("System.Data"))
            {
                cp.ReferencedAssemblies.Add("System.Data");
            }

            CompilerResults res = comp.CompileAssemblyFromFile(cp, file);

            if (res.Errors.HasErrors || res.CompiledAssembly == null)
            {
                Console.WriteLine("Error while compiling generated serializer");
                foreach (CompilerError error in res.Errors)
                {
                    Console.WriteLine(error);
                }

                cp.TempFiles.Delete();
                return(null);
            }

            GenerationResult[] results = gen.GenerationResults;
            for (int n = 0; n < results.Length; n++)
            {
                GenerationResult gres = results[n];
                SerializerData   sd   = batch.Datas [n];
                lock (sd)
                {
                    sd.WriterType   = res.CompiledAssembly.GetType(gres.Namespace + "." + gres.WriterClassName);
                    sd.ReaderType   = res.CompiledAssembly.GetType(gres.Namespace + "." + gres.ReaderClassName);
                    sd.WriterMethod = sd.WriterType.GetMethod(gres.WriteMethodName);
                    sd.ReaderMethod = sd.ReaderType.GetMethod(gres.ReadMethodName);
                    sd.Batch        = null;
                }
            }

            cp.TempFiles.Delete();

            if (!deleteTempFiles)
            {
                Console.WriteLine("Generation finished - " + (DateTime.Now - tim).TotalMilliseconds + " ms");
            }

            return(res.CompiledAssembly);
        }
Exemple #14
0
 void GenerateSerializersAsync(GenerationBatch batch)
 {
     throw new NotImplementedException();
 }
Exemple #15
0
		static Assembly GenerateSerializers (GenerationBatch batch, CompilerParameters cp)
		{
			DateTime tim = DateTime.Now;
			
			XmlMapping[] maps = batch.Maps;
			
			if (cp == null) {
				cp = new CompilerParameters();
				cp.IncludeDebugInformation = false;
				cp.GenerateInMemory = true;
				cp.TempFiles.KeepFiles = !deleteTempFiles;
			}
			
			string file = cp.TempFiles.AddExtension ("cs");
			StreamWriter sw = new StreamWriter (file);
			
			if (!deleteTempFiles)
				Console.WriteLine ("Generating " + file);
			
			SerializationCodeGenerator gen = new SerializationCodeGenerator (maps);
			
			try
			{
				gen.GenerateSerializers (sw);
			}
			catch (Exception ex)
			{
				Console.WriteLine ("Serializer could not be generated");
				Console.WriteLine (ex);
				cp.TempFiles.Delete ();
				return null;
			}
			sw.Close ();
			
			CSharpCodeProvider provider = new CSharpCodeProvider();
			ICodeCompiler comp = provider.CreateCompiler ();
			
			cp.GenerateExecutable = false;
			
			foreach (Type rtype in gen.ReferencedTypes)
			{
				string path = new Uri (rtype.Assembly.CodeBase).LocalPath;
				if (!cp.ReferencedAssemblies.Contains (path))
					cp.ReferencedAssemblies.Add (path);
			}
				
			if (!cp.ReferencedAssemblies.Contains ("System.dll"))
				cp.ReferencedAssemblies.Add ("System.dll");
			if (!cp.ReferencedAssemblies.Contains ("System.Xml"))
				cp.ReferencedAssemblies.Add ("System.Xml");
			if (!cp.ReferencedAssemblies.Contains ("System.Data"))
				cp.ReferencedAssemblies.Add ("System.Data");
			if (!cp.ReferencedAssemblies.Contains ("System.Web.Services"))
				cp.ReferencedAssemblies.Add ("System.Web.Services");
			
			CompilerResults res = comp.CompileAssemblyFromFile (cp, file);
			if (res.Errors.HasErrors || res.CompiledAssembly == null) {
				Console.WriteLine ("Error while compiling generated serializer");
				foreach (CompilerError error in res.Errors)
					Console.WriteLine (error);
					
				cp.TempFiles.Delete ();
				return null;
			}
			
			GenerationResult[] results = gen.GenerationResults;
			for (int n=0; n<results.Length; n++)
			{
				GenerationResult gres = results[n];
				SerializerData sd = batch.Datas [n];
				lock (sd)
				{
					sd.WriterType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.WriterClassName);
					sd.ReaderType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.ReaderClassName);
					sd.WriterMethod = sd.WriterType.GetMethod (gres.WriteMethodName);
					sd.ReaderMethod = sd.ReaderType.GetMethod (gres.ReadMethodName);
					sd.Batch = null;
				}
			}
			
			cp.TempFiles.Delete ();

			if (!deleteTempFiles)
				Console.WriteLine ("Generation finished - " + (DateTime.Now - tim).TotalMilliseconds + " ms");
				
			return res.CompiledAssembly;
		}
Exemple #16
0
		GenerationBatch LoadFromSatelliteAssembly (GenerationBatch batch)
		{
			return batch;
		}
Exemple #17
0
 GenerationBatch LoadFromSatelliteAssembly(GenerationBatch batch)
 {
     return(batch);
 }