src/share/classes/sun/awt/DebugSettings.java

Print this page




  85         "awtdebug.on=true",
  86         "awtdebug.ctrace=false"
  87     };
  88 
  89     /* global instance of the settings object */
  90     private static DebugSettings        instance = null;
  91 
  92     private Properties  props = new Properties();
  93 
  94     static void init() {
  95         if (instance != null) {
  96             return;
  97         }
  98 
  99         NativeLibLoader.loadLibraries();
 100         instance = new DebugSettings();
 101         instance.loadNativeSettings();
 102     }
 103 
 104     private DebugSettings() {
 105         new java.security.PrivilegedAction() {
 106             public Object run() {

 107                 loadProperties();
 108                 return null;
 109             }
 110         }.run();
 111     }
 112 
 113     /*
 114      * Load debug properties from file, then override
 115      * with any command line specified properties
 116      */
 117     private synchronized void loadProperties() {
 118         // setup initial properties
 119         java.security.AccessController.doPrivileged(
 120             new java.security.PrivilegedAction()
 121         {
 122             public Object run() {
 123                 loadDefaultProperties();
 124                 loadFileProperties();
 125                 loadSystemProperties();
 126                 return null;
 127             }
 128         });
 129 
 130         // echo the initial property settings to stdout
 131         if (log.isLoggable(PlatformLogger.FINE)) {
 132             log.fine("DebugSettings:\n{0}" + this);
 133         }
 134     }
 135 
 136     public String toString() {
 137         Enumeration enum_ = props.propertyNames();
 138         ByteArrayOutputStream bout = new ByteArrayOutputStream();
 139         PrintStream pout = new PrintStream(bout);
 140 
 141         while (enum_.hasMoreElements()) {
 142             String key = (String)enum_.nextElement();
 143             String value = props.getProperty(key, "");
 144             pout.println(key + " = " + value);
 145         }
 146         return new String(bout.toByteArray());
 147     }
 148 
 149     /*
 150      * Sets up default property values
 151      */
 152     private void loadDefaultProperties() {
 153         // is there a more inefficient way to setup default properties?
 154         // maybe, but this has got to be close to 100% non-optimal
 155         try {
 156             for ( int nprop = 0; nprop < DEFAULT_PROPS.length; nprop++ ) {
 157                 StringBufferInputStream in = new StringBufferInputStream(DEFAULT_PROPS[nprop]);
 158                 props.load(in);
 159                 in.close();
 160             }
 161         } catch(IOException ioe) {
 162         }


 181         File    propFile = new File(propPath);
 182         try {
 183             println("Reading debug settings from '" + propFile.getCanonicalPath() + "'...");
 184             FileInputStream     fin = new FileInputStream(propFile);
 185             props.load(fin);
 186             fin.close();
 187         } catch ( FileNotFoundException fne ) {
 188             println("Did not find settings file.");
 189         } catch ( IOException ioe ) {
 190             println("Problem reading settings, IOException: " + ioe.getMessage());
 191         }
 192     }
 193 
 194     /*
 195      * load properties from system props (command line spec'd usually),
 196      * overriding default or file properties
 197      */
 198     private void loadSystemProperties() {
 199         // override file properties with system properties
 200         Properties sysProps = System.getProperties();
 201         Enumeration enum_ = sysProps.propertyNames();
 202         while ( enum_.hasMoreElements() ) {
 203             String key = (String)enum_.nextElement();
 204             String value = sysProps.getProperty(key,"");
 205             // copy any "awtdebug" properties over
 206             if ( key.startsWith(PREFIX) ) {
 207                 props.setProperty(key, value);
 208             }
 209         }
 210     }
 211 
 212     /**
 213      * Gets named boolean property
 214      * @param key       Name of property
 215      * @param defval    Default value if property does not exist
 216      * @return boolean value of the named property
 217      */
 218     public synchronized boolean getBoolean(String key, boolean defval) {
 219         String  value = getString(key, String.valueOf(defval));
 220         return value.equalsIgnoreCase("true");
 221     }
 222 
 223     /**


 227      * @return integer value of the named property
 228      */
 229     public synchronized int getInt(String key, int defval) {
 230         String  value = getString(key, String.valueOf(defval));
 231         return Integer.parseInt(value);
 232     }
 233 
 234     /**
 235      * Gets named String property
 236      * @param key       Name of property
 237      * @param defval    Default value if property does not exist
 238      * @return string value of the named property
 239      */
 240     public synchronized String getString(String key, String defval) {
 241         String  actualKeyName = PREFIX + "." + key;
 242         String  value = props.getProperty(actualKeyName, defval);
 243         //println(actualKeyName+"="+value);
 244         return value;
 245     }
 246 
 247     public synchronized Enumeration getPropertyNames() {
 248         Vector          propNames = new Vector();
 249         Enumeration     enum_ = props.propertyNames();
 250 
 251         // remove global prefix from property names
 252         while ( enum_.hasMoreElements() ) {
 253             String propName = (String)enum_.nextElement();
 254             propName = propName.substring(PREFIX.length()+1);
 255             propNames.addElement(propName);
 256         }
 257         return propNames.elements();
 258     }
 259 
 260     private void println(Object object) {
 261         if (log.isLoggable(PlatformLogger.FINER)) {
 262             log.finer(object.toString());
 263         }
 264     }
 265 
 266     private static final String PROP_CTRACE = "ctrace";
 267     private static final int PROP_CTRACE_LEN = PROP_CTRACE.length();
 268 
 269     private native synchronized void setCTracingOn(boolean enabled);
 270     private native synchronized void setCTracingOn(boolean enabled, String file);
 271     private native synchronized void setCTracingOn(boolean enabled, String file, int line);
 272 
 273     private void loadNativeSettings() {
 274         boolean        ctracingOn;
 275 
 276         ctracingOn = getBoolean(PROP_CTRACE, false);
 277         setCTracingOn(ctracingOn);
 278 
 279         //
 280         // Filter out file/line ctrace properties from debug settings
 281         //
 282         Vector                traces = new Vector();
 283         Enumeration         enum_ = getPropertyNames();
 284 
 285         while ( enum_.hasMoreElements() ) {
 286             String key = (String)enum_.nextElement();
 287             if ( key.startsWith(PROP_CTRACE) && key.length() > PROP_CTRACE_LEN ) {
 288                 traces.addElement(key);
 289             }
 290         }
 291 
 292         // sort traces list so file-level traces will be before line-level ones
 293         Collections.sort(traces);
 294 
 295         //
 296         // Setup the trace points
 297         //
 298         Enumeration        enumTraces = traces.elements();
 299 
 300         while ( enumTraces.hasMoreElements() ) {
 301             String        key = (String)enumTraces.nextElement();
 302             String         trace = key.substring(PROP_CTRACE_LEN+1);
 303             String        filespec;
 304             String        linespec;
 305             int                delim= trace.indexOf('@');
 306             boolean        enabled;
 307 
 308             // parse out the filename and linenumber from the property name
 309             filespec = delim != -1 ? trace.substring(0, delim) : trace;
 310             linespec = delim != -1 ? trace.substring(delim+1) : "";
 311             enabled = getBoolean(key, false);
 312             //System.out.println("Key="+key+", File="+filespec+", Line="+linespec+", Enabled="+enabled);
 313 
 314             if ( linespec.length() == 0 ) {
 315             // set file specific trace setting
 316                     setCTracingOn(enabled, filespec);
 317             } else {
 318             // set line specific trace setting
 319                 int        linenum = Integer.parseInt(linespec, 10);
 320                 setCTracingOn(enabled, filespec, linenum);
 321             }


  85         "awtdebug.on=true",
  86         "awtdebug.ctrace=false"
  87     };
  88 
  89     /* global instance of the settings object */
  90     private static DebugSettings instance = null;
  91 
  92     private Properties props = new Properties();
  93 
  94     static void init() {
  95         if (instance != null) {
  96             return;
  97         }
  98 
  99         NativeLibLoader.loadLibraries();
 100         instance = new DebugSettings();
 101         instance.loadNativeSettings();
 102     }
 103 
 104     private DebugSettings() {
 105         java.security.AccessController.doPrivileged(
 106             new java.security.PrivilegedAction<Void>() {
 107                 public Void run() {
 108                     loadProperties();
 109                     return null;
 110                 }
 111             });
 112     }
 113 
 114     /*
 115      * Load debug properties from file, then override
 116      * with any command line specified properties
 117      */
 118     private synchronized void loadProperties() {
 119         // setup initial properties
 120         java.security.AccessController.doPrivileged(
 121             new java.security.PrivilegedAction<Void>() {
 122                 public Void run() {

 123                     loadDefaultProperties();
 124                     loadFileProperties();
 125                     loadSystemProperties();
 126                     return null;
 127                 }
 128             });
 129 
 130         // echo the initial property settings to stdout
 131         if (log.isLoggable(PlatformLogger.FINE)) {
 132             log.fine("DebugSettings:\n{0}" + this);
 133         }
 134     }
 135 
 136     public String toString() {

 137         ByteArrayOutputStream bout = new ByteArrayOutputStream();
 138         PrintStream pout = new PrintStream(bout);
 139         for (String key : props.stringPropertyNames()) {


 140             String value = props.getProperty(key, "");
 141             pout.println(key + " = " + value);
 142         }
 143         return new String(bout.toByteArray());
 144     }
 145 
 146     /*
 147      * Sets up default property values
 148      */
 149     private void loadDefaultProperties() {
 150         // is there a more inefficient way to setup default properties?
 151         // maybe, but this has got to be close to 100% non-optimal
 152         try {
 153             for ( int nprop = 0; nprop < DEFAULT_PROPS.length; nprop++ ) {
 154                 StringBufferInputStream in = new StringBufferInputStream(DEFAULT_PROPS[nprop]);
 155                 props.load(in);
 156                 in.close();
 157             }
 158         } catch(IOException ioe) {
 159         }


 178         File    propFile = new File(propPath);
 179         try {
 180             println("Reading debug settings from '" + propFile.getCanonicalPath() + "'...");
 181             FileInputStream     fin = new FileInputStream(propFile);
 182             props.load(fin);
 183             fin.close();
 184         } catch ( FileNotFoundException fne ) {
 185             println("Did not find settings file.");
 186         } catch ( IOException ioe ) {
 187             println("Problem reading settings, IOException: " + ioe.getMessage());
 188         }
 189     }
 190 
 191     /*
 192      * load properties from system props (command line spec'd usually),
 193      * overriding default or file properties
 194      */
 195     private void loadSystemProperties() {
 196         // override file properties with system properties
 197         Properties sysProps = System.getProperties();
 198         for (String key : sysProps.stringPropertyNames()) {


 199             String value = sysProps.getProperty(key,"");
 200             // copy any "awtdebug" properties over
 201             if ( key.startsWith(PREFIX) ) {
 202                 props.setProperty(key, value);
 203             }
 204         }
 205     }
 206 
 207     /**
 208      * Gets named boolean property
 209      * @param key       Name of property
 210      * @param defval    Default value if property does not exist
 211      * @return boolean value of the named property
 212      */
 213     public synchronized boolean getBoolean(String key, boolean defval) {
 214         String  value = getString(key, String.valueOf(defval));
 215         return value.equalsIgnoreCase("true");
 216     }
 217 
 218     /**


 222      * @return integer value of the named property
 223      */
 224     public synchronized int getInt(String key, int defval) {
 225         String  value = getString(key, String.valueOf(defval));
 226         return Integer.parseInt(value);
 227     }
 228 
 229     /**
 230      * Gets named String property
 231      * @param key       Name of property
 232      * @param defval    Default value if property does not exist
 233      * @return string value of the named property
 234      */
 235     public synchronized String getString(String key, String defval) {
 236         String  actualKeyName = PREFIX + "." + key;
 237         String  value = props.getProperty(actualKeyName, defval);
 238         //println(actualKeyName+"="+value);
 239         return value;
 240     }
 241 
 242     private synchronized List<String> getPropertyNames() {
 243         List<String> propNames = new LinkedList<>();


 244         // remove global prefix from property names
 245         for (String propName : props.stringPropertyNames()) {

 246             propName = propName.substring(PREFIX.length()+1);
 247             propNames.add(propName);
 248         }
 249         return propNames;
 250     }
 251 
 252     private void println(Object object) {
 253         if (log.isLoggable(PlatformLogger.FINER)) {
 254             log.finer(object.toString());
 255         }
 256     }
 257 
 258     private static final String PROP_CTRACE = "ctrace";
 259     private static final int PROP_CTRACE_LEN = PROP_CTRACE.length();
 260 
 261     private native synchronized void setCTracingOn(boolean enabled);
 262     private native synchronized void setCTracingOn(boolean enabled, String file);
 263     private native synchronized void setCTracingOn(boolean enabled, String file, int line);
 264 
 265     private void loadNativeSettings() {
 266         boolean        ctracingOn;
 267 
 268         ctracingOn = getBoolean(PROP_CTRACE, false);
 269         setCTracingOn(ctracingOn);
 270 
 271         //
 272         // Filter out file/line ctrace properties from debug settings
 273         //
 274         List<String> traces = new LinkedList<>();

 275 
 276         for (String key : getPropertyNames()) {
 277             if (key.startsWith(PROP_CTRACE) && key.length() > PROP_CTRACE_LEN) {
 278                 traces.add(key);

 279             }
 280         }
 281 
 282         // sort traces list so file-level traces will be before line-level ones
 283         Collections.sort(traces);
 284 
 285         //
 286         // Setup the trace points
 287         //
 288         for (String key : traces) {



 289             String        trace = key.substring(PROP_CTRACE_LEN+1);
 290             String        filespec;
 291             String        linespec;
 292             int           delim= trace.indexOf('@');
 293             boolean       enabled;
 294 
 295             // parse out the filename and linenumber from the property name
 296             filespec = delim != -1 ? trace.substring(0, delim) : trace;
 297             linespec = delim != -1 ? trace.substring(delim+1) : "";
 298             enabled = getBoolean(key, false);
 299             //System.out.println("Key="+key+", File="+filespec+", Line="+linespec+", Enabled="+enabled);
 300 
 301             if ( linespec.length() == 0 ) {
 302             // set file specific trace setting
 303                     setCTracingOn(enabled, filespec);
 304             } else {
 305             // set line specific trace setting
 306                 int        linenum = Integer.parseInt(linespec, 10);
 307                 setCTracingOn(enabled, filespec, linenum);
 308             }