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.
197 lines
7.5 KiB
197 lines
7.5 KiB
package de.memtext.db; |
|
|
|
import java.io.BufferedReader; |
|
import java.io.File; |
|
import java.io.FileReader; |
|
import java.io.IOException; |
|
import java.lang.reflect.Constructor; |
|
import java.lang.reflect.InvocationTargetException; |
|
import java.sql.Connection; |
|
import java.sql.DriverManager; |
|
import java.sql.SQLException; |
|
import java.util.Iterator; |
|
|
|
import javax.xml.transform.TransformerConfigurationException; |
|
|
|
import org.apache.commons.dbcp.PoolingDriver; |
|
|
|
import de.memtext.baseobjects.coll.NamedObjectSet; |
|
import de.memtext.tree.KeyParentEqualException; |
|
|
|
|
|
public class MemtextPools extends NamedObjectSet { |
|
|
|
private NamedObjectSet pools = new NamedObjectSet(); |
|
|
|
public MemtextPools() { |
|
super(); |
|
} |
|
|
|
public synchronized Connection getConnection(String poolname) |
|
throws SQLException { |
|
if (pools.size() == 0) |
|
throw new IllegalStateException("Kein ConnectionPool gefunden."); |
|
if (!pools.containsItemWithName(poolname)) |
|
throw new SQLException("Kein ConnectionPool für Mandant:" |
|
+ poolname + " gefunden"); |
|
String pooldrv = "jdbc:apache:commons:dbcp:" + poolname; |
|
if (DriverManager.getDriver(pooldrv) == null) { |
|
String msg = "Kein ConnectionPool gefunden "; |
|
if (!poolname.equals("default")) |
|
msg += " für Mandant " + poolname; |
|
throw new SQLException(msg); |
|
} |
|
return DriverManager.getConnection(pooldrv); |
|
} |
|
|
|
public MemtextPool get(String poolname) { |
|
if (!pools.containsItemWithName(poolname)) |
|
throw new IllegalStateException("Kein ConnectionPool (" + poolname |
|
+ ") vorhanden"); |
|
return (MemtextPool) pools.getByName(poolname); |
|
} |
|
|
|
/* void initDefaultOnly() throws SQLException, IOException, |
|
DBServletException { |
|
pools.add(new MemtextPool("default")); |
|
} |
|
*/ |
|
/** |
|
* wenn mehrfach benutzt wird (ConnectionPools für SuperX und Joolap) |
|
* mind ein sollte namensappendix haben, damit unterscheidbar - |
|
* sonst wird z.B. Pool für joolap "default" von später erzeugten Pool für superx "default" |
|
* überschrieben! |
|
*/ |
|
public void init(String subpath,Class poolclass, String nameAppendix) throws SQLException, IOException, |
|
DBServletException { |
|
if (nameAppendix==null) nameAppendix=""; |
|
Class[] params = new Class[2]; |
|
params[0] = String.class; |
|
params[1] = String.class; |
|
Constructor constructor; |
|
try { |
|
constructor = poolclass.getConstructor(params); |
|
|
|
String db_extfile = "mandanten.cfg"; |
|
if (db_extfile.indexOf(File.separator) == -1) { |
|
java.net.URL url = MemtextPools.class.getProtectionDomain() |
|
.getCodeSource().getLocation(); |
|
File myJar = new File(url.getFile()); |
|
File myPath = new File(myJar.getParent()); |
|
String pfad = myPath.getParent(); |
|
if (subpath!=null) pfad+=File.separator+subpath; |
|
db_extfile = pfad + File.separator+db_extfile; ; |
|
|
|
|
|
} |
|
File f = new File(db_extfile); |
|
if (!f.exists()) { |
|
//einfach nur normale db.properties (default) |
|
System.out.print("Aufbau Datenbank-ConnectionPool"); |
|
Object[] args = new Object[2]; |
|
args[0] = "default";//Appendix hängt JoolapPool an |
|
args[1] = subpath; |
|
MemtextPool connectionPool = (MemtextPool) constructor.newInstance(args); |
|
System.out.println(" OK"); |
|
System.out.println(" public/private key "+(connectionPool.hasDSAHandler()?" aktiv ":" nicht aktiv")); |
|
|
|
pools.add(connectionPool); |
|
} else { //mehrereMandanten |
|
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); |
|
Object[] args = new Object[2]; |
|
args[0] = line;//Appendix hängt JoolapPool an |
|
args[1] = subpath; |
|
MemtextPool connectionPool = (MemtextPool) constructor.newInstance(args); |
|
System.out.println("OK"); |
|
System.out.println(" public/private key"+(connectionPool.hasDSAHandler()?" aktiv ":" nicht aktiv")); |
|
|
|
pools.add(connectionPool); |
|
} |
|
bfr.close(); |
|
fr.close(); |
|
} |
|
} catch (SecurityException e) { |
|
e.printStackTrace(); |
|
throw new DBServletException(e.toString()); |
|
} catch (NoSuchMethodException e) { |
|
e.printStackTrace(); |
|
throw new DBServletException(e.toString()); |
|
} catch (IllegalArgumentException e) { |
|
e.printStackTrace(); |
|
throw new DBServletException(e.toString()); |
|
} catch (InstantiationException e) { |
|
e.printStackTrace(); |
|
throw new DBServletException(e.toString()); |
|
} catch (IllegalAccessException e) { |
|
e.printStackTrace(); |
|
throw new DBServletException(e.toString()); |
|
} catch (InvocationTargetException e) { |
|
e.printStackTrace(); |
|
throw new DBServletException(e.toString()); |
|
} |
|
} |
|
|
|
/** |
|
* Destroys all ConnectionPools |
|
* |
|
* @throws Exception |
|
*/ |
|
public void closeAll() throws Exception { |
|
for (Iterator it = pools.iterator(); it.hasNext();) { |
|
MemtextPool pool = (MemtextPool) it.next(); |
|
pool.close(); |
|
} |
|
|
|
} |
|
|
|
public void main(String args[]) { |
|
try { |
|
init("xx",MemtextPool.class, null); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
int i = 1; |
|
} |
|
|
|
public void invalidate(String poolname, Connection con) |
|
throws DBServletException { |
|
if (!pools.containsItemWithName(poolname)) |
|
throw new DBServletException( |
|
"Kann Connection nicht invalidieren - kein ConnectionPool " |
|
+ poolname + " gefunden."); |
|
try { |
|
PoolingDriver driver = (PoolingDriver) DriverManager |
|
.getDriver("jdbc:apache:commons:dbcp:"); |
|
|
|
//driver.invalidateConnection(con); |
|
driver.getConnectionPool(poolname).invalidateObject(con); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
throw new DBServletException("Invalidating connection failed -" + e); |
|
} |
|
|
|
} |
|
|
|
public void resetAllPools() |
|
throws TransformerConfigurationException, KeyParentEqualException, |
|
SQLException, DBServletException { |
|
for (Iterator it = pools.iterator(); it.hasNext();) { |
|
MemtextPool aPool = (MemtextPool) it.next(); |
|
aPool.init(); |
|
} |
|
} |
|
|
|
public void clearLogFiles() throws IOException { |
|
for (Iterator it = pools.iterator(); it.hasNext();) { |
|
MemtextPool aPool = (MemtextPool) it.next(); |
|
aPool.clearLogFiles(); |
|
} |
|
} |
|
} |
|
|
|
//Created on 04.11.2004 at 20:18:11 als SxPools
|
|
|