summaryrefslogtreecommitdiff
path: root/java/demo/Database/library/SessionI.java
blob: bd460271e484505bd50a2187bd967fd21fa365c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

import Demo.*;

class SessionI implements _SessionOperations, _Glacier2SessionOperations
{
    static class SessionDispatchInterceptorI extends Ice.DispatchInterceptor
    {
        SessionDispatchInterceptorI(Ice.Logger logger, ConnectionPool pool, Ice.Object servant, SessionI session)
        {
            _logger = logger;
            _pool = pool;
            _servant = servant;
            _session = session;
        }

        public Ice.DispatchStatus 
        dispatch(Ice.Request request)
        {
            SessionSQLRequestContext context = new SessionSQLRequestContext(_session, _logger, _pool);
            try
            {
                Ice.DispatchStatus status = _servant.ice_dispatch(request, null);
                context.destroyFromDispatch(status == Ice.DispatchStatus.DispatchOK);
                return status;
            }
            catch(JDBCException ex)
            {
                context.error("dispatch to " +
                              request.getCurrent().id.category + "/" + request.getCurrent().id.name +
                              " failed.", ex);
                context.destroyFromDispatch(false);

                // Translate the exception to UnknownException.
                Ice.UnknownException e = new Ice.UnknownException();
                ex.initCause(e);
                throw e;
            }
        }

        private Ice.Logger _logger;
        private ConnectionPool _pool;
        private Ice.Object _servant;
        private SessionI _session;
    }

    synchronized public LibraryPrx
    getLibrary(Ice.Current c)
    {
        if(_destroyed)
        {
            throw new Ice.ObjectNotExistException();
        }
        return _library;
    }

    synchronized public void
    refresh(Ice.Current c)
    {
        if(_destroyed)
        {
            throw new Ice.ObjectNotExistException();
        }
        _timestamp = System.currentTimeMillis();
    }

    synchronized public void
    destroy(Ice.Current c)
    {
        if(_destroyed)
        {
            throw new Ice.ObjectNotExistException();
        }

        _destroyed = true;
        _logger.trace("Session", "session " + c.adapter.getCommunicator().identityToString(c.id) +
                      " is now destroyed.");

        java.util.Iterator<QueryProxyPair> p = _queries.iterator();
        while(p.hasNext())
        {
            try
            {
                p.next().proxy.destroy();
            }
            catch(Ice.ObjectNotExistException e)
            {
                // Ignore, it could have already been destroyed.
            }
        }

        // This method is never called on shutdown of the server.
        c.adapter.remove(_library.ice_getIdentity());
        c.adapter.remove(c.id);
    }

    // Called on application shutdown.
    synchronized public void
    shutdown()
    {
        if(!_destroyed)
        {
            _destroyed = true;

            // Shutdown each of the associated query objects.
            java.util.Iterator<QueryProxyPair> p = _queries.iterator();
            while(p.hasNext())
            {
                p.next().impl.shutdown();
            }
        }
    }

    synchronized public long
    timestamp()
    {
        if(_destroyed)
        {
            throw new Ice.ObjectNotExistException();
        }
        return _timestamp;
    }

    synchronized public void
    add(BookQueryResultPrx proxy, BookQueryResultI impl)
    {
        // If the session has been destroyed, then destroy the book
        // result, and raise an ObjectNotExistException.
        if(_destroyed)
        {
            proxy.destroy();
            throw new Ice.ObjectNotExistException();
        }
        _queries.add(new QueryProxyPair(proxy, impl));
    }

    synchronized public void
    reapQueries()
    {
        if(_destroyed)
        {
            throw new Ice.ObjectNotExistException();
        }

        java.util.Iterator<QueryProxyPair> p = _queries.iterator();
        while(p.hasNext())
        {
            QueryProxyPair pair = p.next();
            try
            {
                pair.proxy.ice_ping();
            }
            catch(Ice.ObjectNotExistException e)
            {
                p.remove();
            }
        }
    }

    SessionI(Ice.Logger logger, ConnectionPool pool, Ice.ObjectAdapter adapter, Ice.Object libraryServant)
    {
        _logger = logger;
        _timestamp = System.currentTimeMillis();

        _library = LibraryPrxHelper.uncheckedCast(
            adapter.addWithUUID(new SessionDispatchInterceptorI(logger, pool, libraryServant, this)));
    }

    static class QueryProxyPair
    {
        QueryProxyPair(BookQueryResultPrx p, BookQueryResultI i)
        {
            proxy = p;
            impl = i;
        }

        BookQueryResultPrx proxy;
        BookQueryResultI impl;
    }

    private java.util.List<QueryProxyPair> _queries = new java.util.LinkedList<QueryProxyPair>();
    private Ice.Logger _logger;
    private boolean _destroyed = false; // true if destroy() was called, false otherwise.
    private long _timestamp; // The last time the session was refreshed.
    private LibraryPrx _library;
}