|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractMap<String,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.
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!
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 |
---|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> |
Nested classes/interfaces inherited from interface java.util.Map |
---|
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(Object pKey)
|
boolean |
containsValue(Object pValue)
|
void |
destroy()
Invoked whenever the lifecycle object is destroyed. |
Object |
get(int pIndex)
Gets the value for a cached member variable. |
Object |
get(String pName)
Gets the value of the property name. |
Bean |
getParent()
Gets the parent, if set. |
Object |
invoke(String pMethod,
Object... pParams)
Invokes a method of this object via reflective call. |
Object |
put(String pPropertyName,
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 |
---|
getObject, put |
Methods inherited from class javax.rad.type.bean.AbstractBean |
---|
entrySet, getBeanType |
Methods inherited from class java.util.AbstractMap |
---|
clear, clone, 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 ILifeCycleObject
public boolean containsKey(Object pKey)
containsKey
in interface Map<String,Object>
containsKey
in class AbstractMap<String,Object>
public boolean containsValue(Object pValue)
containsValue
in interface Map<String,Object>
containsValue
in class AbstractMap<String,Object>
public Object get(String pName)
get
in interface IBean
get
in class AbstractBean<BeanType>
pName
- the property name.
public Object put(String pPropertyName, Object pValue)
put
in interface Map<String,Object>
put
in interface IBean
put
in class AbstractBean<BeanType>
pPropertyName
- the property name.pValue
- the value of the property name.
public Object get(int pIndex)
get
in class Bean
pIndex
- the index of the property from the bean type
null
if it's not possible
to create a value
RuntimeException
- if an error occurs during object creationpublic void setParent(Bean pParent)
pParent
- the parent beanpublic Bean getParent()
null
if not setpublic Object invoke(String pMethod, Object... pParams)
pMethod
- the method namepParams
- the params for the method
null
if the method
doesn't return a value
RuntimeException
- if the desired method is not available or the method throws
an erroror during execution
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |