package votorola.g.web.gwt; // Copyright 2010, 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import com.google.gwt.core.client.*; import com.google.gwt.dom.client.Document; import com.google.gwt.event.shared.GwtEvent; import com.google.gwt.regexp.shared.*; import com.google.gwt.user.client.Window; import com.google.web.bindery.event.shared.EventBus; import com.google.web.bindery.event.shared.SimpleEventBus; import com.google.web.bindery.event.shared.UmbrellaException; import votorola.g.lang.*; import votorola.g.web.*; /** General utilities and services for GWT applications. */ public final class GWTX { /** The single instance of GWTX. */ public static GWTX i() { return instance; } private static GWTX instance; { // constructed by GMod if( instance != null ) throw new IllegalStateException(); instance = GWTX.this; } // ------------------------------------------------------------------------------------ /** The general event bus. */ public EventBus bus() { return bus; } private final EventBus bus = new SimpleEventBus() { public void fireEvent( final GwtEvent e ) { try{ super.fireEvent( e ); } catch( Exception x ) { handleUncaughtException( x ); } // q.v. for reason } public void fireEventFromSource( final GwtEvent e, final Object source ) { try{ super.fireEventFromSource( e, source ); } catch( Exception x ) { handleUncaughtException( x ); } } }; /** The browser under which the application is running. The browser is also specified * on the document body as style class "voB-browser", where browser * is named in lowercase. */ public Browser browser() { return browser; } // No apparent way to read "user.agent" property on which GWT deferred binding is // based. Its values are too coarse in any case. This is more flexible. private final Browser browser; { final String userAgent = Window.Navigator.getUserAgent(); if( userAgent.contains( " Chrome/" )) browser = Browser.CHROME; else browser = Browser.UNKNOWN; Document.get().getBody().addClassName( "voB-" + browser.name().toLowerCase() ); } /** The pattern of a dev mode URL. The pattern splits the URL into groups: (1) the * leading portion, (2) the gwt.codesvr parameter value and (3) the * trailing portion which might be null (or empty depending on the browser). If the * trailing portion is non-null/non-empty, then it may be appended directly to the * leading portion to produce a URL without the gwt.codesvr parameter; * otherwise the leading portion alone will suffice for this purpose provided the * trailing '?' or '&' character is chopped. An example of a dev mode URL is: * *
http://localhost/path?p1=x&q1=x&gwt.codesvr=localhost:9997&p3=x&q3=x
* * The resulting values for the groups are (1) "http://localhost/path?p1=x&q1=x&", * (2) "localhost:9997" and (3) "p3=x&q3=x". * * @see GWT#isProdMode() */ public static final RegExp DEV_MODE_LOCATION_PATTERN = RegExp.compile( "^(.+\\?(?:.+&)?)gwt\\.codesvr=(.+?:[0-9]+)&?(.+)?$" ); // LEADING CODESVR TRAILING /** Shows the exception in an alert box along with instructions for developers to get * more information. Beware that a {@linkplain * GWT#setUncaughtExceptionHandler(GWT.UncaughtExceptionHandler) universal handler} * will not necessarily catch exceptions during event dispatch (GWT 2.3, 2.4, Firefox * and Chrome, is it only DOM events that slip through?). They tend to hit the * JavaScript console as umbrella exceptions with no name or line number. Consider * therefore adding explicit catchers to the dispatcher where possible, otherwise to * the individual handlers. * * @see WebModeExceptions */ public static void handleUncaughtException( final Throwable t ) { final StringBuilder b = new StringBuilder(); b.append( "Wild exception: " ); b.append( t.toString() ); b.append( '\n' ); if( GWT.isProdMode() ) { appendProd( t, b ); b.append( "Consider re-running in devmode where a stack trace is output to the console." ); } else { b.append( '\n' ); t.printStackTrace(); b.append( "A trace was output to the console." ); } Window.alert( b.toString() ); } /** A common string builder cleared for atomic reuse. Consider calling * stringBuilderClear().trimToSize() after building a very large string. */ public static StringBuilder stringBuilderClear() { return StringBuilderX.clear( stringBuilder ); } private static final StringBuilder stringBuilder = new StringBuilder(); /** Returns the same value if it is less than {@linkplain Integer#MAX_VALUE * Integer.MAX_VALUE}, otherwise returns min. This convenience method * serves mainly to document that integral wrapping is not emulated by GWT. * * @see Language support */ public static int wrapMaxInteger( final int value, final int min ) { return value > Integer.MAX_VALUE? min: value; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** Appends details in lieu of a proper stack trace, which is hard to obtain in * production mode because of limitiations in GWT's emulation of Java's stream * classes. */ private static void appendProd( Throwable t, final StringBuilder b ) { appendProd_trace( t, b ); if( t instanceof UmbrellaException ) { b.append( "Caused by: \n" ); final UmbrellaException xU = (UmbrellaException)t; for( Throwable cause: xU.getCauses() ) { b.append( cause.toString() ); appendProd_trace( cause, b ); } } } private static void appendProd_trace( Throwable t, final StringBuilder b ) { final StackTraceElement[] trace = t.getStackTrace(); int eN = trace.length; if( eN == 0 ) { b.append( " No stack trace available. It may help to add this to the module:\n" ); b.append( " \n" ); } else { if( eN > 3 ) eN = 3; // max for( int e = 0; e < eN; ++e ) { final StackTraceElement elem = trace[e]; b.append( " " ); b.append( elem.getClassName() ); b.append( '.' ); b.append( elem.getMethodName() ); b.append( " (" ); b.append( elem.getLineNumber() ); b.append( ")\n" ); } } b.append( "\n" ); } }