public class JavaScriptModuleBuilder extends Object implements IModuleBuilder, IExtensionInitializer, ILayerListener, IShutdownListener
build(java.lang.String, com.ibm.jaggr.core.resource.IResource, javax.servlet.http.HttpServletRequest, java.util.List<com.ibm.jaggr.core.cachekeygenerator.ICacheKeyGenerator>)
.IExtensionInitializer.IExtensionRegistrar
ILayerListener.EventType
Modifier and Type | Field and Description |
---|---|
static String |
MODULE_EXPANDED_DEPS
Name of request attribute containing the per module expanded dependency names.
|
Constructor and Description |
---|
JavaScriptModuleBuilder() |
Modifier and Type | Method and Description |
---|---|
ModuleBuild |
build(String mid,
IResource resource,
javax.servlet.http.HttpServletRequest request,
List<ICacheKeyGenerator> keyGens)
Returns a
ModuleBuild object containing the processed (built)
output for the requested module. |
protected String |
generateModuleIdReg(List<String[]> expDeps,
Map<String,Integer> idMap)
Returns the module name to module id mappings for the expanded dependencies specified by
expDeps as a string which can be provided as input to the client side id registration
function. |
List<ICacheKeyGenerator> |
getCacheKeyGenerators(IAggregator aggregator)
This method may be called, before
IModuleBuilder.build(String, IResource, HttpServletRequest, List)
is called by a separate worker thread, to obtain a cache key generator
for this builder. |
protected List<ICacheKeyGenerator> |
getCacheKeyGenerators(Set<String> dependentFeatures,
boolean hasExpandableRequires) |
static com.google.javascript.jscomp.CompilationLevel |
getCompilationLevel(javax.servlet.http.HttpServletRequest request) |
protected List<com.google.javascript.jscomp.JSSourceFile> |
getJSSource(String mid,
IResource resource,
javax.servlet.http.HttpServletRequest request,
List<ICacheKeyGenerator> keyGens)
Overrideable method for getting the source modules to compile
|
boolean |
handles(String mid,
IResource resource)
Returns true if this module builder handles the specified resource
|
void |
initialize(IAggregator aggregator,
IAggregatorExtension extension,
IExtensionInitializer.IExtensionRegistrar registrar)
Called immediately after the extension is created to set the aggregator
instance that this extension object was created for.
|
boolean |
isScript(javax.servlet.http.HttpServletRequest request) |
String |
layerBeginEndNotifier(ILayerListener.EventType type,
javax.servlet.http.HttpServletRequest request,
List<IModule> modules,
Set<String> dependentFeatures)
Listener notification callback that is called for the event specified by
type . |
protected String |
moduleNameIdEncodingBeginAMD(javax.servlet.http.HttpServletRequest request)
Returns the text to be included at the beginning of the AMD portion of a layer if module name
id encoding is enabled.
|
protected String |
moduleNameIdEncodingBeginLayer(javax.servlet.http.HttpServletRequest request)
Returns the text to be included at the beginning of the layer if module name id
encoding is enabled.
|
protected String |
moduleNameIdEncodingEndLayer(javax.servlet.http.HttpServletRequest request,
List<IModule> modules)
Returns the text to be included at the end of the layer if module name id encoding is
enabled.
|
void |
shutdown(IAggregator aggregator)
This method is called from within the aggregator's
GenericServlet.destroy() method to notify listeners that the servlet
is shutting down. |
public static final String MODULE_EXPANDED_DEPS
public static com.google.javascript.jscomp.CompilationLevel getCompilationLevel(javax.servlet.http.HttpServletRequest request)
public void initialize(IAggregator aggregator, IAggregatorExtension extension, IExtensionInitializer.IExtensionRegistrar registrar)
IExtensionInitializer
initialize
in interface IExtensionInitializer
aggregator
- The aggregator that this extension was created for.extension
- The extension objectregistrar
- A registrar object that can be used to register additional
aggregator extendablespublic String layerBeginEndNotifier(ILayerListener.EventType type, javax.servlet.http.HttpServletRequest request, List<IModule> modules, Set<String> dependentFeatures)
ILayerListener
type
. If the returned string is not null, then the value
will be added to the response stream either before, or after, the layer
content, depending on the event type.layerBeginEndNotifier
in interface ILayerListener
type
- Indicates whether a layer is starting or finishing.request
- The HTTP request object.modules
- The list of modules in the layer. Note that modules added to
the layer by module builders using the
ModuleBuild.addExtraModule(String)
in the list. For the BEGIN_MODULE event, the list contains
only the single module that is being added to the layer.dependentFeatures
- Output - If the returned value depends on any features specified
in the request, then those features should be added to
dependentFeatures
. These will be included in the
construction of the cache key for the layer.public void shutdown(IAggregator aggregator)
IShutdownListener
GenericServlet.destroy()
method to notify listeners that the servlet
is shutting down.shutdown
in interface IShutdownListener
aggregator
- The aggregator that is shutting downpublic ModuleBuild build(String mid, IResource resource, javax.servlet.http.HttpServletRequest request, List<ICacheKeyGenerator> keyGens) throws Exception
IModuleBuilder
ModuleBuild
object containing the processed (built)
output for the requested module.
If an error occurs, this method can throw an exception, or else let an exception that occurs at a lower level propagate up. How the exception gets handled depends largely on whether or not development mode is enabled.
If development mode is not enabled, then the exception is converted to a ServletException and allowed to propagate to the servlet container. This will result in an error response being returned to the client.
If development mode is enabled, then exceptions thrown by this method are handled by creating a module build containing code to invoke console.error() on the client, specifying the exception message as the error text, and the aggregated response is flagged as an error response to prevent caching of the response either on the server or on the client. The content value which is returned by the define function is an empty string.
If a builder would like to handle errors in development mode differently,
either by providing partial content or to have more control over what is
displayed in the client console, then this method can return the error
content in the build output and use the ModuleBuild
constructor
that allows you to specify an error flag. Specifying true for the error
flag will cause the aggregated response containing this module build to
be flagged as an error and the build output will not be cached either on
the server or on the client.
If the request attribute specified by
IHttpTransport.EXPORTMODULENAMES_REQATTRNAME
is false, then this
method SHOULD return an anonymous module, however, if the request
attribute is true, then this method MUST return a named module (the first
parameter of the define function for the module must be the value
specified by mid
).
If, for some reason, a module builder is unable to provide named modules
for a request, then the module builder should register a
ILayerListener
service with the OSGi service registry,
specifying the name of the aggregator as the name
property of the
service, and then set the value of the
IHttpTransport.EXPORTMODULENAMES_REQATTRNAME
request attribute to
false in the
ILayerListener.layerBeginEndNotifier(ILayerListener.EventType, HttpServletRequest, List, Set)
method.
This method may choose to return a build containing multiple named
modules if the request attribute specified by
IHttpTransport.EXPORTMODULENAMES_REQATTRNAME
is true. A builder
that provides i18n resource modules, for example, may choose to include
additional, locale specific, resource modules based on the request
locale(s), however, this may only be done if all the modules in a
response are named. If anonymous modules are being requested (i.e. the
request attribute specified by
IHttpTransport.EXPORTMODULENAMES_REQATTRNAME
is false), then only
the requested module may be included in the build output, regardless of
whether or not the module builds provided by this builder are named.
The module builder may update one or more of the cache key generators in
keyGens
by specifying a new list of cache key generators in
the returned ModuleBuild
object. When a new list is provided,
the cached cache key generator list for this module, and any layers
containing this module, are updated. If this method returns the same list
object specified by keyGens
in the ModuleBuild
, then no updates of the cached key generators are performed. Most module
builders will have no need to update a cached key generator list once it
has been created and should just return
keyGens
in the ModuleBuild
object, however, if
keyGens
is null,
then a new cache key generator list containing only non-provisional
cache key generators MUST be provided in the returned ModuleBuild
.
A module builder should always return the same sized cache key generator
list for any given resource, and the cache key generators in the list
must be the same class, and in the same sequence, for all responses for
the same resource. Note that subclasses of this class may add their own
cache key generators to the result list, so keyGens
may be
larger than what this method previously returned because it contains
contributions from the subclasses. If this class wishes to update the
cache key generator list, it should not try to include any of these
added cache key generators in the new list. It should only include its
own cache key generators. Subclasses are responsible for detecting that
the superclass has updated the list in the result and then adding their
own cache key generators to the new result.
build
in interface IModuleBuilder
mid
- The module idresource
- The resource object for the source module.request
- The HTTP request objectkeyGens
- List of cache key generators for this module that was
obtained by a previous call to this method or
IModuleBuilder.getCacheKeyGenerators(IAggregator)
. If null,
then the ModuleBuild
returned by this method must
specify a new list of non-provisional cache key generators,
otherwise, this method may return a ModuleBuild
object
that specifies keyGen
for the cache key generator.ModuleBuild
objectException
protected List<com.google.javascript.jscomp.JSSourceFile> getJSSource(String mid, IResource resource, javax.servlet.http.HttpServletRequest request, List<ICacheKeyGenerator> keyGens) throws IOException
mid
- the module idresource
- the resourcerequest
- the request objectkeyGens
- the list of cache key generatorsIOException
public List<ICacheKeyGenerator> getCacheKeyGenerators(IAggregator aggregator)
IModuleBuilder
IModuleBuilder.build(String, IResource, HttpServletRequest, List)
is called by a separate worker thread, to obtain a cache key generator
for this builder. If cache keys for this builder depend on module
content, then this method should return a provisional cache key
generator, which generates cache keys based on information in the request
only. Provisional cache keys may be more exclusive than non-provisional
cache keys in matching requests to module builds.
If this method returns a provisional cache key generator, then
IModuleBuilder.build(String, IResource, HttpServletRequest, List)
will be called with a null cache key generator list and that method
MUST return a
ModuleBuild
object with a new immutable list of non-provisional cache key
generators when subsequently called for the same request.
getCacheKeyGenerators
in interface IModuleBuilder
aggregator
- The aggregator instancepublic boolean handles(String mid, IResource resource)
IModuleBuilder
handles
in interface IModuleBuilder
mid
- the module idresource
- the resource for the module idprotected List<ICacheKeyGenerator> getCacheKeyGenerators(Set<String> dependentFeatures, boolean hasExpandableRequires)
protected String moduleNameIdEncodingBeginLayer(javax.servlet.http.HttpServletRequest request)
request
- the http request objectprotected String moduleNameIdEncodingBeginAMD(javax.servlet.http.HttpServletRequest request)
request
- the http request objectprotected String moduleNameIdEncodingEndLayer(javax.servlet.http.HttpServletRequest request, List<IModule> modules)
moduleNameIdEncodingBeginLayer(HttpServletRequest)
as well as invocation of
the registration function and closing and self-invocation of the scoping function.
The format of the data is as described by IHttpTransport.getModuleIdRegFunctionName()
.
request
- the http request objectmodules
- the list of modules in the layerprotected String generateModuleIdReg(List<String[]> expDeps, Map<String,Integer> idMap)
expDeps
as a string which can be provided as input to the client side id registration
function. See IHttpTransport.getModuleIdRegFunctionName()
for a description of the
format of the returned string.expDeps
- List of string arrays containing the expanded module namesidMap
- the module name to module id map. If null, then the returned
string will contain only an array of the module names defined in
expDeps
.public boolean isScript(javax.servlet.http.HttpServletRequest request)
isScript
in interface IModuleBuilder
request
- the request objectCopyright © 2011-2015 IBM. All Rights Reserved.