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 (c) 1998-2001 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 package com.sun.dhcpmgr.data;
  29 
  30 import java.util.Vector;
  31 
  32 /**
  33  * This class provides the functionality to construct an option value of the
  34  * correct type when only the tag we associate with the option value is known.
  35  */
  36 public class OptionValueFactory {
  37     private static OptionsTable optionsTable = OptionsTable.getTable();
  38 
  39     /**
  40      * Construct an option value given the name, and initialize it to the
  41      * provided value.
  42      * @param name the name of the option
  43      * @param value the initial value for the option
  44      * @return an OptionValue of the correct type for this option.  If the name
  45      * or value supplied is invalid in some way, an instance of
  46      * BogusOptionValue is returned and the caller should take appropriate
  47      * action.
  48      */
  49     public static OptionValue newOptionValue(String name, Object value) {
  50         OptionValue v;
  51         try {
  52             v = newOptionValue(name);
  53             v.setValue(value);
  54         } catch (ValidationException e) {
  55             // Not a valid value; put it in the bogus value placeholder
  56             v = new BogusOptionValue(name, value);
  57         }
  58         return v;
  59     }
  60     
  61     /**
  62      * Construct an empty option value given the name
  63      * @param name the name of the option
  64      * @return an OptionValue of the correct type for this option.
  65      */
  66     public static OptionValue newOptionValue(String name) {
  67         if (name.length() == 0) {
  68             // Empty name is not acceptable
  69             return new BogusOptionValue(name);
  70         }
  71         Option opt = optionsTable.get(name);
  72         if (opt == null) {
  73             // Include is not in the options table
  74             if (name.equals("Include")) {
  75                 return new IncludeOptionValue();
  76             } else {
  77                 /*
  78                  * Bogus option name; create a bogus value that callers
  79                  * can pick up later.
  80                  */
  81                  return new BogusOptionValue(name);
  82             }
  83         }
  84 
  85         byte type = opt.getType();
  86         if (type == Option.types[Option.ASCII].getCode()) {
  87             return new AsciiOptionValue(name);
  88         } else if (type == Option.types[Option.BOOLEAN].getCode()) {
  89             return new BooleanOptionValue(name);
  90         } else if (type == Option.types[Option.IP].getCode()) {
  91             return new IPOptionValue(name);
  92         } else if (type == Option.types[Option.OCTET].getCode()) {
  93             return new OctetOptionValue(name);
  94         } else if (type == Option.types[Option.NUMBER].getCode() ||
  95             type == Option.types[Option.UNUMBER8].getCode() ||
  96             type == Option.types[Option.UNUMBER16].getCode() ||
  97             type == Option.types[Option.UNUMBER32].getCode() ||
  98             type == Option.types[Option.UNUMBER64].getCode() ||
  99             type == Option.types[Option.SNUMBER8].getCode() ||
 100             type == Option.types[Option.SNUMBER16].getCode() ||
 101             type == Option.types[Option.SNUMBER32].getCode() ||
 102             type == Option.types[Option.SNUMBER64].getCode()) {
 103             return new NumberOptionValue(name);
 104         } else {
 105             // This should never happen
 106             return new BogusOptionValue(name);
 107         }
 108     }
 109 }