|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.tentackle.appworx.ContextDb
public class ContextDb
The application's database context.
Basically, this is a decorator for a logical Db-connection.
All AppDbObjects refer to a ContextDb instead of a Db.
The ContextDb carries the application's higher level database context
which can be used to implement the so-called multitenancy.
If your application is not multitenant, you don't have to pay
further attention to it.
For multitenant applications, however, the ContextDb can be extended
to hold the ID(s) of the object(s) describing the context. Because
an AppDbObject carries a reference to that context, it can serve as
a root for a logical data space. For example, a AppDbObject.selectAllInContext()
of accounts will deliver only the accounts belonging to that particular tenant.
All classes depending on those extended contexts must also implement
an appropriate extension of ContextDependable. By further subclassing,
a multitenancy hierarchy can be achieved. For example, in a financial
accounting system, the hierarchy could be: TenantDb -> FiscalYearDb -> BookDb
with corresponding TenantDependable -> FiscalYearDependable -> BookDependable.
It is recommended to subclass AppDbObject accordingly, i.e. TenantDbObject ->
FiscalYearDbObject -> BookDbObject and/or to override the methods in ContextDependable
by applying multiple inheritance emulation via the Wurbelizer.
Note: the wurblets support multitenancy by means of the options CONTEXT, CONTEXTID and UNIQUE.
| Constructor Summary | |
|---|---|
ContextDb(Db db)
Creates a default context. |
|
| Method Summary | |
|---|---|
static void |
applyToCollection(ContextDb contextDb,
Collection<? extends ContextDependable> list)
Sets the context in a list of ContextDependables. |
static void |
applyToContextDependable(ContextDb contextDb,
ContextDependable obj)
Sets the context in a ContextDependable, if not null. |
void |
assertPermissions()
Asserts that this context is allowed to be used by the current user. If it is not allowed, a DbRuntimeException is thrown. |
ContextDb |
clone()
ContextDbs may be cloned (e.g. |
int |
compareTo(ContextDb otherContextDb)
Contexts must be comparable. |
boolean |
equals(Object obj)
Overridden to allow for checks whether contexts are equal. |
AppUserInfo |
getAppUserInfo()
AppDbObjects should be connected to a Db via AppUserInfo,
not UserInfo. |
long |
getContextId()
Gets the ID of the context object. |
AppDbObject |
getContextObject()
Gets the object that spans this context. |
Db |
getDb()
Gets the db connection of that context |
String |
getInfo()
Get the long description of this context. |
int |
hashCode()
Returns a hash code value for the object. |
boolean |
isWithinContext(long contextId,
String contextClass)
Determines whether this context belongs to an inheritance hierarchy that is created from a given context object. |
void |
setDb(Db db)
Sets the db connection. Used for objects traveling between the client and the server. |
String |
toString()
Gets the string representation of this context. |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public ContextDb(Db db)
db - the db connection| Method Detail |
|---|
public Db getDb()
public void setDb(Db db)
db - the db connectionassertPermissions()
public AppUserInfo getAppUserInfo()
throws ApplicationException
AppUserInfo,
not UserInfo.
The AppUserInfo extends the UserInfo by a SecurityManager.
ApplicationException - if Db does not provide AppUserInfopublic void assertPermissions()
DbRuntimeException is thrown.
The default implementation does nothing.
Override the method in middle tier servers, for example in a hypothetical
TenantContextDb:
public void assertPermissions() {
if (DbGlobal.serverDb != null && // if in RMI server
!getTenant().isUsageAllowed()) {
throw new DbRuntimeException("malicious client tried to use context " + this);
}
}
DbRuntimeException - if assertion failedpublic AppDbObject getContextObject()
public long getContextId()
public boolean isWithinContext(long contextId,
String contextClass)
The method is invoked from Security.evaluate(org.tentackle.appworx.ContextDb, int) to check whether a
security rule applies to a given context. Note that contextClass is the
basename of the context object's class, for example "Tenant" or "FiscalYear",
and not the classname of the ContextDb.
The default implementation returns contextId == 0 || contextId == getContextId().
This is sufficient for zero or one level of context inheritance. For more than one
level this method must be overwridden in each level. Example for a hypothetical "TenantContextDb":
public boolean isWithinContext(long contextId, String contextClass) {
if (contextClass != null && contextClass.equals("Tenant") && contextId == getContextId()) {
return true;
}
return super.isWithinContext(contextId, contextClass);
}
If the object IDs of the context objects are unique among all context entities the
contextClass can be ignored and the method reduces to:
public boolean isWithinContext(long contextId, String contextClass) {
return contextId == getContextId() ||
super.isWithinContext(contextId, contextClass);
}
contextId - the object ID of a context object, 0 = default contextcontextClass - the class basename of the context object, null = default context
public String toString()
toString in class Objectpublic String getInfo()
public int compareTo(ContextDb otherContextDb)
AppDbObjectCache
may keep several instances of the same object in different contexts.
The default implementation just compares the classes, then the ContextIds
and then the db connections.
compareTo in interface Comparable<ContextDb>otherContextDb - the context to compare this context to
public boolean equals(Object obj)
equals in class Objectobj - the object to check for equality
true if this object is the same as the obj
argument; false otherwise.Object.hashCode(),
Hashtablepublic int hashCode()
java.lang.Objectjava.util.Hashtable.
The general contract of hashCode is:
hashCode method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
hashCode in class ObjectObject.equals(java.lang.Object),
Hashtablepublic ContextDb clone()
clone in class ObjectCloneable
public static void applyToContextDependable(ContextDb contextDb,
ContextDependable obj)
ContextDependable, if not null.
contextDb - the database contextobj - the data object
public static void applyToCollection(ContextDb contextDb,
Collection<? extends ContextDependable> list)
ContextDependables.
contextDb - the database contextlist - the collection of data objects
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||