Skip to content

Commit

Permalink
When an error is thrown, press will now output the original source js…
Browse files Browse the repository at this point in the history
…/css file instead of trying to output the compressed version (which would fail)
  • Loading branch information
dirkmc committed Oct 20, 2010
1 parent 84b9931 commit 4154f49
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 36 deletions.
45 changes: 15 additions & 30 deletions app/press/Plugin.java
Expand Up @@ -7,6 +7,7 @@
public class Plugin extends PlayPlugin {
static ThreadLocal<JSCompressor> jsCompressor = new ThreadLocal<JSCompressor>();
static ThreadLocal<CSSCompressor> cssCompressor = new ThreadLocal<CSSCompressor>();
static ThreadLocal<Boolean> errorOccurred = new ThreadLocal<Boolean>();

@Override
public void onApplicationStart() {
Expand All @@ -23,48 +24,29 @@ public void beforeActionInvocation(Method actionMethod) {
// Before each action, create a new CSS and JS Compressor
jsCompressor.set(new JSCompressor());
cssCompressor.set(new CSSCompressor());
errorOccurred.set(false);
}

/**
* Get the url for the compressed version of the given JS file, in real time
*/
public static String compressedSingleJSUrl(String fileName) {
// This can happen in some extreme error cases
if(jsCompressor.get() == null) {
return "";
}

return jsCompressor.get().compressedSingleFileUrl(fileName);
}

/**
* Get the url for the compressed version of the given CSS file, in real time
*/
public static String compressedSingleCSSUrl(String fileName) {
// This can happen in some extreme error cases
if(cssCompressor.get() == null) {
return "";
}

return cssCompressor.get().compressedSingleFileUrl(fileName);
}

public static String addJS(String fileName, boolean compress) {
// This can happen in some extreme error cases
if(jsCompressor.get() == null) {
return "";
}

// Add files to the JS compressor
return jsCompressor.get().add(fileName, compress);
}

public static String addCSS(String fileName, boolean compress) {
// This can happen in some extreme error cases
if(cssCompressor.get() == null) {
return "";
}

// Add files to the CSS compressor
return cssCompressor.get().add(fileName, compress);
}
Expand All @@ -75,11 +57,6 @@ public static String addCSS(String fileName, boolean compress) {
* compressed file.
*/
public static String compressedJSUrl() {
// This can happen in some extreme error cases
if(jsCompressor.get() == null) {
return "";
}

return jsCompressor.get().compressedUrl();
}

Expand All @@ -89,11 +66,6 @@ public static String compressedJSUrl() {
* file.
*/
public static String compressedCSSUrl() {
// This can happen in some extreme error cases
if(cssCompressor.get() == null) {
return "";
}

return cssCompressor.get().compressedUrl();
}

Expand All @@ -107,6 +79,19 @@ public void afterActionInvocation() {
}
}

@Override
public void onInvocationException(Throwable e) {
errorOccurred.set(true);
}


/**
* Indicates whether or not an error has occurred
*/
public static boolean hasErrorOccurred() {
return errorOccurred.get() == null || errorOccurred.get();
}

/**
* Indicates whether or not compression is enabled.
*/
Expand Down
2 changes: 1 addition & 1 deletion app/views/tags/press/compressed-script.tag
Expand Up @@ -12,6 +12,6 @@
* See the plugin documentation for more information.
*
}*
#{if press.Plugin.enabled() }
#{if press.Plugin.enabled() && !press.Plugin.hasErrorOccurred() }
<script src="${press.Plugin.compressedJSUrl()}" type="text/javascript" language="javascript" charset="utf-8"></script>
#{/if}
2 changes: 1 addition & 1 deletion app/views/tags/press/compressed-stylesheet.tag
Expand Up @@ -16,6 +16,6 @@
* See the plugin documentation for more information.
*
}*
#{if press.Plugin.enabled() }
#{if press.Plugin.enabled() && !press.Plugin.hasErrorOccurred() }
<link href="${press.Plugin.compressedCSSUrl()}" rel="stylesheet" type="text/css" charset="utf-8" #{if _media} media="${_media}"#{/if}></link>
#{/if}
2 changes: 1 addition & 1 deletion app/views/tags/press/script.tag
Expand Up @@ -34,7 +34,7 @@
throw new play.exceptions.TagInternalException("src attribute cannot be empty for press.script tag");
}
}%
#{if press.Plugin.enabled() }
#{if press.Plugin.enabled() && !press.Plugin.hasErrorOccurred() }
${ press.Plugin.addJS(_src, _compress) }
#{/if}
#{else}
Expand Down
7 changes: 6 additions & 1 deletion app/views/tags/press/single-script.tag
@@ -1,6 +1,8 @@
*{
* Outputs a <script> tag whose source is the compressed output of the file
* specified as a parameter
* When the plugin is disabled, outputs a script tag with the original source
* for easy debugging.
*
* eg:
* #{press.single-script "widget.js"}
Expand All @@ -19,6 +21,9 @@
}
}%

#{if press.Plugin.enabled() }
#{if press.Plugin.enabled() && !press.Plugin.hasErrorOccurred() }
<script src="${press.Plugin.compressedSingleJSUrl(_src)}" type="text/javascript" language="javascript" charset="utf-8"></script>
#{/if}
#{else}
<script src="/public/javascripts/${_src}" type="text/javascript" language="javascript" charset="utf-8"></script>
#{/else}
7 changes: 6 additions & 1 deletion app/views/tags/press/single-stylesheet.tag
@@ -1,6 +1,8 @@
*{
* Outputs a <css> tag whose source is the compressed output of the file
* specified as a parameter
* When the plugin is disabled, outputs a css tag with the original source
* for easy debugging.
*
* eg:
* #{press.single-stylesheet "widget.css"}
Expand All @@ -19,6 +21,9 @@
}
}%

#{if press.Plugin.enabled() }
#{if press.Plugin.enabled() && !press.Plugin.hasErrorOccurred() }
<link href="${press.Plugin.compressedSingleCSSUrl(_src)}" rel="stylesheet" type="text/css" charset="utf-8" #{if _media} media="${_media}"#{/if}></link>
#{/if}
#{else}
<link href="/public/stylesheets/${_src}" rel="stylesheet" type="text/css" charset="utf-8" #{if _media} media="${_media}"#{/if}></link>
#{/else}
2 changes: 1 addition & 1 deletion app/views/tags/press/stylesheet.tag
Expand Up @@ -33,7 +33,7 @@
throw new play.exceptions.TagInternalException("src attribute cannot be empty for stylesheet tag");
}
}%
#{if press.Plugin.enabled() }
#{if press.Plugin.enabled() && !press.Plugin.hasErrorOccurred() }
${ press.Plugin.addCSS(_src, _compress) }
#{/if}
#{else}
Expand Down

0 comments on commit 4154f49

Please sign in to comment.