001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: FileIPRange.java 531479 2007-04-23 14:21:35Z andreas $ */
020:
021: package org.apache.lenya.ac.file;
022:
023: import java.io.File;
024: import java.io.Serializable;
025: import java.net.InetAddress;
026: import java.net.UnknownHostException;
027:
028: import org.apache.avalon.framework.configuration.Configuration;
029: import org.apache.avalon.framework.configuration.ConfigurationException;
030: import org.apache.avalon.framework.configuration.DefaultConfiguration;
031: import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
032: import org.apache.avalon.framework.logger.ConsoleLogger;
033: import org.apache.avalon.framework.logger.Logger;
034: import org.apache.lenya.ac.AccessControlException;
035: import org.apache.lenya.ac.ItemManager;
036: import org.apache.lenya.ac.Machine;
037: import org.apache.lenya.ac.impl.AbstractIPRange;
038: import org.apache.lenya.ac.impl.ItemConfiguration;
039: import org.apache.lenya.net.InetAddressUtil;
040:
041: /**
042: * IP range that is stored in a file.
043: */
044: public class FileIPRange extends AbstractIPRange implements
045: Serializable {
046:
047: /**
048: *
049: */
050: private static final long serialVersionUID = 1L;
051:
052: /**
053: * Main method.
054: * @param args The command-line arguments.
055: * @deprecated This should be moved to a JUnit test.
056: */
057: public static void main(String[] args) {
058: if (args.length == 0) {
059: System.out
060: .println("Usage: network, netmask, ip (e.g. 192.168.0.64 255.255.255.240 192.168.0.70)");
061: return;
062: }
063: try {
064: InetAddress networkAddress = InetAddress.getByName(args[0]);
065: InetAddress subnetMask = InetAddress.getByName(args[1]);
066: Machine machine = new Machine(args[2]);
067:
068: InetAddressUtil util = new InetAddressUtil(
069: new ConsoleLogger());
070: if (util.contains(networkAddress, subnetMask, machine
071: .getAddress())) {
072: System.out.println("true");
073: } else {
074: System.out.println("false");
075: }
076: } catch (final UnknownHostException e) {
077: System.err.println(e);
078: } catch (final AccessControlException e) {
079: System.err.println(e);
080: }
081: }
082:
083: /**
084: * Ctor.
085: * @param itemManager The item manager.
086: * @param logger The logger.
087: */
088: public FileIPRange(ItemManager itemManager, Logger logger) {
089: super (itemManager, logger);
090: }
091:
092: /**
093: * Ctor.
094: * @param itemManager The item manager.
095: * @param logger The logger.
096: * @param id The IP range ID.
097: */
098: public FileIPRange(ItemManager itemManager, Logger logger, String id) {
099: super (itemManager, logger, id);
100: FileItemManager fileItemManager = (FileItemManager) itemManager;
101: setConfigurationDirectory(fileItemManager
102: .getConfigurationDirectory());
103: }
104:
105: /**
106: * @see org.apache.lenya.ac.impl.AbstractIPRange#save()
107: */
108: public void save() throws AccessControlException {
109: DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer();
110: Configuration config = createConfiguration();
111:
112: try {
113: serializer.serializeToFile(getFile(), config);
114: } catch (Exception e) {
115: throw new AccessControlException(e);
116: }
117: }
118:
119: /**
120: * Returns the configuration file.
121: * @return A file object.
122: */
123: protected File getFile() {
124: File xmlPath = getConfigurationDirectory();
125: File xmlFile = new File(xmlPath, getId()
126: + FileIPRangeManager.SUFFIX);
127: return xmlFile;
128: }
129:
130: /**
131: * @see org.apache.lenya.ac.Item#configure(org.apache.avalon.framework.configuration.Configuration)
132: */
133: public void configure(Configuration config)
134: throws ConfigurationException {
135: new ItemConfiguration().configure(this , config);
136:
137: String networkAddress = config
138: .getChild(ELEMENT_NETWORK_ADDRESS).getValue();
139: String subnetMask = config.getChild(ELEMENT_SUBNET_MASK)
140: .getValue();
141:
142: try {
143: setNetworkAddress(networkAddress);
144: setSubnetMask(subnetMask);
145: } catch (AccessControlException e) {
146: throw new ConfigurationException("Configuring IP range ["
147: + getId() + "] failed: ", e);
148: }
149:
150: }
151:
152: protected static final String IP_RANGE = "ip-range";
153: protected static final String ELEMENT_NETWORK_ADDRESS = "network-address";
154: protected static final String ELEMENT_SUBNET_MASK = "subnet-mask";
155:
156: /**
157: * Create a configuration from the current user details. Can be used for saving.
158: * @return a <code>Configuration</code>
159: */
160: protected Configuration createConfiguration() {
161: DefaultConfiguration config = new DefaultConfiguration(IP_RANGE);
162: new ItemConfiguration().save(this , config);
163:
164: DefaultConfiguration networkAddressConfig = new DefaultConfiguration(
165: ELEMENT_NETWORK_ADDRESS);
166: networkAddressConfig.setValue(getNetworkAddress()
167: .getHostAddress());
168: config.addChild(networkAddressConfig);
169:
170: DefaultConfiguration subnetMaskConfig = new DefaultConfiguration(
171: ELEMENT_SUBNET_MASK);
172: subnetMaskConfig.setValue(getSubnetMask().getHostAddress());
173: config.addChild(subnetMaskConfig);
174:
175: return config;
176: }
177: }
|