FileDocCategorySizeDatePackage
AddressManager.javaAPI DocExample11733Tue May 29 16:57:16 BST 2007com.sun.xml.ws.tx.common

AddressManager

public class AddressManager extends Object
This class handles all address calculations for the wstx-service enpoints
author
Ryan.Shoemaker@Sun.COM
version
$Revision: 1.2.2.1 $
since
1.0

Fields Summary
private static final TxLogger
logger
private static String
hostAndPort
private static String
secureHostAndPort
private static final String
context
private static String
preferredScheme
private static String
httpPortOverride
private static String
httpsPortOverride
private static String
hostNameOverride
Constructors Summary
private AddressManager()


     
        // get the host and port info from GF
        callGetDefaultVirtualServerHostAndPort();

        // log a message if the preferred scheme has been overridden to http
        if ((preferredScheme.equals("http") && (logger.isLogging(Level.FINEST)))) {
            logger.fine("static initializer", "preferred scheme has been set to: " + preferredScheme);
        }

        // logic to override the dynamic settings
        // users can select a preferred scheme and override the http and https port numbers if necessary
        calculateHostAndPortOverrides();
    
    
Methods Summary
private static voidcalculateHostAndPortOverrides()
Lookup any system props that override the preferred scheme, and http/s ports

        final String hostName = hostAndPort.substring(0, hostAndPort.indexOf(':"));
        if ("http".equals(preferredScheme)) {
            if (httpPortOverride != null) {
                if (logger.isLogging(Level.FINEST)) {
                    logger.finest("static initializer", "http port overriden to: " + httpPortOverride);
                }
                hostAndPort = hostName + ':" + httpPortOverride;
            }
        } else if ("https".equals(preferredScheme)) {
            if (httpsPortOverride != null) {
                if (logger.isLogging(Level.FINEST)) {
                    logger.finest("static initializer", "https port overriden to: " + httpsPortOverride);
                }
                secureHostAndPort = hostName + ':" + httpsPortOverride;
            }
        } else {
            logger.warning("static initializer", LocalizationMessages.PREFERRED_SCHEME_ERROR_2016(preferredScheme));
            preferredScheme = "https";
            if (httpsPortOverride != null) {
                if (logger.isLogging(Level.FINEST)) {
                    logger.finest("static initializer", "https port overriden to: " + httpsPortOverride);
                }
                secureHostAndPort = hostName + ':" + httpsPortOverride;
            }
        }
    
private static voidcallGetDefaultVirtualServerHostAndPort()
Call appserv-core/com.sun.enterprise.webservice.WsTxUtils in GF to get the host and port information.

        try {
            if (logger.isLogging(Level.FINEST)) {
                logger.finest("static initializer", "getting host and port from AS...");
            }
            Class c = Class.forName("com.sun.enterprise.webservice.WsTxUtils");
            Object instance = c.newInstance();
            Method m = c.getMethod("getDefaultVirtualServerHostAndPort", boolean.class);
            hostAndPort = (String) m.invoke(instance, false);
            secureHostAndPort = (String) m.invoke(instance, true);

            if (secureHostAndPort == null || hostAndPort == null) {
                // there must have been an error on the GF-side, so fallback to a default
                fallback();
                logger.warning("getAddress",
                        LocalizationMessages.HOST_AND_PORT_LOOKUP_FAILURE_2015(preferredScheme + "://" + secureHostAndPort));
            }

            // this is an undocumented override
            if (hostNameOverride != null) {
                hostAndPort = hostNameOverride + hostAndPort.substring(hostAndPort.indexOf(':"), hostAndPort.length());
                secureHostAndPort = hostNameOverride + secureHostAndPort.substring(secureHostAndPort.indexOf(':"), secureHostAndPort.length());
                logger.finest("static initializer", "manual hostname overridden to: " + hostNameOverride);
            }

            if (logger.isLogging(Level.FINEST)) {
                logger.finest("static initializer", "hostAndPort: " + hostAndPort);
                logger.finest("static initializer", "secureHostAndPort: " + secureHostAndPort);
            }
        } catch (Throwable t) {  // trap every possible failure calling the gf code
            fallback();
            logger.info("static initializer",
                    LocalizationMessages.HOST_AND_PORT_LOOKUP_FAILURE_2015(preferredScheme + "://" + secureHostAndPort));
            logger.finest("static initializer",
                    LocalizationMessages.HOST_AND_PORT_LOOKUP_FAILURE_2015(preferredScheme + "://" + secureHostAndPort),
                    t);
        }
    
private static voidfallback()
Set the static fields to sane value that might work as a last-ditch effort.

        // worst-case scenario: fall back to https://canonicalhostname:8181, but also set
        // hostAndPort since it is used to ping the services.
        preferredScheme = "https";
        final String hostName = getServiceHostName();
        hostAndPort = hostName + ":8080";
        secureHostAndPort = hostName + ":8181";
    
public static java.net.URIgetAddress(java.lang.Class portType, boolean secure)
Return an address for the specified port type based on the 'secure' flag. If the 'secure' flag is true, the address will begin with "https", otherwise it will begin with "http".

param
portType the endpoint port type
param
secure flag to control whether the address is secure or non-secure
return
a URI representing the endpoint address with the specified secure or non-secure scheme

        StringBuilder addr = new StringBuilder();

        if (secure) {
            addr.append("https://").append(secureHostAndPort).append(context);
        } else { // non-secure
            addr.append("http://").append(hostAndPort).append(context);
        }

        // assign the proper context path onto the end of the scheme, host, port, and context root
        // TODO: are there other addresses we need to generate?
        if (portType == RegistrationCoordinatorPortType.class) {
            addr.append("/wscoor/coordinator/register");
        } else if (portType == RegistrationRequesterPortType.class) {
            addr.append("/wscoor/coordinator/registerResponse");
        } else if (portType == RegistrationPortTypeRPC.class) {
            addr.append("/wscoor/coordinator/synchRegister");
        } else if (portType == CoordinatorPortType.class) {
            addr.append("/wsat/coordinator");
        } else if (portType == ParticipantPortType.class) {
            addr.append("/wsat/2pc");
        } else {
            return null;
        }

        URI uri = null;
        try {
            uri = new URI(addr.toString());
        } catch (URISyntaxException e) {
        }

        return uri;
    
public static java.net.URIgetPreferredAddress(java.lang.Class portType)
Return an address for the specified port type using the preferred scheme.

This method should be used most of the time unless you explicitly know which scheme you want back.

param
portType the endpoint port type
return
a URI representing the preferred endpoint address

        return "http".equals(preferredScheme) ? getAddress(portType, false) : getAddress(portType, true);
    
private static java.lang.StringgetServiceHostName()
This method returns the fully qualified name of the host. If the name can't be resolved (on windows if there isn't a domain specified), just host name is returned

return
the host name

        String hostname = null;
        try {
            // look for full name
            hostname = InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException ex) {
            logger.warning("getServiceHostName", LocalizationMessages.FQDN_LOOKUP_FAILURE_2012(), ex);
            try {
                // fallback to ip address
                hostname = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                logger.warning("getServiceHostName", LocalizationMessages.FQDN_LOOKUP_FAILURE_2012(), e);
            }
        }

        return hostname;