001/* 002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.ecm.platform.reporting.datasource; 020 021import java.util.Map; 022 023import org.nuxeo.runtime.api.Framework; 024 025/** 026 * Wrapps a DataSource configuration 027 * 028 * @author Tiry (tdelprat@nuxeo.com) 029 */ 030public class NuxeoDSConfig { 031 032 protected String driverClass; 033 034 protected String url; 035 036 protected String userName; 037 038 protected String password; 039 040 public static final String H2_PREFIX = "org.h2"; 041 042 public static final String PG_PREFIX = "org.postgresql"; 043 044 public static final String MSSQL_PREFIX = "net.sourceforge.jtds"; 045 046 public static final String MYSQL_PREFIX = "com.mysql.jdbc"; 047 048 public static final String ORACLE_PREFIX = "oracle.jdbc"; 049 050 public NuxeoDSConfig(String dataSourceName, Map<String, String> properties) { 051 if (dataSourceName.startsWith(H2_PREFIX)) { 052 initForH2(properties); 053 } else if (dataSourceName.startsWith(PG_PREFIX)) { 054 initForPostgreSQL(properties); 055 } else if (dataSourceName.startsWith(MSSQL_PREFIX)) { 056 initForMSSQL(properties); 057 } else if (dataSourceName.startsWith(MYSQL_PREFIX)) { 058 initForMySQL(properties); 059 } else if (dataSourceName.startsWith(ORACLE_PREFIX)) { 060 initForOracle(properties); 061 } 062 } 063 064 protected void initForH2(Map<String, String> properties) { 065 userName = getProp(properties, "User"); 066 password = getProp(properties, "Password"); 067 url = getProp(properties, "URL"); 068 driverClass = SupportedDBHelper.getDriver(SupportedDBHelper.H2); 069 } 070 071 protected String getIntegerProp(Map<String, String> properties, String name) { 072 String value = getProp(properties, name); 073 if (value != null) { 074 return value; 075 } 076 return getProp(properties, name + "/Integer"); 077 } 078 079 protected void initForPostgreSQL(Map<String, String> properties) { 080 userName = getProp(properties, "User"); 081 password = getProp(properties, "Password"); 082 url = getProp(properties, "URL"); 083 if (url == null) { 084 url = "jdbc:postgresql://" + getProp(properties, "ServerName") + ":" 085 + getIntegerProp(properties, "PortNumber") + "/" + getProp(properties, "DatabaseName"); 086 } 087 driverClass = getProp(properties, "Driver"); 088 if (driverClass == null) { 089 driverClass = SupportedDBHelper.getDriver(SupportedDBHelper.PGSQL); 090 } 091 } 092 093 protected void initForMSSQL(Map<String, String> properties) { 094 userName = getProp(properties, "User"); 095 password = getProp(properties, "Password"); 096 url = getProp(properties, "URL"); 097 if (url == null) { 098 url = "jdbc:jtds:sqlserver://" + getProp(properties, "ServerName") + ":" 099 + getIntegerProp(properties, "PortNumber") + "/" + getProp(properties, "DatabaseName") 100 + ";useCursors=true"; 101 } 102 driverClass = getProp(properties, "Driver"); 103 if (driverClass == null) { 104 driverClass = SupportedDBHelper.getDriver(SupportedDBHelper.MSSQL); 105 } 106 } 107 108 protected void initForMySQL(Map<String, String> properties) { 109 userName = getProp(properties, "User"); 110 password = getProp(properties, "Password"); 111 url = getProp(properties, "URL"); 112 driverClass = getProp(properties, "Driver"); 113 if (driverClass == null) { 114 driverClass = SupportedDBHelper.getDriver(SupportedDBHelper.MYSQL); 115 } 116 } 117 118 protected void initForOracle(Map<String, String> properties) { 119 userName = getProp(properties, "User"); 120 password = getProp(properties, "Password"); 121 url = getProp(properties, "URL"); 122 driverClass = getProp(properties, "Driver"); 123 if (driverClass == null) { 124 driverClass = SupportedDBHelper.getDriver(SupportedDBHelper.ORACLE); 125 } 126 } 127 128 protected String getProp(Map<String, String> properties, String name) { 129 final String value = properties.get(name); 130 if (value == null) { 131 return null; 132 } 133 return Framework.expandVars(value); 134 } 135 136 public String getDriverClass() { 137 return driverClass; 138 } 139 140 public String getUrl() { 141 return url; 142 } 143 144 public String getUserName() { 145 return userName; 146 } 147 148 public String getPassword() { 149 return password; 150 } 151 152 @Override 153 public String toString() { 154 StringBuffer sb = new StringBuffer(); 155 sb.append("username:"); 156 sb.append(userName); 157 sb.append("\npassword:"); 158 sb.append(password); 159 sb.append("\ndriver:"); 160 sb.append(driverClass); 161 sb.append("\nurl:"); 162 sb.append(url); 163 return sb.toString(); 164 } 165 166}