com.sibvisions.rad.server
Class GenericBean
java.lang.Object
java.util.AbstractMap<String,Object>
javax.rad.type.bean.AbstractBean<BeanType>
javax.rad.type.bean.Bean
com.sibvisions.rad.server.GenericBean
- All Implemented Interfaces:
- Map<String,Object>, IBean
public abstract class GenericBean
- extends Bean
The GenericBean handles the access to the values
of cached members and defined methods. If a member is uninitialized
the GenericBean will try to initialize it by calling
the init method for the member.
Examples for using implementing a GenericBean subclass
The fastest and safest way to use the GenericBean is to implement an init method
for every property. If you have init methods you don't have to implement the get
methods, but it's good style to implement both:
public class Session extends GenericBean
{
private DBAccess initDataSource() throws Exception
{
IConfiguration cfgSession = session.getCurrentSessionConfig();
OracleDBAccess dba = new OracleDBAccess();
dba.setConnection(cfgSession.getProperty("/application/securitymanager/database/url"));
dba.setUser(cfgSession.getProperty("/application/securitymanager/database/username"));
dba.setPassword(cfgSession.getProperty("/application/securitymanager/database/password"));
dba.open();
return dba;
}
private DBStorage initPerson() throws Exception
{
DBStorage dbsPerson = new DBStorage();
dbsPerson.setDBAccess(getDataSource());
dbsPerson.setWriteBackTable("V_PERSON");
dbsPerson.setFromClause("V_PERSON");
dbsPerson.open();
return dbsPerson;
}
public DBAccess getDataSource()
{
return (DBAccess)get("dataSource");
}
public DBStorage getPerson()
{
return (DBStorage)get("person");
}
}
It's also possible to integrate the initialization into the get method, thats recommended. The
disadvantage of this implementation is that more calls will be made (That's the result of avoiding
recursive calls, because getPerson calls get("person") and this calls getPerson again), but you
have the same flexibility as above and you have only one method where your object will be accessed:
public class Session extends GenericBean
{
public DBAccess getDataSource() throws Exception
{
OracleDBAccess dba = (OracleDBAccess)get("dataSource");
if (dba == null)
{
IConfiguration cfgSession = session.getCurrentSessionConfig();
dba = new OracleDBAccess();
dba.setConnection(cfgSession.getProperty("/application/securitymanager/database/url"));
dba.setUser(cfgSession.getProperty("/application/securitymanager/database/username"));
dba.setPassword(cfgSession.getProperty("/application/securitymanager/database/password"));
dba.open();
}
return dba;
}
public DBStorage getPerson() throws Exception
{
DBStorage dbsPerson = (DBStorage)get("person");
if (dbsPerson == null)
{
dbsPerson = new DBStorage();
dbsPerson.setDBAccess(getDataSource());
dbsPerson.setWriteBackTable("V_PERSON");
dbsPerson.setFromClause("V_PERSON");
dbsPerson.open();
}
return dbsPerson;
}
}
The EJB like implementation looks like the following:
public class Session extends GenericBean
{
private DBAccess dba;
private DBStorage dbsPerson;
public DBAccess getDataSource() throws Exception
{
if (dba == null)
{
IConfiguration cfgSession = session.getCurrentSessionConfig();
dba = new OracleDBAccess();
dba.setConnection(cfgSession.getProperty("/application/securitymanager/database/url"));
dba.setUser(cfgSession.getProperty("/application/securitymanager/database/username"));
dba.setPassword(cfgSession.getProperty("/application/securitymanager/database/password"));
dba.open();
put("dataSource", dba);
}
return dba;
}
public DBStorage getPerson() throws Exception
{
if (dbsPerson == null)
{
dbsPerson = new DBStorage();
dbsPerson.setDBAccess(getDataSource());
dbsPerson.setWriteBackTable("V_PERSON");
dbsPerson.setFromClause("V_PERSON");
dbsPerson.open();
}
return dbsPerson;
}
}
The problem with above implementation is that the objects won't be managed from the expected GenericBean,
if use extends from another GenericBean implementation like Session. That's the case because the extended
class inherits all methods from the super class and all objects will be created in the inherited class if
you call a method. But the objects from the super class should be stored in the super class instance!
We recommend to use the second or first implementation mechanism!
It's also possible to ignore lazy loading and generic object access. When you call
get("person") you will get another object as dba, when you didn't put the object.
And with this solutions you get the exception before using the object and that's not
always the right place.
You can use one of the following mechanism:
public class Session extends GenericBean
{
private DBAccess dba = createDataSource();
private DBStorage dbsPerson = createPerson();
public Session() throws Exception
{
//important because the create methods throws Exceptions
}
//dont set the name to initDataSource, unless you put(...) the instance, because thats the name
//of an automatic called method
private DBAccess createDataSource() throws Exception
{
IConfiguration cfgSession = session.getCurrentSessionConfig();
OracleDBAccess dba = new OracleDBAccess();
dba.setConnection(cfgSession.getProperty("/application/securitymanager/database/url"));
dba.setUser(cfgSession.getProperty("/application/securitymanager/database/username"));
dba.setPassword(cfgSession.getProperty("/application/securitymanager/database/password"));
dba.open();
//with this call you can set the method name to initDataSource and you can use get("dataSource")
//and getDataSource without problems
put("dataSource", dba);
return dba;
}
//dont set the name to initDataSource, unless you put(...) the instance, because thats the name
//of an automatic called method
private DBStorage createPerson() throws Exception
{
DBStorage dbsPerson = new DBStorage();
dbsPerson.setDBAccess(getDataSource());
dbsPerson.setWriteBackTable("V_PERSON");
dbsPerson.setFromClause("V_PERSON");
dbsPerson.open();
//with this call you can set the method name to initPerson and you can use get("person")
//and getPerson without problems
put("person", dbsPerson);
return dbsPerson;
}
public DBAccess getDataSource()
{
return dba;
}
public DBStorage getPerson()
{
return dbsPerson;
}
}
Another way is:
public class Session extends GenericBean
{
private DBAccess dba;
private DBStorage dbsPerson;
public Session() throws Exception
{
IConfiguration cfgSession = session.getCurrentSessionConfig();
OracleDBAccess dba = new OracleDBAccess();
dba.setConnection(cfgSession.getProperty("/application/securitymanager/database/url"));
dba.setUser(cfgSession.getProperty("/application/securitymanager/database/username"));
dba.setPassword(cfgSession.getProperty("/application/securitymanager/database/password"));
dba.open();
DBStorage dbsPerson = new DBStorage();
dbsPerson.setDBAccess(getDataSource());
dbsPerson.setWriteBackTable("V_PERSON");
dbsPerson.setFromClause("V_PERSON");
dbsPerson.open();
}
public DBAccess getDataSource()
{
return dba;
}
public DBStorage getPerson()
{
return dbsPerson;
}
}
| Nested classes/interfaces inherited from interface java.util.Map |
Map.Entry<K,V> |
|
Constructor Summary |
GenericBean()
Creates a new instance of GenericBean without a parent. |
| Methods inherited from class java.util.AbstractMap |
clear, clone, containsKey, containsValue, equals, get, hashCode, isEmpty, keySet, putAll, remove, size, toString, values |
GenericBean
public GenericBean()
- Creates a new instance of
GenericBean without a parent.
get
public Object get(String pName)
- Description copied from class:
AbstractBean
- Gets the value of the property name.
- Specified by:
get in interface IBean- Overrides:
get in class AbstractBean<BeanType>
- Parameters:
pName - the property name.
- Returns:
- the value of the property name.
get
public Object get(int pIndex)
- Gets the value for a cached member variable. If the value for the
member is not cached, it will be created with following rules:
- call the init<membername> method
- call the get<membername> method
- delegate to the parent, if available
- Overrides:
get in class Bean
- Parameters:
pIndex - the index of the property from the bean type
- Returns:
- the cached or created value;
null if it's not possible
to create a value
- Throws:
RuntimeException - if an error occurs during object creation
setParent
public void setParent(Bean pParent)
- Sets the parent bean for this bean.
- Parameters:
pParent - the parent bean
getParent
public Bean getParent()
- Gets the parent, if set.
- Returns:
- the parent or
null if not set
invoke
public Object invoke(String pMethod,
Object... pParams)
- Invokes a method of this object via reflective call. If the method is not declared,
te invocation will be delegated to the parent, if available.
- Parameters:
pMethod - the method namepParams - the params for the method
- Returns:
- the return value of the method or
null if the method
doesn't return a value
- Throws:
RuntimeException - if the desired method is not available or the method throws
an erroror during execution
Copyright © 2009 SIB Visions GmbH. All Rights Reserved.