• Views
  • Iteration Report
  • My Iteration Report
  •  
OMERO.server
  • Login
  • Help/Guide
  • About Trac
  • Preferences
  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • View Tickets
  • Search

Context Navigation

  • ← Previous Changeset
  • Next Changeset →

Changeset 2598

Show
Ignore:
Timestamp:
07/05/08 22:16:07 (3 months ago)
Author:
jmoore
Message:

ticket:1018 - Propagating changes to OmeroJava and OmeroCpp

Also add environment methods which have been in OmeroPy for
some time.

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  
    1010import java.util.Collections; 
    1111import java.util.HashSet; 
     12import java.util.List; 
    1213import java.util.Properties; 
    1314import java.util.Set; 
    14  
     15import java.util.UUID; 
     16 
     17import omero.api.IAdminPrx; 
     18import omero.api.ISessionPrx; 
    1519import omero.api.ServiceFactoryPrx; 
    1620import omero.api.ServiceFactoryPrxHelper; 
    … …  
    5761    ServiceFactoryPrx sf; 
    5862 
    59     boolean close_on_destroy = false; 
    60  
    6163    boolean closed = false; 
    6264 
    … …  
    106108            throw new ClientError("No communicator."); 
    107109        } 
     110        // Register Object Factory 
    108111        ObjectFactoryRegistrar.registerObjectFactory(ic, 
    109112                ObjectFactoryRegistrar.INSTANCE); 
     113        // Define our unique identifer (used during close/detach) 
     114        ic.getImplicitContext().put(omero.constants.CLIENTUUID.value, 
     115                UUID.randomUUID().toString()); 
    110116        CLIENTS.add(this); 
    111117    } 
    … …  
    114120    // ========================================================================= 
    115121 
    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     */ 
    120125    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; 
    137133        } 
    138134    } 
    … …  
    143139 
    144140    public ServiceFactoryPrx getServiceFactory() { 
     141        if (sf == null) { 
     142            throw new ClientError("Call createSession() to login."); 
     143        } 
    145144        return sf; 
    146145    } 
    … …  
    161160    public ServiceFactoryPrx createSession(String username, String password) 
    162161            throws CannotCreateSessionException, PermissionDeniedException { 
     162 
     163        // Check the required properties 
    163164        if (username == null) { 
    164165            username = getProperty("omero.user"); 
    … …  
    174175        } 
    175176 
     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 
    176180        Glacier2.SessionPrx prx = getRouter().createSession(username, password); 
    177181        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())); 
    180186        sf = ServiceFactoryPrxHelper.checkedCast(prx); 
    181187        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 
    184192        return this.sf; 
    185193    } 
    … …  
    199207 
    200208    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! 
    210221        } 
    211222 
    … …  
    216227        } catch (Ice.ConnectionLostException cle) { 
    217228            // 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()); 
    235259    } 
    236260 
    … …  
    249273    } 
    250274 
     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    } 
    251297} 
  • trunk/components/blitz/test/omero/itests/UsageTest.java

    r2256 r2598  
    1010 
    1111import junit.framework.TestCase; 
     12import omero.RString; 
    1213 
    1314import org.springframework.util.ResourceUtils; 
    … …  
    1819    @Test 
    1920    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(); 
    2425    } 
    2526 
    2627    @Test 
    2728    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(); 
    3233        client.close(); 
    3334    } 
     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 
    3451} 
  • trunk/components/tools/OmeroCpp/src/omero/client.cpp

    r2256 r2598  
    1717      throw omero::ClientError(__FILE__,__LINE__,"Improper initialization."); 
    1818    } 
     19    // Register Object Factory 
    1920    ObjectFactoryPtr of = new ObjectFactory(); 
    2021    of->registerObjectFactory(ic); 
     22    // Define our unique identifier (used during close/detach) 
     23    ic->getImplicitContext()->put(omero::constants::CLIENTUUID, IceUtil::generateUUID()); 
    2124  } 
    2225 
    2326  client::client(const Ice::InitializationData& id) { 
    2427    ic = Ice::initialize(id); 
    25     close_on_destroy = false; 
    2628    init(ic); 
    2729  } 
    … …  
    3032                 const Ice::InitializationData& id) { 
    3133    ic = Ice::initialize(argc, argv, id); 
    32     close_on_destroy = false; 
    3334    init(ic); 
    3435  } 
    3536 
    3637  client::~client(){ 
    37       if (close_on_destroy && sf) { 
     38      try { 
    3839          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; 
    4842      } 
    4943  } 
    … …  
    5145  omero::api::ServiceFactoryPrx client::createSession(const std::string& _username, const std::string& _password) { 
    5246 
     47      // Check the required properties 
    5348    std::string username, password; 
    5449    if (_username.empty()) { 
    … …  
    7065    } 
    7166 
     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. 
    7270    Ice::RouterPrx prx = ic->getDefaultRouter(); 
    7371    if (!prx) { 
    7472        throw omero::ClientError(__FILE__,__LINE__,"No default router found."); 
    7573    } 
     74    prx = Ice::RouterPrx::uncheckedCast(prx->ice_context(ic->getImplicitContext()->getContext())); 
    7675    Glacier2::RouterPrx router = Glacier2::RouterPrx::checkedCast(prx); 
    7776    if (!router) { 
    … …  
    8180    Glacier2::SessionPrx session; 
    8281    session = router->createSession(username, password); 
     82    if (!session) { 
     83        throw omero::ClientError(__FILE__,__LINE__,"Obtained null object proxy"); 
     84    } 
     85 
     86    // Check type 
    8387    sf = omero::api::ServiceFactoryPrx::checkedCast(session); 
    8488    if (!sf) { 
    85       throw omero::ClientError(__FILE__,__LINE__,"No session obtained."); 
     89      throw omero::ClientError(__FILE__,__LINE__,"Obtained object proxy is not a ServiceFactory."); 
    8690    } 
    8791    return sf; 
    … …  
    8993 
    9094  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(); 
    96101      } 
    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 
    106124  } 
    107125 
     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    } 
    108152} 
    109153 
  • trunk/components/tools/OmeroCpp/src/omero/client.h

    r2256 r2598  
    3232   * omero::api::ServiceFactoryPrx which is the blitz session facade, 
    3333   * 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, 
    3535   * all proxies obtained from this instance are also destroyed. 
    3636   * 
    … …  
    4949    // These are the central instances provided by this class. 
    5050  protected: 
    51     bool close_on_destroy; 
    5251    Ice::CommunicatorPtr ic; 
    5352    omero::api::ServiceFactoryPrx sf; 
    … …  
    7675    /* 
    7776     * Destroys the communicator instance. To have the session destroyed, 
    78      * call close on the client before destruction. Otherwise, the session 
    79      * 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. 
    8079     */ 
    8180    ~client(); 
    … …  
    9998 
    10099    /* 
    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. 
    103103     */ 
    104104    void closeSession(); 
    105105 
    106106    /* 
    107      * If called, then an existing session will be closed during 
    108      * destruction. 
     107     * Environment methods. Allows to store and retrieve 
    109108     */ 
    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(); 
    113118 
    114119  }; 
  • trunk/components/tools/OmeroCpp/test/boost_fixture.cpp

    r2265 r2598  
    102102        omero::client* client = new omero::client(argc, argv); 
    103103        client->createSession(username, password); 
    104         client->closeOnDestroy(); 
     104        //  client->getSession()->closeOnDestroy(); Default. 
    105105        clients.push_back(client); 
    106106        return client; 

Download in other formats:

  • Unified Diff
  • Zip Archive

Trac Powered

Powered by Trac 0.11
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/