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 2001-2002 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 package com.sun.dhcpmgr.cli.dhcpconfig;
  29 
  30 import java.text.MessageFormat;
  31 
  32 import com.sun.dhcpmgr.cli.common.DhcpCliFunction;
  33 import com.sun.dhcpmgr.cli.common.DhcpCliPrint;
  34 import com.sun.dhcpmgr.data.*;
  35 import com.sun.dhcpmgr.bridge.BridgeException;
  36 import com.sun.dhcpmgr.bridge.ExistsException;
  37 
  38 import com.sun.dhcpmgr.common.Importer;
  39 import com.sun.dhcpmgr.common.ImportController;
  40 
  41 /**
  42  * The main class for the "import move data" functionality of dhcpconfig.
  43  */
  44 public class ImportData extends DhcpCfgFunction implements Importer {
  45 
  46     /**
  47      * The valid options associated with importing data.
  48      */
  49     private static final int supportedOptions[] = {
  50         DhcpCfg.FORCE,
  51         DhcpCfg.SIGHUP
  52     };
  53 
  54     /**
  55      * The name of the import file.
  56      */
  57     private String importFile;
  58 
  59     /**
  60      * Simple constructor
  61      */
  62     public ImportData(String importFile) {
  63 
  64         validOptions = supportedOptions;
  65         this.importFile = importFile;
  66 
  67     } // constructor
  68 
  69     /**
  70      * Returns the option flag for this function.
  71      * @returns the option flag for this function.
  72      */
  73     public int getFunctionFlag() {
  74         return (DhcpCfg.IMPORT_DATA);
  75     }
  76 
  77     /**
  78      * Executes the "import move data" functionality.
  79      * @return DhcpCfg.SUCCESS or DhcpCfg.FAILURE
  80      */
  81     public int execute() {
  82 
  83         // Make sure that server is configured as a DHCP server.
  84         //
  85         if (!isServerConfigured()) {
  86             return (DhcpCfg.FAILURE);
  87         }
  88 
  89         // Check the validity of the data store version.
  90         //
  91         if (!isVersionValid(false)) {
  92             return (DhcpCfg.FAILURE);
  93         }
  94 
  95         // Shall we overwrite any existing, conflicting data?
  96         boolean force = options.isSet(DhcpCfg.FORCE);
  97 
  98         // Create import controller and do the import
  99         ImportController controller = new ImportController(this, getDhcpMgr());
 100         controller.setFile(importFile);
 101         if (!controller.importData(force)) {
 102             return (DhcpCfg.FAILURE);
 103         }
 104 
 105         // Signal server if user requested
 106         try {
 107             if (options.isSet(DhcpCfg.SIGHUP)) {
 108                 getSvcMgr().reload();
 109             }
 110         } catch (Throwable e) {
 111             printErrMessage(getString("sighup_failed"));
 112             return (DhcpCfg.FAILURE);
 113         }
 114 
 115         return (DhcpCfg.SUCCESS);
 116 
 117     } // execute
 118 
 119     public void initializeProgress(int length) {
 120         // Do nothing
 121     }
 122 
 123     public void updateProgress(int done, String message) {
 124         // Just print the message
 125         printMessage(message);
 126     }
 127 
 128     public void displayError(String message) {
 129         Object [] arguments = new Object[1];
 130         arguments[0] = message;
 131         printErrMessage(getString("import_error_msg"), arguments);
 132     }
 133 
 134     public void displayErrors(String msg, String label, ActionError [] errs) {
 135         printErrMessage(msg);
 136         String [] args = new String[3];
 137         args[0] = label;
 138         MessageFormat form =
 139             new MessageFormat(getString("import_action_error"));
 140         for (int i = 0; i < errs.length; ++i) {
 141             args[1] = errs[i].getName();
 142             args[2] = errs[i].getException().getMessage();
 143             printErrMessage(form.format(args));
 144         }
 145     }
 146 
 147 } // ImportData