001: /*
002: HttpdBase4J: An embeddable Java web server framework that supports HTTP, HTTPS,
003: templated content and serving content from inside a jar or archive.
004: Copyright (C) 2007 Donald Munro
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not,see http://www.gnu.org/licenses/lgpl.txt
018: */
019:
020: package net.homeip.donaldm.httpdbase4j;
021:
022: import com.sun.net.httpserver.HttpHandler;
023: import java.io.File;
024:
025: /**
026: * Implementation of the abstract Httpd class for web content in the
027: * file system.
028: * @see Httpd
029: * @author Donald Munro
030: */
031: public class FileHttpd extends Httpd implements HttpHandleable,
032: Postable
033: //=======================================================================
034: {
035: /**
036: * Home directory for the HTTP server
037: */
038: protected File m_httpHomeDir = null;
039:
040: /**
041: * Constructor with a home directory.
042: * @param homeDir Home directory for the server
043: */
044: public FileHttpd(File homeDir)
045: //------------------------
046: {
047: super ();
048: m_httpHomeDir = homeDir;
049: }
050:
051: /**
052: * Constructor with a home directory and thread model
053: * @param homeDir Home directory for the server
054: * @param threadModel The thread model to use (SINGLE, MULTI or POOL)
055: */
056: public FileHttpd(File homeDir, ThreadModel threadModel)
057: //-------------------------------------------------
058: {
059: super ();
060: m_httpHomeDir = homeDir;
061: m_threadModel = threadModel;
062: switch (m_threadModel) {
063: case MULTI:
064: m_poolSize = 5;
065: m_poolMax = Integer.MAX_VALUE;
066: break;
067:
068: case POOL:
069: m_poolSize = 10;
070: m_poolMax = 10;
071: break;
072: }
073: }
074:
075: /**
076: * Constructor for a fixed size thread pool based server. Defaults to
077: * threadpool threading model.
078: * @param homeDir Home directory for the server
079: * @param poolSize Size of the thread pool
080: */
081: public FileHttpd(java.io.File homeDir, int poolSize)
082: //-------------------------------------
083: {
084: super ();
085: m_httpHomeDir = homeDir;
086: m_threadModel = ThreadModel.POOL;
087: m_poolSize = poolSize;
088: m_poolMax = poolSize;
089: }
090:
091: /**
092: * Constructor for a thread pool based server/
093: * @param homeDir Home directory for the server
094: * @param poolSize Size of the thread pool
095: * @param maxPoolSize Maximum Size of the thread pool
096: */
097: public FileHttpd(java.io.File homeDir, int poolSize, int maxPoolSize)
098: //-------------------------------------------------------
099: {
100: super ();
101: m_httpHomeDir = homeDir;
102: m_threadModel = ThreadModel.POOL;
103: m_poolSize = poolSize;
104: m_poolMax = maxPoolSize;
105: }
106:
107: /**
108: * Get the home directory in the file system as a File.
109: * @return Home directory
110: */
111: public java.io.File getHomeDir() {
112: return m_httpHomeDir;
113: }
114:
115: /**
116: * Get the home directory in the file system as a String.
117: * @return Home directory
118: */
119: public String getHomePath() {
120: return m_httpHomeDir.getAbsolutePath();
121: }
122:
123: /**
124: * Creates a FileRequestHandler for handling file system
125: * based requests. Can be overidden to provide a user specified
126: * request handler.
127: */
128: @Override
129: protected HttpHandler onCreateRequestHandler()
130: //--------------------------------------------
131: {
132: return new FileRequestHandler(this, m_httpHomeDir, m_isVerbose);
133: }
134: }
|