ICE 3.4.2
[php5-ice-freebsdport.git] / java / demo / Freeze / casino / Server.java
blob3d3601719cf5184aa6362d2615de8298d15fd8d5
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 class Server extends Ice.Application
12 static class ObjectFactory implements Ice.ObjectFactory
14 ObjectFactory(Class factoryClass)
16 _factoryClass = factoryClass;
19 public Ice.Object
20 create(String type)
22 try
24 return (Ice.Object)_factoryClass.newInstance();
26 catch(InstantiationException ex)
28 throw new Ice.InitializationException(ex.toString());
30 catch(IllegalAccessException ex)
32 throw new Ice.InitializationException(ex.toString());
36 public void
37 destroy()
41 private Class _factoryClass;
44 private java.util.Map<String, String>
45 createTypeMap(String defaultFacetType)
47 java.util.Map<String, String> result = new java.util.HashMap<String, String>();
48 result.put("", defaultFacetType);
49 return result;
52 public int
53 run(String[] args)
55 if(args.length > 0)
57 System.err.println(appName() + ": too many arguments");
58 return 1;
61 Ice.Properties properties = communicator().getProperties();
63 _bankEdge = properties.getPropertyAsInt("Bank.Edge");
64 if(_bankEdge < 1)
66 _bankEdge = 1;
68 System.out.println("Bank edge is " + _bankEdge);
71 // Create an object adapter
73 Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Casino");
76 // Register factories
78 communicator().addObjectFactory(new ObjectFactory(BankI.class), CasinoStore.PersistentBank.ice_staticId());
79 communicator().addObjectFactory(new ObjectFactory(PlayerI.class), CasinoStore.PersistentPlayer.ice_staticId());
80 communicator().addObjectFactory(new ObjectFactory(BetI.class), CasinoStore.PersistentBet.ice_staticId());
83 // Create evictors; each type gets its own type-specific evictor
87 // Bank evictor
90 Freeze.ServantInitializer bankInitializer = new Freeze.ServantInitializer()
92 public void
93 initialize(Ice.ObjectAdapter adapter, Ice.Identity identity, String facet, Ice.Object servant)
95 ((BankI)servant).init(_bankPrx, _bankEvictor, _playerEvictor, _betEvictor, _betResolver, _bankEdge);
99 _bankEvictor =
100 Freeze.Util.createTransactionalEvictor(adapter, _envName, "bank",
101 createTypeMap(CasinoStore.PersistentBank.ice_staticId()),
102 bankInitializer, null, true);
104 int size = properties.getPropertyAsInt("Bank.EvictorSize");
105 if(size > 0)
107 _bankEvictor.setSize(size);
110 adapter.addServantLocator(_bankEvictor, "bank");
113 // Player evictor
116 Freeze.ServantInitializer playerInitializer = new Freeze.ServantInitializer()
118 public void
119 initialize(Ice.ObjectAdapter adapter, Ice.Identity identity, String facet, Ice.Object servant)
121 CasinoStore.PersistentPlayerPrx prx =
122 CasinoStore.PersistentPlayerPrxHelper.uncheckedCast(adapter.createProxy(identity));
123 ((PlayerI)servant).init(prx, _playerEvictor, _bankPrx);
127 _playerEvictor =
128 Freeze.Util.createTransactionalEvictor(adapter, _envName, "player",
129 createTypeMap(CasinoStore.PersistentPlayer.ice_staticId()),
130 playerInitializer, null, true);
132 size = properties.getPropertyAsInt("Player.EvictorSize");
133 if(size > 0)
135 _playerEvictor.setSize(size);
138 adapter.addServantLocator(_playerEvictor, "player");
141 // Bet evictor
144 Freeze.ServantInitializer betInitializer = new Freeze.ServantInitializer()
146 public void
147 initialize(Ice.ObjectAdapter adapter, Ice.Identity identity, String facet, Ice.Object servant)
149 ((BetI)servant).init(_betEvictor, _bankEdge);
153 _betEvictor =
154 Freeze.Util.createTransactionalEvictor(adapter, _envName, "bet",
155 createTypeMap(CasinoStore.PersistentBet.ice_staticId()),
156 betInitializer, null, true);
157 size = properties.getPropertyAsInt("Bet.EvictorSize");
158 if(size > 0)
160 _betEvictor.setSize(size);
163 adapter.addServantLocator(_betEvictor, "bet");
166 // Prepare startup
169 _betResolver = new BetResolver();
174 // Retrieve / create the bank
177 Ice.Identity bankId = Ice.Util.stringToIdentity("bank/Montecito");
178 _bankPrx = CasinoStore.PersistentBankPrxHelper.uncheckedCast(adapter.createProxy(bankId));
180 if(!_bankEvictor.hasObject(bankId))
182 _bankEvictor.add(
183 new BankI(_bankPrx, _bankEvictor, _playerEvictor, _betEvictor, _betResolver, _bankEdge),
184 bankId);
188 // reload existing bets into the bet resolver
190 _bankPrx.reloadBets();
193 // Create players / recreate missing players using a transaction
194 // (the transaction is not really necessary here, but a good demo)
197 String[] players =
198 { "al", "bob", "charlie", "dave", "ed", "fred", "gene", "herb", "irvin", "joe", "ken", "lance" };
201 Freeze.Connection connection = Freeze.Util.createConnection(communicator(), _envName);
202 Freeze.Transaction tx = connection.beginTransaction();
204 _playerEvictor.setCurrentTransaction(tx);
206 for(String player : players)
208 Ice.Identity ident = new Ice.Identity(player, "player");
209 if(!_playerEvictor.hasObject(ident))
211 _playerEvictor.add(new PlayerI(), ident);
215 tx.commit();
216 assert(_playerEvictor.getCurrentTransaction() == null);
217 connection.close();
221 // Everything is ready, activate
223 adapter.activate();
225 shutdownOnInterrupt();
226 communicator().waitForShutdown();
227 defaultInterrupt();
229 finally
231 _betResolver.cancel();
233 return 0;
236 Server(String envName)
238 _envName = envName;
241 static public void
242 main(String[] args)
244 Server app = new Server("db");
245 app.main("demo.Freeze.casino.Server", args, "config.server");
248 private String _envName;
250 private CasinoStore.PersistentBankPrx _bankPrx;
251 private Freeze.TransactionalEvictor _bankEvictor;
252 private Freeze.TransactionalEvictor _playerEvictor;
253 private Freeze.TransactionalEvictor _betEvictor;
254 private BetResolver _betResolver;
255 private int _bankEdge;