| 1 |
//---PACKAGE DECLARATION--- |
| 2 |
//package uk.org.iscream.cms.server.util; |
| 3 |
|
| 4 |
//---IMPORTS--- |
| 5 |
import uk.org.iscream.cms.server.util.*; |
| 6 |
import java.net.ServerSocket; |
| 7 |
import java.net.Socket; |
| 8 |
import java.net.InetAddress; |
| 9 |
import java.io.IOException; |
| 10 |
|
| 11 |
/** |
| 12 |
* Access Control List ServerSocket wrapper. Used |
| 13 |
* to wrap a ServerSocket with an ACL, whilst extending |
| 14 |
* ServerSocket to make integration into code easier. |
| 15 |
* Once the ACL has been set, accept() can be used exactly |
| 16 |
* as it would be with a ServerSocket. |
| 17 |
* |
| 18 |
* @author $Author: tdb $ |
| 19 |
* @version $Id: ACLServerSocket.java,v 1.2 2001/12/20 00:59:54 tdb Exp $ |
| 20 |
*/ |
| 21 |
public class ACLServerSocket extends ServerSocket { |
| 22 |
|
| 23 |
//---FINAL ATTRIBUTES--- |
| 24 |
|
| 25 |
/** |
| 26 |
* The current CVS revision of this class |
| 27 |
*/ |
| 28 |
public static final String REVISION = "$Revision: 1.2 $"; |
| 29 |
|
| 30 |
//---STATIC METHODS--- |
| 31 |
|
| 32 |
//---CONSTRUCTORS--- |
| 33 |
|
| 34 |
/** |
| 35 |
* See relevant ServerSocket constructor. |
| 36 |
*/ |
| 37 |
public ACLServerSocket(int port) throws IOException { |
| 38 |
super(port); |
| 39 |
setACL(new ACL()); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* See relevant ServerSocket constructor. |
| 44 |
*/ |
| 45 |
public ACLServerSocket(int port, int backlog) throws IOException { |
| 46 |
super(port, backlog); |
| 47 |
setACL(new ACL()); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* See relevant ServerSocket constructor. |
| 52 |
*/ |
| 53 |
public ACLServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { |
| 54 |
super(port, backlog, bindAddr); |
| 55 |
setACL(new ACL()); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* See relevant ServerSocket constructor, and |
| 60 |
* sets the default acl. |
| 61 |
*/ |
| 62 |
public ACLServerSocket(ACL acl, int port) throws IOException { |
| 63 |
super(port); |
| 64 |
setACL(acl); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* See relevant ServerSocket constructor, and |
| 69 |
* sets the default acl. |
| 70 |
*/ |
| 71 |
public ACLServerSocket(ACL acl, int port, int backlog) throws IOException { |
| 72 |
super(port, backlog); |
| 73 |
setACL(acl); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* See relevant ServerSocket constructor, and |
| 78 |
* sets the default acl. |
| 79 |
*/ |
| 80 |
public ACLServerSocket(ACL acl, int port, int backlog, InetAddress bindAddr) throws IOException { |
| 81 |
super(port, backlog, bindAddr); |
| 82 |
setACL(acl); |
| 83 |
} |
| 84 |
|
| 85 |
//---PUBLIC METHODS--- |
| 86 |
|
| 87 |
/** |
| 88 |
* Essentially has the same behaviour as the |
| 89 |
* ServerSocket.accept() method, except won't |
| 90 |
* return a Socket unless it is permitted by |
| 91 |
* the ACL. Closes Socket's immediately that |
| 92 |
* aren't permitted by the ACL. |
| 93 |
*/ |
| 94 |
public Socket accept() throws IOException { |
| 95 |
while(true) { |
| 96 |
Socket s = super.accept(); |
| 97 |
if(_acl.check(s.getInetAddress())) { |
| 98 |
return s; |
| 99 |
} |
| 100 |
else { |
| 101 |
s.close(); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set the ACL at any point in the operation |
| 108 |
* of the ServerSocket. |
| 109 |
* |
| 110 |
* @param acl the new ACL to apply |
| 111 |
*/ |
| 112 |
public void setACL(ACL acl) { |
| 113 |
_acl = acl; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 118 |
* method to provide clean logging (every class should have this). |
| 119 |
* |
| 120 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
| 121 |
* to format the toString() |
| 122 |
* |
| 123 |
* @return the name of this class and its CVS revision |
| 124 |
*/ |
| 125 |
public String toString() { |
| 126 |
return FormatName.getName( |
| 127 |
_name, |
| 128 |
getClass().getName(), |
| 129 |
REVISION) + ":" + super.toString(); |
| 130 |
} |
| 131 |
|
| 132 |
//---PRIVATE METHODS--- |
| 133 |
|
| 134 |
//---ACCESSOR/MUTATOR METHODS--- |
| 135 |
|
| 136 |
//---ATTRIBUTES--- |
| 137 |
|
| 138 |
/** |
| 139 |
* This is the friendly identifier of the |
| 140 |
* component this class is running in. |
| 141 |
* eg, a Filter may be called "filter1", |
| 142 |
* If this class does not have an owning |
| 143 |
* component, a name from the configuration |
| 144 |
* can be placed here. This name could also |
| 145 |
* be changed to null for utility classes. |
| 146 |
*/ |
| 147 |
private String _name = null; |
| 148 |
|
| 149 |
/** |
| 150 |
* The ACL used by this ACLServerSocket. |
| 151 |
*/ |
| 152 |
private ACL _acl; |
| 153 |
|
| 154 |
//---STATIC ATTRIBUTES--- |
| 155 |
|
| 156 |
//---INNER CLASSES--- |
| 157 |
|
| 158 |
} |