© 2005 BEA Systems, Inc.

com.bea.p13n.management
Class ApplicationHelper

java.lang.Object
  extended bycom.bea.p13n.management.ApplicationHelper

public class ApplicationHelper
extends Object

Utility methods for applications.


Constructor Summary
ApplicationHelper()
           
 
Method Summary
static String getApplicationName()
          Get name of the current application.
static String getContextRoot(String applicationName, String webAppName)
          Retrieve the context root for a webapp.
static InputStream getResourceAsStream(String uri)
          Read a resource (file) from the current application.
static String getWebAppName(HttpServletRequest theRequest)
          Get name of the current WebApp module running the given HTTP Request.
static String getWebAppName(ServletContext theContext)
          Get name of the current WebApp module for the given context.
static String getWebAppUri(String applicationName, String webAppName)
          Retrieve the URI where the webapp is deployed.
static boolean resourceExists(String uri)
          Test if a resource (file) exists in the app.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ApplicationHelper

public ApplicationHelper()
Method Detail

getApplicationName

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.
Throws:
IllegalStateException - If not called from an application context.

getContextRoot

public static String getContextRoot(String applicationName,
                                    String webAppName)
                             throws javax.management.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
Throws:
javax.management.InstanceNotFoundException - if the named application or webapp does not exist
See Also:
getApplicationName(), getWebAppName(ServletContext), getWebAppName(HttpServletRequest), getWebAppUri(String, String)

getResourceAsStream

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
Throws:
IOException - if the file doesn't exist or there is trouble reading the file or problem with deployment or app or something
NullPointerException - if uri is null
See Also:
resourceExists(String)

getWebAppName

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.

Returns:
The WebApp name.
Throws:
IllegalStateException - If not called with a WebLogic Request.
See Also:
getApplicationName(), getWebAppName(ServletContext), getContextRoot(String, String), getWebAppUri(String, String)

getWebAppName

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.
Throws:
IllegalStateException - If not called with a WebLogic ServletContext.
See Also:
getApplicationName(), getWebAppName(HttpServletRequest), getContextRoot(String, String), getWebAppUri(String, String)

getWebAppUri

public static String getWebAppUri(String applicationName,
                                  String webAppName)
                           throws javax.management.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
Throws:
javax.management.InstanceNotFoundException - if the named application or webapp does not exist
See Also:
getApplicationName(), getWebAppName(ServletContext), getWebAppName(HttpServletRequest), getContextRoot(String, String)

resourceExists

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.
Throws:
IOException - if the parent directory of the uri resource does not exist.

© 2005 BEA Systems, Inc.

Copyright © 2005 BEA Systems, Inc. All Rights Reserved