Thursday, February 24, 2011

Learning IOS Programming for Java Developers - Memory Management in Setters and Getters Of Object

Encapsulation is one of the basic building block in Object Oriented paradigm. Encapsulation in Java is achieve by restricting user to directly access the fields of Object and in order to access those fields, setters and getters are written. Objective C is also an object oriented programming language and also provides encapsulation by providing setters and getters to access the object data. Here is the sample class which contains two fields. One is an object and second one is of basic data type.
@interface TestClass : NSObject
{
NSString *name;
int id;
}

- (void) setName : (NSString *) newName;
- (NSString *) name;

- (void) setId : (int) newId;
- (int) id;

"setName" and "name" is setter and getter of "name" field. Similarly "setId" and "id" id setter and getter of "id" field.

Here is the implementation part of class
@implementation TestClass
{
- (void) setName : (NSString *) newName
{
[newName retain];
[name release];
name = newName;
}

- (NSString *) name
{
return name;
}
- (void) setId : (int) newId
{
id = newId;
}

- (int) id
{
return id;
}


}

setName needs your special intention for memory management point of view. First you send message "retain" to parameter, then you release old field by sending message "release" to name pointer. Then you assign new value to name pointer. These three steps are critical because if you change the order like you release old one and then retain new parameter, if both are same, then by sending release message will free the memory and new parameter will also be freed because that was same as old one. By sending retain message to parameter, you actually gain ownership of it and its reference count increases. Since you have also ownership of old one, so you will have to release it otherwise you will lose reference and there will be memory leak. Now as a java developer i can think that if "name" field is null, then sending release message may give something like NullPointerException but in Objective C, sending a message to null is safe and it will not throw any error and will keep executing next line.
Now last thing which is missing is that how to release fields which are retained before releasing object memroy, so for that you will have to overwrite a special method of NSObject "dealloc". This method is called when you send release message to an object and its reference count is 0. In this method you will have to release all those objects which you have retained. Sample implementation for above class is given below:
- (void) dealloc
{
[name release];
[super dealloc];
}


Wednesday, February 23, 2011

How to Learn IOS Programming having experience In Java language

I am creating this post to help those which are working in java programming language and want to explore or work in IOS. I am working in Java for last eight years and now exploring IOS programming so will share my experiences through this post.

Monday, February 7, 2011

Setting datasource name programmatically for ADF Application

While working on an enterprise application for my client, i received a request from the client that data source name should not be hardcoded in the application and it should be configurable because client wants to deploy it more than one instance of application on the same server for different purposes like preview, production etc. So i googled it, and found excellent article 11g Dynamic JDBC Credentials for Model 1, Struts, Trinidad, and ADF Faces Rich Client
Here it is simplified version:
You will have to extend two following classes:

  • EnvInfoProvider
  • DefaultSessionCookieFactory

EnvInfoProvider
This is main interface and datasource name will be set in the implementation of this interace. You will have to implement following three methods:

  • public Object getInfo(String propName, Object object)
  • public void modifyInitialContext(Object initialContext)
  • public int getNumOfRetries()
Only important method for our scenario is getInfo method. It is used to set new datasource name. Sample implementation is given below:

public Object getInfo(String propName, Object object) {
String myDataSourceName = "test"; // You can set it through your logic
if (object instanceof Hashtable) {
Hashtable connectionEnv = (Hashtable)object;
System.out.println(connectionEnv.get(Configuration.JDBC_CONNECTION_NAME));
String contextPath = (String)ADFContext.getCurrent().getSessionScope().get("contextPath");
connectionEnv.put(Configuration.JDBC_DS_NAME,"java:comp/env/jdbc/"+myDataSourceName );
}
return null;
}

public void modifyInitialContext(Object initialContext) {
}

public int getNumOfRetries() {
return 0;
}

