Solved

Setting system properties from within an applet

Posted on 1998-11-15
17
242 Views
Last Modified: 2013-12-29
I need to set system properties from within an (unsigned) applet.
In an application, I use System.getProperties and System.setProperties. This cannot be done in an applet because of security.
The properties themselves are private, and do not pose any security risk.
0
Comment
Question by:mco
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227467
do you really need access to System Properties (esspecially System.setProperties)
you can make your own Properties object, fill it with the appropriate information and use it ...
it seems that
Properties prop = System.getProperties();
causes SecurityException but
String version = System.getProperty("java.version");
works ...

so get your properties one by one, fill your own properties object and ... that;s all ?

hope this helps
  heyhey
/*
  this applet works foe me - Win95 + Netscape 4.05
*/

import java.awt.*;
import java.util.*;
import java.applet.*;

public class Applet1 extends Applet
{
      Label label1;
      Label label2;
      public void init()
      {
            setLayout(null);
            setSize(426,266);
            label1 = new java.awt.Label("text");
            label1.setBounds(48,48,50,20);
            add(label1);
            label2 = new java.awt.Label("text");
            label2.setBounds(48,72,50,20);
            add(label2);
//          Properties prop = System.getProperties();
          String version = System.getProperty("java.version");
          label1.setText(version);
        String os = System.getProperty("os.name");
          label2.setText(os);
          
      }
}

0
 
LVL 1

Author Comment

by:mco
ID: 1227468
Using a private Properties object is not a solution, if I could use private objects
I could also use other data structures.
The point is not using the Properties class, but using the System properties.
I am transforming an existing application that receives env variables in the command
line, into an applet.
I am trying to minimize changes in the code in various packages that read these
variables.

BTW, it is customary to put alternative suggestion (which are not a direct answers) as
comments, so as not to block the question from others.

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227469
ok mco - you CAN'T use System.setProperties() because of security restrictions. if you can, it will be possible for your applet to change the System prpperties and fool some other applets, ok ?

and I don't see why you can use some static variable in your class and instead of calling
System.getProperties() and System.setProperties() you'll call
MyApplet.getProperties() and MyApplet.setProperties()
- its just one ReaplceAll on all your sources ...
 
 heyhey

P.S. i thought that you have problems with getting Properties - and so I suggeted for you to use getProperty not getProperties (on my NC the second one throws security exception).
so if your question is
  'Can I set system properties from within an unsigned applet ?'
your answer is
  'no, you can't'
(and btw
  'no you don't need to')

// just a comment

0
 
LVL 1

Author Comment

by:mco
ID: 1227470
The reason I am hoping that there is a way to do it, is that what I need to do, i.e. set some
private properties, is not a security problem at all. I am using today, in the application,
setProperties, only becaue setProperty does not exist.

Your proposed workaround is a last resort solution only. The ReplaceAll is not so simple as
it sounds. For one, we have many sources in different packages written by different people -
its a coordination and testing headache.
Secondly, and more important, we now use the -D switches in the application invokation, which
automatically set System properties. We would have to do some special argument parsing
for setting some other object.

What I am really missing is a System.setProperty() .

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227471
ok, but System.setProperty() serves some special services !!!
you may have many Properties objects, but the System has only one SYSTEM properties object - it was bad design to use for your internal purposes this object !!!
(or is it because of the -D switch :)
the System Properties object servers maynly for accessing some information about the enviroment (platform). if you are using some other (not concerning the System) informaton - you shouldn't use System object at all ... - make your own Properties object etc.

ok, -D works for application but how will you pass the same properties in applet ??? apletts don't have command line, so you can't use the -D switch ... so your arguments won't be 'automatically set in System properties' . So you can use the System properties obejct at all !!!

you need to have your own Properties object and you " would have to do some special argument parsing for setting this other object. "

or am I misiing something ?
  stay cool :)

// just a comment
0
 
LVL 1

Author Comment

by:mco
ID: 1227472
The situation is so:
The application is code we are hoping not to mess about with too much.
We used System properties because of the -D.

Now for the applet, we are using applet parameters and were hoping of
converting them to System properties, so that the existing application code
would not need modification.

What do you mean System.setProperty() serves some special purpose? It
just does not exist (in 1.1.6) .

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227473
ok, but System.setProperty() serves some special services !!!
you may have many Properties objects, but the System has only one SYSTEM properties object - it was bad design to use for your internal purposes this object !!!
(or is it because of the -D switch :)
the System Properties object servers maynly for accessing some information about the enviroment (platform). if you are using some other (not concerning the System) informaton - you shouldn't use System object at all ... - make your own Properties object etc.

ok, -D works for application but how will you pass the same properties in applet ??? apletts don't have command line, so you can't use the -D switch ... so your arguments won't be 'automatically set in System properties' . So you can use the System properties obejct at all !!!

you need to have your own Properties object and you " would have to do some special argument parsing for setting this other object. "

or am I misiing something ?
  stay cool :)

// just a comment
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227474
you have this class hierarchy
java.utils.Properties extends java.util.Hashtable extends java.util.Dictionary extends java.lang.Object

