allow a whole set of OOM listeners on threads, not just one

cache the hash's base64 value too
use the context's logging more
logging updates
This commit is contained in:
jrandom
2004-04-26 00:57:10 +00:00
committed by zzz
parent 64bcfd23bd
commit 1ad6dde146
5 changed files with 44 additions and 12 deletions

View File

@ -31,6 +31,8 @@ public class AESEngine {
public AESEngine(I2PAppContext ctx) {
_context = ctx;
_log = _context.logManager().getLog(AESEngine.class);
if (getClass() == AESEngine.class)
_log.warn("Warning: AES is disabled");
}
/** Encrypt the payload with the session key
@ -44,7 +46,6 @@ public class AESEngine {
|| (initializationVector.length != 16)) return null;
byte cyphertext[] = new byte[payload.length + (16 - (payload.length % 16))];
_log.warn("Warning: AES is disabled");
System.arraycopy(payload, 0, cyphertext, 0, payload.length);
return cyphertext;
}

View File

@ -25,7 +25,7 @@ import net.i2p.I2PAppContext;
* @author jrandom
*/
public class DummyElGamalEngine extends ElGamalEngine {
private final static Log _log = new Log(DummyElGamalEngine.class);
private Log _log;
/**
* The ElGamal engine should only be constructed and accessed through the
@ -35,6 +35,7 @@ public class DummyElGamalEngine extends ElGamalEngine {
*/
public DummyElGamalEngine(I2PAppContext context) {
super(context);
_log = context.logManager().getLog(DummyElGamalEngine.class);
_log.log(Log.CRIT, "Dummy ElGamal engine in use! NO DATA SECURITY. Danger Will Robinson, Danger!",
new Exception("I really hope you know what you're doing"));
}
@ -93,11 +94,13 @@ public class DummyElGamalEngine extends ElGamalEngine {
}
Hash calcHash = SHA256Generator.getInstance().calculateHash(rv);
if (calcHash.equals(hash)) {
_log.debug("Hash matches: " + DataHelper.toString(hash.getData(), hash.getData().length));
if (_log.shouldLog(Log.DEBUG))
_log.debug("Hash matches: " + DataHelper.toString(hash.getData(), hash.getData().length));
return rv;
} else {
_log.debug("Doesn't match hash [calc=" + calcHash + " sent hash=" + hash + "]\ndata = " + new String(rv),
new Exception("Doesn't match"));
if (_log.shouldLog(Log.DEBUG))
_log.debug("Doesn't match hash [calc=" + calcHash + " sent hash=" + hash + "]\ndata = " + new String(rv),
new Exception("Doesn't match"));
return null;
}
}

View File

@ -25,6 +25,7 @@ public class Hash extends DataStructureImpl {
private final static Log _log = new Log(Hash.class);
private byte[] _data;
private volatile String _stringified;
private volatile String _base64ed;
public final static int HASH_LENGTH = 32;
public final static Hash FAKE_HASH = new Hash(new byte[HASH_LENGTH]);
@ -44,11 +45,13 @@ public class Hash extends DataStructureImpl {
public void setData(byte[] data) {
_data = data;
_stringified = null;
_base64ed = null;
}
public void readBytes(InputStream in) throws DataFormatException, IOException {
_data = new byte[HASH_LENGTH];
_stringified = null;
_base64ed = null;
int read = read(in, _data);
if (read != HASH_LENGTH) throw new DataFormatException("Not enough bytes to read the hash");
}
@ -82,4 +85,11 @@ public class Hash extends DataStructureImpl {
}
return _stringified;
}
public String toBase64() {
if (_base64ed == null) {
_base64ed = super.toBase64();
}
return _base64ed;
}
}

View File

@ -9,6 +9,11 @@ package net.i2p.util;
*
*/
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
/**
* In case its useful later...
* (e.g. w/ native programatic thread dumping, etc)
@ -16,7 +21,7 @@ package net.i2p.util;
*/
public class I2PThread extends Thread {
private static Log _log;
private static OOMEventListener _lsnr;
private static Set _listeners = new HashSet(4);
public I2PThread() {
super();
@ -38,19 +43,29 @@ public class I2PThread extends Thread {
try {
super.run();
} catch (Throwable t) {
if ((t instanceof OutOfMemoryError) && (_lsnr != null)) _lsnr.outOfMemory((OutOfMemoryError) t);
if (t instanceof OutOfMemoryError)
fireOOM((OutOfMemoryError)t);
// we cant assume log is created
if (_log == null) _log = new Log(I2PThread.class);
_log.log(Log.CRIT, "Killing thread " + getName(), t);
}
}
public static void setOOMEventListener(OOMEventListener lsnr) {
_lsnr = lsnr;
private void fireOOM(OutOfMemoryError oom) {
for (Iterator iter = _listeners.iterator(); iter.hasNext(); ) {
OOMEventListener listener = (OOMEventListener)iter.next();
listener.outOfMemory(oom);
}
}
public static OOMEventListener getOOMEventListener() {
return _lsnr;
/** register a new component that wants notification of OOM events */
public static void addOOMEventListener(OOMEventListener lsnr) {
_listeners.add(lsnr);
}
/** unregister a component that wants notification of OOM events */
public static void removeOOMEventListener(OOMEventListener lsnr) {
_listeners.remove(lsnr);
}
public interface OOMEventListener {

View File

@ -269,6 +269,9 @@ public class LogManager {
_consoleBufferSize = DEFAULT_CONSOLEBUFFERSIZE;
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Log set to use the base log file as " + _baseLogfilename);
parseLimits(config);
}