KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > host > HostConfig


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.host;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.types.RawString;
34 import com.caucho.log.Log;
35 import com.caucho.server.deploy.DeployConfig;
36 import com.caucho.util.L10N;
37
38 import javax.annotation.PostConstruct;
39 import java.util.ArrayList JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import java.util.regex.Pattern JavaDoc;
42
43 /**
44  * The configuration for a host in the resin.conf
45  */

46 public class HostConfig extends DeployConfig {
47   static final L10N L = new L10N(HostConfig.class);
48   static final Logger JavaDoc log = Log.open(HostConfig.class);
49
50   // The raw host aliases
51
private ArrayList JavaDoc<String JavaDoc> _hostAliases = new ArrayList JavaDoc<String JavaDoc>();
52   
53   private ArrayList JavaDoc<Pattern JavaDoc> _hostAliasRegexps
54     = new ArrayList JavaDoc<Pattern JavaDoc>();
55
56   private String JavaDoc _hostName;
57
58   // The regexp pattern
59
private Pattern JavaDoc _regexp;
60
61   public HostConfig()
62   {
63     super.setId(null);
64   }
65
66   /**
67    * Sets the host name.
68    */

69   public void setHostName(RawString name)
70     throws ConfigException
71   {
72     _hostName = cleanHostName(name);
73     String JavaDoc hostName = name.getValue();
74
75     if (hostName.indexOf("${") < 0) {
76       for (int i = 0; i < hostName.length(); i++) {
77     char ch = hostName.charAt(i);
78
79     if (ch == ' ' || ch == '\t' || ch == ',') {
80       throw new ConfigException(L.l("Host name `{0}' must not contain multiple names. Use <host-alias> to specify aliases for a host.",
81                     hostName));
82     }
83       }
84     }
85
86     _hostName = hostName;
87   }
88   
89   /**
90    * Gets the host name.
91    */

92   public String JavaDoc getHostName()
93   {
94     return _hostName;
95   }
96
97   /**
98    * Sets the id.
99    */

100   public void setId(RawString id)
101     throws ConfigException
102   {
103     super.setId(cleanHostName(id));
104   }
105
106   /**
107    * Sets the host name.
108    */

109   private String JavaDoc cleanHostName(RawString name)
110     throws ConfigException
111   {
112     String JavaDoc hostName = name.getValue();
113
114     if (hostName.indexOf("${") < 0) {
115       for (int i = 0; i < hostName.length(); i++) {
116     char ch = hostName.charAt(i);
117
118     if (ch == ' ' || ch == '\t' || ch == ',') {
119       throw new ConfigException(L.l("Host name `{0}' must not contain multiple names. Use <host-alias> to specify aliases for a host.",
120                     hostName));
121     }
122       }
123     }
124
125     return hostName;
126   }
127
128   /**
129    * Adds a host alias.
130    */

131   public void addHostAlias(RawString rawName)
132     throws ConfigException
133   {
134     String JavaDoc name = rawName.getValue().trim();
135
136     if (name.indexOf("${") < 0) {
137       for (int i = 0; i < name.length(); i++) {
138     char ch = name.charAt(i);
139
140     if (ch == ' ' || ch == '\t' || ch == ',') {
141       throw new ConfigException(L.l("<host-alias> `{0}' must not contain multiple names. Use multiple <host-alias> tags to specify aliases for a host.",
142                     name));
143     }
144       }
145     }
146
147     if (! _hostAliases.contains(name))
148       _hostAliases.add(name);
149   }
150
151   /**
152    * Returns the host aliases.
153    */

154   public ArrayList JavaDoc<String JavaDoc> getHostAliases()
155   {
156     return _hostAliases;
157   }
158   
159   /**
160    * Adds a host alias regexp.
161    */

162   public void addHostAliasRegexp(String JavaDoc name)
163   {
164     name = name.trim();
165
166     Pattern JavaDoc pattern = Pattern.compile(name, Pattern.CASE_INSENSITIVE);
167
168     if (! _hostAliasRegexps.contains(pattern))
169       _hostAliasRegexps.add(pattern);
170   }
171
172   /**
173    * Returns the host aliases regexps.
174    */

175   public ArrayList JavaDoc<Pattern JavaDoc> getHostAliasRegexps()
176   {
177     return _hostAliasRegexps;
178   }
179
180   /**
181    * Sets the regexp.
182    */

183   public void setRegexp(RawString regexp)
184   {
185     String JavaDoc value = regexp.getValue();
186
187     if (! value.endsWith("$"))
188       value = value + "$";
189     
190     _regexp = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
191   }
192
193   /**
194    * Gets the regexp.
195    */

196   public Pattern JavaDoc getRegexp()
197   {
198     return _regexp;
199   }
200
201   /**
202    * Sets the root-dir (obsolete).
203    */

204   public void setRootDir(RawString rootDir)
205   {
206     setRootDirectory(rootDir);
207   }
208
209   /**
210    * Sets the lazy-init property
211    */

212   public void setLazyInit(boolean lazyInit)
213     throws ConfigException
214   {
215     if (lazyInit)
216       setStartupMode("lazy");
217     else
218       setStartupMode("automatic");
219   }
220
221   /**
222    * Initialize the config.
223    */

224   @PostConstruct
225   public void init()
226   {
227     if (_regexp != null && getId() == null)
228       log.config(L.l("<host regexp=\"{0}\"> should include a <host-name> tag.",
229              _regexp.pattern()));
230   }
231 }
232
Popular Tags