use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class QrCodeUtils method getQRCodeBitmap.
/**
* Generate Bitmap with QR Code based on input.
*
* @param input The data to render as a QR code.
* @param size The preferred width and height of the QR code in pixels.
* @return QR Code as Bitmap
*/
public static Bitmap getQRCodeBitmap(final String input, final int size) {
try {
final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = new QRCodeWriter().encode(input, BarcodeFormat.QR_CODE, size, size, hints);
final int width = result.getWidth();
final int height = result.getHeight();
final int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
final int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final WriterException e) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(QrCodeUtils.class);
if (log.shouldLog(Log.ERROR))
log.error("QrCodeUtils", e);
return null;
}
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class PersistNews method load.
/**
* This does not check for any missing values.
* Any fields in any NewsEntry may be null.
* Content is not sanitized by NewsXMLParser here, do that before storing.
*
* @return non-null, sorted by updated date, newest first
*/
public static List<NewsEntry> load(I2PAppContext ctx) {
Log log = ctx.logManager().getLog(PersistNews.class);
File dir = new File(ctx.getConfigDir(), DIR);
List<NewsEntry> rv = new ArrayList<NewsEntry>();
File[] files = dir.listFiles(new FileSuffixFilter(PFX, SFX));
if (files == null)
return rv;
for (File file : files) {
String name = file.getName();
XMLParser parser = new XMLParser(ctx);
InputStream in = null;
Node node;
boolean error = false;
try {
in = new GZIPInputStream(new FileInputStream(file));
node = parser.parse(in);
NewsEntry entry = extract(node);
if (entry != null) {
rv.add(entry);
} else {
if (log.shouldWarn())
log.warn("load error from " + file);
error = true;
}
} catch (ParserException pe) {
if (log.shouldWarn())
log.warn("load error from " + file, pe);
error = true;
} catch (IOException ioe) {
if (log.shouldWarn())
log.warn("load error from " + file, ioe);
error = true;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
}
if (error)
file.delete();
}
Collections.sort(rv);
return rv;
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class PersistNews method store.
/**
* Store each entry.
* Old entries are always overwritten, as they may change even without the updated date changing.
*
* @param entries each one should be "entry" at the root
* @return success
*/
public static boolean store(I2PAppContext ctx, List<Node> entries) {
Log log = ctx.logManager().getLog(PersistNews.class);
File dir = new SecureDirectory(ctx.getConfigDir(), DIR);
if (!dir.exists())
dir.mkdirs();
StringBuilder buf = new StringBuilder();
boolean rv = true;
for (Node entry : entries) {
Node nid = entry.getNode("id");
if (nid == null) {
if (log.shouldWarn())
log.warn("entry without UUID");
continue;
}
String id = nid.getValue();
if (id == null) {
if (log.shouldWarn())
log.warn("entry without UUID");
continue;
}
String name = idToName(ctx, id);
File file = new File(dir, name);
Writer out = null;
try {
out = new OutputStreamWriter(new GZIPOutputStream(new SecureFileOutputStream(file)));
out.write(XML_START);
XMLParser.toString(buf, entry);
out.write(buf.toString());
buf.setLength(0);
} catch (IOException ioe) {
if (log.shouldWarn())
log.warn("failed store to " + file, ioe);
rv = false;
} finally {
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
}
return rv;
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class PluginStarter method stopPlugin.
/**
* @return true on success
* @throws Exception just about anything, caller would be wise to catch Throwable
*/
public static boolean stopPlugin(RouterContext ctx, String appName) throws Exception {
Log log = ctx.logManager().getLog(PluginStarter.class);
File pluginDir = new File(ctx.getConfigDir(), PLUGIN_DIR + '/' + appName);
if ((!pluginDir.exists()) || (!pluginDir.isDirectory())) {
log.error("Cannot stop nonexistent plugin: " + appName);
return false;
}
// stop things in clients.config
File clientConfig = new File(pluginDir, "clients.config");
if (clientConfig.exists()) {
Properties props = new Properties();
DataHelper.loadProps(props, clientConfig);
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(clientConfig);
runClientApps(ctx, pluginDir, clients, "stop");
}
/*
File consoleDir = new File(pluginDir, "console");
Properties props = RouterConsoleRunner.webAppProperties(consoleDir.getAbsolutePath());
File webappDir = new File(consoleDir, "webapps");
String fileNames[] = webappDir.list(RouterConsoleRunner.WarFilenameFilter.instance());
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
String warName = fileNames[i].substring(0, fileNames[i].lastIndexOf(".war"));
if (Arrays.asList(STANDARD_WEBAPPS).contains(warName)) {
continue;
}
WebAppStarter.stopWebApp(server, warName);
}
}
*/
if (pluginWars.containsKey(appName)) {
Iterator<String> wars = pluginWars.get(appName).iterator();
while (wars.hasNext()) {
String warName = wars.next();
WebAppStarter.stopWebApp(ctx, warName);
}
pluginWars.get(appName).clear();
}
// }
// remove summary bar link
Properties props = pluginProperties(ctx, appName);
String name = stripHTML(props, "consoleLinkName_" + Messages.getLanguage(ctx));
if (name == null)
name = stripHTML(props, "consoleLinkName");
if (name != null && name.length() > 0)
NavHelper.unregisterApp(name);
if (log.shouldLog(Log.WARN))
log.warn("Stopping plugin: " + appName);
return true;
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class PluginStarter method updateAll.
/**
* inline
* @since 0.8.13
*/
private static void updateAll(RouterContext ctx, boolean delay) {
List<String> plugins = getPlugins();
Map<String, String> toUpdate = new HashMap<String, String>();
for (String appName : plugins) {
Properties props = pluginProperties(ctx, appName);
String url = props.getProperty("updateURL");
if (url != null)
toUpdate.put(appName, url);
}
if (toUpdate.isEmpty())
return;
ConsoleUpdateManager mgr = UpdateHandler.updateManager(ctx);
if (mgr == null)
return;
if (mgr.isUpdateInProgress())
return;
if (delay) {
// wait for proxy
mgr.update(TYPE_DUMMY, 3 * 60 * 1000);
mgr.notifyProgress(null, Messages.getString("Checking for plugin updates", ctx));
int loop = 0;
do {
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException ie) {
}
if (loop++ > 40)
break;
} while (mgr.isUpdateInProgress(TYPE_DUMMY));
}
String proxyHost = ctx.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST);
int proxyPort = ConfigUpdateHandler.proxyPort(ctx);
if (proxyPort == ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT && proxyHost.equals(ConfigUpdateHandler.DEFAULT_PROXY_HOST) && ctx.portMapper().getPort(PortMapper.SVC_HTTP_PROXY) < 0) {
mgr.notifyComplete(null, Messages.getString("Plugin update check failed", ctx) + " - " + Messages.getString("HTTP client proxy tunnel must be running", ctx));
return;
}
if (ctx.commSystem().isDummy()) {
mgr.notifyComplete(null, Messages.getString("Plugin update check failed", ctx) + " - " + "VM Comm System");
return;
}
Log log = ctx.logManager().getLog(PluginStarter.class);
int updated = 0;
for (Map.Entry<String, String> entry : toUpdate.entrySet()) {
String appName = entry.getKey();
if (log.shouldLog(Log.WARN))
log.warn("Checking for update plugin: " + appName);
// blocking
if (mgr.checkAvailable(PLUGIN, appName, 60 * 1000) == null) {
if (log.shouldLog(Log.WARN))
log.warn("No update available for plugin: " + appName);
continue;
}
if (log.shouldLog(Log.WARN))
log.warn("Updating plugin: " + appName);
// non-blocking
mgr.update(PLUGIN, appName, 30 * 60 * 1000);
int loop = 0;
do {
// keep going
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException ie) {
}
if (loop++ > 48)
break;
} while (mgr.isUpdateInProgress(PLUGIN, appName));
if (mgr.getUpdateAvailable(PLUGIN, appName) != null)
updated++;
}
if (updated > 0)
mgr.notifyComplete(null, ngettext("1 plugin updated", "{0} plugins updated", updated, ctx));
else
mgr.notifyComplete(null, Messages.getString("Plugin update check complete", ctx));
}
Aggregations