# com.bea.p13n.management
ApplicationHelper Class

public class ApplicationHelper
extends `Object`

Utility methods for applications.

Hierarchy
  `Object`
  `ApplicationHelper`

## Constructor Summary

ApplicationHelper()

## Method Summary

`public static `String`` | `getApplicationName`() Get name of the current application.
`public static `String`` | `getContextRoot`(`String` applicationName, `String` webAppName) Retrieve the context root for a webapp.
`public static `InputStream`` | `getResourceAsStream`(`String` uri) Read a resource (file) from the current application.
`public static `String`` | `getWebAppName`(`ServletContext` theContext) Get name of the current WebApp module for the given context.
`public static `String`` | `getWebAppName`(`HttpServletRequest` theRequest) Get name of the current WebApp module running the given HTTP Request.
`public static `String`` | `getWebAppUri`(`String` applicationName, `String` webAppName) Retrieve the URI where the webapp is deployed.
`public static boolean` | `resourceExists`(`String` uri) Test if a resource (file) exists in the app.

Methods from class `java.lang.`Object``
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Detail

### ApplicationHelper

    public ApplicationHelper()

## Method Detail

### getApplicationName() Method

    public static String getApplicationName()
         throws IllegalStateException

Get name of the current application. This name is unique among applications deployed to the domain, and thus can be useful for naming resources to be scoped to the application.

This method must be called from an application context. That is, some component (EJB, Servlet, etc.) deployed as part of an application (ear), or code called from such a component. The result will be specific to the application containing that component.

The Application Name is the deployed name of the application, and is often the name of the EAR jar file or directory, although this is not always the case.

#### Returns

The name of the application in which the calling component is deployed.

#### Exceptions

``IllegalStateException``
  If not called from an application context.

### getContextRoot(String, String) Method

    public static String getContextRoot(String applicationName,
                                        String webAppName)
         throws InstanceNotFoundException

Retrieve the context root for a webapp. The context root can be used to build a URL to access that webapp. The context root is specified in either the application's or the webapp's deployment descriptors.

This can be used when a webapp needs to build an absolute URL to itself, or a URL to some other webapp.

#### Parameters

applicationName
  the name of the application containing the webapp

webAppName
  the name of the webapp

#### Returns

the context root for the webapp

#### Exceptions

``InstanceNotFoundException``
  if the named application or webapp does not exist

Related Topics

``ApplicationHelper.getApplicationName()``
``ApplicationHelper.getWebAppName(ServletContext)``
``ApplicationHelper.getWebAppName(HttpServletRequest)``
``ApplicationHelper.getWebAppUri(String, String)``

### getResourceAsStream(String) Method

    public static InputStream getResourceAsStream(String uri)
         throws IOException

Read a resource (file) from the current application.

This method must be called from an application context. That is, some component (EJB, Servlet, etc.) deployed as part of an application (ear), or code called from such a component. The result will be specific to the application containing that component.

This will fail (log and throw an IOException) if the file does not exist. Use `resourceExists()` to test existence and prevent the exception being logged.

#### Parameters

uri
  the location of the resource (file) in the application. Path must be relative to the application (ear) and not null.

#### Returns

stream for the resource

#### Exceptions

``IOException``
  if the file doesn't exist or there is trouble reading the file or problem with deployment or app or something

Related Topics

``ApplicationHelper.resourceExists(String)``

### getWebAppName(ServletContext) Method

    public static String getWebAppName(ServletContext theContext)

Get name of the current WebApp module for the given context.

This method is intended to be called from a WebApp component (Servlet) that wants to know its own deployed name. The name is the deployed name of the webapp, and is often (by convention) given the name of the war file or directory, or the webapp's context root, although this is not always the case. This name is unique among webapps in the application, and thus can be useful for naming resources to be scoped to the webapp. Note, however, that the name is not unique within a server or domain, as webapps with the same name may be deployed in other applications.

#### Parameters

theContext
  The Servlet context for which the name is to be returned.

#### Returns

The WebApp name.

Related Topics

``ApplicationHelper.getApplicationName()``
``ApplicationHelper.getWebAppName(HttpServletRequest)``
``ApplicationHelper.getContextRoot(String, String)``
``ApplicationHelper.getWebAppUri(String, String)``

### getWebAppName(HttpServletRequest) Method

    public static String getWebAppName(HttpServletRequest theRequest)

Get name of the current WebApp module running the given HTTP Request.

This method is intended to be called from a WebApp component (Servlet) that wants to know its own deployed name. The name is the deployed name of the webapp, and is often (by convention) given the name of the war file or directory, or the webapp's context root, although this is not always the case. This name is unique among webapps in the application, and thus can be useful for naming resources to be scoped to the webapp. Note, however, that the name is not unique within a server or domain, as webapps with the same name may be deployed in other applications.

#### Parameters

theRequest
  The Request for which the name is to be returned.

#### Returns

The WebApp name.

Related Topics

``ApplicationHelper.getApplicationName()``
``ApplicationHelper.getWebAppName(ServletContext)``
``ApplicationHelper.getContextRoot(String, String)``
``ApplicationHelper.getWebAppUri(String, String)``

### getWebAppUri(String, String) Method

    public static String getWebAppUri(String applicationName,
                                      String webAppName)
         throws InstanceNotFoundException

Retrieve the URI where the webapp is deployed. The URI is the directory or war file name of the deployed webapp, relative to the application root. The returned uri might be a directory or a war file.

This uri can be used with getResourceAsStream to retrieve files from a webapp, for example, when `ServletContext.getResourceAsStream()` will not work. Usually this will be in the case of one webapp "managing" another webapp.

#### Parameters

applicationName
  the name of the application containing the webapp

webAppName
  the name of the webapp

#### Returns

the URI for the webapp, relative to the application root

#### Exceptions

``InstanceNotFoundException``
  if the named application or webapp does not exist

Related Topics

``ApplicationHelper.getApplicationName()``
``ApplicationHelper.getWebAppName(ServletContext)``
``ApplicationHelper.getWebAppName(HttpServletRequest)``
``ApplicationHelper.getContextRoot(String, String)``

### resourceExists(String) Method

    public static boolean resourceExists(String uri)
         throws IOException

Test if a resource (file) exists in the app.

This method must be called from an application context. That is, some component (EJB, Servlet, etc.) deployed as part of an application (ear), or code called from such a component. The result will be specific to the application containing that component.

This will fail (log and throw an IOException) if the parent directory does not exist.

If you are intending to test for a directory, the uri must end with a "/", or it won't match.

#### Parameters

uri
  the location of the resource (file) in the application. Path must be relative to the application (ear) and not null.

#### Returns

true if the resource indicated by the uri is in the application.

#### Exceptions

``IOException``
  if the parent directory of the uri resource does not exist.