hammant 2004/07/28 09:09:24
Modified: src/conf assembly.xml
src/java/org/apache/ftpserver BaseFtpConnection.java
FtpConnection.java FtpDataConnection.java
FtpServerImpl.java FtpWriter.java
SiteCommandHandler.java
src/java/org/apache/ftpserver/ip AbstractIpRestrictor.java
Added: src/java/org/apache/ftpserver AvalonFtpConfig.java
src/java/org/apache/ftpserver/ip AvalonFileIpRestrictor.java
Removed: src/java/org/apache/ftpserver FtpConfig.java
src/java/org/apache/ftpserver/ip FileIpRestrictor.java
Log:
more trimming of Avalon as mandatory framework
Revision Changes Path
1.2 +1 -1 incubator-ftpserver/src/conf/assembly.xml
Index: assembly.xml
===================================================================
RCS file: /home/cvs/incubator-ftpserver/src/conf/assembly.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- assembly.xml 31 Mar 2003 06:50:02 -0000 1.1
+++ assembly.xml 28 Jul 2004 16:09:24 -0000 1.2
@@ -28,7 +28,7 @@
<block class="org.apache.ftpserver.usermanager.PropertiesUserManager"
name="user-manager"/>
<!-- ip restrictor block -->
- <block class="org.apache.ftpserver.ip.FileIpRestrictor"
name="ip-restrictor"/>
+ <block class="org.apache.ftpserver.ip.AvalonFileIpRestrictor"
name="ip-restrictor"/>
<!-- Ftp server block -->
<block class="org.apache.ftpserver.FtpServerImpl" name="ftp-server" >
1.3 +4 -4
incubator-ftpserver/src/java/org/apache/ftpserver/BaseFtpConnection.java
Index: BaseFtpConnection.java
===================================================================
RCS file:
/home/cvs/incubator-ftpserver/src/java/org/apache/ftpserver/BaseFtpConnection.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BaseFtpConnection.java 27 Apr 2003 16:23:44 -0000 1.2
+++ BaseFtpConnection.java 28 Jul 2004 16:09:24 -0000 1.3
@@ -82,7 +82,7 @@
protected static final Class[] METHOD_INPUT_SIG = new Class[]
{FtpRequest.class, FtpWriter.class};
- protected FtpConfig mConfig = null;
+ protected AvalonFtpConfig mConfig = null;
protected FtpStatus mFtpStatus = null;
protected FtpDataConnection mDataConnection = null;
protected FtpUser mUser = null;
@@ -96,7 +96,7 @@
/**
* Set configuration file and the control socket.
*/
- public BaseFtpConnection(FtpConfig ftpConfig) {
+ public BaseFtpConnection(AvalonFtpConfig ftpConfig) {
mConfig = ftpConfig;
mFtpStatus = mConfig.getStatus();
mUser = new FtpUser();
@@ -318,7 +318,7 @@
/**
* Get config object
*/
- public FtpConfig getConfig() {
+ public AvalonFtpConfig getConfig() {
return mConfig;
}
1.3 +2 -2
incubator-ftpserver/src/java/org/apache/ftpserver/FtpConnection.java
Index: FtpConnection.java
===================================================================
RCS file:
/home/cvs/incubator-ftpserver/src/java/org/apache/ftpserver/FtpConnection.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FtpConnection.java 27 Apr 2003 16:23:16 -0000 1.2
+++ FtpConnection.java 28 Jul 2004 16:09:24 -0000 1.3
@@ -110,7 +110,7 @@
/**
* Set configuration file and the control socket.
*/
- public FtpConnection(FtpConfig cfg) {
+ public FtpConnection(AvalonFtpConfig cfg) {
super(cfg);
}
1.2 +3 -3
incubator-ftpserver/src/java/org/apache/ftpserver/FtpDataConnection.java
Index: FtpDataConnection.java
===================================================================
RCS file:
/home/cvs/incubator-ftpserver/src/java/org/apache/ftpserver/FtpDataConnection.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FtpDataConnection.java 31 Mar 2003 06:50:02 -0000 1.1
+++ FtpDataConnection.java 28 Jul 2004 16:09:24 -0000 1.2
@@ -69,7 +69,7 @@
*/
class FtpDataConnection {
- private FtpConfig mConfig = null;
+ private AvalonFtpConfig mConfig = null;
private Socket mDataSoc = null;
private ServerSocket mServSoc = null;
@@ -84,7 +84,7 @@
* Constructor.
* @param cfg ftp config object.
*/
- public FtpDataConnection(FtpConfig cfg) {
+ public FtpDataConnection(AvalonFtpConfig cfg) {
mConfig = cfg;
}
1.3 +3 -3
incubator-ftpserver/src/java/org/apache/ftpserver/FtpServerImpl.java
Index: FtpServerImpl.java
===================================================================
RCS file:
/home/cvs/incubator-ftpserver/src/java/org/apache/ftpserver/FtpServerImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FtpServerImpl.java 25 Oct 2003 19:49:52 -0000 1.2
+++ FtpServerImpl.java 28 Jul 2004 16:09:24 -0000 1.3
@@ -102,7 +102,7 @@
private SocketManager mSocManager = null;
private ConnectionManager mConManager = null;
private Context mContext = null;
- private FtpConfig mConfig = null;
+ private AvalonFtpConfig mConfig = null;
/**
* Default constructor - does nothing.
@@ -115,7 +115,7 @@
*/
public void contextualize(Context context) throws ContextException {
try {
- mConfig = new FtpConfig();
+ mConfig = new AvalonFtpConfig();
mConfig.setLogger(getLogger());
mContext = context;
mConfig.setContext(mContext);
1.2 +3 -3
incubator-ftpserver/src/java/org/apache/ftpserver/FtpWriter.java
Index: FtpWriter.java
===================================================================
RCS file:
/home/cvs/incubator-ftpserver/src/java/org/apache/ftpserver/FtpWriter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FtpWriter.java 31 Mar 2003 06:50:02 -0000 1.1
+++ FtpWriter.java 28 Jul 2004 16:09:24 -0000 1.2
@@ -74,12 +74,12 @@
private OutputStreamWriter mOriginalWriter;
private SpyConnectionInterface mSpy;
- private FtpConfig mConfig;
+ private AvalonFtpConfig mConfig;
/**
* Constructor - set the actual writer object
*/
- public FtpWriter(Socket soc, FtpConfig config) throws IOException {
+ public FtpWriter(Socket soc, AvalonFtpConfig config) throws IOException {
mOriginalWriter = new OutputStreamWriter(soc.getOutputStream());
mConfig = config;
}
1.3 +3 -3
incubator-ftpserver/src/java/org/apache/ftpserver/SiteCommandHandler.java
Index: SiteCommandHandler.java
===================================================================
RCS file:
/home/cvs/incubator-ftpserver/src/java/org/apache/ftpserver/SiteCommandHandler.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SiteCommandHandler.java 23 Jun 2003 16:38:43 -0000 1.2
+++ SiteCommandHandler.java 28 Jul 2004 16:09:24 -0000 1.3
@@ -86,14 +86,14 @@
protected final static Class[] INPUT_SIG = new Class[] {String[].class,
FtpRequest.class};
- private FtpConfig mConfig;
+ private AvalonFtpConfig mConfig;
private FtpUser mUser;
/**
* Constructor - set the configuration object
*/
- public SiteCommandHandler(FtpConfig cfg, FtpUser user) {
+ public SiteCommandHandler(AvalonFtpConfig cfg, FtpUser user) {
mConfig = cfg;
mUser = user;
}
1.1
incubator-ftpserver/src/java/org/apache/ftpserver/AvalonFtpConfig.java
Index: AvalonFtpConfig.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 1997-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software
* itself, if and wherever such third-party acknowledgments
* normally appear.
*
* 4. The names "Incubator", "FtpServer", and "Apache Software Foundation"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: AvalonFtpConfig.java,v 1.1 2004/07/28 16:09:24 hammant Exp $
*/
package org.apache.ftpserver;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.ftpserver.ip.IpRestrictorInterface;
import org.apache.ftpserver.remote.RemoteHandler;
import org.apache.ftpserver.usermanager.UserManagerInterface;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.StringTokenizer;
/**
* Ftp configuration class. It has all ftp server configuration
* parameters. This is not hot-editable. parameters will be loaded
* once during server startup. We can add our own config parameters.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Rana Bhattacharyya</a>
*/
public
class AvalonFtpConfig extends AbstractFtpConfig {
protected Configuration mConf = null;
protected Context mContext = null;
protected Logger mLogger = null;
public AvalonFtpConfig() throws IOException {
}
/**
* Set logger - second step.
*/
public void setLogger(Logger logger) {
mLogger = logger;
}
/**
* Set context - third step.
*/
public void setContext(Context ctx) {
mContext = ctx;
}
/**
* Set component manager - fourth step.
* TODO - case for more blocks here? - PJH
*/
public void setServiceManager(ServiceManager serviceManager) throws
ServiceException {
mIpRestrictor =
(IpRestrictorInterface)serviceManager.lookup(IpRestrictorInterface.ROLE);
mUserManager =
(UserManagerInterface)serviceManager.lookup(UserManagerInterface.ROLE);
}
/**
* Set configuration - fifth step.
*/
public void setConfiguration(Configuration conf) throws Exception {
mConf = conf;
Configuration tmpConf = null;
// get server address
tmpConf = conf.getChild("server-host", false);
if(tmpConf != null) {
mServerAddress = InetAddress.getByName(tmpConf.getValue());
}
// get self address
tmpConf = conf.getChild("self-host", false);
if(tmpConf != null) {
mSelfAddress = InetAddress.getByName(tmpConf.getValue());
}
// get server port
miServerPort = 21;
tmpConf = conf.getChild("ftp-port", false);
if(tmpConf != null) {
miServerPort = tmpConf.getValueAsInteger(miServerPort);
}
// get maximum number of connections
miMaxLogin = 20;
tmpConf = conf.getChild("max-connection", false);
if(tmpConf != null) {
miMaxLogin = tmpConf.getValueAsInteger(miMaxLogin);
}
// get anonymous login allow flag
mbAnonAllowed = true;
tmpConf = conf.getChild("anonymous-login-allowed", false);
if(tmpConf != null) {
mbAnonAllowed = tmpConf.getValueAsBoolean(mbAnonAllowed);
}
// get maximum number of anonymous connections
miAnonLogin = 10;
tmpConf = conf.getChild("anonymous-max-connection", false);
if(tmpConf != null) {
miAnonLogin = tmpConf.getValueAsInteger(miAnonLogin);
}
// get scheduler interval
miPollInterval = 120;
tmpConf = conf.getChild("poll-interval", false);
if(tmpConf != null) {
miPollInterval = tmpConf.getValueAsInteger(miPollInterval);
}
// get rmi port
miRmiPort = java.rmi.registry.Registry.REGISTRY_PORT;
tmpConf = conf.getChild("remote-admin-port", false);
if(tmpConf != null) {
miRmiPort = tmpConf.getValueAsInteger(miRmiPort);
}
// get remote admin allow flag
mbRemoteAdminAllowed = true;
tmpConf = conf.getChild("remote-admin-allowed", false);
if(tmpConf != null) {
mbRemoteAdminAllowed =
tmpConf.getValueAsBoolean(mbRemoteAdminAllowed);
}
// get autometic user home creation flag
mbCreateHome = false;
tmpConf = conf.getChild("create-user-home", false);
if(tmpConf != null) {
mbCreateHome = tmpConf.getValueAsBoolean(mbCreateHome);
}
// get default idle time
miDefaultIdle = 300;
tmpConf = conf.getChild("default-idle-time", false);
if(tmpConf != null) {
miDefaultIdle = tmpConf.getValueAsInteger(miDefaultIdle);
}
// get default root
String defaultRoot = "/";
tmpConf = conf.getChild("default-user-root", false);
if(tmpConf != null) {
defaultRoot = tmpConf.getValue(defaultRoot);
}
mDefaultRoot = new File(defaultRoot);
// get data port number
String dataPort = "0";
tmpConf = conf.getChild("data-port-pool", false);
if(tmpConf != null) {
dataPort = tmpConf.getValue(dataPort);
}
StringTokenizer st = new StringTokenizer(dataPort, ", \t\n\r\f");
miDataPort = new int[st.countTokens()][2];
for(int i=0; i<miDataPort.length; i++) {
miDataPort[i][0] = Integer.parseInt(st.nextToken());
miDataPort[i][1] = 0;
}
// get host addresses
if (mSelfAddress == null) {
mSelfAddress = InetAddress.getLocalHost();
}
if(mServerAddress == null) {
mServerAddress = mSelfAddress;
}
mStatistics = new FtpStatistics(this, new
AvalonFileMonitor(getLogger()));
mConService = new ConnectionService(this, new
AvalonConnectionMonitor(getLogger()));
if (mbRemoteAdminAllowed) {
mRemoteHandler = new RemoteHandler(this, new
AvalonFtpRemoteHandlerMonitor(getLogger()));
}
}
/**
* Get data port. Data port number zero (0) means that
* any available port will be used.
*/
public int getDataPort() {
synchronized(miDataPort) {
int dataPort = -1;
int loopTimes = 2;
Thread currThread = Thread.currentThread();
while( (dataPort==-1) && (--loopTimes >= 0) &&
(!currThread.isInterrupted()) ) {
// search for a free port
for(int i=0; i<miDataPort.length; i++) {
if(miDataPort[i][1] == 0) {
if(miDataPort[i][0] != 0) {
miDataPort[i][1] = 1;
}
dataPort = miDataPort[i][0];
break;
}
}
// no available free port - wait for the release notification
if(dataPort == -1) {
try {
miDataPort.wait();
}
catch(InterruptedException ex) {
}
}
}
return dataPort;
}
}
/**
* Release data port
*/
public void releaseDataPort(int port) {
synchronized(miDataPort) {
for(int i=0; i<miDataPort.length; i++) {
if(miDataPort[i][0] == port) {
miDataPort[i][1] = 0;
break;
}
}
miDataPort.notify();
}
}
/**
* Get logger
*/
public Logger getLogger() {
return mLogger;
}
/**
* Close this config and all the related resources. Ftp server
* <code>FtpServer.stop()</code> method will call this method.
*/
public void dispose() {
// close remote handler
if (mRemoteHandler != null) {
mRemoteHandler.dispose();
mRemoteHandler = null;
}
// close connection service
if(mConService != null) {
mConService.dispose();
mConService = null;
}
// close message queue
if (mQueue != null) {
mQueue.stop();
mQueue = null;
}
}
}
1.3 +2 -69
incubator-ftpserver/src/java/org/apache/ftpserver/ip/AbstractIpRestrictor.java
Index: AbstractIpRestrictor.java
===================================================================
RCS file:
/home/cvs/incubator-ftpserver/src/java/org/apache/ftpserver/ip/AbstractIpRestrictor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbstractIpRestrictor.java 25 Oct 2003 19:49:52 -0000 1.2
+++ AbstractIpRestrictor.java 28 Jul 2004 16:09:24 -0000 1.3
@@ -59,63 +59,17 @@
import java.io.IOException;
-import org.apache.avalon.framework.activity.Disposable;
-import org.apache.avalon.framework.activity.Initializable;
-import org.apache.avalon.framework.configuration.Configurable;
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.context.Context;
-import org.apache.avalon.framework.context.ContextException;
-import org.apache.avalon.framework.context.Contextualizable;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
-
-
/**
* Abstract ip restrictor class.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Rana Bhattacharyya</a>
*/
public
-abstract class AbstractIpRestrictor extends AbstractLogEnabled
- implements IpRestrictorInterface,
- Contextualizable,
- Configurable,
- Initializable,
- Disposable {
+abstract class AbstractIpRestrictor implements IpRestrictorInterface {
- protected Configuration mConfig;
- protected Context mContext;
protected boolean mbAllowIp;
/**
- * Set context object - first step.
- */
- public void contextualize(Context context) throws ContextException {
- mContext = context;
- }
-
- /**
- * Get application context.
- */
- public Context getContext() {
- return mContext;
- }
-
- /**
- * Configure user manager - third step.
- */
- public void configure(Configuration config) throws
ConfigurationException {
- mConfig = config;
-
- // get server address
- Configuration tmpConf = mConfig.getChild("allow-ip", false);
- mbAllowIp = false;
- if(tmpConf != null) {
- mbAllowIp = tmpConf.getValueAsBoolean(mbAllowIp);
- }
- }
-
- /**
* Get allow/ban IP flag.
*/
public boolean isAllowIp() {
@@ -123,30 +77,9 @@
}
/**
- * Get config object.
- */
- public Configuration getConfig() {
- return mConfig;
- }
-
-
- /**
- * Initialize - fourth step.
- */
- public void initialize() throws Exception {
- }
-
- /**
* Reload banned ip list - dummy implementation
*/
public void reload() throws IOException {
- }
-
- /**
- * Close user manager - dummy implementation.
- */
- public void dispose() {
- getLogger().info("Closing ip restrictor...");
}
}
1.1
incubator-ftpserver/src/java/org/apache/ftpserver/ip/AvalonFileIpRestrictor.java
Index: AvalonFileIpRestrictor.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 1997-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software
* itself, if and wherever such third-party acknowledgments
* normally appear.
*
* 4. The names "Incubator", "FtpServer", and "Apache Software Foundation"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: AvalonFileIpRestrictor.java,v 1.1 2004/07/28 16:09:24 hammant Exp $
*/
package org.apache.ftpserver.ip;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.ftpserver.util.IoUtils;
import org.apache.ftpserver.util.RegularExpr;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Collection;
import java.util.Vector;
/**
* This class provides IP restriction functionality.
*
* @phoenix:block
* @phoenix:service name="org.apache.ftpserver.ip.IpRestrictorInterface"
*
* @author <a href="mailto:[EMAIL PROTECTED]">Rana Bhattacharyya</a>
*/
public class AvalonFileIpRestrictor extends AbstractIpRestrictor implements
Contextualizable, Configurable, LogEnabled {
private static final String LINE_SEP =
System.getProperty("line.separator", "\n");
private File mIpFile = null;
private Vector mAllEntries = new Vector();
private Logger logger;
public void enableLogging(Logger logger) {
this.logger = logger;
}
/**
* Set application context.
*/
public void contextualize(Context context) throws ContextException {
File appDir = (File)context.get("app.home");
if(!appDir.exists()) {
appDir.mkdirs();
}
mIpFile = new File(appDir, "ip.properties" );
try {
reload();
}
catch(IOException ex) {
logger.error("IpRestrictor:contextualize()", ex);
throw new ContextException("IpRestrictor:contextualize()", ex);
}
logger.info("IP restrictor file = " + mIpFile);
}
/**
* Read the list from the file.
*/
public synchronized void reload() throws IOException {
BufferedReader br = null;
Vector newEntries = new Vector();
try {
if (mIpFile.exists()) {
br = IoUtils.getBufferedReader(new FileReader(mIpFile));
String line = null;
while((line = br.readLine()) != null) {
line = line.trim();
if(!line.equals("")) {
newEntries.add(line);
}
}
}
mAllEntries = newEntries;
}
finally {
IoUtils.close(br);
}
}
/**
* Get IP resrictor file object.
*/
public File getFile() {
return mIpFile;
}
/**
* Save this IP restriction list.
*/
public synchronized void save() throws IOException {
FileWriter fw = null;
try {
fw = new FileWriter(mIpFile);
Object[] entries = mAllEntries.toArray();
for(int i=entries.length; --i>=0; ) {
fw.write(entries[i].toString());
fw.write(LINE_SEP);
}
}
finally {
IoUtils.close(fw);
}
}
/**
* Check IP permission. Compare it with all the entries in the list.
*/
public boolean hasPermission(InetAddress addr) {
boolean bMatch = false;
Object[] entries = mAllEntries.toArray();
for(int i=entries.length; --i>=0; ) {
RegularExpr regExp = new RegularExpr(entries[i].toString());
bMatch = regExp.isMatch(addr.getHostAddress());
if(bMatch) {
break;
}
}
if (isAllowIp()) {
return bMatch;
}
else {
return !bMatch;
}
}
/**
* Add a new entry.
*/
public void addEntry(String entry) {
entry = entry.trim();
if(entry.equals("")) {
return;
}
mAllEntries.add(entry);
}
/**
* Remove entry
*/
public void removeEntry(String entry) {
mAllEntries.remove(entry);
}
/**
* Get all entries
*/
public Collection getAllEntries() {
return (Collection)mAllEntries.clone();
}
/**
* Remove all entries
*/
public void clear() {
mAllEntries.clear();
}
/**
* Configure user manager - third step.
*/
public void configure(Configuration config) throws ConfigurationException
{
// get server address
Configuration tmpConf = config.getChild("allow-ip", false);
mbAllowIp = false;
if(tmpConf != null) {
mbAllowIp = tmpConf.getValueAsBoolean(mbAllowIp);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]