01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.service.invoker;
19:
20: import org.apache.cxf.common.util.factory.AbstractPool;
21: import org.apache.cxf.common.util.factory.Factory;
22: import org.apache.cxf.common.util.factory.Pool;
23: import org.apache.cxf.common.util.factory.PooledFactory;
24: import org.apache.cxf.message.Exchange;
25: import org.apache.cxf.service.Service;
26: import org.apache.cxf.transport.Session;
27:
28: /**
29: * This scope policy implements one servant instance per session.
30: * <p>
31: *
32: * @author Ben Yu Feb 6, 2006 11:41:08 AM
33: */
34: public class SessionScopePolicy implements ScopePolicy {
35:
36: private static SessionScopePolicy singleton = new SessionScopePolicy();
37:
38: /**
39: * Get the key for caching a service.
40: *
41: * @param service the service.
42: * @return the key.
43: */
44: protected Object getServiceKey(Service service) {
45: return service.getName();
46: }
47:
48: public Factory applyScope(Factory f, Exchange ex) {
49: Service s = ex.get(Service.class);
50: return new PooledFactory(f, getSessionScope(getServiceKey(s),
51: ex.getSession()));
52: }
53:
54: public String toString() {
55: return "session scope";
56: }
57:
58: private static Pool getSessionScope(final Object key,
59: final Session session) {
60: return new AbstractPool() {
61: public Object get() {
62: return session.get(key);
63: }
64:
65: public void set(Object val) {
66: session.put(key, val);
67: }
68:
69: public String toString() {
70: return "session scope";
71: }
72:
73: /*
74: * This is not guaranteed to be safe with concurrent access to
75: * HttpSession. But better than nothing.
76: */
77: protected Object getMutex() {
78: return Service.class;
79: }
80: };
81: }
82:
83: public static ScopePolicy instance() {
84: return singleton;
85: }
86: }
|