Wednesday, September 28, 2011

Difference between Objective C and C#.NET - I

               I am a newbie to Mac. I have seen people saying that learning a new programming language each year is worthy to enrich programming skills .As a beginner to the IT field I feel interesting to explore new things . I would like to share the knowledge I am gaining in finding out the difference between C#.NET and Objective C Programming languages through my blogs so that this will be helpful for any one moving from C# to Objective C.
A direct comparison of both the language seams to be awkward as both are built to use different platform, different frame work, different development environment. Objective C uses Cocoa and Cocoa touch frameworks where as C# language is built to use .NET frame work. Both are rich frameworks in their own purpose and rights as Cocoa and Cocoa touch targets the Mac OS X & iPhone,iPad respectively and .NET obviously the Windows. Similar case with the IDE also, Xcode is the common IDE used for developing Mac application. And Visual studio the usual IDE used for coding C#.NET.
       
       To begin with syntactically Objective C which is advanced form of C by adding object orientation to it. Where as C# I would say derived from C++ and JAVA. The major difference in both these language comes in fact that C# has a dot notation to access methods. Objective C talks within objects by passing messages.  C# releases the head ache of separating the declaration from implementation of classes and methods when compared to Objective C. Unlike C#, in Objective C the classes and methods are declared at one end and implemented at other end. I would like to introduce the way how Objective C differs syntactically for declaring class and methods.
For any xcode project using objective C two files are created by default one in where the declaration is done called .h file and .m where the implementation take place.To declare and implement a class and a method in C#

public class addition
{
public void add(int a,int b)
{
//codes for operation....
//.....
//......
}
}

where as in Objective C the code is 
@interface addition
{
//variable declaration
}
-void add:(int)a:(int)b;
@end
@implementation addition
-void add:(int)a:(int)b;
{
//codes for operation......
//.....
//.....
}
@end

@interface is the keyword used to declare a class and not similar to interface in c# .If you are looking for the interface equivalent of C# in Objective C then it is '@protocol' in Objective C. Any class started must end with an '@end' as shown above. The function 'add' is declared in the class with two parameters separated by the colon . The implementation is done in .m file .

To call a function in C# we go for
addition addobject = new addition();
addobject.add(5,2);

This could be accomplished in Objective C through the following codes
addition *addobject = [[addition alloc]init];
[addobject add:5:2];
           
           Another interesting thing that I noticed was the string syntax in objective C and C#. I have used regular expression in C#.NET where I used @ symbol with strings in double quotes to denote quoted string literals. Where as in Objective C it denote the CoreFoundation string .
Another major fact for any person new to Objective C is the memory management. C# provides automatic garbage collection and these has to be done manually in IOS. (ie) every call to alloc should be matched with end of current scope.I would also like to explain the concept of memory management in my up coming blogs..
Rashid Khaleefa..


Wednesday, September 21, 2011

Obtaining HTTP Request context in Axis2 WebServices


        Recently I integrated REST (Representational State Transfer) and SOAP (Simple Object Access Protocol) WebServices (WS) into a single context. i.e both the service end points were mapped to single context path. The REST WS was a simple authentication servlet application and SOAP WS was a AAR (Axis Archive file) file generated via AXIS2 plugin and deployed with Apache AXIS2 support. Also some of the stateful information from REST service needed to be obtained in SOAP service. But how? SOAP WS is a simple class with some methods exposed as service using Axis framework. Then how can I obtain the details from the other services deployed under same context path.

I was thinking for a solution. I realized that if it is same context then the Servlet Request object can be shared across. So, if values are set in session then it can be transformed across the application with state maintained.

Now, how to get the Session object in a plain class of SOAP WS. After looking into Axis API and surfing across the google I got some implementation to get HTTPServletRequest object.

Here's the implementation :

import org.apache.axis2.context.MessageContext;



MessageContext context =
       MessageContext.getCurrentMessageContext();
HttpServletRequest req = (HttpServletRequest)
       context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
HttpSession session = req.getSession();


What is MessageContext object?

I am sharing the info obtained from Axis2 API
       MessageContext creates an object for every message, regardless of whether it is from the client or server, processed within Axis2. First, the transport-related information is fed into the object, followed by the object model. This message context is then passed into each handler that is registered to process this type of message. Since the Axis2 information processing model is stateless, the context hierarchy is the only place that can be used to store state. Additionally, if the context information is relevant to this particular message being processed, it should be stored within message context.

- Shafeeq Mohammed