|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.AbstractMap<java.lang.String,java.lang.Object>
javax.rad.type.bean.AbstractBean<BeanType>
javax.rad.type.bean.Bean
com.sibvisions.rad.server.GenericBean
public abstract class GenericBean
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.
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 Class Summary |
|---|
| Nested classes/interfaces inherited from class java.util.AbstractMap |
|---|
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V> |
| Nested classes/interfaces inherited from interface java.util.Map |
|---|
java.util.Map.Entry<K,V> |
| Field Summary |
|---|
| Fields inherited from class javax.rad.type.bean.AbstractBean |
|---|
beanType |
| Constructor Summary | |
|---|---|
GenericBean()
Creates a new instance of GenericBean without a parent. |
|
| Method Summary | |
|---|---|
boolean |
containsKey(java.lang.Object pKey)
|
boolean |
containsValue(java.lang.Object pValue)
|
void |
destroy()
Invoked whenever the lifecycle object is destroyed. |
java.lang.Object |
get(int pIndex)
Gets the value for a cached member variable. |
java.lang.Object |
get(java.lang.String pName)
Gets the value of the property name. |
BeanType |
getBeanType()
Gets the property names available by this bean. |
Bean |
getParent()
Gets the parent, if set. |
java.lang.Object |
invoke(java.lang.String pMethod,
java.lang.Object... pParams)
Invokes a method of this object via reflective call. |
java.lang.Object |
put(java.lang.String pPropertyName,
java.lang.Object pValue)
Sets the value of the property name. |
void |
setParent(Bean pParent)
Sets the parent bean for this bean. |
| Methods inherited from class javax.rad.type.bean.Bean |
|---|
clone, getObject, put, putAll |
| Methods inherited from class javax.rad.type.bean.AbstractBean |
|---|
entrySet |
| Methods inherited from class java.util.AbstractMap |
|---|
clear, equals, get, hashCode, isEmpty, keySet, putAll, remove, size, toString, values |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public GenericBean()
GenericBean without a parent.
| Method Detail |
|---|
public void destroy()
destroy in interface ILifeCycleObjectpublic boolean containsKey(java.lang.Object pKey)
containsKey in interface java.util.Map<java.lang.String,java.lang.Object>containsKey in class java.util.AbstractMap<java.lang.String,java.lang.Object>public boolean containsValue(java.lang.Object pValue)
containsValue in interface java.util.Map<java.lang.String,java.lang.Object>containsValue in class java.util.AbstractMap<java.lang.String,java.lang.Object>public java.lang.Object get(java.lang.String pName)
get in interface IBeanget in class AbstractBean<BeanType>pName - the property name.
public java.lang.Object put(java.lang.String pPropertyName,
java.lang.Object pValue)
put in interface java.util.Map<java.lang.String,java.lang.Object>put in interface IBeanput in class AbstractBean<BeanType>pPropertyName - the property name.pValue - the value of the property name.
public java.lang.Object get(int pIndex)
get in class BeanpIndex - the index of the property from the bean type
null if it's not possible
to create a value
java.lang.RuntimeException - if an error occurs during object creationpublic BeanType getBeanType()
getBeanType in interface IBeangetBeanType in class AbstractBean<BeanType>public void setParent(Bean pParent)
pParent - the parent beanpublic Bean getParent()
null if not set
public java.lang.Object invoke(java.lang.String pMethod,
java.lang.Object... pParams)
throws java.lang.Throwable
pMethod - the method namepParams - the parameters for the method
null if the method
doesn't return a value
java.lang.Throwable - if the desired method is not available or the method throws
an error during execution
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||