Changeset 99

Show
Ignore:
Timestamp:
05/26/05 00:06:16 (4 years ago)
Author:
pragma
Message:

5/26/05
- started the beginnings of the dsp conformance suite
- completed dsp:include tag
- noted additional bugs, including potential multithreaded resource issue with the library cache
- refined additions to DSPServlet and DSPResponse that will be needed to support additional dsp tags.
- noted possibility of extending dsp:include and dsp:tag to incorporate http, https and ftp as targets.

Files:

Legend:

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

    r67 r99  
    5252 
    5353    public static char[] pathToNamespace(char[] path){ 
    54         return path.replace("\\",".").replace("/","."); 
     54        //TODO: improve this to be more intellegent in handling non-namespace chars 
     55        return path.replace(".","_").replace("\\",".").replace("/","."); 
    5556    } 
    5657 
  • trunk/dsp/ServletCompilationContext.d

    r98 r99  
    3030private import dsp.RequestConfig; 
    3131private import dsp.servlet.DSPServletContext; 
     32private import dsp.servlet.DSPServlet; 
     33 
     34private import xml.utf8all; 
    3235 
    3336struct ServletCompilationContext{ 
    3437    RequestConfig requestConfig; 
    3538    DSPServletContext servletContext; 
     39    DSPServlet servlet; 
     40    XMLParser parser; 
    3641    ServletGenerator generator; 
    3742    TagLibrary tags; 
  • trunk/dsp/ServletCompiler.d

    r97 r99  
    6060        context.generator = new ServletGenerator(); 
    6161        context.tags = new StandardTagLibrary(); 
     62        context.parser = parser; 
    6263         
    6364        parser.parse(new DSPProcessor(context), provider, source); 
  • trunk/dsp/Util.d

    r97 r99  
    9292    CompleteBufferReader reader = new CompleteBufferReader(buf); 
    9393    return reader.read(); 
     94    conduit.close(); 
    9495} 
  • trunk/dsp/servlet/DSPLibrary.d

    r98 r99  
    4242    protected Library lib; 
    4343    public ServiceHandle service; 
     44    public bit inUse; 
    4445    protected DependencyHandle dependencies; 
    4546     
  • trunk/dsp/servlet/DSPResponse.d

    r97 r99  
    3030private import dsp.servlet.DSPAttributes; 
    3131private import dsp.servlet.RuntimeTag; 
     32private import dsp.servlet.DSPServlet; 
    3233 
    3334private import mango.servlet.ServletResponse; 
     
    4243    char[] exception; 
    4344    HttpWriter[] writerStack;    
    44     HttpWriter writer; 
     45    HttpWriter dspWriter; 
    4546    RuntimeTag[char[]][] customTagScopeStack; 
    4647    RuntimeTag[] tagStack; 
    47          
     48    DSPServlet servlet; 
     49     
    4850    public this(IProviderBridge bridge){ 
    4951        super(bridge); 
     
    5153    } 
    5254     
     55    void setServlet(DSPServlet servlet){ 
     56        this.servlet = servlet; 
     57    } 
     58     
    5359    HttpWriter getWriter(){ 
    54         //return writer; 
    55         if(!writer) writer = super.getWriter(); 
    56         return writer; 
     60        if(!dspWriter) dspWriter = super.getWriter(); 
     61        return dspWriter; 
    5762    } 
    5863     
     
    7378        HttpWriter newWriter = new HttpWriter(new Buffer()); 
    7479        writerStack ~= newWriter; 
    75         writer = newWriter; 
     80        dspWriter = newWriter; 
    7681    } 
    7782     
     
    8489        writerStack.length = len-1;  
    8590         
    86         char[] data = cast(char[])writer.getBuffer().getContent(); 
    87         writer = oldWriter; 
     91        char[] data = cast(char[])dspWriter.getBuffer().getContent(); 
     92        dspWriter = oldWriter; 
    8893         
    8994        return data; 
     
    120125    } 
    121126     
    122     RuntimeTag addTag(char[] tagname,char[] filename){ 
     127    RuntimeTag addTag(IDSPRequest request,char[] tagname,char[] filename){ 
    123128        RuntimeTag tag; 
    124         //TODO: get the tag handle from the context somehow 
    125         //tag.handle = context.getServletHandle(filename); 
     129        // get the appropriate servlet handle from the servlet 
     130        tag.handle = servlet.getServletHandleForFile(request,filename); 
    126131         
    127132        customTagScopeStack[$-1][tagname] = tag; 
    128133    } 
    129134         
    130     void startTag(char[] name){ 
    131         RuntimeTag tag = findTag(name); 
     135    void startTag(char[] tagname){ 
     136        RuntimeTag tag = findTag(tagname); 
    132137        pushCurrentTag(tag.clone()); 
    133138         
     
    136141        } 
    137142        else{ 
    138             writer.put("<" ~ name); 
     143            dspWriter.put("<" ~ tagname); 
    139144        } 
    140145    } 
     
    146151        } 
    147152        else{ 
    148             writer.put(" " ~ name ~ "= '" ~ value.toString() ~ "'"); 
     153            dspWriter.put(" " ~ name ~ "= '" ~ value.toString() ~ "'"); 
    149154        } 
    150155    } 
     
    152157    void endTagAttributes(){ 
    153158        if(!getCurrentTag().isNull){ 
    154             writer.put(">"); 
     159            dspWriter.put(">"); 
    155160        } 
    156161    } 
    157162     
    158     void endTag(IDSPRequest request,char[] name){ 
     163    void endTag(IDSPRequest request,char[] tagname){ 
    159164        RuntimeTag tag = getCurrentTag(); 
    160165        if(!tag.isNull){ 
     
    170175        } 
    171176        else{ 
    172             writer.put("</" ~ name ~ ">"); 
     177            dspWriter.put("</" ~ tagname ~ ">"); 
    173178        } 
    174179        popCurrentTag(); 
  • trunk/dsp/servlet/DSPServlet.d

    r97 r99  
    3535private import dsp.servlet.DSPLibrary; 
    3636private import dsp.servlet.DSPLibraryCache; 
     37private import dsp.servlet.ServiceHandle; 
    3738 
    3839private import dsp.DSPException; 
     
    7273    private static Logger logger; 
    7374    protected DSPLibraryCache cache; 
     75    DSPServletContext context; 
    7476 
    7577    static this (){ 
    7678        logger = Logger.getLogger ("dsp.servlets"); 
     79    } 
     80     
     81    public this(DSPServletContext context){ 
     82        this.context = context; 
    7783    } 
    7884 
     
    8288        </desc> 
    8389    */ 
    84     DSPLibrary recoverLibrary(DSPServletContext context,FilePath path){ 
     90    DSPLibrary recoverLibrary(FilePath path){ 
    8591        char[] libraryPath = context.getBasePath ~ "/" ~ context.getConfiguration.get("cache-directory"); 
    8692        libraryPath ~= path.getName[1..$] ~ ".dll"; 
     
    105111        </desc> 
    106112    */ 
    107     DSPLibrary buildLibrary(DSPServletContext context,FilePath path){ 
     113    DSPLibrary buildLibrary(FilePath path){ 
    108114        DSPLibrary lib; 
    109115        RequestConfig cfg; 
     
    136142         
    137143        ServletCompilationContext scc; 
     144        scc.servlet = this; 
    138145        scc.requestConfig = cfg; 
    139146        scc.servletContext = context; 
     
    146153        lib = new DSPLibrary(cfg.destServletFilename); 
    147154 
     155        return lib; 
     156    } 
     157     
     158    protected DSPLibrary getServletServiceHandle(char[] pathInfo,FilePath path){ 
     159        // find the needed dll on the fly 
     160        DSPLibrary lib = cast(DSPLibrary)cache.get(pathInfo); 
     161 
     162        debug{ 
     163            if(lib){ 
     164                printf("cache lib: %.*s = %.*s\n",pathInfo,lib.toString()); 
     165            } 
     166            else{ 
     167                printf("cache lib: %.*s = [not present]\n",request.getPathInfo); 
     168            } 
     169        } 
     170 
     171        // attempt to recover the lib from the file cache 
     172        if(!lib){ 
     173            lib = recoverLibrary(path); 
     174            cache.put(pathInfo,lib); // update the cache with new or empty lib 
     175        } 
     176 
     177        // rebuild the library if recovery fails or is out of date 
     178        if(!lib){ 
     179            lib = buildLibrary(path); 
     180            cache.put(pathInfo,lib); // update the cache 
     181        } 
     182        else{ 
     183            if(lib.isOutOfDate(context.getBasePath,path)){ 
     184                lib.unload(); 
     185                cache.put(pathInfo,null); // remove the old entry 
     186                lib = buildLibrary(path); 
     187                cache.put(pathInfo,lib); // update the cache             
     188            } 
     189        } 
     190         
    148191        return lib; 
    149192    } 
     
    155198            IDSPRequest dspRequest = cast(IDSPRequest)request; 
    156199            IDSPResponse dspResponse = cast(IDSPResponse)response; 
    157             DSPServletContext context = cast(DSPServletContext)request.getContext(); 
    158200 
    159201            assert(dspRequest); 
     
    161203            assert(context); 
    162204             
     205            dspResponse.setServlet(this); 
    163206            dspResponse.setContentType("text/html"); 
    164207 
    165208            // bind to the needed dll on the fly 
    166209             
    167             DSPLibrary lib = cast(DSPLibrary)cache.get(request.getPathInfo); 
    168              
    169             debug{ 
    170                 if(lib){ 
    171                     printf("cache lib: %.*s = %.*s\n",request.getPathInfo,lib.toString()); 
    172                 } 
    173                 else{ 
    174                     printf("cache lib: %.*s = [not present]\n",request.getPathInfo); 
    175                 } 
    176             } 
    177              
    178             // attempt to recover the lib from the file cache 
    179             if(!lib){ 
    180                 lib = recoverLibrary(context,path); 
    181                 cache.put(request.getPathInfo,lib); // update the cache with new or empty lib 
    182             } 
    183              
    184             // rebuild the library if recovery fails or is out of date 
    185             if(!lib){ 
    186                 lib = buildLibrary(context,path); 
    187                 cache.put(request.getPathInfo,lib); // update the cache 
    188             } 
    189             else{ 
    190                 if(lib.isOutOfDate(context.getBasePath,path)){ 
    191                     lib.unload(); 
    192                     cache.put(request.getPathInfo,null); // remove the old entry 
    193                     lib = buildLibrary(context,path); 
    194                     cache.put(request.getPathInfo,lib); // update the cache          
    195                 } 
    196             } 
     210            DSPLibrary lib = getServletServiceHandle(request.getPathInfo,path); 
    197211             
    198212            // run the servlet 
     
    215229        } 
    216230    } 
    217          
     231     
     232    //* delegate class that allows for binding of additional servlets 
     233    class LibraryDynamicHandle{ 
     234        DSPServlet servlet; 
     235        FilePath path; 
     236        char[] requestPathInfo; 
     237         
     238        public this(DSPServlet servlet,FilePath path,char[] requestPathInfo){  
     239            this.servlet = servlet; 
     240            this.path = path; 
     241            this.requestPathInfo = requestPathInfo; 
     242        } 
     243         
     244        void handle(IDSPRequest request,IDSPResponse response){ 
     245            DSPLibrary lib = servlet.getServletServiceHandle(requestPathInfo,path); 
     246            lib.service(request,response); 
     247        } 
     248    } 
     249     
     250    //* delegate class that allows for binding of files as 'servlets' 
     251    class FileDynamicHandle{ 
     252        char[] filename; 
     253        public this(char[] filename){ this.filename = filename; } 
     254        void handle(IDSPRequest request,IDSPResponse response){ 
     255            response.copyFile(request.getContext, filename); 
     256        } 
     257    } 
     258     
     259    //TODO: http:// delegate class 
     260    class HttpDynamicHandle{ 
     261        char[] url; 
     262        public this(char[] url){ this.url = url; } 
     263        void handle(IDSPRequest request,IDSPResponse response){ 
     264            //TODO: generate http request based on passed data 
     265        } 
     266    } 
     267     
     268    //* returns a delegate for the requested binding 
     269    public ServiceHandleDelegate getServletHandleForFile(IDSPRequest request,char[] filename){ 
     270        FilePath requestPath = new FilePath(request.getPathInfo); 
     271        FilePath path = new FilePath(requestPath.getPath() ~ filename); 
     272         
     273        //TODO: include support for http://, https:// and file:// protocols. 
     274         
     275        if(path.getExtension() == "dsp"){ 
     276            return &((new LibraryDynamicHandle(this,path,request.getPathInfo)).handle); 
     277        } 
     278         
     279        return &((new FileDynamicHandle(path.toString())).handle); 
     280    } 
     281             
    218282    debug private import mango.http.utils.Dictionary; 
    219283 
  • trunk/dsp/servlet/DSPServletContext.d

    r97 r99  
    8484         
    8585        // set servlet 
    86         provider.addMapping("/*", provider.addServlet(new DSPServlet(),"dsp",this)); 
     86        provider.addMapping("/*", provider.addServlet(new DSPServlet(this),"dsp",this)); 
    8787    } 
    8888     
  • trunk/dsp/servlet/IDSPResponse.d

    r97 r99  
    2828private import dsp.servlet.RuntimeTag; 
    2929private import dsp.servlet.IDSPRequest; 
     30private import dsp.servlet.DSPServlet; 
    3031 
    3132private import mango.servlet.model.IServletResponse; 
     
    3839     
    3940    void setException(char[] e); 
     41     
     42    void setServlet(DSPServlet servlet); 
     43     
    4044    bit isExceptionSet(); 
    4145    char[] getException(); 
     
    4751    void endTagScope(); 
    4852     
    49     RuntimeTag addTag(char[] tagname,char[] filename); 
    50     void startTag(char[] name); 
     53    RuntimeTag addTag(IDSPRequest request,char[] tagname,char[] filename); 
     54    void startTag(char[] tagname); 
    5155    void setTagAttribute(char[] name,Box value); 
    5256    void endTagAttributes(); 
    53     void endTag(IDSPRequest request,char[] name); 
     57    void endTag(IDSPRequest request,char[] tagname); 
    5458} 
  • trunk/dsp/servlet/RuntimeTag.d

    r98 r99  
    3131struct RuntimeTag{ 
    3232    DSPAttributes attributes; 
    33     ServiceHandle handle; 
     33    ServiceHandleDelegate handle; 
    3434     
    3535    RuntimeTag clone(){ 
  • trunk/dsp/servlet/ServiceHandle.d

    r98 r99  
    3030 
    3131extern(C) alias void function(IDSPRequest request,IDSPResponse response) ServiceHandle; 
     32alias void delegate(IDSPRequest request,IDSPResponse response) ServiceHandleDelegate; 
    3233 
  • trunk/dsp/tags/Include.d

    r98 r99  
    2828private import xml.utf8all; 
    2929 
     30private import dsp.Util; 
    3031private import dsp.ServletGenerator; 
    3132private import dsp.TagLibrary; 
     
    4647        char[] filename = attribs["file"]; 
    4748         
    48         //TODO: find a way to read in a file 
    49         char[] filedata
     49        //find a way to read in a file 
     50        char[] filedata = getEntireConduit(context.servletContext.getResourceAsFile(filename))
    5051         
    5152        // process the data according to 'as' 
     
    6869             
    6970        case "dsp": 
    70             //TODO: find a way to insert the text back into the input stream 
     71            context.parser.insertText(filedata); 
    7172            break; 
    7273             
  • trunk/xml/XMLParser.d

    r97 r99  
    173173        } 
    174174        return null; // dummy        
     175    } 
     176     
     177    public void insertText(CharT[] text){ 
     178        source = source[0..cursor] ~ text ~ source[cursor..source.length]; 
    175179    } 
    176180