001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037:
038: package com.sun.portal.oracleportlet.util;
039:
040: import java.sql.*;
041: import java.util.*;
042: import java.text.*;
043: import java.util.logging.*;
044:
045: import javax.servlet.http.HttpServletRequest;
046:
047: /**
048: * This is a utility class that contains method to obtain a connection to Oracle server
049: *
050: * @author Zahid Syed
051: */
052:
053: public class OracleConnectionUtils implements OraclePortletConstants {
054:
055: private static Logger logger = OraclePortletLogger.getLogger();
056:
057: public static Connection getOracleConnection(
058: HttpServletRequest httpReq) {
059:
060: Connection conn = null;
061:
062: ResourceBundle rbundle = null;
063:
064: try {
065: rbundle = ResourceBundle.getBundle("oracleconfig");
066: } catch (MissingResourceException excp) {
067: logMessage(Level.SEVERE,
068: "getOracleConnection : File oracleconfig.properties NOT found : "
069: + excp);
070: return conn;
071: }
072:
073: String oraSSOConfig = rbundle
074: .getString(SSO_ORACLE_CONFIG_PREF_NAME);
075: Properties oraProperties = getOracleProperties(httpReq,
076: oraSSOConfig);
077:
078: if (oraProperties == null) {
079: logMessage(Level.SEVERE,
080: "getOracleConnection : Could not get properties from getOracleProperties");
081: return conn;
082: } else {
083: String hostname = (String) oraProperties
084: .get(ORACLE_HOST_NAME);
085: String portnum = (String) oraProperties
086: .get(ORACLE_PORT_NUM);
087: String sid = (String) oraProperties.get(ORACLE_SID);
088: String userid = (String) oraProperties.get(ORACLE_USER_ID);
089: String password = (String) oraProperties
090: .get(ORACLE_USER_PWD);
091:
092: try {
093: logMessage(Level.INFO,
094: "Trying to get Oracle Connection...");
095: Class.forName("oracle.jdbc.driver.OracleDriver");
096: conn = DriverManager.getConnection("jdbc:oracle:thin:@"
097: + hostname + ":" + portnum + ":" + sid, userid,
098: password);
099: logMessage(Level.INFO, "Obtained Oracle Connection...");
100: } catch (Exception ex) {
101: logMessage(Level.SEVERE,
102: "Exception while obtaining Oracle Connection : "
103: + ex);
104: }
105:
106: return conn;
107: }
108: }
109:
110: private static Properties getOracleProperties(
111: HttpServletRequest httpReq, String oraSSOConfigName) {
112:
113: Properties props = null;
114: try {
115:
116: OracleSSOAdapterUtils ssoaUtils = new OracleSSOAdapterUtils(
117: httpReq, oraSSOConfigName);
118: props = ssoaUtils.getOracleProperties();
119:
120: } catch (Exception ex) {
121: logMessage(Level.SEVERE,
122: "getOracleProperties: While obtaining props from getOracleProperties - "
123: + ex);
124: return props;
125: }
126: return props;
127: }
128:
129: /**
130: * This method logs the debug messages
131: **/
132: private static void logMessage(Object debugLevel, String msg) {
133:
134: logger.log((Level) debugLevel, msg);
135: }
136:
137: }
|