Jan Stary wrote:

> On Dec 28 17:01:41, Patrick Hess wrote:
> :Berin Loritsch wrote:
> :
> :> Are you calling the lookup like this:
> :> manager.lookup( DataSourceComponent.ROLE + "Selector" ); //?
> :
> :No - I didn't! :-) Thank you very much - I got the example working!
> 
> Hi,
> 
> the very same code doesn't work for me:
> 
> <my-system>
>   <datasources>
>   <jdbc name="documents">
>        <pool-controller min="3" max="10"/>
>        <!--<auto-commit>false</auto-commit>-->
>        <driver>org.gjt.mm.mysql.Driver</driver>
>        <dburl>jdbc:mysql://localhost/casopis</dburl>
>        <user>dbuzivatel</user>
>        <password>heslo</password>
>   </jdbc>
>   </datasources>
>   <repository>
>       <dbpool>documents</dbpool>
>   </repository>
> </my-system>
> 
> 
> and when compiling
> 
> public class Hans implements Component, Composable, Disposable {
>       ComponentManager manager;
>       JdbcDataSource datasrc;
> 
>     public void compose(ComponentManager manager) {
>               if(this.manager == null) {
>                       this.manager = manager;
>                       datasrc = this.manager.lookup(
>                               DataSourceComponent.ROLE + "Selector");
>               }
>       }
> }
> 
> Am I missing something?



Elementry, my dear Watson.

The interface for the ComponentManager/Selector states that it returns a Component.
You must cast the Component as the type you require.

Please note, with what you have defined up there, the correct approach is:

public class Hans implements Component, Composable, Disposable {
     ComponentManager manager;
     DataSourceComponent datasrc;

     public void compose(ComponentManager manager) {
         if (this.manager == null) {
             this.manager = manager;
             ComponentSelector selector = (ComponentSelector) this.manager.lookup(
                                           DataSourceComponent.ROLE + "Selector");

             datasrc = (DataSourceComponent) selector.select("documents");
         }
     }
}

The mistakes you had in your code follow:

1) Only store a reference to the interface of a Component.  Instead of the
    JdbcDataSource, you want to use DataSourceComponent.  That frees you
    to change the configuration to use InformixDataSource or J2eeDataSource
    later.

2) You must explicitly cast your retrieved Component to the work interface
    (remember number 1--never the implementation).

3) With the particular Role hierarchy you chose, You can have multiple
    DataSourceComponents in your system--therefore you must use the
    ComponentSelector to retrieve it.

-- 

"They that give up essential liberty to obtain a little temporary safety
  deserve neither liberty nor safety."
                 - Benjamin Franklin


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to