« Return to Thread: Best way to share object instances between modules?

Re: Best way to share object instances between modules?

by Wade Chandler :: Rate this Message:

Reply to Author | View in Thread

--- Sergio Lopes <knitter.is@...> wrote:
> >  Can you attach your layer.xml file? I'll have a look at it. Do you have a folder defined?
> File is not that big, though I believe you must have better things to
> do than look at my code ;)

Sure, I have a lot to do, but I think once I show you this you'll have it down, and I won't have
to explain the whole thing to you again. You can start playing around with this stuff and help
some others when they ask ;-).

> These are the two variations I tried following the documentation
> <filesystem>
>     <folder name="CatalogEngine">
>         <file name="de-berlios-jfindmyfiles-catalog-CatalogEngine.instance">
>             <attr name="instanceClass"
> stringvalue="de.berlios.jfindmyfiles.catalog.CatalogEngine"/>
>         </file>
>     </folder>
> </filesystem>
>

This should work fine, though you might want to include some uniqueness in the name such as

<folder name="com-yourdomain-CatalogEngine">

just in case others have modules you want to use which might have a CatalogEngine of some kind
using the same name. You'd know more about your application and what you are using.

Also note that

<file name="de-berlios-jfindmyfiles-catalog-CatalogEngine.instance"/>

is all you need in this case. The instance attribute stuff is so you could just have the name be
"CatalogEngine" or "CatalogEngineImpl" and not worry about the .instance. The files with a name
ending in .instance are instantiated. The .instance on the end is a command to NB.


> or
>
> <filesystem>
>     <file name="de-berlios-jfindmyfiles-catalog-CatalogEngine.instance">
>         <attr name="instanceClass"
> stringvalue="de.berlios.jfindmyfiles.catalog.CatalogEngine"/>
>     </file>
> </filesystem>
>

Now, you can make this work, but it is going to be a big lookup including "everything" from the
system...and I mean more than the default lookup as the default is based on META-INF/services.

> This is on the layer file of the Catalog module, I'm trying to look
> for the instance on the GUI module.
>

Sure, I think now we need to look at the code where you are creating your Lookup. It should be
like this for the entry above with the folder:

Lookup lu = Lookups.forPath("/CatalogEngine");
CatalogEngine ce = lu.lookup(CatalogEngine.class);

and for the one with no folder:

Lookup lu = Lookups.forPath("/");
CatalogEngine ce = lu.lookup(CatalogEngine.class);

Just be sure and store this Lookup off some where. That way when you need to use it you can use
it without having to recreate the entire thing over and over. Then as data changes in your folder
you can have different instances etc changed in your lookup without having to create the whole
thing again.

If you want to have the idea of a merged set of CatalogEngines then you can name your folder
CatalogEngines and loop over each one looking to see if one contains what you are trying to find.
You would use Lookup.lookupResult or lookupAll for this.

Anyways, if this doesn't get you going let me know, but remember what Tom was telling you about
interfaces. With Lookup you can have a folder or tree with instances inside it and have things
implement interfaces so you can change out the implementation or as I said with CatalogEngine
have multiple ones which you can query...this would work sort of like the JDBC connection manager
or the NB editor and data loader stuff.

Wade


==================
Wade Chandler, CCE
Software Engineer and Developer, Certified Forensic Computer Examiner, NetBeans Dream Team Member, and NetBeans Board Member
http://www.certified-computer-examiner.com
http://wiki.netbeans.org/wiki/view/NetBeansDreamTeam
http://www.netbeans.org

 « Return to Thread: Best way to share object instances between modules?

LightInTheBox - Buy quality products at wholesale price