|
@@ -1,103 +1,103 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.core; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.commonfoundation.typeutils.Objects; |
5 |
|
- |
|
6 |
|
- |
import java.util.*; |
7 |
|
- |
|
8 |
|
- |
@SuppressWarnings({"UnusedDeclaration"}) |
9 |
|
- |
public class ServerContext { |
10 |
|
- |
private static final ThreadLocal<ServerContext> DATA = new ThreadLocal<ServerContext>(); |
11 |
|
- |
|
12 |
|
- |
/** |
13 |
|
- |
* Get the current Thread's (ThreadLocal) Server Context. |
14 |
|
- |
* |
15 |
|
- |
* @return the current Server Context |
16 |
|
- |
* |
17 |
|
- |
* @throws IllegalStateException if 'set' has not been called on this Thread. |
18 |
|
- |
*/ |
19 |
|
- |
public static ServerContext get() { |
20 |
|
- |
ServerContext zContext = DATA.get(); |
21 |
|
- |
if ( zContext == null ) { |
22 |
|
- |
throw new IllegalStateException( "No Server Context for current Thread" ); |
23 |
|
- |
} |
24 |
|
- |
return zContext; |
25 |
|
- |
} |
26 |
|
- |
|
27 |
|
- |
/** |
28 |
|
- |
* Clear (or remove) the current Thread's (ThreadLocal) Server Context. |
29 |
|
- |
*/ |
30 |
|
- |
public static void clear() { |
31 |
|
- |
DATA.remove(); |
32 |
|
- |
} |
33 |
|
- |
|
34 |
|
- |
/** |
35 |
|
- |
* Set the current Thread's (ThreadLocal) Server Context to 'this'. |
36 |
|
- |
*/ |
37 |
|
- |
public void set() { |
38 |
|
- |
DATA.set( this ); |
39 |
|
- |
} |
40 |
|
- |
|
41 |
|
- |
private ContextID mContextID; |
42 |
|
- |
private ServerSession mServerSession; |
43 |
|
- |
|
44 |
|
- |
public ServerContext( ContextID pContextID, ServerSession pServerSession ) { |
45 |
|
- |
Objects.assertNotNull( "ContextID", mContextID = pContextID ); |
46 |
|
- |
Objects.assertNotNull( "ServerSession", mServerSession = pServerSession ); |
47 |
|
- |
} |
48 |
|
- |
|
49 |
|
- |
public ContextID getContextID() { |
50 |
|
- |
return mContextID; |
51 |
|
- |
} |
52 |
|
- |
|
53 |
|
- |
public ServerSession getServerSession() { |
54 |
|
- |
return mServerSession; |
55 |
|
- |
} |
56 |
|
- |
|
57 |
|
- |
private static final ServerStore TEMPLATE_ServerStore = new ServerStore(); |
58 |
|
- |
|
59 |
|
- |
public static ServerStore getTemplateServerStore() { |
60 |
|
- |
return TEMPLATE_ServerStore; |
61 |
|
- |
} |
62 |
|
- |
|
63 |
|
- |
private static final ServerStore MASTER_ServerStore = new ServerStore(); |
64 |
|
- |
|
65 |
|
- |
public static ServerStore getMasterServerStore() { |
66 |
|
- |
return MASTER_ServerStore; |
67 |
|
- |
} |
68 |
|
- |
|
69 |
|
- |
public ServerStore getServerStore() { |
70 |
|
- |
synchronized ( SERVER_STORES ) { |
71 |
|
- |
ServerStore zServerStore = SERVER_STORES.get( mContextID ); |
72 |
|
- |
if ( zServerStore == null ) { |
73 |
|
- |
SERVER_STORES.put( mContextID, zServerStore = new ServerStore( TEMPLATE_ServerStore ) ); |
74 |
|
- |
} |
75 |
|
- |
return zServerStore; |
76 |
|
- |
} |
77 |
|
- |
} |
78 |
|
- |
|
79 |
|
- |
public static void clearServerStore( ContextID pContextID ) { |
80 |
|
- |
if ( pContextID != null ) { |
81 |
|
- |
synchronized ( SERVER_STORES ) { |
82 |
|
- |
LLremoveServerStore( pContextID ); |
83 |
|
- |
} |
84 |
|
- |
} |
85 |
|
- |
} |
86 |
|
- |
|
87 |
|
- |
private static final Map<ContextID, ServerStore> SERVER_STORES = new HashMap<ContextID, ServerStore>(); |
88 |
|
- |
|
89 |
|
- |
public static void clearAllServerStores() { |
90 |
|
- |
synchronized ( SERVER_STORES ) { |
91 |
|
- |
for ( ContextID zContextID : SERVER_STORES.keySet() ) { |
92 |
|
- |
LLremoveServerStore( zContextID ); |
93 |
|
- |
} |
94 |
|
- |
} |
95 |
|
- |
} |
96 |
|
- |
|
97 |
|
- |
private static void LLremoveServerStore( ContextID pContextID ) { |
98 |
|
- |
ServerStore zServerStore = SERVER_STORES.remove( pContextID ); |
99 |
|
- |
if ( zServerStore != null ) { |
100 |
|
- |
zServerStore.dispose(); |
101 |
|
- |
} |
102 |
|
- |
} |
103 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.core; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
5 |
+ |
|
|
6 |
+ |
import java.util.*; |
|
7 |
+ |
|
|
8 |
+ |
@SuppressWarnings({"UnusedDeclaration"}) |
|
9 |
+ |
public class ServerContext { |
|
10 |
+ |
private static final ThreadLocal<ServerContext> DATA = new ThreadLocal<ServerContext>(); |
|
11 |
+ |
|
|
12 |
+ |
/** |
|
13 |
+ |
* Get the current Thread's (ThreadLocal) Server Context. |
|
14 |
+ |
* |
|
15 |
+ |
* @return the current Server Context |
|
16 |
+ |
* |
|
17 |
+ |
* @throws IllegalStateException if 'set' has not been called on this Thread. |
|
18 |
+ |
*/ |
|
19 |
+ |
public static ServerContext get() { |
|
20 |
+ |
ServerContext zContext = DATA.get(); |
|
21 |
+ |
if ( zContext == null ) { |
|
22 |
+ |
throw new IllegalStateException( "No Server Context for current Thread" ); |
|
23 |
+ |
} |
|
24 |
+ |
return zContext; |
|
25 |
+ |
} |
|
26 |
+ |
|
|
27 |
+ |
/** |
|
28 |
+ |
* Clear (or remove) the current Thread's (ThreadLocal) Server Context. |
|
29 |
+ |
*/ |
|
30 |
+ |
public static void clear() { |
|
31 |
+ |
DATA.remove(); |
|
32 |
+ |
} |
|
33 |
+ |
|
|
34 |
+ |
/** |
|
35 |
+ |
* Set the current Thread's (ThreadLocal) Server Context to 'this'. |
|
36 |
+ |
*/ |
|
37 |
+ |
public void set() { |
|
38 |
+ |
DATA.set( this ); |
|
39 |
+ |
} |
|
40 |
+ |
|
|
41 |
+ |
private ContextID mContextID; |
|
42 |
+ |
private ServerSession mServerSession; |
|
43 |
+ |
|
|
44 |
+ |
public ServerContext( ContextID pContextID, ServerSession pServerSession ) { |
|
45 |
+ |
Confirm.isNotNull( "ContextID", mContextID = pContextID ); |
|
46 |
+ |
Confirm.isNotNull( "ServerSession", mServerSession = pServerSession ); |
|
47 |
+ |
} |
|
48 |
+ |
|
|
49 |
+ |
public ContextID getContextID() { |
|
50 |
+ |
return mContextID; |
|
51 |
+ |
} |
|
52 |
+ |
|
|
53 |
+ |
public ServerSession getServerSession() { |
|
54 |
+ |
return mServerSession; |
|
55 |
+ |
} |
|
56 |
+ |
|
|
57 |
+ |
private static final ServerStore TEMPLATE_ServerStore = new ServerStore(); |
|
58 |
+ |
|
|
59 |
+ |
public static ServerStore getTemplateServerStore() { |
|
60 |
+ |
return TEMPLATE_ServerStore; |
|
61 |
+ |
} |
|
62 |
+ |
|
|
63 |
+ |
private static final ServerStore MASTER_ServerStore = new ServerStore(); |
|
64 |
+ |
|
|
65 |
+ |
public static ServerStore getMasterServerStore() { |
|
66 |
+ |
return MASTER_ServerStore; |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
public ServerStore getServerStore() { |
|
70 |
+ |
synchronized ( SERVER_STORES ) { |
|
71 |
+ |
ServerStore zServerStore = SERVER_STORES.get( mContextID ); |
|
72 |
+ |
if ( zServerStore == null ) { |
|
73 |
+ |
SERVER_STORES.put( mContextID, zServerStore = new ServerStore( TEMPLATE_ServerStore ) ); |
|
74 |
+ |
} |
|
75 |
+ |
return zServerStore; |
|
76 |
+ |
} |
|
77 |
+ |
} |
|
78 |
+ |
|
|
79 |
+ |
public static void clearServerStore( ContextID pContextID ) { |
|
80 |
+ |
if ( pContextID != null ) { |
|
81 |
+ |
synchronized ( SERVER_STORES ) { |
|
82 |
+ |
LLremoveServerStore( pContextID ); |
|
83 |
+ |
} |
|
84 |
+ |
} |
|
85 |
+ |
} |
|
86 |
+ |
|
|
87 |
+ |
private static final Map<ContextID, ServerStore> SERVER_STORES = new HashMap<ContextID, ServerStore>(); |
|
88 |
+ |
|
|
89 |
+ |
public static void clearAllServerStores() { |
|
90 |
+ |
synchronized ( SERVER_STORES ) { |
|
91 |
+ |
for ( ContextID zContextID : SERVER_STORES.keySet() ) { |
|
92 |
+ |
LLremoveServerStore( zContextID ); |
|
93 |
+ |
} |
|
94 |
+ |
} |
|
95 |
+ |
} |
|
96 |
+ |
|
|
97 |
+ |
private static void LLremoveServerStore( ContextID pContextID ) { |
|
98 |
+ |
ServerStore zServerStore = SERVER_STORES.remove( pContextID ); |
|
99 |
+ |
if ( zServerStore != null ) { |
|
100 |
+ |
zServerStore.dispose(); |
|
101 |
+ |
} |
|
102 |
+ |
} |
|
103 |
+ |
} |