1 17 18 19 20 package org.apache.lenya.ac.file; 21 22 import java.io.File ; 23 import java.io.Serializable ; 24 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.apache.avalon.framework.configuration.DefaultConfiguration; 28 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer; 29 import org.apache.lenya.ac.AccessControlException; 30 import org.apache.lenya.ac.IPRange; 31 import org.apache.lenya.ac.Machine; 32 import org.apache.lenya.ac.impl.AbstractIPRange; 33 import org.apache.lenya.ac.impl.ItemConfiguration; 34 35 38 public class FileIPRange extends AbstractIPRange implements Serializable { 39 40 47 public static void main(String [] args) { 48 if (args.length == 0) { 49 System.out.println( 50 "Usage: network, netmask, ip (e.g. 192.168.0.64 255.255.255.240 192.168.0.70)"); 51 return; 52 } 53 IPRange ipr = new FileIPRange(); 54 try { 55 ipr.setNetworkAddress(args[0]); 56 ipr.setSubnetMask(args[1]); 57 if (ipr.contains(new Machine(args[2]))) { 58 System.out.println("true"); 59 } else { 60 System.out.println("false"); 61 } 62 } catch (Exception e) { 63 System.err.println(e); 64 } 65 } 66 67 70 public FileIPRange() { 71 } 72 73 81 public FileIPRange(File configurationDirectory, String id) { 82 super(id); 83 setConfigurationDirectory(configurationDirectory); 84 } 85 86 89 public void save() throws AccessControlException { 90 DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer(); 91 Configuration config = createConfiguration(); 92 93 try { 94 serializer.serializeToFile(getFile(), config); 95 } catch (Exception e) { 96 throw new AccessControlException(e); 97 } 98 } 99 100 105 protected File getFile() { 106 File xmlPath = getConfigurationDirectory(); 107 File xmlFile = new File (xmlPath, getId() + FileIPRangeManager.SUFFIX); 108 return xmlFile; 109 } 110 111 114 public void configure(Configuration config) throws ConfigurationException { 115 new ItemConfiguration().configure(this, config); 116 117 String networkAddress = config.getChild(ELEMENT_NETWORK_ADDRESS).getValue(); 118 String subnetMask = config.getChild(ELEMENT_SUBNET_MASK).getValue(); 119 120 try { 121 setNetworkAddress(networkAddress); 122 setSubnetMask(subnetMask); 123 } catch (AccessControlException e) { 124 throw new ConfigurationException("Configuring IP range [" + getId() + "] failed: ", e); 125 } 126 127 } 128 129 public static final String IP_RANGE = "ip-range"; 130 public static final String ELEMENT_NETWORK_ADDRESS = "network-address"; 131 public static final String ELEMENT_SUBNET_MASK = "subnet-mask"; 132 133 138 protected Configuration createConfiguration() { 139 DefaultConfiguration config = new DefaultConfiguration(IP_RANGE); 140 new ItemConfiguration().save(this, config); 141 142 DefaultConfiguration networkAddressConfig = 143 new DefaultConfiguration(ELEMENT_NETWORK_ADDRESS); 144 networkAddressConfig.setValue(getNetworkAddress().getHostAddress()); 145 config.addChild(networkAddressConfig); 146 147 DefaultConfiguration subnetMaskConfig = new DefaultConfiguration(ELEMENT_SUBNET_MASK); 148 subnetMaskConfig.setValue(getSubnetMask().getHostAddress()); 149 config.addChild(subnetMaskConfig); 150 151 return config; 152 } 153 } 154 | Popular Tags |