1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * ident        "%Z%%M% %I%     %E% SMI"
  24  *
  25  * Copyright 1998-2002 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 package com.sun.dhcpmgr.data;
  29 
  30 import java.net.*;
  31 import java.util.*;
  32 
  33 public class IPOptionValue extends OptionValue {
  34     private String name;
  35     private Vector addrs;
  36     private boolean valid;
  37     
  38     // Serialization id for this class
  39     static final long serialVersionUID = -7894568061270794048L;
  40 
  41     protected IPOptionValue(String name) {
  42         this.name = name;
  43         addrs = null;
  44         valid = false;
  45     }
  46     
  47     public void setValue(Object value) throws ValidationException {
  48         // Find option in option definition table in order to validate the data
  49         Option option = OptionsTable.getTable().get(name);
  50         if (option == null) {
  51             Object [] args = { name };
  52             throwException("invalid_option", args);
  53         }
  54         Vector newAddrs = new Vector();
  55         if (value instanceof String) {  
  56             if (((String)value).length() == 0) {
  57                 // Empty strings aren't acceptable
  58                 Object [] args = { name,
  59                     Option.getTypeDhcptabString(option.getType()) };
  60                 throwException("invalid_option_value", args);
  61             }    
  62             /*
  63              * Break string apart at whitespace and use it to construct
  64              * a vector of IPAddresses
  65              */
  66             StringTokenizer st = new StringTokenizer((String)value, " ");
  67             while (st.hasMoreTokens()) {
  68                 newAddrs.addElement(new IPAddress(st.nextToken()));
  69             }
  70         } else if (value instanceof InetAddress) {
  71             newAddrs.addElement(value);
  72         } else if (value instanceof IPAddress) {
  73             newAddrs.addElement(value);
  74         } else if (!(value instanceof Vector)) {
  75             // Can't handle anything else but a vector of addresses
  76             Object [] args = { name,
  77                 Option.getTypeDhcptabString(option.getType()) };
  78             throwException("invalid_option_value", args);
  79         } else {
  80             // Make sure vector only contains InetAddresses or IPAddresses
  81             newAddrs = (Vector)value;
  82             for (Enumeration en = newAddrs.elements(); en.hasMoreElements(); ) {
  83                 Object o = en.nextElement();
  84                 if (!(o instanceof InetAddress) && !(o instanceof IPAddress)) {
  85                     Object [] args = { name,
  86                         Option.getTypeDhcptabString(option.getType()) };
  87                     throwException("invalid_option_value", args);
  88                 }
  89             }
  90         }
  91         if ((newAddrs.size() % option.getGranularity()) != 0) {
  92             Object [] args = { name,
  93                 Integer.toString(option.getGranularity()) };
  94             throwException("invalid_option_granularity", args);
  95         }
  96         if ((option.getMaximum() != 0) &&
  97             (newAddrs.size() / option.getGranularity()) >
  98                 option.getMaximum()) {
  99             Object [] args = { name, Integer.toString(option.getMaximum()) };
 100             throwException("invalid_option_maximum", args);
 101         }
 102         
 103         addrs = newAddrs;
 104         valid = true;
 105     }
 106     
 107     public String getName() {
 108         return name;
 109     }
 110     
 111     public String getValue() {
 112         if (addrs == null || addrs.size() == 0) {
 113             return "";
 114         }
 115         StringBuffer buf = new StringBuffer();
 116         for (Enumeration en = addrs.elements(); en.hasMoreElements(); ) {
 117             Object o = en.nextElement();
 118             if (buf.length() != 0) {
 119                 buf.append(' ');
 120             }
 121             if (o instanceof IPAddress) {
 122                 buf.append(((IPAddress)o).getHostAddress());
 123             } else {
 124                 buf.append(((InetAddress)o).getHostAddress());
 125             }
 126         }
 127         return buf.toString();
 128     }
 129     
 130     public String toString() {
 131         return (getName() + "=" + getValue());
 132     }
 133     
 134     public boolean isValid() {
 135         return valid;
 136     }
 137     
 138     public Object clone() {
 139         IPOptionValue v = new IPOptionValue(name);
 140         if (addrs != null) {
 141                 v.addrs = (Vector)addrs.clone();
 142         }
 143         v.valid = valid;
 144         return v;
 145     }
 146 }