summaryrefslogtreecommitdiff
path: root/java/demo/Ice/session/SessionI.java
blob: bca977d64ee27f917a0d78fe3f003436ce02d498 (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
// **********************************************************************
//
// Copyright (c) 2003-2007 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 extends _SessionDisp
{
    public
    SessionI(String name)
    {
	_name = name;
	_timestamp = System.currentTimeMillis();
	System.out.println("The session " + _name + " is now created.");
    }

    synchronized public HelloPrx
    createHello(Ice.Current c)
    {
	if(_destroy)
	{
	    throw new Ice.ObjectNotExistException();
	}
	HelloPrx hello = HelloPrxHelper.uncheckedCast(c.adapter.addWithUUID(new HelloI(_name, _nextId++)));
	_objs.add(hello);
	return hello;
    }

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

    synchronized public String
    getName(Ice.Current c)
    {
	if(_destroy)
	{
	    throw new Ice.ObjectNotExistException();
	}
	return _name;
    }
    
    synchronized public void
    destroy(Ice.Current c)
    {
	if(_destroy)
	{
	    throw new Ice.ObjectNotExistException();
	}

	_destroy = true;
	System.out.println("The session " + _name + " is now destroyed.");
	try
	{
	    c.adapter.remove(c.id);
	    java.util.Iterator p = _objs.iterator();
	    while(p.hasNext())
	    {
		c.adapter.remove(((HelloPrx)p.next()).ice_getIdentity());
	    }
	}
	catch(Ice.ObjectAdapterDeactivatedException e)
	{
	    // This method is called on shutdown of the server, in
	    // which case this exception is expected.
	}
	_objs.clear();
    }

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

    private String _name;
    private boolean _destroy = false; // true if destroy() was called, false otherwise.
    private long _timestamp; // The last time the session was refreshed.
    private int _nextId = 0; // The id of the next hello object. This is used for tracing purposes.
    private java.util.List _objs = new java.util.LinkedList(); // List of per-client allocated Hello objects.
}