1 /*
   2  * Copyright (c) 2001, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.jmx.snmp.internal;
  26 
  27 import java.util.Hashtable;
  28 import com.sun.jmx.snmp.SnmpEngineId;
  29 import com.sun.jmx.snmp.SnmpUnknownModelLcdException;
  30 import com.sun.jmx.snmp.SnmpUnknownSubSystemException;
  31 /**
  32  * Class to extend in order to develop a customized Local Configuration Datastore. The Lcd is used by the <CODE>SnmpEngine</CODE> to store and retrieve data.
  33  *<P> <CODE>SnmpLcd</CODE> manages the Lcds needed by every {@link com.sun.jmx.snmp.internal.SnmpModel SnmpModel}. It is possible to add and remove {@link com.sun.jmx.snmp.internal.SnmpModelLcd SnmpModelLcd}.</P>
  34  * <p><b>This API is a Sun Microsystems internal API  and is subject
  35  * to change without notice.</b></p>
  36  * @since 1.5
  37  */
  38 public abstract class SnmpLcd {
  39 
  40     class SubSysLcdManager {
  41         private Hashtable<Integer, SnmpModelLcd> models =
  42                 new Hashtable<Integer, SnmpModelLcd>();
  43 
  44         public void addModelLcd(int id,
  45                                 SnmpModelLcd usmlcd) {
  46             models.put(id, usmlcd);
  47         }
  48 
  49         public SnmpModelLcd getModelLcd(int id) {
  50             return models.get(id);
  51         }
  52 
  53         public SnmpModelLcd removeModelLcd(int id) {
  54             return models.remove(id);
  55         }
  56     }
  57 
  58 
  59     private Hashtable<SnmpSubSystem, SubSysLcdManager> subs =
  60             new Hashtable<SnmpSubSystem, SubSysLcdManager>();
  61 
  62     /**
  63      * Returns the number of time the engine rebooted.
  64      * @return The number of reboots or -1 if the information is not present in the Lcd.
  65      */
  66     public abstract int getEngineBoots();
  67     /**
  68      * Returns the engine Id located in the Lcd.
  69      * @return The engine Id or null if the information is not present in the Lcd.
  70      */
  71     public abstract String getEngineId();
  72 
  73     /**
  74      * Persists the number of reboots.
  75      * @param i Reboot number.
  76      */
  77     public abstract void storeEngineBoots(int i);
  78 
  79     /**
  80      * Persists the engine Id.
  81      * @param id The engine Id.
  82      */
  83     public abstract void  storeEngineId(SnmpEngineId id);
  84     /**
  85      * Adds an Lcd model.
  86      * @param sys The subsytem managing the model.
  87      * @param id The model Id.
  88      * @param lcd The Lcd model.
  89      */
  90     public void addModelLcd(SnmpSubSystem sys,
  91                             int id,
  92                             SnmpModelLcd lcd) {
  93 
  94         SubSysLcdManager subsys = subs.get(sys);
  95         if( subsys == null ) {
  96             subsys = new SubSysLcdManager();
  97             subs.put(sys, subsys);
  98         }
  99 
 100         subsys.addModelLcd(id, lcd);
 101     }
 102      /**
 103      * Removes an Lcd model.
 104      * @param sys The subsytem managing the model.
 105      * @param id The model Id.
 106      */
 107     public void removeModelLcd(SnmpSubSystem sys,
 108                                 int id)
 109         throws SnmpUnknownModelLcdException, SnmpUnknownSubSystemException {
 110 
 111         SubSysLcdManager subsys = subs.get(sys);
 112         if( subsys != null ) {
 113             SnmpModelLcd lcd = subsys.removeModelLcd(id);
 114             if(lcd == null) {
 115                 throw new SnmpUnknownModelLcdException("Model : " + id);
 116             }
 117         }
 118         else
 119             throw new SnmpUnknownSubSystemException(sys.toString());
 120     }
 121 
 122     /**
 123      * Gets an Lcd model.
 124      * @param sys The subsytem managing the model
 125      * @param id The model Id.
 126      * @return The Lcd model or null if no Lcd model were found.
 127      */
 128     public SnmpModelLcd getModelLcd(SnmpSubSystem sys,
 129                                     int id) {
 130         SubSysLcdManager subsys = subs.get(sys);
 131 
 132         if(subsys == null) return null;
 133 
 134         return subsys.getModelLcd(id);
 135     }
 136 }