admin@glassfish.java.net

CODE REVIEW: thread safety: Escape.java

From: Lloyd L Chambers <Lloyd.Chambers_at_Sun.COM>
Date: Thu, 03 May 2007 10:36:58 -0700

TIMEOUT: 17:00 PST Friday May 4

https://glassfish.dev.java.net/issues/show_bug.cgi?id=2123

Who owns com.sun.enterprise.diagnostics.report.Escape anyway?

These fixes remove the thread safety issues and should not impact
existing clients because the thread-unsafe features turned out not to
be used.

Lloyd
---------------


RCS file: /cvs/glassfish/appserv-core/src/java/com/sun/enterprise/
diagnostics/report/html/Escape.java,v
retrieving revision 1.3
diff -w -u -r1.3 Escape.java
--- src/java/com/sun/enterprise/diagnostics/report/html/
Escape.java 25 Dec 2005 04:15:31 -0000 1.3
+++ src/java/com/sun/enterprise/diagnostics/report/html/
Escape.java 27 Apr 2007 00:10:21 -0000
@@ -30,23 +30,55 @@
/**
   * Implement HTML escapes. Additional escapes can be added.
   * <p>
- * This class is a singleton. If you subclass and override the
- * <code>escape</code> methods, use <code>setInstance</code> to
- * install your handler.
+ * This class is a singleton.
   */
-public class Escape {
+final class Escape {

      /** A value to signal an undefined entity. */
      public static final int UNDEFINED = -1;

      /** The instance to use. */
- private static Escape instance = null;
+ private static final Escape nonHexInstance = new Escape( false );
+ //private static final Escape hexInstance = new Escape( true );
+
+ /**
+ * Get the escape instance to use to escape strings. This version
+ * does not use hexadecimal.
+ * @return The instance to use.
+ * @see #setInstance(Escape)
+ */
+ public static final Escape getNonHexInstance() {
+ return nonHexInstance;
+ }
+
+ /**
+ @deprecated use getNonHexInstance
+ */
+ public static final Escape getInstance() {
+ return getNonHexInstance();
+ }
+
+ /**
+ APPARENTLY NOT USED
+ * Get the escape instance to use to escape strings. This version
+ * uses hex.
+ * @return The instance to use.
+ * @see #setInstance(Escape)
+ public static final Escape getHexInstance() {
+ return hexInstance;
+ }
+ */
+

      /**
       * If true, use hexadecimal character references. If false,
       * use decimal character references.
       */
- private boolean useHex = false;
+ private final boolean useHex;
+
+ private Escape( final boolean useHexIn ) {
+ useHex = useHexIn;
+ }

      /**
       * These are the entities which are always replaced on output.
Add
@@ -611,46 +643,9 @@
       * This field holds the list of non-alphanumeric characters to
       * preserve as-is in URLs.
       */
- private String preserve = "_-!.~#()*" + ",;:$&+=" + "?/[]@";
+ private static final String preserve = "_-!.~#()*" + ",;:$&+=" +
"?/[]@";


- /**
- * Make a new escape instance. This method is protected since only
- * subclasses should use it. Do not create instances of this class
- * directly; use {_at_link #getInstance()} to get the correct
- * <code>Escape</code> instance to use.
- */
- protected Escape() {
- super();
- }
-
-
- /**
- * Get the escape instance to use to escape strings.
- * @return The instance to use.
- * @see #setInstance(Escape)
- */
- public static final Escape getInstance() {
- if (instance == null) {
- instance = new Escape();
- }
- return instance;
- }
-
-
- /**
- * Set the instance to use to escape strings.
- * @param escape The instance to use.
- * @return The instance to use.
- * @see #getInstance()
- */
- public static final Escape setInstance(Escape escape) {
- if (escape == null) {
- throw new NullPointerException("Escape instance is null.");
- }
- instance = escape;
- return instance;
- }


      /**
@@ -828,21 +823,6 @@


      /**
- * Specify whether to use hexadecimal character references of the
- * form <code>&amp;#xN;</code>, where N is the hex character code.
- * The alternative is decimal character references of the form
- * <code>&amp;#N;</code>, where N is the decimal character code.
- * @param flag The setting.
- * @return This escape.
- * @see #encodeAsEntity(char)
- */
- public Escape setUseHex(boolean flag) {
- useHex = flag;
- return this;
- }
-
-
- /**
       * Add a new entity to this escape.
       * @param entity The entity name. There can be an ampersand at
       * the start and a
semicolon at the end, but these
@@ -850,7 +830,7 @@
       * @param value The value of the entity, as a single
character.
       * @return This escape.
       */
- public Escape setEntity(String entity, char value) {
+ private Escape setEntity(String entity, char value) {
          if (entity == null) {
              throw new NullPointerException("The entity name is
null.");
          }