Changeset 64

Show
Ignore:
Timestamp:
05/02/05 23:26:28 (4 years ago)
Author:
pragma
Message:

Updated DSP: servertest is closer to proposed design. Servlets now build on demand for a test website laid out in /dspconf. Servlets are not yet sensitive to dependency date/time changes, and will only build on the first request.

Will investigate single-dll design for whole webs as a means to circumvent the large compile sizes seen thus far (100kb under win32).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dsp/DSPGrammar.d

    r53 r64  
    5151 
    5252    public static char[] pathToNamespace(char[] path){ 
    53         return replace(path,"\\","."); 
     53        return path.replace("\\",".").replace("/","."); 
    5454    } 
    5555 
     
    9292             
    9393            static this(){ 
    94                 printf("servlet library init!\n"); 
    9594                dsp.servlet.ServletRegistry.registerServletHandle(&` ~ servletFunctionName(name) ~ `,` ~ "`default`" ~ `); 
    9695            }` ~ newline 
  • trunk/dsp/servlet/DSPServlet.d

    r61 r64  
    2828private import misc.Library; 
    2929 
    30 private import dsp.DSPException; 
    31   
    3230private import dsp.servlet.DSPRequest; 
    3331private import dsp.servlet.IDSPRequest; 
    3432private import dsp.servlet.DSPResponse; 
    3533private import dsp.servlet.IDSPResponse; 
     34private import dsp.servlet.DSPServletContext; 
    3635 
    37 import mango.servlet.Servlet; 
    38 import mango.io.FilePath; 
    39 import mango.io.Uri; 
    40 import mango.log.Logger; 
     36private import dsp.DSPException; 
     37private import dsp.ServletCompiler; 
     38private import dsp.RequestConfig; 
     39 
     40private import mango.servlet.Servlet; 
     41private import mango.io.FilePath; 
     42private import mango.io.Uri; 
     43private import mango.log.Logger; 
     44 
     45private import std.string; 
    4146 
    4247extern(C) alias void function(IDSPRequest request,IDSPResponse response) ServiceHandle; 
    4348 
    4449class DSPServlet : Servlet{ 
    45     Library servletLibrary; 
    46     ServiceHandle handle; 
     50    Library[char[]] libs; 
    4751     
    4852    private static Logger logger; 
     
    5155        logger = Logger.getLogger ("dsp.servlets"); 
    5256    }    
    53      
    54 /*  public this(Library servletLibrary){ 
    55         this.servletLibrary = servletLibrary; 
    56         handle = cast(ServiceHandle)library.getSymbol("service"); 
    57     } 
    5857 
    59     public this(char[] libraryPath){ 
    60         servletLibrary = new Library(libraryPath); 
    61          
    62         assert(servletLibrary.isLoaded()); 
    63          
    64         handle = cast(ServiceHandle)servletLibrary.getSymbol("service"); 
    65         assert(handle); 
    66     } 
    67  
    68  
    69     void service (IServletRequest request, IServletResponse response){ 
    70         char[] path = request.getServletPath(); 
    71          
    72         response.setContentType("text/html"); 
    73          
    74         IDSPRequest dspRequest = cast(IDSPRequest)request; 
    75         IDSPResponse dspResponse = cast(IDSPResponse)response; 
    76          
    77         assert(dspRequest); 
    78         assert(dspResponse); 
    79          
    80         handle(dspRequest,dspResponse); 
    81          
    82         if(dspResponse.isExceptionSet){ 
    83             throw new DSPException("servlet: " ~ dspResponse.getException()); 
    84         } 
    85     } 
    86 */ 
    8758    public this(){ 
    8859    } 
    8960     
     61    public ~this(){ 
     62        foreach(Library lib; libs){ 
     63            if(lib.isLoaded) lib.unload(); 
     64        } 
     65    } 
     66     
     67    Library getLibrary(DSPServletContext context,FilePath path){ 
     68        //TODO: make concurrent 
     69         
     70        Library lib = libs[path.toString()]; 
     71         
     72        //TODO: add check for file existence 
     73        //TODO: add check for library date and its validity vs the source file 
     74        if(!lib){ 
     75            RequestConfig cfg; 
     76             
     77            // configuration 
     78             
     79            cfg.pwd = context.getBasePath() ~ "\\"; 
     80            cfg.pwd = cfg.pwd.replace("/","\\"); 
     81            cfg.ext = path.getExtension(); 
     82             
     83            //HACK: filepath leaves prefixed '/' character on 
     84            cfg.name = path.getName()[1..$]; 
     85             
     86            cfg.modulePath = path.getPath(); 
     87            cfg.requestModule = cfg.modulePath ~ cfg.name; 
     88            cfg.dspSourceFilename = cfg.pwd ~ cfg.name ~ "." ~ cfg.ext; 
     89 
     90            cfg.tempDirectory = cfg.pwd ~ context.getConfiguration.get("temp-directory"); 
     91            cfg.cacheDirectory = cfg.pwd ~ context.getConfiguration.get("cache-directory"); 
     92 
     93            cfg.dSourceFilename = cfg.tempDirectory ~ cfg.name ~ ".d"; 
     94            cfg.destServletFilename = cfg.cacheDirectory ~ cfg.name ~ ".dll"; 
     95 
     96            cfg.runtimeDirectory = context.getConfiguration.get("runtime-directory"); 
     97 
     98            // debug 
     99            printf("%.*s\n",cfg.toString()); 
     100 
     101            // build the servlet library 
     102            ServletCompiler compiler = new ServletCompiler(); 
     103            compiler.CompileServlet(cfg); 
     104 
     105            // keep the library for later 
     106            lib = new Library(cfg.destServletFilename); 
     107            libs[path.toString()] = lib; 
     108        } 
     109        return lib; 
     110    } 
     111     
    90112    void service (IServletRequest request, IServletResponse response){ 
    91         FilePath path = new FilePath(request.getUri); 
     113        FilePath path = new FilePath(request.getPathInfo); 
    92114         
    93115        if(path.getExtension == "dsp"){ 
    94116            IDSPRequest dspRequest = cast(IDSPRequest)request; 
    95117            IDSPResponse dspResponse = cast(IDSPResponse)response; 
     118            DSPServletContext context = cast(DSPServletContext)request.getContext(); 
    96119 
    97120            assert(dspRequest); 
    98121            assert(dspResponse); 
     122            assert(context); 
    99123 
    100             //TODO: bind to the needed dll on the fly 
     124            // bind to the needed dll on the fly 
     125             
     126            Library lib = getLibrary(context,path); 
     127            if(lib){ 
     128                ServiceHandle handle = cast(ServiceHandle)lib.getSymbol("service"); 
    101129 
    102             dspResponse.getWriter.put("<h2>This is a test for DSP servlets</h2>"); 
     130                // run the service handle 
     131                if(handle){ 
     132                    handle(dspRequest,dspResponse); 
     133                } 
     134                else{ 
     135                    throw new DSPException("servlet library does not have a valid service handle"); 
     136                } 
    103137 
    104             if(dspResponse.isExceptionSet){ 
    105                 throw new DSPException("servlet: " ~ dspResponse.getException()); 
     138                dspResponse.getWriter.put("<h2>This is a test for DSP servlets</h2>"); 
     139 
     140                if(dspResponse.isExceptionSet){ 
     141                    throw new DSPException("servlet: " ~ dspResponse.getException()); 
     142                } 
     143            } 
     144            else{ 
     145                throw new DSPException("could not load library"); 
    106146            } 
    107147        } 
    108148        else{ 
    109149            //TODO: use configuration and handling rules  
    110             //TODO: provide a listing if the file doesn't exist 
     150            //TODO: provide a directory listing if the file doesn't exist 
    111151            logger.info ("request for file: " ~ request.getUri.getPath ~ " (" ~ request.getPathInfo ~ ")"); 
    112152            response.copyFile (request.getContext, request.getPathInfo);         
  • trunk/dsp/servlet/ServletRegistry.d

    r54 r64  
    6565    catch(Object o){ 
    6666        response.setException("general exception: " ~ o.toString());     
    67     }   
     67    } 
    6868} 
  • trunk/dspconf/config.xml

    r61 r64  
    11<?xml version="1.0"?> 
    22<config> 
    3     <temp-directory value="_temp/"/> 
    4     <cache-directory value="_cache/"/> 
    5     <runtime-directory value="../runtime/"/> 
     3    <temp-directory value="_temp\"/> 
     4    <cache-directory value="_cache\"/> 
     5    <runtime-directory value="c:\dev\dsp\trunk\runtime/"/> 
    66    <dmdopts></dmdopts> 
    77</config> 
  • trunk/dspconf/dsp02.dsp

    r57 r64  
    11<?xml version="1.0" ?> 
    22<?dsp 
    3     import mango.io.FileConduit; 
    4      
    5     FileConduit fc; 
     3 
    64?> 
    75<html/> 
  • trunk/servertest.d

    r61 r64  
    7070import dsp.servlet.DSPServletContext; 
    7171 
    72 class FileServlet : MethodServlet 
    73 { 
    74         private static Logger logger; 
    75  
    76         /*********************************************************************** 
    77          
    78                 get a Logger for this class 
    79  
    80         ***********************************************************************/ 
    81  
    82         static this () 
    83         { 
    84                 logger = Logger.getLogger ("mango.servlets.File"); 
    85         } 
    86  
    87         /*********************************************************************** 
    88          
    89                 support GET requests only! All other method requests will 
    90                 return an error to the user-agent 
    91  
    92         ***********************************************************************/ 
    93  
    94         void doGet (IServletRequest request, IServletResponse response) 
    95         {    
    96                 logger.info ("request for file: " ~ request.getUri.getPath ~ " (" ~ request.getPathInfo ~ ")"); 
    97  
    98                 response.copyFile (request.getContext, request.getPathInfo); 
    99         } 
    100 } 
    101  
    10272void testServer (IProvider provider) 
    10373{        
     
    131101 
    132102        // create a context for example dsp servlets 
    133         sp.addContext (new DSPServletContext (sp,"/dsp","c:/dev/dsp/trunk/dspconf"));  
    134  
    135         // point the default context to the same path 
    136         //sp.addContext (new ServletContext ("", "c:/dev/dsp/trunk/dspconf")); 
    137  
    138         // map all other requests to our file servlet 
    139         //sp.addMapping ("/", sp.addServlet (new FileServlet, "files")); 
     103        sp.addContext (new DSPServletContext (sp,"/foo/bar","c:/dev/dsp/trunk/dspconf"));  
    140104     
    141105        // fire up a server