Package anvil.server

Source Code of anvil.server.Server

/*
* $Id: Server.java,v 1.47 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server;

import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Writer;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import anvil.Product;
import anvil.java.util.BindingEnumeration;
import anvil.Log;
import anvil.ForgingException;
import anvil.core.Any;
import anvil.core.AnyString;
import anvil.java.util.BindingEnumeration;
import anvil.session.Session;
import anvil.session.SessionContainer;
import anvil.script.Function;
import anvil.script.ClassType;
import anvil.script.InterfaceType;
import anvil.script.ModuleCache;
import anvil.script.Module;
import anvil.script.statements.Statement;

/**
* class Server
*
* @author: Jani Lehtim�ki
*/
public class Server extends Domain
{

  public static final String VERSION = "anvil/" + anvil.Version.getVersion();
 
  private ServerControl _serverControl = null;

  private DirContainer            DEFAULT_CONTAINER;
  private LoggingPreferences      DEFAULT_LOGGING;
  private AccessPreferences       DEFAULT_ACCESS;
  private SessionPreferences      DEFAULT_SESSION;
  private LocalizationPreferences DEFAULT_LOCALIZATION;
  private CompilerPreferences     DEFAULT_COMPILER;
  private ModulePreferences       DEFAULT_MODULE;

  protected Domain[]    _domains = new Domain[0];
  protected ModuleCache _cache = null;
  protected long        _upSince = System.currentTimeMillis();
  protected long        _requestsServed = 0;


  public Server(ServerControl serverControl)
  {
    super(null);
    _serverControl = serverControl;
    _pathinfo = "/";
    setContainer("file:/");
    DEFAULT_CONTAINER    = new DirContainer(new File("/"));
    DEFAULT_LOGGING      = new LoggingPreferences(this);
    DEFAULT_ACCESS       = new AccessPreferences(this);
    DEFAULT_SESSION      = new SessionPreferences(this);
    DEFAULT_LOCALIZATION = new LocalizationPreferences(this);
    DEFAULT_COMPILER     = new CompilerPreferences(this);
    DEFAULT_MODULE       = new ModulePreferences(this);
  }
 
  public String toString()
  {
    return "server";
  }
 
 
  public long getUpSince()
  {
    return _upSince;
  }


  public long getRequestsServed()
  {
    return _requestsServed;
  }

 
  public void serveRequest()
  {
    _requestsServed ++;
  }


  public Server getServer()
  {
    return this;
  }
 

  public Domain getDomain()
  {
    return this;
  }


  public String getPathinfo()
  {
    return _pathinfo;
  }


  public String getPath()
  {
    return _pathinfo;
  }
 

  public String getParentPath()
  {
    return "";
  }


  public String getBasePath()
  {
    return _pathinfo;
  }


  public Container getContainer()
  {
    if (_thecontainer != null) {
      return _thecontainer;
    } else {
      return DEFAULT_CONTAINER;
    }
  }


  public String getDispatcher()
  {
    return _dispatcher;
  }
 

  public Zone getDispatcherZone()
  {
    return this;
  }
 

  public String getDirectoryIndex()
  {
    return _index;
  }
 

  public String getNamespace()
  {
    return _namespace;
  }


