rana_b 02/03/06 06:02:56
Added: ftpserver/src/java/org/apache/avalon/ftpserver/ip
IpRestrictor.java IpRestrictor.xinfo
IpRestrictorInterface.java
Log:
second stage of refactoring
Revision Changes Path
1.1
jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/ip/IpRestrictor.java
Index: IpRestrictor.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.ftpserver.ip;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Vector;
import java.util.Iterator;
import java.util.Collection;
import org.apache.avalon.phoenix.Block;
import org.apache.avalon.phoenix.BlockContext;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.ftpserver.util.IoUtils;
import org.apache.avalon.ftpserver.util.RegularExpr;
/**
* This class provides IP restriction functionality.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Rana Bhattacharyya</a>
*/
public
class IpRestrictor extends AbstractLogEnabled
implements IpRestrictorInterface,
Block,
Contextualizable {
private final static String LINE_SEP =
System.getProperty("line.separator", "\n");
private BlockContext mContext = null;
private File mIpFile = null;
private Vector mAllEntries = new Vector();
/**
* Default constructor.
*/
public IpRestrictor() {
}
/**
* Set application context.
*/
public void contextualize(Context context) throws ContextException {
mContext = (BlockContext)context;
mIpFile = new File(mContext.getBaseDirectory(), "ip.properties");
try {
reload();
}
catch(IOException ex) {
getLogger().error("IpRestrictor:contextualize()", ex);
ex.printStackTrace();
throw new ContextException("IpRestrictor:contextualize()", ex);
}
getLogger().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;
}
}
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();
}
}
1.1
jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/ip/IpRestrictor.xinfo
Index: IpRestrictor.xinfo
===================================================================
<?xml version="1.0"?>
<blockinfo>
<!-- section to describe IpRestrictor block -->
<block>
<version>1.0</version>
</block>
<!-- services that are offered by this block -->
<services>
<service name="org.apache.avalon.ftpserver.ip.IpRestrictorInterface"
version="1.0" />
</services>
</blockinfo>
1.1
jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/ip/IpRestrictorInterface.java
Index: IpRestrictorInterface.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.ftpserver.ip;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Collection;
/**
* IP Restrictor interface
*
* @author <a href="mailto:[EMAIL PROTECTED]">Rana Bhattacharyya</a>
*/
public
interface IpRestrictorInterface {
public final static String ROLE =
"org.apache.avalon.ftpserver.ip.IpRestrictorInterface";
/**
* Reload data from store.
*/
void reload() throws IOException;
/**
* Save data into store.
*/
void save() throws IOException;
/**
* Check IP permission.
*/
boolean hasPermission(InetAddress addr);
/**
* Clear all entries.
*/
void clear();
/**
* Add new entry
*/
void addEntry(String str);
/**
* Remove entry
*/
void removeEntry(String str);
/**
* Get all entries
*/
Collection getAllEntries();
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>