Class ServiceSession

  • All Implemented Interfaces:
    java.lang.Comparable
    Direct Known Subclasses:
    NetSession

    public class ServiceSession
    extends java.lang.Object
    implements java.lang.Comparable
    This class must be implemented for all implementation of the service session. This kind of session provides an authenticated environment for all the service thread.
    Author:
    javaito
    • Constructor Summary

      Constructors 
      Constructor Description
      ServiceSession​(java.util.UUID id)  
    • Method Summary

      Modifier and Type Method Description
      void addEgressPackage​(long size)
      Adds the amount of bytes egress for this session
      void addGrant​(Grants.Grant grant)
      Add a grant into the session.
      void addIngressPackage​(long size)
      Adds the amount of bytes ingress for this session
      static void addServiceSessionSource​(ServiceSession.ServiceSessionSource source)
      Add session source into the global repository.
      protected void addThreadTime​(long time)
      Add system use time to specific session in nanoseconds.
      static <O> O callAs​(java.util.concurrent.Callable<O> callable, ServiceSession newIdentity)
      Run the callable instance in the same thread but in the scope of other identity, when the callable code ends the identity is removed automatically no matter how it's finished.
      int compareTo​(java.lang.Object object)
      Compare this session with other object.
      boolean containsGrant​(java.lang.String grantId)  
      <S extends ServiceSession>
      S
      currentIdentity()
      Return the last identity added into the session.
      void endThread()
      End some thread over this session.
      boolean equals​(java.lang.Object obj)
      Verify if the object to compare is an instance of service session and then verify if the current id is equals to the id of the service session into the parameter object.
      static <S extends ServiceSession>
      S
      findSession​(java.util.Map<java.lang.String,​java.lang.Object> sessionBean)
      Returns the service session instance rebuilding using the bean as parameter.
      static <S extends ServiceSession>
      S
      findSession​(java.util.UUID sessionId)
      Finds the service session using the id of the session.
      <O> O get​(java.lang.String propertyName)
      Returns a session property.
      java.util.Map<java.lang.String,​java.lang.Object> getBody()
      Returns the serializable body of the session instance.
      static <S extends ServiceSession>
      S
      getCurrentIdentity()
      Return the current identity associated to the current thread.
      ServiceSession.LayerStackElement getCurrentLayer()
      Returns the first layer of the session layer stack.
      static <S extends ServiceSession>
      S
      getCurrentSession()
      This method obtain the current session from the current thread.
      java.util.Set<Grants.Grant> getGrants()
      Returns the grants set of the session.
      static ServiceSession getGuestSession()
      Return the instance of the guest session.
      java.util.UUID getId()
      Return the session id.
      ServiceSession.LayerStackElement getInvokerLayer()
      Returns the second layer of the session layer stack.
      java.util.Collection<ServiceSession.LayerStackElement> getLayerStack()
      Returns the layer stack of the session.
      java.util.Locale getLocale()
      Returns locale of the session.
      java.util.Map<java.lang.String,​java.lang.Object> getProperties()
      Return the properties name of the session.
      java.lang.String getSessionName()
      Return the session name.
      static ServiceSession getSystemSession()
      Return the instance of the system session.
      boolean isGuestSession()
      Verify if the current session is the guest session.
      boolean isSystemSession()
      Verify if the current session is the system session.
      protected void onEndThread()
      Call to hook of the ends thread.
      protected void onStartThread()
      Call to hook of the starts thread.
      void put​(java.lang.String propertyName, java.lang.Object propertyValue)
      Put a property over the session.
      void putAll​(java.util.Map<java.lang.String,​java.lang.Object> properties)
      Put all the properties over the session.
      void putLayer​(ServiceSession.LayerStackElement element)
      Add an element into the layer stack.
      <O> O remove​(java.lang.String propertyName)
      Removes a session property.
      void removeGrant​(Grants.Grant grant)
      Removes the grant of the session.
      void removeLayer()
      Removes the head of the layer stack.
      static void runAs​(java.lang.Runnable runnable, ServiceSession newIdentity)
      Run the runnable instance in the same thread but in the scope of other identity, when the runnable code ends the identity is removed automatically no matter how it's finished.
      void setLocale​(java.util.Locale locale)
      Set locale of the session.
      void setSessionName​(java.lang.String sessionName)
      Set the session name.
      void startThread()
      Start some thread over this session.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ServiceSession

        public ServiceSession​(java.util.UUID id)
    • Method Detail

      • runAs

        public static void runAs​(java.lang.Runnable runnable,
                                 ServiceSession newIdentity)
        Run the runnable instance in the same thread but in the scope of other identity, when the runnable code ends the identity is removed automatically no matter how it's finished.
        Parameters:
        runnable - Runnable instance.
        newIdentity - Identity to run the runnable code.
      • callAs

        public static <O> O callAs​(java.util.concurrent.Callable<O> callable,
                                   ServiceSession newIdentity)
        Run the callable instance in the same thread but in the scope of other identity, when the callable code ends the identity is removed automatically no matter how it's finished.
        Type Parameters:
        O - Expected return type.
        Parameters:
        callable - Callable instance.
        newIdentity - Identity to run the callable.
        Returns:
        Returns the same value of the callable instance.
      • currentIdentity

        public final <S extends ServiceSession> S currentIdentity()
        Return the last identity added into the session.
        Type Parameters:
        S - Expected identity type.
        Returns:
        Service session that represents the current identity.
      • getId

        public final java.util.UUID getId()
        Return the session id.
        Returns:
        Session id.
      • setSessionName

        public final void setSessionName​(java.lang.String sessionName)
        Set the session name.
        Parameters:
        sessionName - Session name.
      • getSessionName

        public final java.lang.String getSessionName()
        Return the session name.
        Returns:
        Session Session name.
      • startThread

        public final void startThread()
        Start some thread over this session.
      • onStartThread

        protected void onStartThread()
        Call to hook of the starts thread.
      • endThread

        public final void endThread()
        End some thread over this session.
      • onEndThread

        protected void onEndThread()
        Call to hook of the ends thread.
      • getProperties

        public final java.util.Map<java.lang.String,​java.lang.Object> getProperties()
        Return the properties name of the session.
        Returns:
        Unmodifiable properties map.
      • putAll

        public final void putAll​(java.util.Map<java.lang.String,​java.lang.Object> properties)
        Put all the properties over the session.
        Parameters:
        properties - Properties.
      • put

        public final void put​(java.lang.String propertyName,
                              java.lang.Object propertyValue)
        Put a property over the session.
        Parameters:
        propertyName - Property name.
        propertyValue - Property value.
      • get

        public final <O> O get​(java.lang.String propertyName)
        Returns a session property.
        Type Parameters:
        O - Expected return type.
        Parameters:
        propertyName - Property name.
        Returns:
        Session value.
      • remove

        public final <O> O remove​(java.lang.String propertyName)
        Removes a session property.
        Type Parameters:
        O - Expected return type.
        Parameters:
        propertyName - Session property name.
        Returns:
        Session value removed.
      • putLayer

        public final void putLayer​(ServiceSession.LayerStackElement element)
        Add an element into the layer stack.
        Parameters:
        element - Layer stack element.
      • removeLayer

        public final void removeLayer()
        Removes the head of the layer stack.
      • getLayerStack

        public final java.util.Collection<ServiceSession.LayerStackElement> getLayerStack()
        Returns the layer stack of the session.
        Returns:
        Layer stack.
      • getInvokerLayer

        public final ServiceSession.LayerStackElement getInvokerLayer()
        Returns the second layer of the session layer stack.
        Returns:
        Invoker layer.
      • getLocale

        public final java.util.Locale getLocale()
        Returns locale of the session.
        Returns:
        Session locale.
      • setLocale

        public final void setLocale​(java.util.Locale locale)
        Set locale of the session.
        Parameters:
        locale - Session locale.
      • addThreadTime

        protected void addThreadTime​(long time)
        Add system use time to specific session in nanoseconds.
        Parameters:
        time - System use time in nanoseconds.
      • addIngressPackage

        public void addIngressPackage​(long size)
        Adds the amount of bytes ingress for this session
        Parameters:
        size - Amount of bytes.
      • addEgressPackage

        public void addEgressPackage​(long size)
        Adds the amount of bytes egress for this session
        Parameters:
        size - Amount of bytes.
      • addGrant

        public final void addGrant​(Grants.Grant grant)
        Add a grant into the session.
        Parameters:
        grant - Grant instance.
      • removeGrant

        public final void removeGrant​(Grants.Grant grant)
        Removes the grant of the session.
        Parameters:
        grant - Grant instance.
      • getGrants

        public final java.util.Set<Grants.Grant> getGrants()
        Returns the grants set of the session.
        Returns:
        Grants set.
      • containsGrant

        public final boolean containsGrant​(java.lang.String grantId)
      • isSystemSession

        public final boolean isSystemSession()
        Verify if the current session is the system session.
        Returns:
        True if the current session is the system session.
      • isGuestSession

        public final boolean isGuestSession()
        Verify if the current session is the guest session.
        Returns:
        True if the current session is the guest session.
      • compareTo

        public final int compareTo​(java.lang.Object object)
        Compare this session with other object.
        Specified by:
        compareTo in interface java.lang.Comparable
        Parameters:
        object - Object to compare.
        Returns:
        Return an integer to represent the difference between this session an the object.
      • equals

        public final boolean equals​(java.lang.Object obj)
        Verify if the object to compare is an instance of service session and then verify if the current id is equals to the id of the service session into the parameter object.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - Other service session.
        Returns:
        Return true if the id of both service session are equals.
      • addServiceSessionSource

        public static void addServiceSessionSource​(ServiceSession.ServiceSessionSource source)
        Add session source into the global repository.
        Parameters:
        source - Session source.
      • findSession

        public static <S extends ServiceSession> S findSession​(java.util.UUID sessionId)
        Finds the service session using the id of the session.
        Type Parameters:
        S - Expected service session type.
        Parameters:
        sessionId - Id of the session.
        Returns:
        Service session.
      • findSession

        public static <S extends ServiceSession> S findSession​(java.util.Map<java.lang.String,​java.lang.Object> sessionBean)
        Returns the service session instance rebuilding using the bean as parameter.
        Type Parameters:
        S - Expected session kind.
        Parameters:
        sessionBean - Session bean instance.
        Returns:
        Service session instance.
      • getSystemSession

        public static final ServiceSession getSystemSession()
        Return the instance of the system session.
        Returns:
        System session.
      • getGuestSession

        public static final ServiceSession getGuestSession()
        Return the instance of the guest session.
        Returns:
        Guest session.
      • getCurrentSession

        public static final <S extends ServiceSession> S getCurrentSession()
        This method obtain the current session from the current thread.
        Type Parameters:
        S - Expected session type.
        Returns:
        Current session.
      • getCurrentIdentity

        public static final <S extends ServiceSession> S getCurrentIdentity()
        Return the current identity associated to the current thread.
        Type Parameters:
        S - Expected session type.
        Returns:
        Current identity.
      • getBody

        public java.util.Map<java.lang.String,​java.lang.Object> getBody()
        Returns the serializable body of the session instance.
        Returns:
        Serializable instance.