  public int getContentProcessing()
  {
    int c = _content;
    if (c == -1) {
      return Statement.CONTENT_COMPRESS;
    } else {
      return c;
    }
  }

 
  public boolean isHidden()
  {
    if (_hidden != null) {
      return _hidden.booleanValue();
    } else {
      return false;
   
  }
 

  public boolean shouldInvalidate()
  {
    if (_invalidate != null) {
      return _invalidate.booleanValue();
    } else {
      return false;
   
  }
 
 
  public ModuleCache getCache()
  {
    return _cache;
  }
 

  public boolean printPretty()
  {
    if (_pretty != null) {
      return _pretty.booleanValue();
    } else {
      return false;
   
  }


  public boolean getAssert()
  {
    if (_assert != null) {
      return _assert.booleanValue();
    } else {
      return true;
   
  }
 
 
  public boolean getDebug()
  {
    if (_debug != null) {
      return _debug.booleanValue();
    } else {
      return true;
   
  }



  public String getContentType(String extension)
  {
    if (_bindprefs != null) {
      String type = _bindprefs.lookup(extension);
      if (type != null) {
        return type;
      }
    }
    return MimeTypes.getContentType(extension);
  }



  public ContentHandler getContentHandler(String type)
  {
    if (_handlerprefs != null) {
      ContentHandler handler = _handlerprefs.lookup(type);
      if (handler != null) {
        return handler;
      }
    }
    if (type.equals(MimeTypes.APPLICATION_ANVIL_SCRIPT)) {
      return ScriptContentHandler.INSTANCE;
    } else {
      return DefaultContentHandler.INSTANCE;
    }
  } 


  public LoggingPreferences getLoggingPreferences()
  {
    if (_loggingprefs == null) {
      return DEFAULT_LOGGING;
    } else {
      return _loggingprefs;
    }
  }
 
 
  public AccessPreferences getAccessPreferences()
  {
    if (_accessprefs == null) {
      return DEFAULT_ACCESS;
    } else {
      return _accessprefs;
    }
  }
  public SessionPreferences getSessionPreferences()
  {
    if (_sessionprefs == null) {
      return DEFAULT_SESSION;
    } else {
      return _sessionprefs;
    }
  }   

  public LocalizationPreferences getLocalizationPreferences()
  {
    if (_localizationprefs == null) {
      return DEFAULT_LOCALIZATION;
    } else {
      return _localizationprefs;
    }
  }   


  public CompilerPreferences getCompilerPreferences()
  {
    if (_compilerprefs == null) {
      return DEFAULT_COMPILER;
    } else {
      return _compilerprefs;
    }
  }   
 
 
  public ModulePreferences getModulePreferences()
  {
    if (_moduleprefs == null) {
      return DEFAULT_MODULE;
    } else {
      return _moduleprefs;
    }
  }   


  public BindPreferences getBindPreferences()
  {
    return _bindprefs;
 
     
 
  public ApplicationPreferences getApplicationPreferences()
  {
    return _applicationprefs;
  }    

 
  public int getType()
  {
    return SERVER;
  }


  public Object getPreference(String name)
  {
    return super.getPreference(name);
  }

 
  public boolean setPreference(String name, String value)
  {
    return super.setPreference(name, value);
  }
 
 
  public boolean configure(Configurable configurable)
  {
    if (configurable.getType() == DOMAIN) {
      Domain domain = (Domain)configurable;
      domain.setParent(this);
      int length = _domains.length;
      Domain[] domains = new Domain[length+1];
      if (length>0) {
        System.arraycopy(_domains, 0, domains, 0, length);
      }
      domains[length] = domain;
      _domains = domains;
      return true;
    } else {
      return super.configure(configurable);
    }
  }

 
  public void deleteConfiguration(Configurable configurable)
  {
    if (configurable.getType() == DOMAIN) {
      Domain domain = (Domain)configurable;
      domain.setParent(null);
      int length = _domains.length;
      for(int i=0; i<length; i++) {
        if (_domains[i] == domain) {
          Domain[] domains = new Domain[length-1];
          if (i>0) {
            System.arraycopy(_domains, 0, domains, 0, i);
          }
          if (i<length-1) {
            System.arraycopy(_domains, i+1, domains, i, length-i+1);
          }
          _domains = domains;
          break;
        }
      }
     
    } else {
      super.deleteConfiguration(configurable);
    }
  } 
 
 

  protected void getConfigurations(ArrayList v)
  {
    int length = _domains.length;
    for(int i=0; i<length; i++) {
      v.add(_domains[i]);
    }
    super.getConfigurations(v);
  }


  public void save() throws IOException
  {
    if (_serverControl != null) {
      _serverControl.write();
    }
  }
 

  public void reread() throws Throwable
  {
    if (_serverControl != null) {
      _serverControl.reread(System.out);
    }
  }
    
 
  public synchronized void start()
  {
    super.start();
    _cache = new ModuleCache(this);
    final int n = _domains.length;
    for(int i=0; i<n; i++) {
      Domain domain = _domains[i];
      if (_hasPolicy) {
        domain.enablePolicy();
      }
      domain.start();
    }
    log().info("Server started.");
  }


  public synchronized void stop()
  {
    log().info("Server stopping...");
    final int n = _domains.length;
    for(int i=0; i<n; i++) {
      _domains[i].stop();
   
    _cache.stop();
    _cache = null;
    super.stop();
 


  public Zone resolveDomain(String hostname)
  {
    if (hostname == null) {
      return this;
    }
    String hostname2 = null;
    int i = hostname.indexOf(':');
    if (i>0) {
      hostname2 = hostname.substring(0, i);
    }
    Domain domain = this;
    if (hostname != null) {
      Domain[] domains = _domains;
      int n = domains.length;
      for(i=0; i<n; i++) {
        if (domains[i].acceptsDomain(hostname, hostname2)) {
          domain = domains[i];
          break;
        }
      }
    }
    return domain;
  }


  public Product resolveProduct(String filename) throws Throwable
  {
    Zone zone = resolveZone(filename);
    Address address = zone.resolve(filename);
    Module script = getCache().load(address).getModule();
    return new Product(address, System.out, script);
 


  public void service(String filename, Any[] args)
  {
    try {
      Product product = resolveProduct(filename);
      if (args == null) {
        args = new Any[0];
      }
      product.forge("main", new anvil.core.AnyList(args));
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }


}
TOP

Related Classes of anvil.server.Server

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.