Changeset 2598
- Timestamp:
- 07/05/08 22:16:07 (3 months ago)
- Location:
- trunk/components
- Files:
-
- 5 modified
-
blitz/src/omero/client.java (modified) (10 diffs)
-
blitz/test/omero/itests/UsageTest.java (modified) (2 diffs)
-
tools/OmeroCpp/src/omero/client.cpp (modified) (6 diffs)
-
tools/OmeroCpp/src/omero/client.h (modified) (4 diffs)
-
tools/OmeroCpp/test/boost_fixture.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/components/blitz/src/omero/client.java
r2256 r2598 10 10 import java.util.Collections; 11 11 import java.util.HashSet; 12 import java.util.List; 12 13 import java.util.Properties; 13 14 import java.util.Set; 14 15 import java.util.UUID; 16 17 import omero.api.IAdminPrx; 18 import omero.api.ISessionPrx; 15 19 import omero.api.ServiceFactoryPrx; 16 20 import omero.api.ServiceFactoryPrxHelper; … … 57 61 ServiceFactoryPrx sf; 58 62 59 boolean close_on_destroy = false;60 61 63 boolean closed = false; 62 64 … … 106 108 throw new ClientError("No communicator."); 107 109 } 110 // Register Object Factory 108 111 ObjectFactoryRegistrar.registerObjectFactory(ic, 109 112 ObjectFactoryRegistrar.INSTANCE); 113 // Define our unique identifer (used during close/detach) 114 ic.getImplicitContext().put(omero.constants.CLIENTUUID.value, 115 UUID.randomUUID().toString()); 110 116 CLIENTS.add(this); 111 117 } … … 114 120 // ========================================================================= 115 121 116 public void closeOnDestroy() { 117 close_on_destroy = true; 118 } 119 122 /** 123 * Equivalent to OmeroPy's __del__ or OmeroCpp's omero::client::~client() 124 */ 120 125 public void close() { 121 if (close_on_destroy) { 122 try { 123 closeSession(); 124 } catch (Exception e) { 125 // ok. 126 } 127 } 128 if (ic != null) { 129 try { 130 ic.getLogger() 131 .trace("omero.client.destroy", "Destroying " + ic); 132 ic.destroy(); 133 ic = null; 134 } catch (Exception e) { 135 // ok 136 } 126 try { 127 closeSession(); 128 } catch (Exception e) { 129 System.out.println("Ignoring error in client.close()"); 130 e.printStackTrace(); 131 } finally { 132 closed = true; 137 133 } 138 134 } … … 143 139 144 140 public ServiceFactoryPrx getServiceFactory() { 141 if (sf == null) { 142 throw new ClientError("Call createSession() to login."); 143 } 145 144 return sf; 146 145 } … … 161 160 public ServiceFactoryPrx createSession(String username, String password) 162 161 throws CannotCreateSessionException, PermissionDeniedException { 162 163 // Check the required properties 163 164 if (username == null) { 164 165 username = getProperty("omero.user"); … … 174 175 } 175 176 177 // Acquire router and get the proxy 178 // For whatever reason, we have to se the context 179 // on the router context here as well 176 180 Glacier2.SessionPrx prx = getRouter().createSession(username, password); 177 181 if (null == prx) { 178 throw new ClientError("No session obtained"); 179 } 182 throw new ClientError("Obtained null object proxy"); 183 } 184 prx = Glacier2.SessionPrxHelper.uncheckedCast(prx.ice_context(ic 185 .getImplicitContext().getContext())); 180 186 sf = ServiceFactoryPrxHelper.checkedCast(prx); 181 187 if (sf == null) { 182 throw new ClientError("No session obtained"); 183 } 188 throw new ClientError( 189 "Obtained object proxy is not a ServiceFactory"); 190 } 191 184 192 return this.sf; 185 193 } … … 199 207 200 208 public void closeSession() { 201 if (this.sf == null) { 202 return; // EARLY EXIT 203 } 204 205 try { 206 sf.close(); 207 sf = null; 208 } catch (Exception e) { 209 // what can we do 209 210 ServiceFactoryPrx old = this.sf; 211 if (this.sf != null) { 212 this.sf = null; 213 } 214 215 if (ic == null && sf != null) { 216 ic = sf.ice_getCommunicator(); 217 } 218 219 if (ic == null) { 220 return; // EARLY EXIT! 210 221 } 211 222 … … 216 227 } catch (Ice.ConnectionLostException cle) { 217 228 // ok. Exception will always be thrown 218 } 219 } 220 221 public Object getInput(String key) { 222 throw new UnsupportedOperationException(); 223 } 224 225 public Object getOutput(String key) { 226 throw new UnsupportedOperationException(); 227 } 228 229 public void setInput(String key, Object value) { 230 throw new UnsupportedOperationException(); 231 } 232 233 public void setOutput(String key, Object value) { 234 throw new UnsupportedOperationException(); 229 } finally { 230 ic = null; 231 } 232 } 233 234 // Environment methods 235 // ========================================================================= 236 237 public RType getInput(String key) throws ServerError { 238 return env().getInput(sess(), key); 239 } 240 241 public RType getOutput(String key) throws ServerError { 242 return env().getOutput(sess(), key); 243 } 244 245 public void setInput(String key, RType value) throws ServerError { 246 env().setInput(sess(), key, value); 247 } 248 249 public void setOutput(String key, RType value) throws ServerError { 250 env().setOutput(sess(), key, value); 251 } 252 253 public List<String> getInputKeys() throws ServerError { 254 return env().getInputKeys(sess()); 255 } 256 257 public List<String> getOutputKeys() throws ServerError { 258 return env().getOutputKeys(sess()); 235 259 } 236 260 … … 249 273 } 250 274 275 /** 276 * Helper method to access session environment 277 */ 278 protected ISessionPrx env() throws ServerError { 279 if (sf == null) { 280 throw new ClientError("No session active"); 281 } 282 ISessionPrx s = sf.getSessionService(); 283 return s; 284 } 285 286 /** 287 * Helper method to access session id 288 */ 289 protected String sess() throws ServerError { 290 if (sf == null) { 291 throw new ClientError("No session active"); 292 } 293 IAdminPrx a = sf.getAdminService(); 294 String u = a.getEventContext().sessionUuid; 295 return u; 296 } 251 297 } -
trunk/components/blitz/test/omero/itests/UsageTest.java
r2256 r2598 10 10 11 11 import junit.framework.TestCase; 12 import omero.RString; 12 13 13 14 import org.springframework.util.ResourceUtils; … … 18 19 @Test 19 20 public void testClientClosedAutomatically() throws Exception { 20 File f1 = ResourceUtils.getFile("classpath: ice.config");21 File f2 = ResourceUtils.getFile("classpath:local.properties");22 omero.client client = new omero.client(f1, f2);23 client. closeOnDestroy();21 File f1 = ResourceUtils.getFile("classpath:local.properties"); 22 omero.client client = new omero.client(f1); 23 client.createSession(); 24 client.getServiceFactory().closeOnDestroy(); 24 25 } 25 26 26 27 @Test 27 28 public void testClientClosedManually() throws Exception { 28 File f1 = ResourceUtils.getFile("classpath: ice.config");29 File f2 = ResourceUtils.getFile("classpath:local.properties");30 omero.client client = new omero.client(f1, f2);31 client. closeOnDestroy();29 File f1 = ResourceUtils.getFile("classpath:local.properties"); 30 omero.client client = new omero.client(f1); 31 client.createSession(); 32 client.getServiceFactory().closeOnDestroy(); 32 33 client.close(); 33 34 } 35 36 @Test 37 public void testUseSharedMemory() throws Exception { 38 File f1 = ResourceUtils.getFile("classpath:local.properties"); 39 omero.client client = new omero.client(f1); 40 client.createSession(); 41 42 assertEquals(0, client.getInputKeys().size()); 43 client.setInput("a", new omero.RString("b")); 44 assertEquals(1, client.getInputKeys().size()); 45 assertTrue(client.getInputKeys().contains("a")); 46 assertEquals("b", ((RString) client.getInput("a")).val); 47 48 client.close(); 49 } 50 34 51 } -
trunk/components/tools/OmeroCpp/src/omero/client.cpp
r2256 r2598 17 17 throw omero::ClientError(__FILE__,__LINE__,"Improper initialization."); 18 18 } 19 // Register Object Factory 19 20 ObjectFactoryPtr of = new ObjectFactory(); 20 21 of->registerObjectFactory(ic); 22 // Define our unique identifier (used during close/detach) 23 ic->getImplicitContext()->put(omero::constants::CLIENTUUID, IceUtil::generateUUID()); 21 24 } 22 25 23 26 client::client(const Ice::InitializationData& id) { 24 27 ic = Ice::initialize(id); 25 close_on_destroy = false;26 28 init(ic); 27 29 } … … 30 32 const Ice::InitializationData& id) { 31 33 ic = Ice::initialize(argc, argv, id); 32 close_on_destroy = false;33 34 init(ic); 34 35 } 35 36 36 37 client::~client(){ 37 if (close_on_destroy && sf){38 try { 38 39 closeSession(); 39 } 40 if (ic) { 41 try { 42 ic->destroy(); 43 } catch (const Ice::Exception& ex) { 44 cerr << "Caught Ice exception while destroying communicator." << endl; 45 cerr << ex << endl; 46 } 47 ic = Ice::CommunicatorPtr(); 40 } catch (...) { 41 std::cout << "Ignoring error in ~client"<< std::endl; 48 42 } 49 43 } … … 51 45 omero::api::ServiceFactoryPrx client::createSession(const std::string& _username, const std::string& _password) { 52 46 47 // Check the required properties 53 48 std::string username, password; 54 49 if (_username.empty()) { … … 70 65 } 71 66 67 // Acquire router and get the proxy 68 // For whatever reason, we have to se the context 69 // on the router context here as well. 72 70 Ice::RouterPrx prx = ic->getDefaultRouter(); 73 71 if (!prx) { 74 72 throw omero::ClientError(__FILE__,__LINE__,"No default router found."); 75 73 } 74 prx = Ice::RouterPrx::uncheckedCast(prx->ice_context(ic->getImplicitContext()->getContext())); 76 75 Glacier2::RouterPrx router = Glacier2::RouterPrx::checkedCast(prx); 77 76 if (!router) { … … 81 80 Glacier2::SessionPrx session; 82 81 session = router->createSession(username, password); 82 if (!session) { 83 throw omero::ClientError(__FILE__,__LINE__,"Obtained null object proxy"); 84 } 85 86 // Check type 83 87 sf = omero::api::ServiceFactoryPrx::checkedCast(session); 84 88 if (!sf) { 85 throw omero::ClientError(__FILE__,__LINE__," No session obtained.");89 throw omero::ClientError(__FILE__,__LINE__,"Obtained object proxy is not a ServiceFactory."); 86 90 } 87 91 return sf; … … 89 93 90 94 void client::closeSession() { 91 if (sf) { 92 try { 93 sf->close(); 94 } catch (const Ice::Exception& ex) { 95 // ok 95 96 omero::api::ServiceFactoryPrx old = sf; 97 sf = omero::api::ServiceFactoryPrx(); 98 99 if (!ic && old) { 100 ic = old->ice_getCommunicator(); 96 101 } 97 sf = omero::api::ServiceFactoryPrx(); 98 } 99 Ice::RouterPrx prx = ic->getDefaultRouter(); 100 Glacier2::RouterPrx router = Glacier2::RouterPrx::checkedCast(prx); 101 try { 102 router->destroySession(); 103 } catch (const Ice::ConnectionLostException& cle) { 104 // ok. Always thrown. 105 } 102 103 if (ic) { 104 105 Ice::RouterPrx prx = ic->getDefaultRouter(); 106 Glacier2::RouterPrx router = Glacier2::RouterPrx::checkedCast(prx); 107 if (router) { 108 try { 109 router->destroySession(); 110 } catch (const Ice::ConnectionLostException& cle) { 111 // ok. Always thrown. 112 } // TODO what about SNEE 113 } 114 115 try { 116 ic->destroy(); 117 } catch (const Ice::Exception& ex) { 118 cerr << "Caught Ice exception while destroying communicator." << endl; 119 cerr << ex << endl; 120 } 121 ic = Ice::CommunicatorPtr(); 122 } 123 106 124 } 107 125 126 // Environment methods 127 // ====================================================================== 128 omero::RTypePtr client::getInput(const string& key) { 129 return env()->getInput(sess(), key); 130 } 131 omero::RTypePtr client::getOutput(const string& key) { 132 return env()->getOutput(sess(), key); 133 } 134 void client::setInput(const string& key, omero::RTypePtr value) { 135 env()->setInput(sess(), key, value); 136 } 137 void client::setOutput(const string& key, omero::RTypePtr value) { 138 env()->setOutput(sess(), key, value); 139 } 140 vector<string> client::getInputKeys() { 141 env()->getInputKeys(sess()); 142 } 143 vector<string> client::getOutputKeys() { 144 env()->getOutputKeys(sess()); 145 } 146 omero::api::ISessionPrx client::env() { 147 return sf->getSessionService(); 148 } 149 const std::string client::sess() { 150 return sf->getAdminService()->getEventContext()->sessionUuid; 151 } 108 152 } 109 153 -
trunk/components/tools/OmeroCpp/src/omero/client.h
r2256 r2598 32 32 * omero::api::ServiceFactoryPrx which is the blitz session facade, 33 33 * from which all other proxies can be obtained. Once the 34 * ServiceFactoryPrx is destroyed , times out, or close() is called,34 * ServiceFactoryPrx is destroyed or the session times out, 35 35 * all proxies obtained from this instance are also destroyed. 36 36 * … … 49 49 // These are the central instances provided by this class. 50 50 protected: 51 bool close_on_destroy;52 51 Ice::CommunicatorPtr ic; 53 52 omero::api::ServiceFactoryPrx sf; … … 76 75 /* 77 76 * Destroys the communicator instance. To have the session destroyed, 78 * call close on the client before destruction. Otherwise, the session79 * will be destroyed by the server on timeout.77 * call getSession().closeOnDestroy() on the client before destruction. 78 * Otherwise, the session will be destroyed by the server on timeout. 80 79 */ 81 80 ~client(); … … 99 98 100 99 /* 101 * Frees server-side resources. This method attempts to do everything 102 * it can without throwing an exception. 100 * Closes the Router connection created by createSession(). Due to a bug in Ice, 101 * only one connection is allowed per communicator, so we also destroy the 102 * communicator. 103 103 */ 104 104 void closeSession(); 105 105 106 106 /* 107 * If called, then an existing session will be closed during 108 * destruction. 107 * Environment methods. Allows to store and retrieve 109 108 */ 110 void closeOnDestroy() { 111 close_on_destroy = true; 112 } 109 omero::RTypePtr getInput(const std::string& key); 110 omero::RTypePtr getOutput(const std::string& key); 111 void setInput(const std::string& key, omero::RTypePtr value); 112 void setOutput(const std::string& key, omero::RTypePtr value); 113 std::vector<std::string> getInputKeys(); 114 std::vector<std::string> getOutputKeys(); 115 protected: 116 const std::string sess(); 117 omero::api::ISessionPrx env(); 113 118 114 119 }; -
trunk/components/tools/OmeroCpp/test/boost_fixture.cpp
r2265 r2598 102 102 omero::client* client = new omero::client(argc, argv); 103 103 client->createSession(username, password); 104 client->closeOnDestroy();104 // client->getSession()->closeOnDestroy(); Default. 105 105 clients.push_back(client); 106 106 return client;
