aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/example/corba/StockDispenserImpl.java
blob: 15adfe559bca71aba32f12dbe3f38f13ca9602b4 (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
package example.corba;

import org.omg.CosNaming.*;

/*
 * This class implements the server side of the example.
 *
 * $Id: StockDispenserImpl.java,v 1.4 2001/11/19 23:19:20 momjian Exp $
 */
public class StockDispenserImpl extends stock._StockDispenserImplBase
{
	private int maxObjects = 10;
	private int numObjects = 0;
	private StockItemStatus[] stock = new StockItemStatus[maxObjects];

	public StockDispenserImpl(String[] args, String name, int num)
	{
		super();

		try
		{
			// get reference to orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

			// prestart num objects
			if (num >= maxObjects)
				num = maxObjects;
			numObjects = num;
			for (int i = 0;i < numObjects;i++)
			{
				stock[i] = new StockItemStatus();
				stock[i].ref = new StockItemImpl(args, "StockItem" + (i + 1));
				orb.connect(stock[i].ref);
			}
		}
		catch (org.omg.CORBA.SystemException e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * This method, defined in stock.idl, reserves a slot in the dispenser
	 */
	public stock.StockItem reserveItem() throws stock.StockException
	{
		for (int i = 0;i < numObjects;i++)
		{
			if (!stock[i].inUse)
			{
				stock[i].inUse = true;
				System.out.println("Reserving slot " + i);
				return stock[i].ref;
			}
		}
		return null;
	}

	/*
	 * This releases a slot from the dispenser
	 */
	public void releaseItem(stock.StockItem item) throws stock.StockException
	{
		for (int i = 0;i < numObjects;i++)
		{
			if (stock[i].ref.getInstanceName().equals(item.getInstanceName()))
			{
				stock[i].inUse = false;
				System.out.println("Releasing slot " + i);
				return;
			}
		}
		System.out.println("Reserved object not a member of this dispenser");
		return;
	}

	/*
	 * This class defines a slot in the dispenser
	 */
	class StockItemStatus
	{
		StockItemImpl ref;
		boolean inUse;

		StockItemStatus()
		{
			ref = null;
			inUse = false;
		}
	}

}