View Javadoc

1   package org.jscsi.target.settings.entry;
2   
3   
4   import org.jscsi.target.TargetServer;
5   import org.jscsi.target.settings.KeySet;
6   import org.jscsi.target.settings.NegotiationStatus;
7   import org.jscsi.target.settings.NegotiationType;
8   import org.jscsi.target.settings.TextParameter;
9   
10  
11  /**
12   * This {@link Entry} sub-class is used for managing and negotiation String parameters. The <i>value</i> part of the
13   * corresponding <i>key-value</i> pairs can be made up of one or several comma-separated strings.
14   * <p>
15   * The default or negotiated value can be accessed via the {@link #getStringValue()} method.
16   * 
17   * @see Entry
18   * @author Andreas Ergenzinger
19   */
20  public final class StringEntry extends Entry {
21  
22      /**
23       * Contains all values supported by the jSCSI Target.
24       */
25      private final String[] supportedValues;
26  
27      /**
28       * The {@link StringEntry} constructor.
29       * 
30       * @param keySet contains all relevant keys
31       * @param negotiationType declared or negotiated
32       * @param use determines under which circumstances the parameter may be negotiated
33       * @param negotiationStatus indicates whether there is a default value or if the parameter must be negotiated
34       * @param supportedValues all values supported by the jSCSI Target
35       * @param defaultValue the default value or <code>null</code>
36       */
37      public StringEntry (final KeySet keySet, final NegotiationType negotiationType, final Use use, final NegotiationStatus negotiationStatus, final String[] supportedValues, final Object defaultValue) {
38          super(keySet, negotiationType, use, negotiationStatus, defaultValue);
39          this.supportedValues = supportedValues;
40      }
41  
42      @Override
43      protected boolean inProtocolValueRange (Object values) {
44          // receives a String array
45          // (size of array has already been checked in parseOffer(...))
46          final String[] vals = (String[]) values;
47          // check for illegal characters
48          for (String s : vals)
49              if (!TextParameter.checkTextValueFormat(s)) return false;
50          return true;
51      }
52  
53      @Override
54      protected Object parseOffer (TargetServer target, String values) {
55  
56          // receives a comma-separated list of string values (or a single value)
57          // enforce that declaration consists of exactly one value
58          final String[] split = TextParameter.splitValues(values);
59          if (negotiationType == NegotiationType.DECLARED && split.length > 1) return null;// protocol error
60          return split;
61      }
62  
63      @Override
64      protected void processDeclaration (Object values) {
65          // receives a String array or length 1
66          value = ((String[]) values)[0];
67      }
68  
69      @Override
70      protected String processNegotiation (Object values) {
71          // receives a String array
72          final String[] requestedValues = (String[]) values;
73          final String[] commonValues = TextParameter.intersect(supportedValues, requestedValues);
74          // reject if no commonly supported values
75          if (commonValues.length == 0) return null;
76          // otherwise, save and return negotiation result
77          value = commonValues[0];
78          return commonValues[0];
79      }
80  
81      @Override
82      public String getStringValue () {
83          return (String) value;
84      }
85  
86      @Override
87      public Entry copy () {
88          final StringEntry e = new StringEntry(keySet, negotiationType, use, negotiationStatus, supportedValues, (String) value);
89          e.alreadyNegotiated = this.alreadyNegotiated;
90          return e;
91      }
92  }