How can I serialize and deserialize a private type? - ada

I'm working with the GNAT.OS_Lib module to implement a process manager for Linux. I'm using the subprograms in that module to spawn processes and get back PIDs, and now I want to save those PIDs to a file so they can be retrieved later to kill the process, get its status, etc. The problem is, the Process_Id type is private:
type Process_Id is private; -- s-os_lib.ads, line 743
The module provides a Pid_To_Integer function, but not the inverse. How can I serialize and deserialize Process_Ids?

Study section K.2 "Language Defined Attributes" in the Ada Language Reference Manual http://www.ada-auth.org/standards/12rm/html/RM-K-2.html
The attributes to study are S'Read, S'Write.
Your file must be created as a stream file. The Process_Id'Write attribute will serialize and write to the stream file. The Process_Id'Read attribute will read and de-serialize the data in the file.
If S'Read and S'Write do not work for you because of the nature of a compound type you should use the S'Input and S'Output attributes, which will read and write any bounds or discriminants.
S'Input and S'Output will work correctly with all types.

Related

How can I access context property (incoming file name) in transformation (custom xslt)?

Many historical posts about BizTalk Context Accessor (CodePlex), but all links are broken. Is there a state-of-the-art context accessor functoid / component to be used today? Or, is there any other way like creating helper class or something like it?
My aim is to add file name (without path) into the destination message in a map using Custom XSLT. No existing orchestration, only picking up a file and running a map to transform message from source to destination format (that requires source file name added to it...).
I solved my problem (this time) using an orchestration where I can access the context of the incoming message easily, and after mapping, inject/update the outgoing message with the file name.
I had one additional problem to solve that helped me accept using orchestration as solution this time. Two flies in one stroke.
(Problem was - note to self - I wanted to reuse destination schema in another debatching scenario, i.e. it was a envelope schema. Funny thing, BizTalk was not able to resolve body content schema if map was run in receive port. However, running map inside an orchestration, it was able to resolve the body content schema and mapping to envelope schema as destination worked.)
An alternative to the Context Accessor functiod is to use the BRE Pipeline Framework, and read the context property and inject it into the XML Payload.

How does golang implement reflection?

Using reflection, we can get the type name, storage size and the function of the given type(such as uint64, user-defined struct and so on). Even, we can modify some fields of the given type.
How does golang implement reflections? I guess the following ways:
Every type in golang, including user-defined type, itself has the information about type name, fields name and the function name. Golang reflection just reads these information or call the function.
Through some mechanism, Golang can get the type name, storage size and so on. And the type itself doesn’t have these information.
I have read the golang reflection code roughly. I guessed that golang used the second way.
Who can describe the concrete implement of reflection? Or recommend me some documents? Reading all code is difficult for me.
This is just an overview and it might be not 100% accurate but hopefully you will find it helpful.
At build time Go linker will embed information about all types used by the application into the executable (https://github.com/golang/go/blob/master/src/runtime/symtab.go#L39)
Each interface value contains a pointer to the data type descriptor (https://github.com/golang/go/blob/master/src/runtime/type.go#L14)
During conversion from a type that is known at compile time to an interface value Go compiler will point type descriptor of this interface value to the concrete type descriptor (it is known at compile time!).
E.g. when you call reflect.TypeOf(uint(87)):
an interface value is created by the compiler that references uint type descriptor
this interface value is passed to reflect.TypeOf function as argument
reflect.TypeOf function uses type descriptor that has been stored by the linker in the executable to get the align (and other) information about uint type.
The description of interfaces is well described here: The Laws of Reflection.
A variable of interface type stores a pair: the concrete value
assigned to the variable, and that value's type descriptor.
Basically, type are known statically from your code. More flexible interface types keep the original underlying type for getting back to the original data type.

Why do we need to instantiate a type at run time?

I am new to the usage of reflection in Java/scala. It is not quite clear to me why we need to instantiate a type at runtime. An example would be the best. Thanks a lot.
I will give you a general example of where runtime type instantiation or in general inspection of a types is useful. Think of the Plugin Pattern. Assume you want to create an application that allows users to create plugins. You don't have the plugins the users are going to make in the future, at hand. How are you able to use their plugins after you have released your application? You need to be able to inspect their plugins for a method your application requires and then call said method.
In order to enable this, language designers create a platform in which you are able to query a module (jar in java, assemblies in .net) for the types it defines and the methods, fields, etc it contains. You can then call any method, instantiate any type you want and basically interact with the module as if you had the module at compile time and you were referencing it(well not exactly but you get the point).
Here's and example of a method call that happens at runtime. You can assume that we have already created foo from a string we get from a configuration file at runtime. foo was specified as the name of the jar file containing the plugin types. I don't want to provide the instantiation code as it would make this too bloated, but here is the method:
Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);
As you see, we basically got the type of the class foo, we queried it for a method using the method's name and then called it. By doing so we extended the functionality of our program with the plugin at runtime.

Modifying a Biztalk message from custom code

Disclaimer: I am a complete biztalk newbie.
I need to be able to read and potentially edit 4 nodes in a biztalk message; preferably this needs to be done from a c# helper class as I am making a service call and also have unit tests written for this.
I already have this class wired up and it works with the XLANGMessage class, the problem I am running into is at this point in the orchestration the message is a Schema based type and doesn't seem to have any way for me to modify it.
I've done some reading and found a few ideas but have not been able to confirm if any of these can work from custom code.
1 write a map to transform the incoming message to the desired type
or
2 write something like this in your helper component to transform the message
public XmlDocument TransformMessage(XLANGMessage message)
Then pass the result document to a biztalk message in a message assignment shape.
responseMessage = xmlDocument;
You may get better performance if you pass streams instead of messages around.
You can pass messages into and out of C# helper classes easily. The simplest way is just to treat input parameters and return values as of type System.Xml.XmlDocument. The XLANG/s engine will safely cast back and forth from the XLANGMessage type to XmlDocument.
As you are essentially creating a "new" instance of the message (messages are immutable in BizTalk), the call to your helper class needs to be performed in a Message Assignment shape, with the outer Construct shape constructing the copy of your original message.
public static XmlDocument UpdateMyMessage(XmlDocument sourceMessage)
{
/* Do stuff to your Message here */
return sourceMessage;
}
A best-practice to consider is to declare all your C# helper methods as Static. This will avoid any issues with de/serialisation of your helper class during dehydration.
Are BizTalk messages immutable?
Generally speaking they are however, by creating a “corrective” orchestration and using a pass by reference option on the incoming message parameter, an existing message can be modified.

Flex Air: Read "ActionScript object" from file then cast?

It is quite simple to do it, you write the object down to file, then you read it:
http://corlan.org/2008/09/02/storing-data-locally-in-air/
http://cookbooks.adobe.com/post_Storing_ActionScript_Objects_in_the_Encrypted_Loca-10563.html
My questions are
why when we put [RemoteClass(alias="foo.Bar")] into VO, it can be
cast automatically (otherwise the type of the deserialized object is
Generic Object with correct properties data inside it)?
Is there another way to achieve it without RemoteClass tag? (Using metadata tag is preference)
Thanks
The answer is in the page you linked to:
Note that the alias is a key that is stored with the class instance
and links the class definition with the specific object that is stored
in the ByteArray when an instance of that object is serialized. This
key can be any unique string identifying this class, but convention is
to use the fully normalized package and class name.
That's why you get a generic object if you omit the alias - the deserialization method does not know what to do with the data, unless you specify to which class the values should be mapped.
Yes, there is: registerClassAlias() does the same thing. But the metadata tag is much prettier to read ;)

Resources