001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.host;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.types.RawString;
034: import com.caucho.log.Log;
035: import com.caucho.server.deploy.DeployConfig;
036: import com.caucho.util.L10N;
037:
038: import javax.annotation.PostConstruct;
039: import java.util.ArrayList;
040: import java.util.logging.Logger;
041: import java.util.regex.Pattern;
042:
043: /**
044: * The configuration for a host in the resin.conf
045: */
046: public class HostConfig extends DeployConfig {
047: static final L10N L = new L10N(HostConfig.class);
048: static final Logger log = Log.open(HostConfig.class);
049:
050: // The raw host aliases
051: private ArrayList<String> _hostAliases = new ArrayList<String>();
052:
053: private ArrayList<Pattern> _hostAliasRegexps = new ArrayList<Pattern>();
054:
055: private String _hostName;
056:
057: // The regexp pattern
058: private Pattern _regexp;
059:
060: public HostConfig() {
061: super .setId(null);
062: }
063:
064: /**
065: * Sets the host name.
066: */
067: public void setHostName(RawString name) throws ConfigException {
068: _hostName = cleanHostName(name);
069:
070: if (_hostName.indexOf("${") < 0) {
071: for (int i = 0; i < _hostName.length(); i++) {
072: char ch = _hostName.charAt(i);
073:
074: if (ch == ' ' || ch == '\t' || ch == ',') {
075: throw new ConfigException(
076: L
077: .l(
078: "Host name `{0}' must not contain multiple names. Use <host-alias> to specify aliases for a host.",
079: _hostName));
080: }
081: }
082: }
083:
084: if (_hostName.startsWith("xn--")) {
085: String domainName = DomainName.fromAscii(_hostName);
086:
087: if (!_hostAliases.contains(domainName))
088: _hostAliases.add(domainName);
089: }
090: }
091:
092: /**
093: * Gets the host name.
094: */
095: public String getHostName() {
096: return _hostName;
097: }
098:
099: /**
100: * Sets the id.
101: */
102: public void setId(RawString id) throws ConfigException {
103: String cleanName = cleanHostName(id);
104:
105: setId(cleanName);
106: }
107:
108: public void setId(String cleanName) {
109: super .setId(cleanName);
110:
111: if (_hostName == null)
112: _hostName = cleanName;
113:
114: if (cleanName.startsWith("xn--")) {
115: String name = DomainName.fromAscii(cleanName);
116:
117: if (!_hostAliases.contains(name))
118: _hostAliases.add(name);
119: }
120: }
121:
122: /**
123: * Sets the host name.
124: */
125: private String cleanHostName(RawString name) throws ConfigException {
126: String hostName = name.getValue();
127:
128: if (hostName.indexOf("${") < 0) {
129: for (int i = 0; i < hostName.length(); i++) {
130: char ch = hostName.charAt(i);
131:
132: if (ch == ' ' || ch == '\t' || ch == ',') {
133: throw new ConfigException(
134: L
135: .l(
136: "Host name `{0}' must not contain multiple names. Use <host-alias> to specify aliases for a host.",
137: hostName));
138: }
139: }
140: }
141:
142: return hostName;
143: }
144:
145: /**
146: * Adds a host alias.
147: */
148: public void addHostAlias(RawString rawName) throws ConfigException {
149: String name = rawName.getValue().trim();
150:
151: if (name.indexOf("${") < 0) {
152: for (int i = 0; i < name.length(); i++) {
153: char ch = name.charAt(i);
154:
155: if (ch == ' ' || ch == '\t' || ch == ',') {
156: throw new ConfigException(
157: L
158: .l(
159: "<host-alias> `{0}' must not contain multiple names. Use multiple <host-alias> tags to specify aliases for a host.",
160: name));
161: }
162: }
163: }
164:
165: if (!_hostAliases.contains(name))
166: _hostAliases.add(name);
167: }
168:
169: /**
170: * Returns the host aliases.
171: */
172: public ArrayList<String> getHostAliases() {
173: return _hostAliases;
174: }
175:
176: /**
177: * Adds a host alias regexp.
178: */
179: public void addHostAliasRegexp(String name) {
180: name = name.trim();
181:
182: Pattern pattern = Pattern.compile(name,
183: Pattern.CASE_INSENSITIVE);
184:
185: if (!_hostAliasRegexps.contains(pattern))
186: _hostAliasRegexps.add(pattern);
187: }
188:
189: /**
190: * Returns the host aliases regexps.
191: */
192: public ArrayList<Pattern> getHostAliasRegexps() {
193: return _hostAliasRegexps;
194: }
195:
196: /**
197: * Sets the regexp.
198: */
199: public void setRegexp(RawString regexp) {
200: String value = regexp.getValue();
201:
202: if (!value.endsWith("$"))
203: value = value + "$";
204:
205: _regexp = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
206: }
207:
208: /**
209: * Gets the regexp.
210: */
211: public Pattern getRegexp() {
212: return _regexp;
213: }
214:
215: /**
216: * Sets the root-dir (obsolete).
217: */
218: public void setRootDir(RawString rootDir) {
219: setRootDirectory(rootDir);
220: }
221:
222: /**
223: * Sets the lazy-init property
224: */
225: public void setLazyInit(boolean lazyInit) throws ConfigException {
226: if (lazyInit)
227: setStartupMode("lazy");
228: else
229: setStartupMode("automatic");
230: }
231:
232: /**
233: * Initialize the config.
234: */
235: @PostConstruct
236: public void init() {
237: if (_regexp != null && getHostName() == null)
238: log
239: .config(L
240: .l(
241: "<host regexp=\"{0}\"> should include a <host-name> tag.",
242: _regexp.pattern()));
243: }
244: }
|