and in the System class you have one object of type Properties which is initialized at strartup time with tha appripriate information about the enciroment

class System {
    private static Properties props;
    private static native Properties initProperties(Properties props);

    private static void initializeSystemClass() {
      props = new Properties();
      initProperties(props);
      FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
      FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
      FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
      setIn0(new BufferedInputStream(fdIn));
      setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
      setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
    }

so
  System.getProperties() returns this props object.
  System.setProperties() will change this props object.
  System.getPropertiy(String key) returns just one element from the props object.

it's not a magic - you have all the sistem properties gathered in one Onject (Properties - a kind of Hashtable) so you can easy access them

Key      Description of Associated Value
java.version      Java version number
java.vendor      Java vendor-specific string
java.vendor.url      Java vendor URL
java.home        Java installation directory

you have some security restriction for accessing them from applet and (of course) for changing them in applet.
but you don't need the Security.props object at all (which is private and you can get it with System.getProperties) at all (and nobody will give this object to unsigned applet, because once you have this Object, you can put whatever you want inside it)

you need just one simple Properties object - not the System one, ok ?
I just suggested that you can make this object a static member of some of your class so that all your objects can access it.

and again - no you can't change the System.properties object at all ...

hope this helps
  heyhey

// just a comment
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:mco
ID: 1227475
You say: " no you can't change the System.properties object at all ... "

Please wait for a couple of days, if I don't get some trick to do it.
Post this sentence again as an answer and you shall be rewarded.

You see, the problem with negative answers, is you have to be sure it is right.
A positive answer can be checked.

Thank you

0
 
LVL 6

Expert Comment

by:gadio
ID: 1227476
mco, just a side comment: Regardless to the question of if its possible or not, the use of system variables all over the code is a design problem. Whenever there is a design problem, sooner or later someone pays for it, and it seems that this time its your turn (assuming that there is not way of doing what you want). I suggest that you make the effort of correcting it asap. Its difficult I'm sure, but not as it will be within a year or two. 8-}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227477
ok, i understand :)

I think that you are not allowed to change it because of security restrictions - this object contains the properties of the System. Maybe you'll find more information in the Applet Security documentation ... i don't know. but i still think that using some other Static class with appropriate methods (like OurStatic.GetProperty and OurStatic.GetPropertis) will be almost painless ...
 
hope this helps ...
(sorry, but i got the real problem too lately - your '-G' options fills the System properties and you wanted to do exactly the same thing with applet - the problem was how to fill the Applet  parameters into a System.properties object - and again "no you can't change the System.properties object at all ... form an undigned Applet" ...
and if you can - it should be a Bug :)

ok that's the answer
0
 
LVL 1

Author Comment

by:mco
ID: 1227478
heyhey_,
My punctuation mixed you up.
What I mean is wait for a couple of days (2 days) and THEN post your answer.
With the answer posted right now, the question is locked and no one else can reply.

On Tuesday, just repost this answer
Sorry

0
 
LVL 4

Expert Comment

by:evijay
ID: 1227479
Applets cannot write system properties. Also they cannot read many system properties. For more info see this javasoft tutorial excerpt at http://java.sun.com/docs/books/tutorial/essential/system/properties.html

Security consideration: Applets can access some, but not all system properties. For a
          complete list of the system properties that can and cannot be used by applets, refer to
          Getting System Properties. Applets cannot write system properties.
0
 
LVL 4

Expert Comment

by:evijay
ID: 1227480
If you want applet wide properties the best thing is to declare a global static Properties object in a helper class say Util and populate it. The method of reading parameters sent by the <Param ..> tag of html is thru Applet.getParameterInfo(). in the applet init method, read this info and copy them to the Util.sysProps object.

public class Util {
    public static Properties sysProps = new Properties();
}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227481
evijay:

The question was:

>> I need to set system properties from within an (unsigned) applet.

and fi you FIRST read all the discussion you will see those lines

>>      You say: " no you can't change the System.properties object at all ... "
>>      Please wait for a couple of days, if I don't get some trick to do it.
>>      Post this sentence again as an answer and you shall be rewarded.

so the problem is HOW TO FILL THE SYSTEM PROPERTIES OBJECT (so that mco won't need to change any code) and this problem was COMMENTED and ANSWERED quite a few times !!!

so please READ before ANSWERING.
(Everybody can make design suggestions that already have been made)
>>      and I don't see why you can use some static variable in your class and instead of calling
>>      System.getProperties() and System.setProperties() you'll call
>>      MyApplet.getProperties() and MyApplet.setProperties()
>>      - its just one ReplaceAll on all your sources ...
       
ok ?


0
 
LVL 1

Author Comment

by:mco
ID: 1227482
evijay: heyhey's last comment says it all - you should read the history of the question before
 replying.

heyhey: you can post the answer NOW and get your A grade.
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 130 total points
ID: 1227483
ok so the problem is

>> I need to set system properties from within an (unsigned) applet.

and the answer is - no, because of security restrictions you are not allowed to access  the system properties object (from unsigned applet). you are allowed to see (only) some of the system properties using
System.getProperty(String)
method

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now