forked from I2P_Developers/i2p.i2p
SusiDNS:
- Move some methods to BaseBean - Locking on config file accesses - Remove static log and context
This commit is contained in:
@@ -42,7 +42,7 @@ import net.i2p.util.SecureFileOutputStream;
|
||||
|
||||
public class AddressbookBean extends BaseBean
|
||||
{
|
||||
protected String book, action, serial, lastSerial, filter, search, hostname, destination;
|
||||
protected String book, filter, search, hostname, destination;
|
||||
protected int beginIndex, endIndex;
|
||||
private Properties addressbook;
|
||||
private int trClass;
|
||||
@@ -92,7 +92,7 @@ public class AddressbookBean extends BaseBean
|
||||
loadConfig();
|
||||
String filename = properties.getProperty( getBook() + "_addressbook" );
|
||||
// clean up the ../ with getCanonicalPath()
|
||||
File path = new File(ConfigBean.addressbookPrefix, filename);
|
||||
File path = new File(addressbookDir(), filename);
|
||||
try {
|
||||
return path.getCanonicalPath();
|
||||
} catch (IOException ioe) {}
|
||||
@@ -111,12 +111,6 @@ public class AddressbookBean extends BaseBean
|
||||
return entries;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
public String getBook()
|
||||
{
|
||||
if( book == null || ( !book.equalsIgnoreCase( "master" ) &&
|
||||
@@ -130,14 +124,7 @@ public class AddressbookBean extends BaseBean
|
||||
public void setBook(String book) {
|
||||
this.book = DataHelper.stripHTML(book); // XSS
|
||||
}
|
||||
public String getSerial() {
|
||||
lastSerial = "" + Math.random();
|
||||
action = null;
|
||||
return lastSerial;
|
||||
}
|
||||
public void setSerial(String serial) {
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
/** Load addressbook and apply filter, returning messages about this. */
|
||||
public String getLoadBookMessages()
|
||||
{
|
||||
@@ -178,7 +165,7 @@ public class AddressbookBean extends BaseBean
|
||||
message = generateLoadMessage();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Debug.debug( e.getClass().getName() + ": " + e.getMessage() );
|
||||
warn(e);
|
||||
} finally {
|
||||
if (fis != null)
|
||||
try { fis.close(); } catch (IOException ioe) {}
|
||||
@@ -330,7 +317,7 @@ public class AddressbookBean extends BaseBean
|
||||
save();
|
||||
message += "<br>" + _("Address book saved.");
|
||||
} catch (Exception e) {
|
||||
Debug.debug( e.getClass().getName() + ": " + e.getMessage() );
|
||||
warn(e);
|
||||
message += "<br>" + _("ERROR: Could not write addressbook file.");
|
||||
}
|
||||
}
|
||||
@@ -355,7 +342,7 @@ public class AddressbookBean extends BaseBean
|
||||
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new SecureFileOutputStream( ConfigBean.addressbookPrefix + filename );
|
||||
fos = new SecureFileOutputStream(new File(addressbookDir(), filename));
|
||||
addressbook.store( fos, null );
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
@@ -480,24 +467,4 @@ public class AddressbookBean extends BaseBean
|
||||
protected int totalSize() {
|
||||
return entries.length;
|
||||
}
|
||||
|
||||
/** translate */
|
||||
protected static String _(String s) {
|
||||
return Messages.getString(s);
|
||||
}
|
||||
|
||||
/** translate */
|
||||
protected static String _(String s, Object o) {
|
||||
return Messages.getString(s, o);
|
||||
}
|
||||
|
||||
/** translate */
|
||||
protected static String _(String s, Object o, Object o2) {
|
||||
return Messages.getString(s, o, o2);
|
||||
}
|
||||
|
||||
/** translate (ngettext) @since 0.8.7 */
|
||||
protected static String ngettext(String s, String p, int n) {
|
||||
return Messages.getString(n, s, p);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package i2p.susi.dns;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.util.OrderedProperties;
|
||||
|
||||
/**
|
||||
* Holds methods common to several Beans.
|
||||
@@ -16,8 +18,9 @@ public class BaseBean
|
||||
{
|
||||
protected final I2PAppContext _context;
|
||||
protected final Properties properties;
|
||||
protected String action, lastSerial, serial;
|
||||
|
||||
private long configLastLoaded = 0;
|
||||
private long configLastLoaded;
|
||||
private static final String PRIVATE_BOOK = "private_addressbook";
|
||||
private static final String DEFAULT_PRIVATE_BOOK = "../privatehosts.txt";
|
||||
|
||||
@@ -27,35 +30,56 @@ public class BaseBean
|
||||
public static final String DEFAULT_THEME = "light";
|
||||
public static final String BASE_THEME_PATH = "/themes/susidns/";
|
||||
public static final String PROP_PW_ENABLE = "routerconsole.auth.enable";
|
||||
private static final String ADDRESSBOOK_DIR = "addressbook";
|
||||
private static final String CONFIG_FILE = "config.txt";
|
||||
|
||||
public BaseBean()
|
||||
{
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
properties = new Properties();
|
||||
properties = new OrderedProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from ConfigBean.addressbookPrefix
|
||||
*/
|
||||
protected File addressbookDir() {
|
||||
return new File(_context.getRouterDir(), ADDRESSBOOK_DIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from ConfigBean.configFileName
|
||||
*/
|
||||
protected File configFile() {
|
||||
return new File(addressbookDir(), CONFIG_FILE);
|
||||
}
|
||||
|
||||
protected void loadConfig()
|
||||
{
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
if( !properties.isEmpty() && currentTime - configLastLoaded < 10000 )
|
||||
return;
|
||||
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
properties.clear();
|
||||
fis = new FileInputStream( ConfigBean.configFileName );
|
||||
properties.load( fis );
|
||||
// added in 0.5, for compatibility with 0.4 config.txt
|
||||
if( properties.getProperty(PRIVATE_BOOK) == null)
|
||||
properties.setProperty(PRIVATE_BOOK, DEFAULT_PRIVATE_BOOK);
|
||||
configLastLoaded = currentTime;
|
||||
synchronized (BaseBean.class) {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if( !properties.isEmpty() && currentTime - configLastLoaded < 10000 )
|
||||
return;
|
||||
reload();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Debug.debug( e.getClass().getName() + ": " + e.getMessage() );
|
||||
} finally {
|
||||
if (fis != null)
|
||||
try { fis.close(); } catch (IOException ioe) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from ConfigBean
|
||||
*/
|
||||
protected void reload() {
|
||||
try {
|
||||
synchronized (BaseBean.class) {
|
||||
properties.clear();
|
||||
// use loadProps to trim
|
||||
DataHelper.loadProps(properties, configFile());
|
||||
// added in 0.5, for compatibility with 0.4 config.txt
|
||||
if( properties.getProperty(PRIVATE_BOOK) == null)
|
||||
properties.setProperty(PRIVATE_BOOK, DEFAULT_PRIVATE_BOOK);
|
||||
configLastLoaded = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
warn(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,4 +130,84 @@ public class BaseBean
|
||||
// return the map.
|
||||
return themes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
public String getSerial() {
|
||||
lastSerial = Long.toString(_context.random().nextLong());
|
||||
action = null;
|
||||
return lastSerial;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
public void setSerial(String serial) {
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
protected static String _(String s) {
|
||||
return Messages.getString(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
protected static String _(String s, Object o) {
|
||||
return Messages.getString(s, o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
protected static String _(String s, Object o, Object o2) {
|
||||
return Messages.getString(s, o, o2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate (ngettext)
|
||||
* @since 0.9.13 moved from subclasses
|
||||
*/
|
||||
protected static String ngettext(String s, String p, int n) {
|
||||
return Messages.getString(n, s, p);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from Debug
|
||||
*/
|
||||
protected void debug(String msg) {
|
||||
Log log = _context.logManager().getLog(getClass());
|
||||
if (log.shouldLog(Log.DEBUG))
|
||||
log.debug(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.13 moved from Debug
|
||||
*/
|
||||
protected void warn(Throwable t) {
|
||||
Log log = _context.logManager().getLog(getClass());
|
||||
if (log.shouldLog(Log.WARN))
|
||||
log.warn("SusiDNS", t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,80 +35,49 @@ import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.util.OrderedProperties;
|
||||
|
||||
public class ConfigBean implements Serializable {
|
||||
public class ConfigBean extends BaseBean implements Serializable {
|
||||
|
||||
/*
|
||||
* as this is not provided as constant in addressbook, we define it here
|
||||
*/
|
||||
public static final String addressbookPrefix =
|
||||
(new File(I2PAppContext.getGlobalContext().getRouterDir(), "addressbook")).getAbsolutePath()
|
||||
+ File.separatorChar;
|
||||
public static final String configFileName = addressbookPrefix + "config.txt";
|
||||
|
||||
private String action, config;
|
||||
private String serial, lastSerial;
|
||||
private String config;
|
||||
private boolean saved;
|
||||
|
||||
public static String getConfigFileName() {
|
||||
return configFileName;
|
||||
}
|
||||
|
||||
public String getfileName() {
|
||||
return getConfigFileName();
|
||||
return configFile().toString();
|
||||
}
|
||||
|
||||
public boolean isSaved() {
|
||||
return saved;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getConfig()
|
||||
{
|
||||
if( config != null )
|
||||
return config;
|
||||
|
||||
reload();
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
private void reload()
|
||||
@Override
|
||||
protected void reload()
|
||||
{
|
||||
File file = new File( configFileName );
|
||||
if( file != null && file.isFile() ) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
try {
|
||||
// use loadProps to trim
|
||||
Properties props = new OrderedProperties();
|
||||
DataHelper.loadProps(props, file);
|
||||
for (Map.Entry<Object, Object> e : props.entrySet()) {
|
||||
buf.append((String) e.getKey()).append('=')
|
||||
.append((String) e.getValue()).append('\n');
|
||||
}
|
||||
config = buf.toString();
|
||||
saved = true;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
super.reload();
|
||||
StringBuilder buf = new StringBuilder(256);
|
||||
for (Map.Entry<Object, Object> e : properties.entrySet()) {
|
||||
buf.append((String) e.getKey()).append('=')
|
||||
.append((String) e.getValue()).append('\n');
|
||||
}
|
||||
config = buf.toString();
|
||||
saved = true;
|
||||
}
|
||||
|
||||
private void save()
|
||||
{
|
||||
File file = new File( configFileName );
|
||||
try {
|
||||
// use loadProps to trim, use storeProps to sort and get line endings right
|
||||
Properties props = new OrderedProperties();
|
||||
DataHelper.loadProps(props, new ByteArrayInputStream(config.getBytes("UTF-8")));
|
||||
DataHelper.storeProps(props, file);
|
||||
synchronized (BaseBean.class) {
|
||||
DataHelper.storeProps(props, configFile());
|
||||
}
|
||||
saved = true;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
@@ -145,20 +114,4 @@ public class ConfigBean implements Serializable {
|
||||
message = "<p class=\"messages\">" + message + "</p>";
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getSerial()
|
||||
{
|
||||
lastSerial = "" + Math.random();
|
||||
action = null;
|
||||
return lastSerial;
|
||||
}
|
||||
|
||||
public void setSerial(String serial ) {
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
/** translate */
|
||||
private static String _(String s) {
|
||||
return Messages.getString(s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Created on Sep 02, 2005
|
||||
*
|
||||
* This file is part of susidns project, see http://susi.i2p/
|
||||
*
|
||||
* Copyright (C) 2005 <susi23@mail.i2p>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* $Revision: 1.1 $
|
||||
*/
|
||||
|
||||
package i2p.susi.dns;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
public class Debug
|
||||
{
|
||||
private static Log _log;
|
||||
private static I2PAppContext _context;
|
||||
|
||||
static
|
||||
{
|
||||
try {
|
||||
_context = I2PAppContext.getGlobalContext(); // new I2PAppContext();
|
||||
_log = _context.logManager().getLog(Debug.class);
|
||||
}
|
||||
catch( NoClassDefFoundError e ) {
|
||||
_context = null;
|
||||
_log = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void debug( String msg )
|
||||
{
|
||||
if( _log != null ) {
|
||||
_log.debug( msg );
|
||||
}
|
||||
else {
|
||||
System.err.println( "DEBUG: [susidns] " + msg );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,7 @@ public class NamingServiceBean extends AddressbookBean
|
||||
if (isDirect())
|
||||
return super.getLoadBookMessages();
|
||||
NamingService service = getNamingService();
|
||||
Debug.debug("Searching within " + service + " with filename=" + getFileName() + " and with filter=" + filter + " and with search=" + search);
|
||||
debug("Searching within " + service + " with filename=" + getFileName() + " and with filter=" + filter + " and with search=" + search);
|
||||
String message = "";
|
||||
try {
|
||||
LinkedList<AddressBean> list = new LinkedList<AddressBean>();
|
||||
@@ -168,7 +168,7 @@ public class NamingServiceBean extends AddressbookBean
|
||||
searchProps.setProperty("search", search.toLowerCase(Locale.US));
|
||||
results = service.getEntries(searchProps);
|
||||
|
||||
Debug.debug("Result count: " + results.size());
|
||||
debug("Result count: " + results.size());
|
||||
for (Map.Entry<String, Destination> entry : results.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
if( filter != null && filter.length() > 0 ) {
|
||||
@@ -201,7 +201,7 @@ public class NamingServiceBean extends AddressbookBean
|
||||
message = generateLoadMessage();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Debug.debug( e.getClass().getName() + ": " + e.getMessage() );
|
||||
warn(e);
|
||||
}
|
||||
if( message.length() > 0 )
|
||||
message = "<p>" + message + "</p>";
|
||||
|
||||
@@ -40,29 +40,33 @@ import net.i2p.util.SecureFileOutputStream;
|
||||
|
||||
public class SubscriptionsBean extends BaseBean
|
||||
{
|
||||
private String action, fileName, content, serial, lastSerial;
|
||||
private String fileName, content;
|
||||
private static final String SUBS_FILE = "config.txt";
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getFileName()
|
||||
{
|
||||
loadConfig();
|
||||
|
||||
fileName = ConfigBean.addressbookPrefix + properties.getProperty( "subscriptions", "subscriptions.txt" );
|
||||
|
||||
fileName = subsFile().toString();
|
||||
return fileName;
|
||||
}
|
||||
|
||||
private void reload()
|
||||
/**
|
||||
* @since 0.9.13
|
||||
*/
|
||||
private File subsFile() {
|
||||
return new File(addressbookDir(), SUBS_FILE);
|
||||
}
|
||||
|
||||
private void reloadSubs() {
|
||||
synchronized(SubscriptionsBean.class) {
|
||||
locked_reloadSubs();
|
||||
}
|
||||
}
|
||||
|
||||
private void locked_reloadSubs()
|
||||
{
|
||||
File file = new File( getFileName() );
|
||||
if( file != null && file.isFile() ) {
|
||||
File file = subsFile();
|
||||
if(file.isFile()) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
@@ -83,9 +87,15 @@ public class SubscriptionsBean extends BaseBean
|
||||
}
|
||||
}
|
||||
|
||||
private void save()
|
||||
private void save() {
|
||||
synchronized(SubscriptionsBean.class) {
|
||||
locked_save();
|
||||
}
|
||||
}
|
||||
|
||||
private void locked_save()
|
||||
{
|
||||
File file = new File( getFileName() );
|
||||
File file = subsFile();
|
||||
try {
|
||||
// trim and sort
|
||||
List<String> urls = new ArrayList<String>();
|
||||
@@ -133,7 +143,7 @@ public class SubscriptionsBean extends BaseBean
|
||||
message = _("Subscriptions saved.");
|
||||
}
|
||||
} else if (action.equals(_("Reload"))) {
|
||||
reload();
|
||||
reloadSubs();
|
||||
message = _("Subscriptions reloaded.");
|
||||
}
|
||||
}
|
||||
@@ -148,17 +158,6 @@ public class SubscriptionsBean extends BaseBean
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getSerial()
|
||||
{
|
||||
lastSerial = "" + Math.random();
|
||||
action = null;
|
||||
return lastSerial;
|
||||
}
|
||||
|
||||
public void setSerial(String serial ) {
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
// will come from form with \r\n line endings
|
||||
this.content = content;
|
||||
@@ -169,13 +168,8 @@ public class SubscriptionsBean extends BaseBean
|
||||
if( content != null )
|
||||
return content;
|
||||
|
||||
reload();
|
||||
reloadSubs();
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
/** translate */
|
||||
private static String _(String s) {
|
||||
return Messages.getString(s);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user