1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.apphosting
.utils
.config
;
5 import com
.google
.net
.base
.CidrAddressBlock
;
7 import java
.util
.ArrayList
;
11 * Parsed dos.xml file.
13 * Any additions to this class should also be made to the YAML
14 * version in DosYamlReader.java.
20 * Describes a single blacklist entry.
22 public static class BlacklistEntry
{
27 /** Create an empty blacklist entry. */
28 public BlacklistEntry() {
33 /** Records the human-readable description of this blacklist entry. */
34 public void setDescription(String description
) {
35 this.desc
= description
.replace('\n', ' ');
38 /** Records the subnet of this blacklist entry. */
39 public void setSubnet(String subnet
) {
41 CidrAddressBlock parsedSubnet
= CidrAddressBlock
.create(subnet
);
43 } catch (IllegalArgumentException iae
) {
45 throw new AppEngineConfigException("subnet " + subnet
+ " failed to parse",
50 public String
getSubnet() {
54 public String
getDescription() {
59 private List
<BlacklistEntry
> blacklistEntries
;
61 /** Create an empty configuration object. */
63 blacklistEntries
= new ArrayList
<BlacklistEntry
>();
67 * Puts a new blacklist entry into the list defined by the config file.
69 * @throws AppEngineConfigException if the previously-last blacklist entry is
71 * @return the new entry
73 public BlacklistEntry
addNewBlacklistEntry() {
75 BlacklistEntry entry
= new BlacklistEntry();
76 blacklistEntries
.add(entry
);
80 public void addBlacklistEntry(BlacklistEntry entry
) {
82 blacklistEntries
.add(entry
);
87 * Check that the last blacklist entry defined is complete.
88 * @throws AppEngineConfigException if it is not.
90 public void validateLastEntry() {
91 if (blacklistEntries
.size() == 0) {
94 BlacklistEntry last
= blacklistEntries
.get(blacklistEntries
.size() - 1);
95 if (last
.getSubnet() == null) {
96 throw new AppEngineConfigException("no subnet for blacklist");
101 * Get the YAML equivalent of this dos.xml file.
103 * @return contents of an equivalent {@code dos.yaml} file.
105 public String
toYaml() {
106 StringBuilder builder
= new StringBuilder("blacklist:\n");
107 for (BlacklistEntry ent
: blacklistEntries
) {
108 builder
.append("- subnet: " + ent
.getSubnet() + "\n");
109 if (!ent
.getDescription().equals("")) {
110 builder
.append(" description: " + ent
.getDescription() + "\n");
113 return builder
.toString();