Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

need help with a solution probably with reflection or classLoader

Thread view: 
epicwinter@hotmail.com - 09 Jan 2006 21:16 GMT
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
Jakub Stuglik - 09 Jan 2006 21:53 GMT
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
epicwinter@hotmail.com - 09 Jan 2006 22:16 GMT
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?
Jakub Stuglik - 09 Jan 2006 22:54 GMT
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
epicwinter@hotmail.com - 09 Jan 2006 23:02 GMT
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.
Jakub Stuglik - 09 Jan 2006 23:21 GMT
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
epicwinter@hotmail.com - 09 Jan 2006 23:34 GMT
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.
Chris Uppal - 10 Jan 2006 10:58 GMT
> 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
steve - 17 Jan 2006 23:21 GMT
> I am working on a large application that requires access to secure
> data.  When the user or permissions are changed i would like to
[quoted text clipped - 37 lines]
>
> thx

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

based on the user access rights to the data?

steve


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.