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