FileDocCategorySizeDatePackage
RailsAdapter.javaAPI DocGlassfish v2 API8945Thu Jun 21 14:52:16 BST 2007com.sun.grizzly.rails

RailsAdapter

public class RailsAdapter extends com.sun.enterprise.web.connector.grizzly.standalone.StaticResourcesAdapter implements org.apache.coyote.Adapter
Adapter implementation that brige JRuby on Rails with Grizzly.
author
TAKAI Naoto
author
Jean-Francois Arcand

Fields Summary
private static final String
RFC_2616_FORMAT
private static final int
RAILS_TOKEN
private final String
publicDirectory
private RubyObjectPool
pool
private RailAsyncFilter
railAsyncFilter
Constructors Summary
public RailsAdapter(RubyObjectPool pool, RailAsyncFilter railAsyncFilter)

    
         
        super();
        this.pool = pool;
        this.publicDirectory = pool.getRailsRoot() + "/public";
        this.railAsyncFilter = railAsyncFilter;
    
Methods Summary
public voidafterService(org.apache.coyote.Request req, org.apache.coyote.Response res)

        try {
            req.action(ActionCode.ACTION_POST_REQUEST, null);
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            // Recycle the wrapper request and response
            req.recycle();
            res.recycle();
        }
    
public voidfireAdapterEvent(java.lang.String type, java.lang.Object data)

    
private java.nio.channels.SocketChannelgetChannel(org.apache.coyote.Response res)

        SocketChannelOutputBuffer buffer = (SocketChannelOutputBuffer) res.getOutputBuffer();
        SocketChannel channel = buffer.getChannel();

        return channel;
    
private booleanmodifiedSince(org.apache.coyote.Request req, java.io.File file)

        try {
            String since = req.getMimeHeaders().getHeader("If-Modified-Since");
            if (since == null) {
                return false;
            }

            Date date = new SimpleDateFormat(RFC_2616_FORMAT, Locale.US).parse(since);
            if (date.getTime() > file.lastModified()) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            return false;
        }
    
public voidservice(org.apache.coyote.Request req, org.apache.coyote.Response res)

 
        MessageBytes mb = req.requestURI();
        ByteChunk requestURI = mb.getByteChunk();

        try{
            String uri = requestURI.toString();
            File file = new File(publicDirectory,uri);
            if (file.isDirectory()) {
                uri += "index.html";
                file = new File(file,uri);
            }

            if (file.canRead()) {
                super.service(req, res);      
                return;
            } else {
                serviceRails(req, res);
            }

            res.finish();
        } catch (Exception e) {
            if (SelectorThread.logger().isLoggable(Level.SEVERE)) {
                SelectorThread.logger().log(Level.SEVERE, e.getMessage());
            }

            throw e;
        }
    
private voidserviceRails(org.apache.coyote.Request req, org.apache.coyote.Response res)

        Ruby runtime = null;            
        RailsToken rt = (RailsToken)req.getNote(RAILS_TOKEN);
        if (rt == null){
            rt = new RailsToken();
        }
        rt.req = req;        
        try {
            runtime = pool.bollowRuntime();
            if (runtime == null){
                throw new IllegalStateException();
            }

            req.doRead(rt.readChunk);
            ((InternalOutputBuffer)res.getOutputBuffer()).commit();
            res.setCommitted(true);

            IRubyObject reqObj = JavaEmbedUtils.javaToRuby(runtime, req);
            IRubyObject loggerObj = JavaEmbedUtils.javaToRuby(runtime, SelectorThread.logger());

            OutputStream os = 
                ((InternalOutputBuffer)res.getOutputBuffer()).getOutputStream();
            
            RubyIO iObj = new RubyIO(runtime, rt.inputStream);
            RubyIO oObj = new RubyIO(runtime, os);

            runtime.defineReadonlyVariable("$req", reqObj);
            runtime.defineReadonlyVariable("$stdin", iObj);
            runtime.defineReadonlyVariable("$stdout", oObj);
            runtime.defineReadonlyVariable("$logger", loggerObj);

            runtime.getLoadService().load("dispatch.rb");
        } catch (RaiseException e) {
            RubyException exception = e.getException();

            System.err.println(e.getMessage());
            exception.printBacktrace(System.err);

            throw e;
        } finally {
            rt.recycle();
            req.setNote(RAILS_TOKEN,rt);
            if (runtime != null) {
                pool.returnRuntime(runtime);
                railAsyncFilter.unpark();
            }
        }