KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > service > HttpRemoteService


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
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  ****************************************************************/

19
20 package org.apache.cayenne.remote.service;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.apache.cayenne.DataChannel;
29 import org.apache.cayenne.remote.RemoteSession;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * A {@link org.apache.cayenne.remote.RemoteService} implementation that stores
35  * server context information in HTTP sessions.
36  *
37  * @since 1.2
38  * @author Andrus Adamchik
39  */

40 public abstract class HttpRemoteService extends BaseRemoteService {
41
42     static final String JavaDoc SESSION_ATTRIBUTE = "HttpRemoteService.ServerSession";
43
44     // keep logger non-static so that it could be garbage collected with this instance..
45
private final Log logObj = LogFactory.getLog(HttpRemoteService.class);
46
47     private Map JavaDoc sharedChannels = new HashMap JavaDoc();
48
49     /**
50      * Returns an HttpSession associated with the current request in progress.
51      */

52     protected abstract HttpSession JavaDoc getSession(boolean create);
53
54     /**
55      * Returns a ServerSession object that represents Cayenne-related state associated
56      * with the current session. If ServerSession hasn't been previously saved, returns
57      * null.
58      */

59     protected ServerSession getServerSession() {
60         HttpSession JavaDoc httpSession = getSession(true);
61         return (ServerSession) httpSession.getAttribute(SESSION_ATTRIBUTE);
62     }
63
64     /**
65      * Creates a new ServerSession with a dedicated DataChannel. Returned ServerSession is
66      * stored in HttpSession for future reuse.
67      */

68     protected ServerSession createServerSession() {
69
70         HttpSession JavaDoc httpSession = getSession(true);
71
72         DataChannel channel = createChannel();
73         RemoteSession remoteSession = createRemoteSession(
74                 httpSession.getId(),
75                 null,
76                 false);
77         ServerSession serverSession = new ServerSession(remoteSession, channel);
78
79         httpSession.setAttribute(SESSION_ATTRIBUTE, serverSession);
80         return serverSession;
81     }
82
83     /**
84      * Creates a new ServerSession based on a shared DataChannel. Returned ServerSession
85      * is stored in HttpSession for future reuse.
86      *
87      * @param name shared session name used to lookup a shared DataChannel.
88      */

89     protected ServerSession createServerSession(String JavaDoc name) {
90         if (name == null) {
91             throw new IllegalArgumentException JavaDoc("Name is null for shared session.");
92         }
93
94         HttpSession JavaDoc httpSession = getSession(true);
95         DataChannel channel;
96
97         synchronized (sharedChannels) {
98             channel = getSharedChannel(name);
99             if (channel == null) {
100                 channel = createChannel();
101                 saveSharedChannel(name, channel);
102                 logObj.debug("Starting a new shared channel: " + name);
103             }
104             else {
105                 logObj.debug("Joining existing shared channel: " + name);
106             }
107         }
108
109         RemoteSession remoteSession = createRemoteSession(httpSession.getId(), name, true);
110
111         ServerSession serverSession = new ServerSession(remoteSession, channel);
112         httpSession.setAttribute(SESSION_ATTRIBUTE, serverSession);
113         return serverSession;
114     }
115
116     protected DataChannel getSharedChannel(String JavaDoc name) {
117         WeakReference JavaDoc ref = (WeakReference JavaDoc) sharedChannels.get(name);
118         return (ref != null) ? (DataChannel) ref.get() : null;
119     }
120
121     protected void saveSharedChannel(String JavaDoc name, DataChannel channel) {
122         // wrap value in a WeakReference so that channels can be deallocated when all
123
// sessions that reference this channel time out...
124
sharedChannels.put(name, new WeakReference JavaDoc(channel));
125     }
126 }
127
Popular Tags