1 48 49 package com.caucho.services.server; 50 51 import javax.servlet.ServletException ; 52 import javax.servlet.ServletRequest ; 53 import java.util.HashMap ; 54 55 58 public class ServiceContext { 59 private static final ThreadLocal _localContext = new ThreadLocal (); 60 61 private ServletRequest _request; 62 private String _serviceName; 63 private String _objectId; 64 private int _count; 65 private HashMap _headers = new HashMap (); 66 67 private ServiceContext() 68 { 69 } 70 71 78 public static void begin(ServletRequest request, 79 String serviceName, 80 String objectId) 81 throws ServletException 82 { 83 ServiceContext context = (ServiceContext) _localContext.get(); 84 85 if (context == null) { 86 context = new ServiceContext(); 87 _localContext.set(context); 88 } 89 90 context._request = request; 91 context._serviceName = serviceName; 92 context._objectId = objectId; 93 context._count++; 94 } 95 96 99 public static ServiceContext getContext() 100 { 101 return (ServiceContext) _localContext.get(); 102 } 103 104 107 public void addHeader(String header, Object value) 108 { 109 _headers.put(header, value); 110 } 111 112 115 public Object getHeader(String header) 116 { 117 return _headers.get(header); 118 } 119 120 123 public static Object getContextHeader(String header) 124 { 125 ServiceContext context = (ServiceContext) _localContext.get(); 126 127 if (context != null) 128 return context.getHeader(header); 129 else 130 return null; 131 } 132 133 136 public static ServletRequest getContextRequest() 137 { 138 ServiceContext context = (ServiceContext) _localContext.get(); 139 140 if (context != null) 141 return context._request; 142 else 143 return null; 144 } 145 146 149 public static String getContextServiceName() 150 { 151 ServiceContext context = (ServiceContext) _localContext.get(); 152 153 if (context != null) 154 return context._serviceName; 155 else 156 return null; 157 } 158 159 162 public static String getContextObjectId() 163 { 164 ServiceContext context = (ServiceContext) _localContext.get(); 165 166 if (context != null) 167 return context._objectId; 168 else 169 return null; 170 } 171 172 175 public static void end() 176 { 177 ServiceContext context = (ServiceContext) _localContext.get(); 178 179 if (context != null && --context._count == 0) { 180 context._request = null; 181 182 context._headers.clear(); 183 } 184 } 185 186 191 public static ServletRequest getRequest() 192 { 193 ServiceContext context = (ServiceContext) _localContext.get(); 194 195 if (context != null) 196 return context._request; 197 else 198 return null; 199 } 200 201 206 public static String getServiceName() 207 { 208 ServiceContext context = (ServiceContext) _localContext.get(); 209 210 if (context != null) 211 return context._serviceName; 212 else 213 return null; 214 } 215 216 221 public static String getObjectId() 222 { 223 ServiceContext context = (ServiceContext) _localContext.get(); 224 225 if (context != null) 226 return context._objectId; 227 else 228 return null; 229 } 230 } 231 | Popular Tags |