DefaultSessionCookieFactory
In order to configure custom implementation of EnvInfoProvider, you will have to extend DefaultSessionCookieFactory and override createSessionCookie method. Sample implementation is given below:
public SessionCookie createSessionCookie(String name,
String value,
ApplicationPool pool,
Properties properties) {

SessionCookie cookie = super.createSessionCookie(name, value, pool, properties);
Hashtable env = pool.getEnvironment();

env.remove(Configuration.JDBC_CONNECTION_NAME);
EnvInfoProvider provider = new MyEnvInfoProvider();
cookie.setEnvInfoProvider(provider);
return cookie;
}
Now last step is to configure this custom SessionCookieFactory. Open bc4j.xcfg of application module and add following element as a child of respected AppModuleConfig element

Thursday, July 29, 2010

Accessing Runtime metadata for ViewObject and EntityObject


In Oracle ADF, ViewObject and EntityObject are two basic concepts and whole application revolves around these two type of objects. EntityObject corresponds to database table while ViewObject corresponds to Database View. ViewObject can be based on EntityObject, SQL or on transient attributes. Normally you create and design these two objects throug drag and drop in JDeveloper but sometimes you need some meta information about these two objects in business logic. In order to get metainfo, some understanding of underlying structure of these two objects should be.
Figure shown above illustrates the three primary interfaces ADF provides for accessing runtime metadata about view objects and entity objects. The ViewObject interface extends the StructureDef interface. The class representing the entity definition (EntityDefImpl) also implements this interface. As its name implies, the StructureDef defines the structure and the component and provides access to a collection of AttributeDef objects that offer runtime metadata about each attribute in the view object row or entity row. Using an AttributeDef, you can access its companion AttributeHints object to reference hints like the display label, format mask, tooltip, etc.
Now you can use this understanding to get runtime meta info about view object or entity object e.g. if i want to find whether a ViewObject is readonly non entity based object or not. i can find it by:

  • isFullSql() is true

    This method returns true if the view object's SQL query is completely specified by the developer, as opposed to having the select list derived automatically based on the participating entity usages.

  • getEntityDefs() is null

    This method returns an array of EntityDefImpl objects representing the view object's entity usages. If it returns null, then the view object has no entity usages.




Wednesday, May 19, 2010

Integrated Weblogic Server Domain

I was developing an application on JDeveloper 11g and deploying it on intergrated weblogic server. In my application i was authenticating using my own authentication provider but when i configured it on weblogic, weblogic failed to start. I was not finding any configuration file where i can revert security settings of weblogic but after some try i found it and thought to share it with others. When you install JDeveloper on windows, it places configuration files under "C:\Documents and Settings\user_name\Application Data\JDeveloper". Here you can find all settings for jdeveloper and integrated weblogic. Under this directory there is a folder with something "system11.....". Now to change weblogic related settings, open "defaultDomain/config.xml" and change it accordingly.

Tuesday, December 29, 2009

Programmatically Navigating to another page in adf / jsf

Here is the sample code which can be used to programmatically navigating to another page in jsf or adf

FacesContext fctx = FacesContext.getCurrentInstance();
Application application = fctx.getApplication();
NavigationHandler navHandler = application.getNavigationHandler();
navHandler.handleNavigation(fctx,null, "name of navigation case");

Doing some work before showing Popup in ADF

Here is the way to do any processing before calling popup. First of all create a method in backing bean and assign this to action of "commandButton" or "commandLink". Sample signature for you method is as follows:

public void myMethod()
{
/**
* Place your processing logic here and then call showPoup method
**/
showPopup(popupId);
}

Signature of showPopup is:

public static void showPopup(String popupId)
{
FacesContext context = FacesContext.getCurrentInstance();

ExtendedRenderKitService extRenderKitSrvc =
Service.getRenderKitService(context, ExtendedRenderKitService.class);
extRenderKitSrvc.addScript(context,
"AdfPage.PAGE.findComponent('" + popupId + "').show();");
}