org.apache.wicket.markup.html
Class PackageResource

java.lang.Object
  extended by org.apache.wicket.Resource
      extended by org.apache.wicket.markup.html.WebResource
          extended by org.apache.wicket.markup.html.PackageResource
All Implemented Interfaces:
Serializable, IClusterable, IRequestListener, IResourceListener, IPackageResourceGuard, IModifiable
Direct Known Subclasses:
CompressedPackageResource, CSSPackageResource

public class PackageResource
extends WebResource
implements IModifiable, IPackageResourceGuard

Represents a localizable static resource.

Use like eg:

 PackageResource IMG_UNKNOWN = PackageResource.get(EditPage.class, "questionmark.gif");
 
where the static resource references image 'questionmark.gif' from the the package that EditPage is in to get a package resource.

Access to resources can be granted or denied via a IPackageResourceGuard. Please see IResourceSettings.getPackageResourceGuard() as well.

Author:
Jonathan Locke, Eelco Hillenius, Juergen Donnerstag
See Also:
Serialized Form

Nested Class Summary
static class PackageResource.PackageResourceBlockedException
          Exception thrown when the creation of a package resource is not allowed.
 
Field Summary
static Pattern EXTENSION_CSS
          Deprecated. Will be removed in 2.0; contribute resources one by one instead
static Pattern EXTENSION_JS
          Deprecated. Will be removed in 2.0; contribute resources one by one instead
 
Fields inherited from interface org.apache.wicket.IResourceListener
INTERFACE
 
Constructor Summary
protected PackageResource(Class<?> scope, String path, Locale locale, String style)
          Hidden constructor.
 
Method Summary
 boolean accept(Class<?> scope, String path)
          Whether the package resource that can be reached using the provided parameters may be accessed.
static void bind(Application application, Class<?> scope, Pattern pattern)
          Deprecated. Since Wicket 1.2.1 this method is effectively a no-op. package resources are automatically tried and bound as shared resources so that they don't have to be pre-registered anymore. Will be removed in 2.0
static void bind(Application application, Class<?> scope, Pattern pattern, boolean recurse)
          Deprecated. Since Wicket 1.2.1 this method is effectively a no-op. package resources are automatically tried and bound as shared resources so that they don't have to be pre-registered anymore. Will be removed in 2.0
static void bind(Application application, Class<?> scope, String name)
          Binds a resource to the given application object.
static void bind(Application application, Class<?> scope, String name, Locale locale)
          Binds a resource to the given application object.
static void bind(Application application, Class<?> scope, String name, Locale locale, String style)
          Binds a resource to the given application object.
static boolean exists(Class<?> scope, String path, Locale locale, String style)
          Gets whether a resource for a given set of criteria exists.
static PackageResource[] get(Class<?> scope, Pattern pattern)
          Deprecated. Will be removed in 2.0; contribute resources one by one instead
static PackageResource[] get(Class<?> scope, Pattern pattern, boolean recurse)
          Deprecated. Will be removed in 2.0; contribute resources one by one instead
static PackageResource get(Class<?> scope, String path)
          Gets a non-localized resource for a given set of criteria.
static PackageResource get(Class<?> scope, String path, Locale locale, String style)
          Gets the resource for a given set of criteria.
 String getAbsolutePath()
          Gets the absolute path of the resource.
 Locale getLocale()
          Gets the locale.
 String getPath()
          Gets the path this resource was created with.
 IResourceStream getResourceStream()
          Returns a new IResourceStream that provides all data for the request.
 IResourceStream getResourceStream(boolean failOnError)
           
 Class<?> getScope()
          Gets the scoping class, used for class loading and to determine the package.
 String getStyle()
          Gets the style.
 Time lastModifiedTime()
          Returns the last modified time of resource
protected static PackageResource newPackageResource(Class<?> scope, String path, Locale locale, String style)
          Create a new PackageResource
 String toString()
           
 
Methods inherited from class org.apache.wicket.markup.html.WebResource
configureResponse, getCacheDuration, setHeaders
 
Methods inherited from class org.apache.wicket.Resource
getParameters, invalidate, isCacheable, onResourceRequested, setCacheable, setParameters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EXTENSION_CSS

@Deprecated
public static final Pattern EXTENSION_CSS
Deprecated. Will be removed in 2.0; contribute resources one by one instead
common extension pattern for css files; matches all files with extension 'css'.


EXTENSION_JS

@Deprecated
public static final Pattern EXTENSION_JS
Deprecated. Will be removed in 2.0; contribute resources one by one instead
common extension pattern for javascript files; matches all files with extension 'js'.

Constructor Detail

PackageResource

protected PackageResource(Class<?> scope,
                          String path,
                          Locale locale,
                          String style)
Hidden constructor.

Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in
path - The path to the resource
locale - The locale of the resource
style - The style of the resource
Method Detail

bind

@Deprecated
public static void bind(Application application,
                                   Class<?> scope,
                                   Pattern pattern)
Deprecated. Since Wicket 1.2.1 this method is effectively a no-op. package resources are automatically tried and bound as shared resources so that they don't have to be pre-registered anymore. Will be removed in 2.0

Binds the resources that match the provided pattern to the given application object. Will create any resources if not already in the shared resources of the application object.

Parameters:
application - The application to bind to.
scope - The scope of the resource.
pattern - A regular expression to match against the contents of the package of the provided scope class (eg ".\*\.js" will add all the files with extension "js" from that package).

bind

@Deprecated
public static void bind(Application application,
                                   Class<?> scope,
                                   Pattern pattern,
                                   boolean recurse)
