KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > file > FileIPRange


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: FileIPRange.java 169164 2005-05-08 21:12:53Z gregor $ */
19
20 package org.apache.lenya.ac.file;
21
22 import java.io.File JavaDoc;
23 import java.io.Serializable JavaDoc;
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 /**
36  * IP range that is stored in a file.
37  */

38 public class FileIPRange extends AbstractIPRange implements Serializable JavaDoc {
39
40     /**
41      * Main method.
42      *
43      * @param args
44      * The command-line arguments.
45      * @deprecated This should bemoved to a JUnit test.
46      */

47     public static void main(String JavaDoc[] 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 JavaDoc e) {
63             System.err.println(e);
64         }
65     }
66
67     /**
68      * Ctor.
69      */

70     public FileIPRange() {
71     }
72
73     /**
74      * Ctor.
75      *
76      * @param configurationDirectory
77      * The configuration directory.
78      * @param id
79      * The IP range ID.
80      */

81     public FileIPRange(File JavaDoc configurationDirectory, String JavaDoc id) {
82         super(id);
83         setConfigurationDirectory(configurationDirectory);
84     }
85
86     /**
87      * @see org.apache.lenya.ac.impl.AbstractIPRange#save()
88      */

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 JavaDoc e) {
96             throw new AccessControlException(e);
97         }
98     }
99
100     /**
101      * Returns the configuration file.
102      *
103      * @return A file object.
104      */

105     protected File JavaDoc getFile() {
106         File JavaDoc xmlPath = getConfigurationDirectory();
107         File JavaDoc xmlFile = new File JavaDoc(xmlPath, getId() + FileIPRangeManager.SUFFIX);
108         return xmlFile;
109     }
110
111     /**
112      * @see org.apache.lenya.ac.Item#configure(org.apache.avalon.framework.configuration.Configuration)
113      */

114     public void configure(Configuration config) throws ConfigurationException {
115         new ItemConfiguration().configure(this, config);
116
117         String JavaDoc networkAddress = config.getChild(ELEMENT_NETWORK_ADDRESS).getValue();
118         String JavaDoc 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 JavaDoc IP_RANGE = "ip-range";
130     public static final String JavaDoc ELEMENT_NETWORK_ADDRESS = "network-address";
131     public static final String JavaDoc ELEMENT_SUBNET_MASK = "subnet-mask";
132
133     /**
134      * Create a configuration from the current user details. Can be used for saving.
135      *
136      * @return a <code>Configuration</code>
137      */

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