dev@javaserverfaces.java.net

[REVIEW] Per application configuration of JS compression

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Mon, 09 Oct 2006 11:59:38 -0700


SECTION: Modified Files
----------------------------
M src/com/sun/faces/renderkit/RenderKitUtils.java
  - Make sure that JS compression is indeed configurable
    on a per-application bases by storing the loaded
    result in the servlet context instead of a static
    variable.


SECTION: Diffs
----------------------------
Index: src/com/sun/faces/renderkit/RenderKitUtils.java
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/RenderKitUtils.java,v
retrieving revision 1.28
diff -u -r1.28 RenderKitUtils.java
--- src/com/sun/faces/renderkit/RenderKitUtils.java 6 Oct 2006 19:09:23 -0000 1.28
+++ src/com/sun/faces/renderkit/RenderKitUtils.java 9 Oct 2006 18:58:56 -0000
@@ -197,7 +197,7 @@
      * <p>JavaScript to be rendered when a commandLink is used.
      * This may be expaned to include other uses.</p>
      */
- private static volatile String SUN_JSF_JS = null;
+ private static String SUN_JSF_JS = RIConstants.FACES_PREFIX + "sunJsfJs";
                           
     
     protected static final Logger LOGGER =
@@ -968,7 +968,7 @@
      * @param JSString the string to compress
      * @return the compressed string
      */
- public static String compressJS(String JSString) {
+ public static char[] compressJS(String JSString) {
 
         BufferedReader reader = new BufferedReader(new StringReader(JSString));
         StringWriter writer = new StringWriter(1024);
@@ -981,7 +981,7 @@
                 line = line.trim();
                 writer.write(line);
             }
- return writer.toString();
+ return writer.toString().toCharArray();
         } catch (IOException ioe) {
             // won't happen
         }
@@ -1002,7 +1002,8 @@
     public static void writeSunJS(FacesContext context, Writer writer)
     throws IOException {
         loadSunJsfJs(context);
- writer.write(SUN_JSF_JS);
+ writer.write((char[]) context.getExternalContext().getApplicationMap()
+ .get(SUN_JSF_JS));
     }
     
     
@@ -1016,10 +1017,13 @@
      * @return the JavaScript sans comments and blank lines
      */
     private static void loadSunJsfJs(FacesContext context) {
-
- if (SUN_JSF_JS == null) {
+ Map<String,Object> appMap =
+ context.getExternalContext().getApplicationMap();
+ char[] sunJsfJs = (char[]) appMap.get(SUN_JSF_JS);
+ if (sunJsfJs == null) {
             synchronized (XHTML_ATTR_PREFIX) {
- if (SUN_JSF_JS == null) {
+ sunJsfJs = (char[]) appMap.get(SUN_JSF_JS);
+ if (sunJsfJs == null) {
                     BufferedReader reader = null;
                     try {
                         URL url = Util.getCurrentLoader(null)
@@ -1055,13 +1059,14 @@
                               .getInstance(context.getExternalContext())
                               .getBooleanContextInitParameter(
                                     BooleanWebContextInitParameter.CompressJavaScript)) {
- SUN_JSF_JS = compressJS(builder.toString());
+ sunJsfJs = compressJS(builder.toString());
                         } else {
- SUN_JSF_JS = builder.toString();
+ sunJsfJs = builder.toString().toCharArray();
                         }
+ appMap.put(SUN_JSF_JS, sunJsfJs);
                     } catch (IOException ioe) {
                         LOGGER.log(Level.SEVERE,
- "jsf.renderkit.resstatemgr.clientbuf_not_integer",
+ "jsf.renderkit.util.cannot_load_js",
                                    ioe);
                     } finally {
                         if (reader != null) {