Class DefaultDomainContext

java.lang.Object
org.tentackle.pdo.DefaultDomainContext
All Implemented Interfaces:
Serializable, Cloneable, Comparable<DomainContext>, DomainContext, SessionHolder

public class DefaultDomainContext extends Object implements DomainContext
The default application's domain context.
Author:
harald
See Also:
  • Constructor Details

    • DefaultDomainContext

      public DefaultDomainContext(Session session, boolean sessionImmutable)
      Creates a default context.
      Parameters:
      session - the session, null if thread-local
      sessionImmutable - true if session cannot be changed anymore
    • DefaultDomainContext

      public DefaultDomainContext(Session session)
      Creates a mutable default context.
      Parameters:
      session - the session, null if thread-local
  • Method Details

    • getNames

      public List<String> getNames()
      Description copied from interface: DomainContext
      Gets the names of this context.

      Notice: the names must not be part in equals, hashCode or compareTo implementations, because they denote only sub contexts. As a result, all sub contexts of the same root context are considered logically equal from a multi-tenancy perspective.

      Specified by:
      getNames in interface DomainContext
      Returns:
      the unmodifiable list of names, never null, never empty, the first element is always the empty string
    • isSessionThreadLocal

      public boolean isSessionThreadLocal()
      Specified by:
      isSessionThreadLocal in interface SessionHolder
    • getSession

      public Session getSession()

      If the context's session is null the thread's local session is returned.
      Specified by:
      getSession in interface SessionHolder
      See Also:
    • setSession

      public void setSession(Session session)
      Specified by:
      setSession in interface SessionHolder
    • setSessionImmutable

      public void setSessionImmutable(boolean sessionImmutable)
      Sets the session to immutable.
      Specified by:
      setSessionImmutable in interface SessionHolder
      Parameters:
      sessionImmutable - true if session cannot be changed anymore
    • isSessionImmutable

      public boolean isSessionImmutable()
      Returns whether the session is immutable.
      Specified by:
      isSessionImmutable in interface SessionHolder
      Returns:
      true if immutable
    • getSessionInstanceNumber

      public int getSessionInstanceNumber()
      Specified by:
      getSessionInstanceNumber in interface SessionHolder
    • getSessionInfo

      public SessionInfo getSessionInfo()
      Specified by:
      getSessionInfo in interface SessionHolder
    • assertPermissions

      public void assertPermissions()
      Description copied from interface: DomainContext
      Asserts that this context is allowed to be used by the current user.
      Only invoked when session is updated after deserialization (objects traveling between JVMs).

      If it is not allowed, a PersistenceException must be thrown.
      Override the method in middle tier servers, usually only necessary in multi-tenant applications. The default implementation does nothing.

      Specified by:
      assertPermissions in interface DomainContext
    • getContextPdo

      public PersistentDomainObject<?> getContextPdo()
      Description copied from interface: DomainContext
      Gets the object that spans this context.
      The default implementation returns null.
      Specified by:
      getContextPdo in interface DomainContext
      Returns:
      the root object, null if in default context
    • getContextId

      public long getContextId()
      Description copied from interface: DomainContext
      Gets the ID of the context object.
      The default implementation returns 0.
      Specified by:
      getContextId in interface DomainContext
      Returns:
      the object ID, 0 if in default context
    • isWithinContext

      public boolean isWithinContext(long contextId, int contextClassId)
      Description copied from interface: DomainContext
      Determines whether this context belongs to an inheritance hierarchy that is created from a given context object.

      The method is invoked from the security manager to check whether a security rule applies to a given context. Note that contextClassId is the class id of the context object's class.

      The default implementation returns contextId &lt; 0 || contextId == getContextId().
      This is sufficient for zero or one level of context inheritance. For more than one level this method must be overridden in each level. Example:

        boolean isWithinContext(long contextId, int contextClassId) {
          return contextClassId == BLAH_CLASS_ID && contextId == getContextId() ||
                 super.isWithinContext(contextId, contextClassId);
        }
       
      If the object IDs of the context objects are unique among all context entities the contextClass can be ignored and the method reduces to:
        boolean isWithinContext(long contextId, int contextClassId) {
          return contextId == getContextId() ||
                 super.isWithinContext(contextId, contextClassId);
        }
       
      Specified by:
      isWithinContext in interface DomainContext
      Parameters:
      contextId - the object ID of a context object, 0 = default context
      contextClassId - the class id of the context object, 0 = default context
      Returns:
      true if within that context, false if context does not apply
    • isWithinContext

      public boolean isWithinContext(String name)
      Description copied from interface: DomainContext
      Checks whether the given name equals the name of this or one of its parent contexts.
      Notice that the empty string is always within the context, since the topmost parent context is always unnamed.
      Specified by:
      isWithinContext in interface DomainContext
      Parameters:
      name - the requested name
      Returns:
      true if within context, false if not
    • toString

      public String toString()
      Gets the string representation of this context. The default implementation returns the string of the context object, or the empty string (not the null-string!), if no such object, which is the case for the plain context.
      Overrides:
      toString in class Object
      Returns:
      the string
    • toGenericString

      public String toGenericString()
      Description copied from interface: DomainContext
      Returns the generic string representation of this context.

      Use this for logging as it will not invoke methods on other objects.

      Specified by:
      toGenericString in interface DomainContext
      Returns:
      the String as Classname[contextId]
    • toDiagnosticString

      public String toDiagnosticString()
      Description copied from interface: DomainContext
      Get the long diagnostic string of this context.
      Used for logging, for example.
      Specified by:
      toDiagnosticString in interface DomainContext
      Returns:
      the long info
    • compareTo

      public int compareTo(DomainContext otherContext)
      Compares this domain context with another domain context.
      The default implementation just compares the class, the contextId and the session.
      Checking against the null context returns 1.
      Specified by:
      compareTo in interface Comparable<DomainContext>
      Parameters:
      otherContext - the context to compare this context to
    • equals

      public boolean equals(Object obj)

      Overridden to check whether contexts are equal.
      The default implementation checks the class, the contextId and session for equality. Checking against the null context returns false.

      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • cloneKeepRoot

      public DefaultDomainContext cloneKeepRoot()
      Description copied from interface: DomainContext
      Clones this context.
      Same as DomainContext.clone(), but keeps the root entity, if any is set.
      Specified by:
      cloneKeepRoot in interface DomainContext
      Returns:
      the context's clone
    • clone

      public DefaultDomainContext clone()
      Description copied from interface: DomainContext
      Clones this context.

      The clone gets the root entity and sessionImmutable cleared.

      Specified by:
      clone in interface DomainContext
      Overrides:
      clone in class Object
      Returns:
      the context's clone
    • cloneKeepRoot

      public DefaultDomainContext cloneKeepRoot(String name)
      Description copied from interface: DomainContext
      Clones this context with another context name.
      Same as DomainContext.clone(String), but keeps the root entity, if any is set.
      Specified by:
      cloneKeepRoot in interface DomainContext
      Parameters:
      name - the name of the new context
      Returns:
      the context's clone
    • clone

      public DefaultDomainContext clone(String name)
      Description copied from interface: DomainContext
      Clones this context with another context name.

      Same as DomainContext.clone(), but appends the given name to the current context names.
      The new name must be different from the current ones, not null, not containing only whitespaces and not containing a colon (: is used to concatenate the context names in toGenericString).

      Specified by:
      clone in interface DomainContext
      Parameters:
      name - the name of the new context
      Returns:
      the context's clone
    • getClonedContext

      public DomainContext getClonedContext()
      Description copied from interface: DomainContext
      Gets the context this context was cloned from.
      Specified by:
      getClonedContext in interface DomainContext
      Returns:
      null if not cloned or original context isn't available anymore (GC'd or was remote)
    • getThreadLocalSessionContext

      public DomainContext getThreadLocalSessionContext()
      Description copied from interface: DomainContext
      Gets a clone of this context with a thread-local session.

      Notice: A thread-local domain context is always a non-root context.

      Specified by:
      getThreadLocalSessionContext in interface DomainContext
      Returns:
      the context with a thread-local session
    • clearThreadLocalSessionContext

      public void clearThreadLocalSessionContext()
      Description copied from interface: DomainContext
      Clears the cached clone of this context.
      Forces cloning on next DomainContext.getThreadLocalSessionContext().
      Specified by:
      clearThreadLocalSessionContext in interface DomainContext
    • getNonRootContext

      public DefaultDomainContext getNonRootContext()
      Description copied from interface: DomainContext
      Gets a domain context which does not belong to a root entity.
      This is the original context the root-context has been cloned from.
      Specified by:
      getNonRootContext in interface DomainContext
      Returns:
      the non-root context
    • isRootContext

      public boolean isRootContext()
      Description copied from interface: DomainContext
      Returns whether this is a root entity context.
      Specified by:
      isRootContext in interface DomainContext
      Returns:
      true if root context
    • getRootContext

      public DomainContext getRootContext(PersistentDomainObject<?> rootEntity)
      Description copied from interface: DomainContext
      Creates a root context for a given root entity.
      If this context is already a root context for the same root-entity, nothing will be created and this context returned.
      Specified by:
      getRootContext in interface DomainContext
      Parameters:
      rootEntity - the root entity
      Returns:
      the root context
    • writeReplace

      public Object writeReplace() throws ObjectStreamException
      This does the trick to set up the non-transient rootId and rootClassId when sent via rmi the first time.
      Returns:
      me
      Throws:
      ObjectStreamException - to fulfill the signature only
      See Also:
    • getRootEntity

      public PersistentDomainObject<?> getRootEntity()
      Description copied from interface: DomainContext
      Gets the root entity for this context.
      Specified by:
      getRootEntity in interface DomainContext
      Returns:
      the root entity, null if none
    • getRootClassId

      public int getRootClassId()
      Description copied from interface: DomainContext
      Gets the class-ID of the root entity.
      Specified by:
      getRootClassId in interface DomainContext
      Returns:
      the class-ID, 0 if there is no root entity
    • getRootId

      public long getRootId()
      Description copied from interface: DomainContext
      Gets the ID of the root entity.
      Specified by:
      getRootId in interface DomainContext
      Returns:
      the ID, 0 if root entity is new or there is no root entity