| 1 | |
package org.jscsi.target; |
| 2 | |
|
| 3 | |
|
| 4 | |
import java.io.File; |
| 5 | |
import java.io.IOException; |
| 6 | |
import java.net.InetAddress; |
| 7 | |
import java.util.ArrayList; |
| 8 | |
import java.util.List; |
| 9 | |
|
| 10 | |
import javax.xml.XMLConstants; |
| 11 | |
import javax.xml.parsers.DocumentBuilder; |
| 12 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 13 | |
import javax.xml.parsers.ParserConfigurationException; |
| 14 | |
import javax.xml.transform.dom.DOMResult; |
| 15 | |
import javax.xml.transform.dom.DOMSource; |
| 16 | |
import javax.xml.validation.Schema; |
| 17 | |
import javax.xml.validation.SchemaFactory; |
| 18 | |
import javax.xml.validation.Validator; |
| 19 | |
|
| 20 | |
import org.jscsi.target.scsi.lun.LogicalUnitNumber; |
| 21 | |
import org.jscsi.target.settings.TextKeyword; |
| 22 | |
import org.jscsi.target.storage.IStorageModule; |
| 23 | |
import org.jscsi.target.storage.JCloudsStorageModule; |
| 24 | |
import org.jscsi.target.storage.RandomAccessStorageModule; |
| 25 | |
import org.jscsi.target.storage.SynchronizedRandomAccessStorageModule; |
| 26 | |
import org.w3c.dom.Document; |
| 27 | |
import org.w3c.dom.Element; |
| 28 | |
import org.w3c.dom.Node; |
| 29 | |
import org.w3c.dom.NodeList; |
| 30 | |
import org.w3c.dom.Text; |
| 31 | |
import org.xml.sax.SAXException; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public class Configuration { |
| 43 | |
|
| 44 | |
public static final String ELEMENT_TARGET_LIST = "TargetList"; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public static final String ELEMENT_TARGET = "Target"; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public static final String ELEMENT_SYNCFILESTORAGE = "SyncFileStorage"; |
| 55 | |
public static final String ELEMENT_ASYNCFILESTORAGE = "AsyncFileStorage"; |
| 56 | |
public static final String ELEMENT_JCLOUDSSTORAGE = "JCloudsStorage"; |
| 57 | |
public static final String ELEMENT_FILESTORAGE = "FileStorage"; |
| 58 | |
public static final String ELEMENT_CREATE = "Create"; |
| 59 | |
public static final String ATTRIBUTE_SIZE = "size"; |
| 60 | |
|
| 61 | |
|
| 62 | |
public static final String ELEMENT_ALLOWSLOPPYNEGOTIATION = "AllowSloppyNegotiation"; |
| 63 | |
public static final String ELEMENT_PORT = "Port"; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 0 | private static final File CONFIG_DIR = new File(new StringBuilder("src").append(File.separator).append("main").append(File.separator).append("resources").append(File.separator).toString()); |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | 0 | public static final File CONFIGURATION_SCHEMA_FILE = new File(CONFIG_DIR, "jscsi-target.xsd"); |
| 77 | |
|
| 78 | |
|
| 79 | 0 | public static final File CONFIGURATION_CONFIG_FILE = new File(CONFIG_DIR, "jscsi-target.xml"); |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
protected final List<Target> targets; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
protected String targetAddress; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
protected int port; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
protected boolean allowSloppyNegotiation; |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | 0 | protected final int targetPortalGroupTag = 1; |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | 0 | protected final LogicalUnitNumber logicalUnitNumber = new LogicalUnitNumber(0L); |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | 0 | protected final int outMaxRecvDataSegmentLength = 8192; |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | 0 | private final int maxRecvTextPduSequenceLength = 4; |
| 144 | |
|
| 145 | 0 | public Configuration (final String pTargetAddress) throws IOException { |
| 146 | 0 | port = 3260; |
| 147 | |
|
| 148 | 0 | if (pTargetAddress.equals("")) { |
| 149 | 0 | targetAddress = InetAddress.getLocalHost().getHostAddress(); |
| 150 | |
|
| 151 | |
} else { |
| 152 | 0 | targetAddress = pTargetAddress; |
| 153 | |
} |
| 154 | |
|
| 155 | 0 | targets = new ArrayList<Target>(); |
| 156 | 0 | } |
| 157 | |
|
| 158 | |
public int getInMaxRecvTextPduSequenceLength () { |
| 159 | 0 | return maxRecvTextPduSequenceLength; |
| 160 | |
} |
| 161 | |
|
| 162 | |
public int getOutMaxRecvDataSegmentLength () { |
| 163 | 0 | return outMaxRecvDataSegmentLength; |
| 164 | |
} |
| 165 | |
|
| 166 | |
public String getTargetAddress () { |
| 167 | 0 | return targetAddress; |
| 168 | |
} |
| 169 | |
|
| 170 | |
public int getPort () { |
| 171 | 0 | return port; |
| 172 | |
} |
| 173 | |
|
| 174 | |
public boolean getAllowSloppyNegotiation () { |
| 175 | 0 | return allowSloppyNegotiation; |
| 176 | |
} |
| 177 | |
|
| 178 | |
public int getTargetPortalGroupTag () { |
| 179 | 0 | return targetPortalGroupTag; |
| 180 | |
} |
| 181 | |
|
| 182 | |
public LogicalUnitNumber getLogicalUnitNumber () { |
| 183 | 0 | return logicalUnitNumber; |
| 184 | |
} |
| 185 | |
|
| 186 | |
public List<Target> getTargets () { |
| 187 | 0 | return targets; |
| 188 | |
} |
| 189 | |
|
| 190 | |
public static Configuration create (final String pTargetAddress) throws SAXException , ParserConfigurationException , IOException { |
| 191 | 0 | return create(CONFIGURATION_SCHEMA_FILE, CONFIGURATION_CONFIG_FILE, pTargetAddress); |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
public static Configuration create (final File schemaLocation, final File configFile, final String pTargetAddress) throws SAXException , ParserConfigurationException , IOException { |
| 203 | 0 | final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
| 204 | 0 | final Schema schema = schemaFactory.newSchema(schemaLocation); |
| 205 | |
|
| 206 | |
|
| 207 | 0 | final Validator validator = schema.newValidator(); |
| 208 | |
|
| 209 | 0 | final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); |
| 210 | 0 | domFactory.setNamespaceAware(true); |
| 211 | 0 | final DocumentBuilder builder = domFactory.newDocumentBuilder(); |
| 212 | 0 | final Document doc = builder.parse(configFile); |
| 213 | |
|
| 214 | 0 | final DOMSource source = new DOMSource(doc); |
| 215 | 0 | final DOMResult result = new DOMResult(); |
| 216 | |
|
| 217 | 0 | validator.validate(source, result); |
| 218 | 0 | Document root = (Document) result.getNode(); |
| 219 | |
|
| 220 | |
|
| 221 | 0 | Configuration returnConfiguration = new Configuration(pTargetAddress); |
| 222 | 0 | Element targetListNode = (Element) root.getElementsByTagName(ELEMENT_TARGET_LIST).item(0); |
| 223 | 0 | NodeList targetList = targetListNode.getElementsByTagName(ELEMENT_TARGET); |
| 224 | 0 | for (int curTargetNum = 0; curTargetNum < targetList.getLength(); curTargetNum++) { |
| 225 | 0 | Target curTargetInfo = parseTargetElement((Element) targetList.item(curTargetNum)); |
| 226 | 0 | synchronized (returnConfiguration.targets) { |
| 227 | 0 | returnConfiguration.targets.add(curTargetInfo); |
| 228 | 0 | } |
| 229 | |
|
| 230 | |
} |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | 0 | if (root.getElementsByTagName(ELEMENT_PORT).getLength() > 0) |
| 236 | 0 | returnConfiguration.port = Integer.parseInt(root.getElementsByTagName(ELEMENT_PORT).item(0).getTextContent()); |
| 237 | |
else |
| 238 | 0 | returnConfiguration.port = 3260; |
| 239 | |
|
| 240 | |
|
| 241 | 0 | final Node allowSloppyNegotiationNode = root.getElementsByTagName(ELEMENT_ALLOWSLOPPYNEGOTIATION).item(0); |
| 242 | 0 | if (allowSloppyNegotiationNode == null) |
| 243 | 0 | returnConfiguration.allowSloppyNegotiation = false; |
| 244 | |
else |
| 245 | 0 | returnConfiguration.allowSloppyNegotiation = Boolean.parseBoolean(allowSloppyNegotiationNode.getTextContent()); |
| 246 | |
|
| 247 | 0 | return returnConfiguration; |
| 248 | |
|
| 249 | |
} |
| 250 | |
|
| 251 | |
protected static Target parseTargetElement (Element targetElement) throws IOException { |
| 252 | |
|
| 253 | |
|
| 254 | 0 | Node nextNode = chopWhiteSpaces(targetElement.getFirstChild()); |
| 255 | |
|
| 256 | |
|
| 257 | 0 | String targetName = nextNode.getTextContent(); |
| 258 | |
|
| 259 | |
|
| 260 | 0 | nextNode = chopWhiteSpaces(nextNode.getNextSibling()); |
| 261 | 0 | String targetAlias = ""; |
| 262 | 0 | if (nextNode.getLocalName().equals(TextKeyword.TARGET_ALIAS)) { |
| 263 | 0 | targetAlias = nextNode.getTextContent(); |
| 264 | 0 | nextNode = chopWhiteSpaces(nextNode.getNextSibling()); |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | 0 | Class<? extends IStorageModule> kind = null; |
| 269 | 0 | switch (nextNode.getLocalName()) { |
| 270 | |
case ELEMENT_SYNCFILESTORAGE : |
| 271 | 0 | kind = SynchronizedRandomAccessStorageModule.class; |
| 272 | 0 | break; |
| 273 | |
case ELEMENT_ASYNCFILESTORAGE : |
| 274 | 0 | kind = RandomAccessStorageModule.class; |
| 275 | 0 | break; |
| 276 | |
case ELEMENT_JCLOUDSSTORAGE : |
| 277 | 0 | kind = JCloudsStorageModule.class; |
| 278 | |
break; |
| 279 | |
} |
| 280 | |
|
| 281 | |
|
| 282 | 0 | nextNode = nextNode.getFirstChild(); |
| 283 | 0 | nextNode = chopWhiteSpaces(nextNode); |
| 284 | |
|
| 285 | 0 | String storageFilePath = nextNode.getTextContent(); |
| 286 | |
|
| 287 | |
|
| 288 | 0 | nextNode = chopWhiteSpaces(nextNode.getNextSibling()); |
| 289 | 0 | long storageLength = -1; |
| 290 | 0 | boolean create = true; |
| 291 | 0 | if (nextNode.getLocalName().equals(ELEMENT_CREATE)) { |
| 292 | 0 | Node sizeAttribute = nextNode.getAttributes().getNamedItem(ATTRIBUTE_SIZE); |
| 293 | 0 | storageLength = Math.round(((Double.valueOf(sizeAttribute.getTextContent())) * Math.pow(1024, 3))); |
| 294 | 0 | } else { |
| 295 | 0 | storageLength = new File(storageFilePath).length(); |
| 296 | 0 | create = false; |
| 297 | |
|
| 298 | |
} |
| 299 | 0 | final IStorageModule module = RandomAccessStorageModule.open(new File(storageFilePath), storageLength, create, kind); |
| 300 | |
|
| 301 | 0 | return new Target(targetName, targetAlias, module); |
| 302 | |
|
| 303 | |
} |
| 304 | |
|
| 305 | |
protected static Node chopWhiteSpaces (final Node node) { |
| 306 | 0 | Node toIterate = node; |
| 307 | 0 | while (toIterate instanceof Text && toIterate.getTextContent().trim().length() == 0) { |
| 308 | 0 | toIterate = toIterate.getNextSibling(); |
| 309 | |
} |
| 310 | 0 | return toIterate; |
| 311 | |
} |
| 312 | |
|
| 313 | |
} |