need help with a solution probably with reflection or classLoader

E

epicwinter

I am working on a large application that requires access to secure
data. When the user or permissions are changed i would like to
automatically update all these classes without having to reload them
manually. The approach I am trying to take is by having them all
implement an interface:

public interface SecurityIF
{
public void securityChanged();
}

In this manner they can individual handle the security updates they
need to independantly of eachother . My problem is how to invoke this.
Originally I was only implementing this interface within swing classes
and so I was able to extract all the swing components within my frame
recursively and if then check if they implemented this method and
execute it if so:

public updateSecurity()
{
Component[] compenents = extractAllComponents();
for (int i = 0; i < compenents i++)
{
Component component = (Component)
componentVector.get(i);
if (component != null & component
instanceof SecurityIF
{
SecurityIF
component).securityChanged();();
}
}
}

Now I would like to expand this outside my swing classes to services
and such.

Is there a way to do get all the classes loaded that implement this
interface and then execute the securityChanged() method on them?

thx
 
J

Jakub Stuglik

Hi.

I think you could handle this by implementing Observer design pattern - that
is register all your components that need to be updated after
permissions/user change on some property change in the class aware of the
change. You can also use your own implementation - it could be your
interface SecurityIF as an observer and some methods like
addSecurityListener, removeSecurityListener and fireSecurityChange in the
class aware of change.
If there is a lot of clasees observing this change, or operations done by
them are time-expensive I recommend not to call
fireSecurityChange/firePropertyChange in the EventDispatcherThread, but make
another one for this purpose and show the user aprioprate dialog or sth.

Kuba
 
E

epicwinter

Good suggestion, that would work but it means i have to pass around
this "observer" and I would rather avoid that. But this certainly is
an option. Do you think it is possible to do this using reflection?
 
J

Jakub Stuglik

I understand that you want to reconfigure existing instaces of some classes,
not to reload them (make a new instances). I honestly say that I don't know
if you can get to all instances of classes implementing your interface in
the JVM instance (or all instaces of classes at all) and if it's possible at
all. Moreover, the observer pattern is a common solution to such a design
problems and it's a good solution here I think, unless there are some other
aspects of the situation you want to resolve that make it not good.
As far as I know the reflection in java allows you to get meta-data of
classes, instantiate them, call members, etc.
At last I think that if it was something allowing you getting instaces of
classes residing in JVM it would not be the gratest solution to check if it
implements your interface and call some method on it because it is really a
lot of instaces in even very simple program. And one more think - using a
lot of reflection isn't probably a good idea to, beacause it seems to be
slower than normal uses of classes and more errornous too.

I would be happy to know if it is some mechanism to get all instances in the
JVM instance, so if you get to something please let us know.

Greets,
Kuba
 
E

epicwinter

Some good points. I am in the same boat in that i just don't know if
such a solution exists but it sure would make it easy.

The only thing I don't like about the observer pattern is I would like
my classes to be more detached and not be required to know so much
about the underlying framework. To use the observer pattern you have
to couple your subject classes to the observer and it is just kind of a
hassle.
 
J

Jakub Stuglik

But in the observer pattern the class that fires events doesn't know who is
registered (he doesn't care and he doesn't care what will the registered
classes do about the event - just notificates them) - only the classes that
are interested in obtaining notifications have to know where to register.
If you want it to be inverse maybe you should think about creating some
mechanism in your application that could idenify instaces (some kind of tree
using uniquely created codes - i.e created for a path in tree for a classes
implementing some interface or something like this) and a manager that could
do search in the tree for instances implementing some interface that you're
interested in. It's just a loose idea - there are probably milions of such
solutions.
Maybe you could also use some of the ideas of IoC (Inversion of Control) or
AOP (Aspect Oriented Programming) paradigms.

Sorry I couldn't help you much:)
Good luck.

Kuba
 
E

epicwinter

Thanks for all your input. I am actually using an IOC contianer but i
can't really think of an effective way to utiliize it without coupling
it to tightly with my code.
 
C

Chris Uppal

In this manner they can individual handle the security updates they
need to independantly of eachother . My problem is how to invoke this.
[...]
Is there a way to do get all the classes loaded that implement this
interface and then execute the securityChanged() method on them?

Not easily. You /could/ use custom classloaders to ensure that you had a
handle on every loaded class which implemented the interface, but that would
not give you a list of the /objects/ which were instances of those classes --
and it's that list you would need for these purposes.

I suggest another way of doing it would be to control the way that objects
/find/ the security information in the first place. For instance, if the only
way that any object could discover the "current" security information was by
registering as an Observer of security-info changes. I think you are
over-estimating the coupling introduced by the Observer patten.

-- chris
 
S

steve

I am working on a large application that requires access to secure
data. When the user or permissions are changed i would like to
automatically update all these classes without having to reload them
manually. The approach I am trying to take is by having them all
implement an interface:

public interface SecurityIF
{
public void securityChanged();
}

In this manner they can individual handle the security updates they
need to independantly of eachother . My problem is how to invoke this.
Originally I was only implementing this interface within swing classes
and so I was able to extract all the swing components within my frame
recursively and if then check if they implemented this method and
execute it if so:

public updateSecurity()
{
Component[] compenents = extractAllComponents();
for (int i = 0; i < compenents i++)
{
Component component = (Component)
componentVector.get(i);
if (component != null & component
instanceof SecurityIF
{
SecurityIF
component).securityChanged();();
}
}
}

Now I would like to expand this outside my swing classes to services
and such.

Is there a way to do get all the classes loaded that implement this
interface and then execute the securityChanged() method on them?

thx

you mean like locking down buttons & menus , scroll areas for different
users?

based on the user access rights to the data?

steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top