001: /**
002: * $Id: SAPConfiguration.java,v 1.2 2005/08/12 07:38:22 nk137934 Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.sapportlet.config;
014:
015: import java.util.ResourceBundle;
016: import java.util.Enumeration;
017: import java.util.MissingResourceException;
018: import java.util.logging.Logger;
019: import com.sun.portal.sapportlet.*;
020:
021: /**
022: * This class is used to read SAP related configuration data.
023: * The config parameters are initilaized from a file called
024: * "sapconfig.properties". This file needs to be present
025: * in the classpath of any web application using this library.
026: * It would typically be present in WEB-INF/classes directory.
027: *
028: * @author nk137934
029: */
030: public class SAPConfiguration implements SAPPortletConstants {
031:
032: private static ResourceBundle sapConfig = null;
033: private static boolean initFailed = false;
034: private static Logger logger = Logger.getLogger(LOGGER_NAMESPACE);
035:
036: // Static initializer that reads the configuration file.
037: // If read succeeds, initFailed is set to true.
038: static {
039: try {
040: sapConfig = ResourceBundle.getBundle(SAP_CONFIG_FILE_NAME);
041: } catch (MissingResourceException mrExcp) {
042: logger.severe("Could not read the sap config file.");
043: logger.severe("Check if config file:"
044: + SAP_CONFIG_FILE_NAME + ".properties"
045: + " is present in Classpath ");
046: initFailed = true;
047: }
048:
049: logger.config("Start --- SAP Configuration Info ---");
050: Enumeration allKeys = sapConfig.getKeys();
051: while (allKeys.hasMoreElements()) {
052: String element = (String) allKeys.nextElement();
053: logger.severe(element + "=" + sapConfig.getString(element));
054: }
055: logger.config("End --- SAP Configuration Info ---");
056:
057: }
058:
059: /** Private Constructor */
060: private SAPConfiguration() {
061: }
062:
063: /**
064: * Returns the endpoint host name
065: * @throws com.sun.portal.sapportlet.config.SAPConfigurationException On configuration error
066: * @return Host name
067: */
068: public static String getEndpointHost()
069: throws SAPConfigurationException {
070: checkConfig();
071: return sapConfig.getString(CONFIG_ENDPOINT_HOST);
072: }
073:
074: /**
075: * Return the endpoint port number
076: * @throws com.sun.portal.sapportlet.config.SAPConfigurationException On configuration error
077: * @return Endpoint port
078: */
079: public static String getEndpointPort()
080: throws SAPConfigurationException {
081: checkConfig();
082: return sapConfig.getString(CONFIG_ENDPOINT_PORT);
083: }
084:
085: /**
086: * Returns the SAP client id
087: * @throws com.sun.portal.sapportlet.config.SAPConfigurationException On configuration error
088: * @return SAP client ID
089: */
090: public static String getClientID() throws SAPConfigurationException {
091: checkConfig();
092: return sapConfig.getString(CONFIG_CLIENTID);
093: }
094:
095: /**
096: * Returns the endpoint address. This is constructed using the
097: * endpoint hostname and the endpoint port
098: * @throws com.sun.portal.sapportlet.config.SAPConfigurationException On configuration error
099: * @return Endpoint address
100: */
101: public static String getEndpointAddress()
102: throws SAPConfigurationException {
103:
104: String endHost = getEndpointHost();
105: String endPort = getEndpointPort();
106: String clientID = getClientID();
107:
108: if (endHost == null || endPort == null) {
109: throw new SAPConfigurationException(
110: "Both endpointHost and "
111: + "endpointPort need to be specified");
112: }
113:
114: if (clientID == null) {
115: clientID = "000";
116: logger.warning("sap.clientID not specified");
117: logger.warning("Default clientID 000 will be used");
118: }
119: return "http://" + endHost + ":" + endPort
120: + "/sap/bc/soap/rfc?sap-client=" + clientID;
121:
122: }
123:
124: /**
125: * This method checks if the init has suceeded or not. If init
126: * has failed and SAPConfiguration excpetion is thrown.
127: * @throws SAPConfigurationException if init has failed.
128: */
129: private static void checkConfig() throws SAPConfigurationException {
130: if (initFailed)
131: throw new SAPConfigurationException(
132: "Could not read config file:"
133: + SAP_CONFIG_FILE_NAME);
134:
135: }
136: }
|