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.boot;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.program.ConfigProgram;
034: import com.caucho.config.program.ContainerProgram;
035: import com.caucho.loader.EnvironmentBean;
036: import com.caucho.loader.EnvironmentClassLoader;
037: import com.caucho.util.L10N;
038: import com.caucho.vfs.Path;
039:
040: import javax.annotation.PostConstruct;
041: import java.util.ArrayList;
042: import java.util.HashMap;
043:
044: public class BootManager implements EnvironmentBean {
045: private static final L10N L = new L10N(BootManager.class);
046:
047: private boolean _isWatchdogManagerConfig;
048:
049: private ArrayList<ContainerProgram> _clusterDefaultList = new ArrayList<ContainerProgram>();
050:
051: private HashMap<String, WatchdogClient> _watchdogMap = new HashMap<String, WatchdogClient>();
052:
053: private ClassLoader _classLoader;
054:
055: private WatchdogArgs _args;
056:
057: private Path _resinHome;
058: private Path _rootDirectory;
059:
060: private ManagementConfig _management;
061: private String _password;
062:
063: BootManager(WatchdogArgs args) {
064: _args = args;
065:
066: _classLoader = new EnvironmentClassLoader();
067: }
068:
069: WatchdogArgs getArgs() {
070: return _args;
071: }
072:
073: public Path getResinHome() {
074: if (_resinHome != null)
075: return _resinHome;
076: else
077: return _args.getResinHome();
078: }
079:
080: public void setRootDirectory(Path rootDirectory) {
081: _rootDirectory = rootDirectory;
082: }
083:
084: public Path getRootDirectory() {
085: if (_rootDirectory != null)
086: return _rootDirectory;
087: else
088: return _args.getRootDirectory();
089: }
090:
091: public Path getLogDirectory() {
092: Path logDirectory = _args.getLogDirectory();
093:
094: if (logDirectory != null)
095: return logDirectory;
096: else
097: return getRootDirectory().lookup("log");
098: }
099:
100: public ClassLoader getClassLoader() {
101: return _classLoader;
102: }
103:
104: /**
105: * Adds the management configuration
106: */
107: public void setManagement(ManagementConfig management) {
108: _management = management;
109: }
110:
111: /**
112: * Returns the management password.
113: */
114: public String getAdminCookie() {
115: if (_management != null)
116: return _management.getAdminCookie();
117: else
118: return _password;
119: }
120:
121: /**
122: * Finds a server.
123: */
124: public WatchdogClient findClient(String id) {
125: return _watchdogMap.get(id);
126: }
127:
128: /**
129: * Finds a server.
130: */
131: public void addClient(WatchdogClient client) {
132: _watchdogMap.put(client.getId(), client);
133: }
134:
135: /**
136: * Creates the watchdog-manager config
137: */
138: public WatchdogManagerConfig createWatchdogManager() {
139: _isWatchdogManagerConfig = true;
140:
141: return new WatchdogManagerConfig();
142: }
143:
144: /**
145: * Adds a new default to the cluster.
146: */
147: public void addClusterDefault(ContainerProgram program) {
148: _clusterDefaultList.add(program);
149: }
150:
151: public ClusterConfig createCluster() {
152: ClusterConfig cluster = new ClusterConfig();
153:
154: for (int i = 0; i < _clusterDefaultList.size(); i++)
155: _clusterDefaultList.get(i).configure(cluster);
156:
157: return cluster;
158: }
159:
160: public ServerCompatConfig createServer() {
161: return new ServerCompatConfig();
162: }
163:
164: /**
165: * Ignore items we can't understand.
166: */
167: public void addThreadPool(ConfigProgram program) {
168: }
169:
170: public void setGroupName(ConfigProgram program) {
171: }
172:
173: public void setUserName(ConfigProgram program) {
174: }
175:
176: public void setMinFreeMemory(ConfigProgram program) {
177: }
178:
179: public void setSecurityManager(ConfigProgram program) {
180: }
181:
182: public void addSecurityProvider(Class providerClass) {
183: }
184:
185: public void setEnvironmentSystemProperties(ConfigProgram program) {
186: }
187:
188: //
189: // configuration classes
190: //
191:
192: public class WatchdogManagerConfig {
193: private ArrayList<ContainerProgram> _watchdogDefaultList = new ArrayList<ContainerProgram>();
194:
195: public void setWatchdogPort(int watchdogPort) {
196: if (_args.getWatchdogPort() == 0)
197: _args.setWatchdogPort(watchdogPort);
198: }
199:
200: /**
201: * Adds a new server to the cluster.
202: */
203: public void addWatchdogDefault(ContainerProgram program) {
204: _watchdogDefaultList.add(program);
205: }
206:
207: public WatchdogConfig createWatchdog() {
208: WatchdogConfig config = new WatchdogConfig(getArgs());
209:
210: for (int i = 0; i < _watchdogDefaultList.size(); i++)
211: _watchdogDefaultList.get(i).configure(config);
212:
213: return config;
214: }
215:
216: public void addWatchdog(WatchdogConfig config)
217: throws ConfigException {
218: if (findClient(config.getId()) != null)
219: throw new ConfigException(
220: L
221: .l(
222: "<server id='{0}'> is a duplicate server. servers must have unique ids.",
223: config.getId()));
224:
225: addClient(new WatchdogClient(BootManager.this , config));
226: }
227: }
228:
229: public class ClusterConfig {
230: private ArrayList<ContainerProgram> _serverDefaultList = new ArrayList<ContainerProgram>();
231:
232: /**
233: * Adds a new server to the cluster.
234: */
235: public void addServerDefault(ContainerProgram program) {
236: _serverDefaultList.add(program);
237: }
238:
239: public void addManagement(ManagementConfig management) {
240: BootManager.this .setManagement(management);
241: }
242:
243: public WatchdogConfig createServer() {
244: WatchdogConfig config = new WatchdogConfig(getArgs());
245:
246: for (int i = 0; i < _serverDefaultList.size(); i++)
247: _serverDefaultList.get(i).configure(config);
248:
249: return config;
250: }
251:
252: public void addServer(WatchdogConfig config)
253: throws ConfigException {
254: if (_isWatchdogManagerConfig)
255: return;
256:
257: if (findClient(config.getId()) != null)
258: throw new ConfigException(
259: L
260: .l(
261: "<server id='{0}'> is a duplicate server. servers must have unique ids.",
262: config.getId()));
263:
264: addClient(new WatchdogClient(BootManager.this , config));
265: }
266:
267: /**
268: * Ignore items we can't understand.
269: */
270: public void addBuilderProgram(ConfigProgram program) {
271: }
272: }
273:
274: public class ServerCompatConfig {
275: public ClusterCompatConfig createCluster() {
276: return new ClusterCompatConfig();
277: }
278:
279: public SrunCompatConfig createHttp() {
280: return new SrunCompatConfig();
281: }
282:
283: /**
284: * Ignore items we can't understand.
285: */
286: public void addBuilderProgram(ConfigProgram program) {
287: }
288: }
289:
290: public class ClusterCompatConfig {
291: public SrunCompatConfig createSrun() {
292: return new SrunCompatConfig();
293: }
294:
295: /**
296: * Ignore items we can't understand.
297: */
298: public void addBuilderProgram(ConfigProgram program) {
299: }
300: }
301:
302: public class SrunCompatConfig {
303: private String _id = "";
304:
305: public void setId(String id) {
306: _id = id;
307: }
308:
309: public void setServerId(String id) {
310: _id = id;
311: }
312:
313: /**
314: * Ignore items we can't understand.
315: */
316: public void addBuilderProgram(ConfigProgram program) {
317: }
318:
319: @PostConstruct
320: public void init() {
321: if (_isWatchdogManagerConfig)
322: return;
323:
324: WatchdogClient client = findClient(_id);
325:
326: if (client != null)
327: return;
328:
329: WatchdogConfig config = new WatchdogConfig(getArgs());
330:
331: client = new WatchdogClient(BootManager.this, config);
332:
333: _watchdogMap.put(_id, client);
334: }
335: }
336: }
|