File: StockDispenserImpl.java

package info (click to toggle)
libpgjava 8.1-405-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,484 kB
  • ctags: 4,266
  • sloc: java: 32,071; xml: 3,017; sql: 21; makefile: 18; sh: 10
file content (92 lines) | stat: -rw-r--r-- 2,398 bytes parent folder | download
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.
 *
 * $PostgreSQL: pgjdbc/example/corba/StockDispenserImpl.java,v 1.7 2004/11/09 08:43:15 jurka 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;
        }
    }

}