001: /*
002: * $Id: Server.java,v 1.47 2002/09/16 08:05:06 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.server;
011:
012: import java.io.IOException;
013: import java.io.File;
014: import java.io.FileNotFoundException;
015: import java.io.Writer;
016: import java.io.PrintWriter;
017: import java.io.OutputStream;
018: import java.net.URL;
019: import java.util.ArrayList;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import anvil.Product;
024: import anvil.java.util.BindingEnumeration;
025: import anvil.Log;
026: import anvil.ForgingException;
027: import anvil.core.Any;
028: import anvil.core.AnyString;
029: import anvil.java.util.BindingEnumeration;
030: import anvil.session.Session;
031: import anvil.session.SessionContainer;
032: import anvil.script.Function;
033: import anvil.script.ClassType;
034: import anvil.script.InterfaceType;
035: import anvil.script.ModuleCache;
036: import anvil.script.Module;
037: import anvil.script.statements.Statement;
038:
039: /**
040: * class Server
041: *
042: * @author: Jani Lehtimäki
043: */
044: public class Server extends Domain {
045:
046: public static final String VERSION = "anvil/"
047: + anvil.Version.getVersion();
048:
049: private ServerControl _serverControl = null;
050:
051: private DirContainer DEFAULT_CONTAINER;
052: private LoggingPreferences DEFAULT_LOGGING;
053: private AccessPreferences DEFAULT_ACCESS;
054: private SessionPreferences DEFAULT_SESSION;
055: private LocalizationPreferences DEFAULT_LOCALIZATION;
056: private CompilerPreferences DEFAULT_COMPILER;
057: private ModulePreferences DEFAULT_MODULE;
058:
059: protected Domain[] _domains = new Domain[0];
060: protected ModuleCache _cache = null;
061: protected long _upSince = System.currentTimeMillis();
062: protected long _requestsServed = 0;
063:
064: public Server(ServerControl serverControl) {
065: super (null);
066: _serverControl = serverControl;
067: _pathinfo = "/";
068: setContainer("file:/");
069: DEFAULT_CONTAINER = new DirContainer(new File("/"));
070: DEFAULT_LOGGING = new LoggingPreferences(this );
071: DEFAULT_ACCESS = new AccessPreferences(this );
072: DEFAULT_SESSION = new SessionPreferences(this );
073: DEFAULT_LOCALIZATION = new LocalizationPreferences(this );
074: DEFAULT_COMPILER = new CompilerPreferences(this );
075: DEFAULT_MODULE = new ModulePreferences(this );
076: }
077:
078: public String toString() {
079: return "server";
080: }
081:
082: public long getUpSince() {
083: return _upSince;
084: }
085:
086: public long getRequestsServed() {
087: return _requestsServed;
088: }
089:
090: public void serveRequest() {
091: _requestsServed++;
092: }
093:
094: public Server getServer() {
095: return this ;
096: }
097:
098: public Domain getDomain() {
099: return this ;
100: }
101:
102: public String getPathinfo() {
103: return _pathinfo;
104: }
105:
106: public String getPath() {
107: return _pathinfo;
108: }
109:
110: public String getParentPath() {
111: return "";
112: }
113:
114: public String getBasePath() {
115: return _pathinfo;
116: }
117:
118: public Container getContainer() {
119: if (_thecontainer != null) {
120: return _thecontainer;
121: } else {
122: return DEFAULT_CONTAINER;
123: }
124: }
125:
126: public String getDispatcher() {
127: return _dispatcher;
128: }
129:
130: public Zone getDispatcherZone() {
131: return this ;
132: }
133:
134: public String getDirectoryIndex() {
135: return _index;
136: }
137:
138: public String getNamespace() {
139: return _namespace;
140: }
141:
142: public int getContentProcessing() {
143: int c = _content;
144: if (c == -1) {
145: return Statement.CONTENT_COMPRESS;
146: } else {
147: return c;
148: }
149: }
150:
151: public boolean isHidden() {
152: if (_hidden != null) {
153: return _hidden.booleanValue();
154: } else {
155: return false;
156: }
157: }
158:
159: public boolean shouldInvalidate() {
160: if (_invalidate != null) {
161: return _invalidate.booleanValue();
162: } else {
163: return false;
164: }
165: }
166:
167: public ModuleCache getCache() {
168: return _cache;
169: }
170:
171: public boolean printPretty() {
172: if (_pretty != null) {
173: return _pretty.booleanValue();
174: } else {
175: return false;
176: }
177: }
178:
179: public boolean getAssert() {
180: if (_assert != null) {
181: return _assert.booleanValue();
182: } else {
183: return true;
184: }
185: }
186:
187: public boolean getDebug() {
188: if (_debug != null) {
189: return _debug.booleanValue();
190: } else {
191: return true;
192: }
193: }
194:
195: public String getContentType(String extension) {
196: if (_bindprefs != null) {
197: String type = _bindprefs.lookup(extension);
198: if (type != null) {
199: return type;
200: }
201: }
202: return MimeTypes.getContentType(extension);
203: }
204:
205: public ContentHandler getContentHandler(String type) {
206: if (_handlerprefs != null) {
207: ContentHandler handler = _handlerprefs.lookup(type);
208: if (handler != null) {
209: return handler;
210: }
211: }
212: if (type.equals(MimeTypes.APPLICATION_ANVIL_SCRIPT)) {
213: return ScriptContentHandler.INSTANCE;
214: } else {
215: return DefaultContentHandler.INSTANCE;
216: }
217: }
218:
219: public LoggingPreferences getLoggingPreferences() {
220: if (_loggingprefs == null) {
221: return DEFAULT_LOGGING;
222: } else {
223: return _loggingprefs;
224: }
225: }
226:
227: public AccessPreferences getAccessPreferences() {
228: if (_accessprefs == null) {
229: return DEFAULT_ACCESS;
230: } else {
231: return _accessprefs;
232: }
233: }
234:
235: public SessionPreferences getSessionPreferences() {
236: if (_sessionprefs == null) {
237: return DEFAULT_SESSION;
238: } else {
239: return _sessionprefs;
240: }
241: }
242:
243: public LocalizationPreferences getLocalizationPreferences() {
244: if (_localizationprefs == null) {
245: return DEFAULT_LOCALIZATION;
246: } else {
247: return _localizationprefs;
248: }
249: }
250:
251: public CompilerPreferences getCompilerPreferences() {
252: if (_compilerprefs == null) {
253: return DEFAULT_COMPILER;
254: } else {
255: return _compilerprefs;
256: }
257: }
258:
259: public ModulePreferences getModulePreferences() {
260: if (_moduleprefs == null) {
261: return DEFAULT_MODULE;
262: } else {
263: return _moduleprefs;
264: }
265: }
266:
267: public BindPreferences getBindPreferences() {
268: return _bindprefs;
269: }
270:
271: public ApplicationPreferences getApplicationPreferences() {
272: return _applicationprefs;
273: }
274:
275: public int getType() {
276: return SERVER;
277: }
278:
279: public Object getPreference(String name) {
280: return super .getPreference(name);
281: }
282:
283: public boolean setPreference(String name, String value) {
284: return super .setPreference(name, value);
285: }
286:
287: public boolean configure(Configurable configurable) {
288: if (configurable.getType() == DOMAIN) {
289: Domain domain = (Domain) configurable;
290: domain.setParent(this );
291: int length = _domains.length;
292: Domain[] domains = new Domain[length + 1];
293: if (length > 0) {
294: System.arraycopy(_domains, 0, domains, 0, length);
295: }
296: domains[length] = domain;
297: _domains = domains;
298: return true;
299: } else {
300: return super .configure(configurable);
301: }
302: }
303:
304: public void deleteConfiguration(Configurable configurable) {
305: if (configurable.getType() == DOMAIN) {
306: Domain domain = (Domain) configurable;
307: domain.setParent(null);
308: int length = _domains.length;
309: for (int i = 0; i < length; i++) {
310: if (_domains[i] == domain) {
311: Domain[] domains = new Domain[length - 1];
312: if (i > 0) {
313: System.arraycopy(_domains, 0, domains, 0, i);
314: }
315: if (i < length - 1) {
316: System.arraycopy(_domains, i + 1, domains, i,
317: length - i + 1);
318: }
319: _domains = domains;
320: break;
321: }
322: }
323:
324: } else {
325: super .deleteConfiguration(configurable);
326: }
327: }
328:
329: protected void getConfigurations(ArrayList v) {
330: int length = _domains.length;
331: for (int i = 0; i < length; i++) {
332: v.add(_domains[i]);
333: }
334: super .getConfigurations(v);
335: }
336:
337: public void save() throws IOException {
338: if (_serverControl != null) {
339: _serverControl.write();
340: }
341: }
342:
343: public void reread() throws Throwable {
344: if (_serverControl != null) {
345: _serverControl.reread(System.out);
346: }
347: }
348:
349: public synchronized void start() {
350: super .start();
351: _cache = new ModuleCache(this );
352: final int n = _domains.length;
353: for (int i = 0; i < n; i++) {
354: Domain domain = _domains[i];
355: if (_hasPolicy) {
356: domain.enablePolicy();
357: }
358: domain.start();
359: }
360: log().info("Server started.");
361: }
362:
363: public synchronized void stop() {
364: log().info("Server stopping...");
365: final int n = _domains.length;
366: for (int i = 0; i < n; i++) {
367: _domains[i].stop();
368: }
369: _cache.stop();
370: _cache = null;
371: super .stop();
372: }
373:
374: public Zone resolveDomain(String hostname) {
375: if (hostname == null) {
376: return this ;
377: }
378: String hostname2 = null;
379: int i = hostname.indexOf(':');
380: if (i > 0) {
381: hostname2 = hostname.substring(0, i);
382: }
383: Domain domain = this ;
384: if (hostname != null) {
385: Domain[] domains = _domains;
386: int n = domains.length;
387: for (i = 0; i < n; i++) {
388: if (domains[i].acceptsDomain(hostname, hostname2)) {
389: domain = domains[i];
390: break;
391: }
392: }
393: }
394: return domain;
395: }
396:
397: public Product resolveProduct(String filename) throws Throwable {
398: Zone zone = resolveZone(filename);
399: Address address = zone.resolve(filename);
400: Module script = getCache().load(address).getModule();
401: return new Product(address, System.out, script);
402: }
403:
404: public void service(String filename, Any[] args) {
405: try {
406: Product product = resolveProduct(filename);
407: if (args == null) {
408: args = new Any[0];
409: }
410: product.forge("main", new anvil.core.AnyList(args));
411: } catch (Throwable t) {
412: t.printStackTrace();
413: }
414: }
415:
416: }
|