views:

63

answers:

2

Wondering if there is a way to change the object on the heap that other objects are referencing.

What I am specifically trying to do is manage my transient configuration. What I am doing is loading "bound" configuration from JAXB or JPA. I have a manager which maintains some threads to check if those config stores change. If they do, I wish to load the configuration from the store again (creates a new instance of the config) and REPLACE the "stale" configuration instance with the new one on the heap, so any objects referencing the configuration data will get the latest.

I understand I'll likely be running into a nightmare with having to deal with the hierarchical object references - but I simply want to learn about the various potential approaches before I decide to simply document not to create local reference and always call from the config manager if you expect the latest =)

Any ideas how to do this? I'm not too familiar with AOP...but from what I know about it...I am thinking this might provide an avenue to achieve this.

Any other ideas are welcome, of course =)

Steve

+3  A: 

I don't know anything about JAXB or JPA, but here's what I'd do. Give your various objects a reference to a wrapper for the config. Then you can update the config (in a synchronized manner) without needing to change those references:

interface Config { String getSomeProperty(); }

class ConcreteConfig implements Config{
    public String getSomeProperty() {
        return "some value";
    }
}

class ConfigWrapper implements Config {
    private Config backing;
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    public void setBacking(ConfigBacking backing) {
        try {
            lock.writeLock().lock();
            this.backing = backing;
        } finally {
            lock.writeLock().unlock();
        }
    }

    @Override
    public String getSomeProperty() {
        try {
            lock.readLock().lock();
            return backing.getSomeProperty();
        } finally {
            lock.readLock().unlock();
        }
    }
}

Then you would only distribute the instance of ConfigWrapper, and can freely reassign the backing object whenever you want.

Mark Peters
+1 Mark beat me to the punch but this is exactly how I would go about it also. AOP would be overkill compared with a simple wrapper object like in this example.
rancidfishbreath
Thanks Mark. I thought of this, but the issue comes to the JAXB/JPA bound objects. These will create a hierarchical object structure, supporting both inheritance and aggregation. While "clearing" a collection and than re-adding the items abstractly isn't too hard...I wanted to find a more elegant solution. I also considered the locking to handle the schronization, but I had concern about those configurations that may be related, but not within the same object, let alone the same getter/setter. It almost would need a transaction in this case.
Steve Siebert
A: 

You can check out the Spring framework. If my memory serves, it supports something like that (maybe this?).

Little Bobby Tables
@LBT First - awesome name =) Second, I have not. I'm going to use this manager as an OSGi service/bundle and use it in both standard SE and EJB containers. Actually, this will eventually be part of a service of my own "distributed application" container - I'll take a look a Spring, and if they did it, see how they did it....but I certainly don't want to depend on Spring. Thanks for the tip!!
Steve Siebert