Package org.hcjf.service
Class ServiceSession
- java.lang.Object
-
- org.hcjf.service.ServiceSession
-
- All Implemented Interfaces:
java.lang.Comparable
- Direct Known Subclasses:
NetSession
public class ServiceSession extends java.lang.Object implements java.lang.ComparableThis 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classServiceSession.LayerStackElementstatic interfaceServiceSession.ServiceSessionSourceThis interface provides the generic gateway to find service session by id
-
Constructor Summary
Constructors Constructor Description ServiceSession(java.util.UUID id)
-
Method Summary
Modifier and Type Method Description voidaddEgressPackage(long size)Adds the amount of bytes egress for this sessionvoidaddGrant(Grants.Grant grant)Add a grant into the session.voidaddIngressPackage(long size)Adds the amount of bytes ingress for this sessionstatic voidaddServiceSessionSource(ServiceSession.ServiceSessionSource source)Add session source into the global repository.protected voidaddThreadTime(long time)Add system use time to specific session in nanoseconds.static <O> OcallAs(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.intcompareTo(java.lang.Object object)Compare this session with other object.booleancontainsGrant(java.lang.String grantId)<S extends ServiceSession>
ScurrentIdentity()Return the last identity added into the session.voidendThread()End some thread over this session.booleanequals(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>
SfindSession(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>
SfindSession(java.util.UUID sessionId)Finds the service session using the id of the session.<O> Oget(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>
SgetCurrentIdentity()Return the current identity associated to the current thread.ServiceSession.LayerStackElementgetCurrentLayer()Returns the first layer of the session layer stack.static <S extends ServiceSession>
SgetCurrentSession()This method obtain the current session from the current thread.java.util.Set<Grants.Grant>getGrants()Returns the grants set of the session.static ServiceSessiongetGuestSession()Return the instance of the guest session.java.util.UUIDgetId()Return the session id.ServiceSession.LayerStackElementgetInvokerLayer()Returns the second layer of the session layer stack.java.util.Collection<ServiceSession.LayerStackElement>getLayerStack()Returns the layer stack of the session.java.util.LocalegetLocale()Returns locale of the session.java.util.Map<java.lang.String,java.lang.Object>getProperties()Return the properties name of the session.java.lang.StringgetSessionName()Return the session name.static ServiceSessiongetSystemSession()Return the instance of the system session.booleanisGuestSession()Verify if the current session is the guest session.booleanisSystemSession()Verify if the current session is the system session.protected voidonEndThread()Call to hook of the ends thread.protected voidonStartThread()Call to hook of the starts thread.voidput(java.lang.String propertyName, java.lang.Object propertyValue)Put a property over the session.voidputAll(java.util.Map<java.lang.String,java.lang.Object> properties)Put all the properties over the session.voidputLayer(ServiceSession.LayerStackElement element)Add an element into the layer stack.<O> Oremove(java.lang.String propertyName)Removes a session property.voidremoveGrant(Grants.Grant grant)Removes the grant of the session.voidremoveLayer()Removes the head of the layer stack.static voidrunAs(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.voidsetLocale(java.util.Locale locale)Set locale of the session.voidsetSessionName(java.lang.String sessionName)Set the session name.voidstartThread()Start some thread over this session.
-
-
-
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.
-
getCurrentLayer
public final ServiceSession.LayerStackElement getCurrentLayer()
Returns the first layer of the session layer stack.- Returns:
- Current layer.
-
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:
compareToin interfacejava.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:
equalsin classjava.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.
-
-