SuperX-Kernmodul
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

218 lines
7.6 KiB

package de.superx.servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import javax.xml.transform.TransformerConfigurationException;
import org.apache.commons.dbcp.PoolingDriver;
import de.memtext.baseobjects.coll.NamedObjectSet;
import de.memtext.util.DateUtils;
import de.superx.common.DBServletException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* New Connection pool making use of Jakarta Commons noch nicht im Einsatz
TODO funktionen in memtext.db.MemtextPools nutzen
**/
public class SxPools extends NamedObjectSet {
private static Logger logger = LoggerFactory.getLogger(SuperXManager.class);
private static final long serialVersionUID = 1L;
private static NamedObjectSet pools = new NamedObjectSet();
private static boolean hasMandanten=false;
private SxPools() {
super();
}
public static synchronized Connection getConnection(String poolname)
throws SQLException {
if (pools.size() == 0)
{
IllegalStateException e=new IllegalStateException("Kein Datenbank-ConnectionPool gefunden.");
e.printStackTrace();
throw e;
}
if (!pools.containsItemWithName(poolname))
throw new SQLException("Kein Datenbank-ConnectionPool für Mandant:"
+ poolname + " gefunden");
String pooldrv = "jdbc:apache:commons:dbcp:" + poolname;
if (DriverManager.getDriver(pooldrv) == null) {
String msg = "Kein Datenbank-ConnectionPool gefunden ";
if (!poolname.equals("default"))
msg += " für Mandant " + poolname;
throw new SQLException(msg);
}
return ((SxPool)pools.getByName(poolname)).getConnection();
// return DriverManager.getConnection(pooldrv);
}
public static boolean hasMandanten()
{
return hasMandanten;
}
public static int count()
{
return pools.size();
}
public static SxPool get(String poolname) {
if (!pools.containsItemWithName(poolname)) throw new IllegalStateException("Kein Datenbank-ConnectionPool ("+poolname+") vorhanden");
return (SxPool) pools.getByName(poolname);
}
public static Collection<String> getMandantenIds()
{
Collection<String> result=new HashSet<String>();
Iterator<SxPool> it=pools.iterator();
while (it.hasNext())
{
result.add(it.next().getName());
}
return result;
}
/**
* 9/11 MB
* @param poolname
* @return
*/
public static boolean hasPool(String poolname)
{
return (pools!=null&&pools.containsItemWithName(poolname));
}
static void initDefaultOnly() throws SQLException, IOException, DBServletException {
pools.add(new SxPool("default"));
}
public static void init() throws SQLException, IOException, DBServletException {
String mandantenCfg = SuperXManager.getWEB_INFPfad()+File.separator+"mandanten.cfg";
File f = new File(mandantenCfg);
if (!f.exists()) {
//einfach nur normale db.properties (default)
logger.debug(DateUtils.getTodayString()+" "+DateUtils.getNowString());
System.out.print("Aufbau Datenbank-ConnectionPool");
SxPool connectionPool = new SxPool("default");
logger.debug(" OK");
logger.debug(" eingeschränkter Datenbankuser für Verbindung: "+connectionPool.isRestrictedConnection());
logger.debug(" public/private key"+(connectionPool.hasDSAHandler()?" aktiv ":" nicht aktiv"));
pools.add(connectionPool);
} else { //mehrereMandanten
hasMandanten=true;
FileReader fr = new FileReader(f);
BufferedReader bfr=new BufferedReader(fr);
String line;
while ((line = bfr.readLine()) != null) {
System.out.print("Aufbau Datenbank-ConnectionPool für "
+ line );
try {
SxPool connectionPool = new SxPool(line);
logger.debug("OK");
logger.debug(" eingeschränkter Datenbankuser für Verbindung: "+connectionPool.isRestrictedConnection());
logger.debug(" public/private key"+(connectionPool.hasDSAHandler()?" aktiv ":" nicht aktiv"));
pools.add(connectionPool);
}
catch (Exception e)
{
String msg="ERROR: Verbindung für "+line+" nicht erfolgreich ("+e+")";
System.out.println("ERROR: Verbindung für "+line+" nicht erfolgreich ("+e+")");
logger.info(msg);
}
}
bfr.close();
fr.close();
}
}
/**
* Destroys all ConnectionPools
*
* @throws Exception
*/
static void closeAll() throws Exception {
//TODO java >1.4 kurznotation
for (Iterator<SxPool> it = pools.iterator(); it.hasNext();) {
SxPool pool = (SxPool) it.next();
pool.close();
}
}
public static void main(String args[]) {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
int i = 1;
}
public static void invalidate(String poolname, Connection con)
throws DBServletException {
if (!pools.containsItemWithName(poolname))
throw new DBServletException("Kann Connection nicht invalidieren - kein Datenbank-ConnectionPool "+poolname+" gefunden.");
try {
PoolingDriver driver = (PoolingDriver) DriverManager
.getDriver("jdbc:apache:commons:dbcp:" + poolname);
driver.invalidateConnection(con);
// driver.getConnectionPool(poolname).invalidateObject(con);
} catch (Exception e) {
e.printStackTrace();
throw new DBServletException("Invalidating connection failed -" + e);
}
}
public static String getPoolFinRechteInfos()
{
StringBuffer result=new StringBuffer();
for (Iterator it = pools.iterator(); it.hasNext();) {
SxPool aPool = (SxPool) it.next();
result.append("Mandant: "+aPool.getName()+" - SxFinRechtevariante:"+aPool.getFinRightVariantName()+"<br>");
}
return result.toString();
}
public static void resetAllPools()
throws TransformerConfigurationException,
SQLException, DBServletException {
for (Iterator it = pools.iterator(); it.hasNext();) {
SxPool aPool = (SxPool) it.next();
try {
aPool.init();
}
catch (Exception e)
{
logger.debug("ERROR: Datenreset für "+aPool.getName()+" fehlgeschlagen:"+e);
e.printStackTrace();
}
}
logger.debug(DateUtils.getNowString()+" Alle SuperX-Pools neu geladen");
}
public static void clearLogFiles() throws IOException {
for (Iterator it = pools.iterator(); it.hasNext();) {
SxPool aPool = (SxPool) it.next();
aPool.clearLogFiles();
}
}
}
//Created on 04.11.2004 at 20:18:11