KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > notification > whiteboard > WhiteBoardFactory


1 package demo.notification.whiteboard;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.ArrayList JavaDoc;
7
8 import org.omg.CORBA.IntHolder JavaDoc;
9 import org.omg.CORBA.ORB JavaDoc;
10 import org.omg.CosEventChannelAdmin.AlreadyConnected;
11 import org.omg.CosNaming.NameComponent JavaDoc;
12 import org.omg.CosNaming.NamingContext JavaDoc;
13 import org.omg.CosNaming.NamingContextHelper JavaDoc;
14 import org.omg.CosNotification.Property;
15 import org.omg.CosNotification.UnsupportedAdmin;
16 import org.omg.CosNotification.UnsupportedQoS;
17 import org.omg.CosNotifyChannelAdmin.AdminLimitExceeded;
18 import org.omg.CosNotifyChannelAdmin.EventChannel;
19 import org.omg.CosNotifyChannelAdmin.EventChannelFactory;
20 import org.omg.CosNotifyChannelAdmin.EventChannelFactoryHelper;
21 import org.omg.PortableServer.POA JavaDoc;
22 import org.omg.PortableServer.POAHelper JavaDoc;
23
24 import org.jacorb.notification.EventChannelFactoryImpl;
25
26 /**
27  * WhiteBoardFactory.java
28  *
29  *
30  * @author Alphonse Bendt
31  * @version $Id: WhiteBoardFactory.java,v 1.5 2004/04/28 13:52:02 alphonse.bendt Exp $
32  */

33
34 public class WhiteBoardFactory extends IFactoryPOA implements IFactoryOperations {
35
36     HashMap JavaDoc boards;
37     EventChannelFactory channelFactory_;
38     POA JavaDoc poa_;
39     ORB JavaDoc orb_;
40
41     public WhiteBoardFactory(ORB JavaDoc orb, POA JavaDoc poa, EventChannelFactory ecf) {
42         _this(orb);
43         boards = new HashMap JavaDoc();
44         orb_ = orb;
45         poa_ = poa;
46         channelFactory_ = ecf;
47     }
48
49     public IWhiteBoard getCreateWhiteboard(String JavaDoc name) {
50         try {
51             WhiteBoard board = null;
52             board = (WhiteBoard)boards.get(name);
53             if (board == null) {
54                 synchronized(boards) {
55                     board = (WhiteBoard)boards.get(name);
56                     if (board == null) {
57                         System.out.println("Create board "+name);
58                         IntHolder JavaDoc _channelId = new IntHolder JavaDoc();
59                         EventChannel _channel =
60                             channelFactory_.create_channel(new Property[0], new Property[0], _channelId);
61
62                         IntHolder JavaDoc _adminId = new IntHolder JavaDoc();
63
64                         board = new WhiteBoard(orb_, _channel);
65                         boards.put(name,board);
66                     }
67                 }
68             }
69             System.out.println("return board");
70
71             return board._this(orb_);
72         } catch (UnsupportedAdmin ua) {
73             ua.printStackTrace();
74         } catch (UnsupportedQoS uqos) {
75             uqos.printStackTrace();
76         } catch (AdminLimitExceeded ale) {
77             ale.printStackTrace();
78         }
79         throw new RuntimeException JavaDoc();
80     }
81
82     public String JavaDoc[] listAllWhiteboards() {
83         return (String JavaDoc[]) boards.keySet().toArray(new String JavaDoc[0]);
84     }
85
86
87     public static void main(String JavaDoc[] args) {
88         // CORBA initialisierung
89
try {
90             ORB JavaDoc _orb = ORB.init(args, null);
91
92             POA JavaDoc _poa =
93                 POAHelper.narrow(_orb.resolve_initial_references("RootPOA"));
94
95             NamingContext JavaDoc nc =
96                 NamingContextHelper.narrow(_orb.resolve_initial_references("NameService"));
97
98             EventChannelFactory _factory;
99             if (args != null && args.length == 1) {
100                 _factory =
101                     EventChannelFactoryHelper.narrow(_orb.string_to_object(args[0]));
102             } else {
103                 _factory = EventChannelFactoryHelper.narrow(_orb.resolve_initial_references("NotificationService"));
104             }
105
106             org.omg.CORBA.Object JavaDoc cob =
107                 _poa.servant_to_reference(new WhiteBoardFactory(_orb, _poa, _factory));
108
109             NameComponent JavaDoc [] name = new NameComponent JavaDoc[1];
110             name[0] = new NameComponent JavaDoc( "WhiteBoard", "Factory");
111
112             // an Namen binden
113
nc.rebind(name, cob);
114             _poa.the_POAManager().activate();
115
116             System.out.println("Whiteboard online !");
117
118             boolean non_exist = false;
119             try {
120                 non_exist = _factory._non_existent();
121             } catch (org.omg.CORBA.SystemException JavaDoc e) {
122                 non_exist = true;
123             }
124
125             if (non_exist) {
126                 System.out.println("NotificationService not available !");
127                 System.exit(1);
128             }
129
130             _orb.run();
131         } catch (Exception JavaDoc e) {
132             e.printStackTrace();
133             System.exit(1);
134         }
135     }
136 } // WhiteBoardFactory
137

138
Popular Tags