Deprecated. Since Wicket 1.2.1 this method is effectively a no-op. package resources are automatically tried and bound as shared resources so that they don't have to be pre-registered anymore. Will be removed in 2.0

Binds the resources that match the provided pattern to the given application object. Will create any resources if not already in the shared resources of the application object and does that recursively when the recurse parameter is true, or just for the scoped package if that parameter is false

Parameters:
application - The application to bind to.
scope - The scope of the resource.
pattern - A regular expression to match against the contents of the package of the provided scope class (eg ".\*\.js" will add all the files with extension "js" from that package).
recurse - Whether this method should recurse into sub packages

bind

public static void bind(Application application,
                        Class<?> scope,
                        String name)
Binds a resource to the given application object. Will create the resource if not already in the shared resources of the application object.

Parameters:
application - The application to bind to.
scope - The scope of the resource.
name - The name of the resource (like "myfile.js")

bind

public static void bind(Application application,
                        Class<?> scope,
                        String name,
                        Locale locale)
Binds a resource to the given application object. Will create the resource if not already in the shared resources of the application object.

Parameters:
application - The application to bind to.
scope - The scope of the resource.
name - The name of the resource (like "myfile.js")
locale - The locale of the resource.

bind

public static void bind(Application application,
                        Class<?> scope,
                        String name,
                        Locale locale,
                        String style)
Binds a resource to the given application object. Will create the resource if not already in the shared resources of the application object.

Parameters:
application - The application to bind to.
scope - The scope of the resource.
name - The name of the resource (like "myfile.js")
locale - The locale of the resource.
style - The style of the resource.

exists

public static boolean exists(Class<?> scope,
                             String path,
                             Locale locale,
                             String style)
Gets whether a resource for a given set of criteria exists.

Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the class in which you call this method
path - The path to the resource
locale - The locale of the resource
style - The style of the resource (see Session)
Returns:
true if a resource could be loaded, false otherwise

get

@Deprecated
public static PackageResource[] get(Class<?> scope,
                                               Pattern pattern)
Deprecated. Will be removed in 2.0; contribute resources one by one instead

Gets non-localized resources for a given set of criteria. Multiple resource can be loaded for the same criteria if they match the pattern. If no resources were found, this method returns null.

Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the calling class/ the class in which you call this method
pattern - Regexp pattern to match resources
Returns:
The resources, never null but may be empty

get

@Deprecated
public static PackageResource[] get(Class<?> scope,
                                               Pattern pattern,
                                               boolean recurse)
Deprecated. Will be removed in 2.0; contribute resources one by one instead

Gets non-localized resources for a given set of criteria. Multiple resource can be loaded for the same criteria if they match the pattern. If no resources were found, this method returns null.

Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the calling class/ the class in which you call this method
pattern - Regexp pattern to match resources
recurse - Whether this method should recurse into sub packages
Returns:
The resources, never null but may be empty

get

public static PackageResource get(Class<?> scope,
                                  String path)
Gets a non-localized resource for a given set of criteria. Only one resource will be loaded for the same criteria.

Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the calling class/ the class in which you call this method
path - The path to the resource
Returns:
The resource

get

public static PackageResource get(Class<?> scope,
                                  String path,
                                  Locale locale,
                                  String style)
Gets the resource for a given set of criteria. Only one resource will be loaded for the same criteria.

Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the class in which you call this method
path - The path to the resource
locale - The locale of the resource
style - The style of the resource (see Session)
Returns:
The resource

newPackageResource

protected static PackageResource newPackageResource(Class<?> scope,
                                                    String path,
                                                    Locale locale,
                                                    String style)
Create a new PackageResource

Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the class in which you call this method
path - The path to the resource
locale - The locale of the resource
style - The style of the resource (see Session)
Returns:
The resource

getAbsolutePath

public final String getAbsolutePath()
Gets the absolute path of the resource.

Returns:
the absolute resource path

getLocale

public final Locale getLocale()
Gets the locale.

Returns:
The Locale of this package resource

getPath

public final String getPath()
Gets the path this resource was created with.

Returns:
the path

getResourceStream

public IResourceStream getResourceStream()
Description copied from class: Resource
Returns a new IResourceStream that provides all data for the request. Despite its name, this method should return a new IResourceStream instance each time it is invoked (otherwise the InputStream returned from the IResourceStream would be at EOF when a second request is handled for the same Resource). The caller must invoke IResourceStream.close() when finished.

Specified by:
getResourceStream in class Resource
Returns:
Gets the resource for the component.

getResourceStream

public IResourceStream getResourceStream(boolean failOnError)
Parameters:
failOnError - throw an AbortException when resource does not exist
Returns:
Gets the resource for the component.

getScope

public final Class<?> getScope()
Gets the scoping class, used for class loading and to determine the package.

Returns:
the scoping class

getStyle

public final String getStyle()
Gets the style.

Returns:
the style

lastModifiedTime

public Time lastModifiedTime()
Returns the last modified time of resource

Specified by:
lastModifiedTime in interface IModifiable
Returns:
last modified time or null if the time can not be determined

accept

public boolean accept(Class<?> scope,
                      String path)
Description copied from interface: IPackageResourceGuard
Whether the package resource that can be reached using the provided parameters may be accessed.

Specified by:
accept in interface IPackageResourceGuard
Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in
path - The path to the resource
Returns:
True if access is permitted, false otherwise
See Also:
IPackageResourceGuard.accept(java.lang.Class, java.lang.String)

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.