ICE 3.4.2
[php5-ice-freebsdport.git] / java / test / Freeze / evictor / RemoteEvictorI.java
blob943bc61bf9808c6abd0c83fca79f92a2a1eb9420
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 package test.Freeze.evictor;
11 import test.Freeze.evictor.Test.*;
13 public final class RemoteEvictorI extends _RemoteEvictorDisp
15 private static void
16 test(boolean b)
18 if(!b)
20 throw new RuntimeException();
24 static class Initializer implements Freeze.ServantInitializer
26 public void
27 initialize(Ice.ObjectAdapter adapter, Ice.Identity ident, String facet, Ice.Object servant)
29 if(servant instanceof AccountI)
31 AccountI account = (AccountI)servant;
32 account.init((Freeze.TransactionalEvictor)_evictor);
34 else if(facet.length() == 0)
36 ServantI servantImpl = (ServantI) ((_ServantTie) servant).ice_delegate();
37 servantImpl.init(_remoteEvictor, _evictor);
39 else
41 ServantI servantImpl = (ServantI) ((_FacetTie) servant).ice_delegate();
42 servantImpl.init(_remoteEvictor, _evictor);
46 void init(RemoteEvictorI remoteEvictor, Freeze.Evictor evictor)
48 _remoteEvictor = remoteEvictor;
49 _evictor = evictor;
52 private RemoteEvictorI _remoteEvictor;
53 private Freeze.Evictor _evictor;
56 RemoteEvictorI(Ice.Communicator communicator, String envName, String category, boolean transactional)
58 _envName = envName;
59 _category = category;
62 // NOTE: COMPILERBUG: The timeout here is required for MacOS X. It shouldn't be too low since
63 // some operations can take some time to complete on slow machines.
65 _evictorAdapter = communicator.
66 createObjectAdapterWithEndpoints(java.util.UUID.randomUUID().toString(), "default -t 60000");
68 Initializer initializer = new Initializer();
70 if(transactional)
72 _evictor = Freeze.Util.createTransactionalEvictor(_evictorAdapter, envName, category, null, initializer,
73 null, true);
75 else
77 _evictor = Freeze.Util.createBackgroundSaveEvictor(_evictorAdapter, envName, category, initializer, null,
78 true);
82 // Check that we can get an iterator on a non-existing facet
84 Freeze.EvictorIterator p = _evictor.getIterator("foo", 1);
85 test(p.hasNext() == false);
87 initializer.init(this, _evictor);
89 _evictorAdapter.addServantLocator(_evictor, category);
90 _evictorAdapter.activate();
93 public void
94 setSize(int size, Ice.Current current)
96 _evictor.setSize(size);
99 public ServantPrx
100 createServant(String id, int value, Ice.Current current)
101 throws AlreadyRegisteredException, EvictorDeactivatedException
103 Ice.Identity ident = new Ice.Identity();
104 ident.category = _category;
105 ident.name = id;
106 _ServantTie tie = new _ServantTie();
107 tie.ice_delegate(new ServantI(tie, this, _evictor, value));
110 return ServantPrxHelper.uncheckedCast(_evictor.add(tie, ident));
112 catch(Ice.AlreadyRegisteredException e)
114 throw new AlreadyRegisteredException();
116 catch(Ice.ObjectAdapterDeactivatedException e)
118 throw new EvictorDeactivatedException();
120 catch(Freeze.EvictorDeactivatedException e)
122 throw new EvictorDeactivatedException();
124 catch(Ice.LocalException e)
126 System.out.println("Throwing " + e.toString());
127 throw e;
131 public ServantPrx
132 getServant(String id, Ice.Current current)
134 Ice.Identity ident = new Ice.Identity();
135 ident.category = _category;
136 ident.name = id;
137 return ServantPrxHelper.uncheckedCast(_evictorAdapter.createProxy(ident));
140 public void
141 saveNow(Ice.Current current)
143 if(_evictor instanceof Freeze.BackgroundSaveEvictor)
145 _evictor.getIterator("", 1);
148 // Otherwise everything is always saved
152 public void
153 deactivate(Ice.Current current)
155 _evictorAdapter.destroy();
156 current.adapter.remove(current.adapter.getCommunicator().stringToIdentity(_category));
159 public void
160 destroyAllServants(String facet, Ice.Current current)
163 // Only for test purpose: don't use such a small value in
164 // a real application!
166 int batchSize = 2;
168 Freeze.EvictorIterator p = _evictor.getIterator(facet, batchSize);
169 while(p.hasNext())
171 _evictor.remove((Ice.Identity) p.next());
175 final public String
176 envName()
178 return _envName;
181 private final String _envName;
182 private String _category;
183 private Freeze.Evictor _evictor;
184 private Ice.ObjectAdapter _evictorAdapter;