6

I would like to know when a System property is changed. I have an application, in an application server, that somehow is changing a system property (System.setProperty() I think). I was taking a look and I have found different approaches:

  1. JPDA?
  2. Observer & Observable?
  3. Property change listener?
  4. JMX?

Any suggestions? Thanks in advance.

Community
  • 1
  • 1
Gaucho
  • 899
  • 1
  • 12
  • 25
  • You mentioned JMX. Are you trying to detect the property from outside of your application? – Gray May 22 '12 at 21:33
  • 1
    You can try running with a security manager/policy that prohibits changes and see where the 'splosion happens. – bmargulies May 22 '12 at 21:38
  • 1
    Why do you need to know whether the System property is changed? If you don't need a synchronous notification, why not just spawn a scheduled thread to check periodically? (The simplest thing that could work?) – Sam Goldberg May 22 '12 at 21:46
  • Thank you very much guys!Ok, I do not mind, the best would be to detect it from "outside" the app, but I think that I am allowed to add any hook (filter, listener...) to it. Yes, you are right, was my first attempt, but the configuration of the java policies is a little bit tough... I need it because it is changing the XML parser and it is screwing up my server. Yes I am also in run a thread for checking it, but what I would like the most is to find out who is changing it. – Gaucho May 23 '12 at 10:11

2 Answers2

9

You can replace the system Properties with your own custom subclass.

MyProperties newProps = new MyProperties(System.getProperties());
System.setProperties(newProps);

Then, just write a subclass of Properties which hooks the relevant methods.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Thank you jtahlborn! Yes I could, are you thinking in some kind of decorator (like the HttpServletRequestWrapper for the HttpServletRequest)? The thing is that the application should invoke my implementation... – Gaucho May 23 '12 at 10:15
  • 1
    @Gaucho - it doesn't really need to be decorator. a simple subclass which overrides the methods you care about is all you need. you just need to copy the existing system properties into your impl. – jtahlborn May 23 '12 at 12:40
  • mmm, interesting, and how I can make that the application invokes my implementation instead the default one? Thanks! – Gaucho Jun 06 '12 at 15:58
  • jtahlborn, you rock! That's right I have just implemented a simple class that extends java.util.Properties and overrides the setProperty(String, String). If the application tries to set a "forbidden" property I throws a RunTimeException. Thank you very much! – Gaucho Jun 07 '12 at 07:43
1

If you primarily want to find the issue, I would look at using the SecurityManager for this. It has a couple methods available checkPropertiesAccess() and checkPropertyAccess() which look like they will help you to find the issue, but not necessarily be able to 'notify you when a system property has changed.'

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • Thank you Nicholas! Yes, that was my first attempt: try {securityManager.checkPermission(new PropertyPermission("javax.xml.parsers.DocumentBuilderFactory","write"));} catch (SecurityException securityException) {.../... But you need to create a security manager first. See http://stackoverflow.com/questions/10624468/deny-jvm-system-property-write – Gaucho May 23 '12 at 10:20