All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.grizzly.jruby.RubyObjectPool Maven / Gradle / Ivy

/*
 * 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
 * 
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License. You can obtain
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the GPL Version 2 section of the License file that
 * accompanied this code.  If applicable, add the following below the License
 * Header, with the fields enclosed by brackets [] replaced by your own
 * identifying information: "Portions Copyrighted [year]
 * [name of copyright owner]"
 * 
 * Contributor(s):
 * 
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 *
 */
package com.sun.grizzly.jruby;

import com.sun.grizzly.http.SelectorThread;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.LoadService;
import org.jruby.util.ClassCache;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

/**
 * An object pool for ruby runtime.
 *
 * TODO: Make the pool auto increase and provide more config flexibility
 *
 * @author TAKAI Naoto
 * @author Pramod Gopinath
 * @author Vivek Pandey
 */
public class RubyObjectPool {

    /**
     * How long to wait before given up.
     */
    public static final long DEFAULT_TIMEOUT = 360L;

    /**
     * JRUBY_LIB directory
     */
    private final String jrubyLib;

    /**
     * The number of runtime.
     */
    private final int defaultNumberOfRuntime = 5;

    /**
     * Runtime queue
     */
    private final BlockingQueue queue = new LinkedBlockingQueue();

    /**
     * Is Grizzly ARP enabled.
     */
    private final boolean asyncEnabled;

    private final int numberOfRuntime;

    private final String railsRoot;

    /**
     * Create a pool of Ruby runtime. This can be created for the whole container and each of the runtime can be used
     * from the pool to setup Rails instance that serves the request.
     *
     * @param jrubyLib     non-null, location to the directory where jruby libraries can be found
     * @param numRunTime   number of runtime to be created.
     * @param asyncEnabled whether grizzly ARP needs to be enabled
     */
    public RubyObjectPool(String railsRoot, String jrubyLib, int numRunTime, boolean asyncEnabled) {
        this.railsRoot = railsRoot;

        this.jrubyLib = jrubyLib;
        this.asyncEnabled = asyncEnabled;
        if (numRunTime > 0)
            this.numberOfRuntime = numRunTime;
        else
            this.numberOfRuntime = defaultNumberOfRuntime;
    }

    /**
     * Retrives ruby runtime from the object pool.
     *
     * @return JRuby runtime.
     */
    public Ruby borrowRuntime() {
        // Temporarily blocking on the queue. Change this when the request suspend/resume is introduced in v3
        if (isAsyncEnabled()) {
            try {
                return queue.poll(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        } else {
            return queue.poll();
        }
    }

    /**
     * Returns runtime to the object pool.
     *
     * @param runtime - JRuby Rails runtime
     */
    public void returnRuntime(Ruby runtime) {
        queue.offer(runtime);
    }

    /**
     * Starts the object pool.
     */
    public void start() {
        try {
            if (jrubyLib == null) {
                throw new IllegalStateException("jrubyLib or railsRoot can not be null.");
            }
            int pnum = Runtime.getRuntime().availableProcessors();
            ExecutorService exec = Executors.newFixedThreadPool(pnum + 1);

            for (int i = 0; i < numberOfRuntime; i++) {
                exec.execute(new Runnable() {
                    public void run() {
                        long startTime = System.currentTimeMillis();
                        Ruby runtime = initializeRubyRuntime();
                        SelectorThread.logger().log(Level.INFO,
                                "JRuby and Rails instance instantiation took : " +
                                        (System.currentTimeMillis() - startTime) + "ms");
                        queue.offer(runtime);
                    }
                });
            }
            exec.shutdown();
            exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    /**
     * Shutdowns the object pool.
     */
    public void stop() {
        for (Ruby ruby : queue) {
            ruby.tearDown();
        }
        queue.clear();
    }

    private Ruby setupRails(Ruby runtime) {
//        long startTime = System.currentTimeMillis();
        LoadService loadService = runtime.getLoadService();

        // load rails
        loadService.require(railsRoot + "/config/environment");
        loadService.require("cgi/force_nph");
        loadService.require("dispatcher");

//        SelectorThread.logger().log(Level.INFO,
//                "loadRailsLibraries took: " +
//                        (System.currentTimeMillis() - startTime) + "ms");
        IRubyObject responder = (JavaEmbedUtils.newRuntimeAdapter()).eval(runtime, getDispatcherString());
        runtime.defineReadonlyVariable("$responder", responder);
//        SelectorThread.logger().log(Level.INFO,
//                "Rails Application: " + railsRoot + " instance instantiation took : " +
//                        (System.currentTimeMillis() - startTime) + "ms");
        return runtime;
    }

    private Ruby initializeRubyRuntime() {
        ArrayList libs = new ArrayList();
        libs.add("META-INF/jruby.home/lib/ruby/site_ruby/1.8");
        ClassCache cache = new ClassCache(RubyObjectPool.class.getClassLoader());
        RubyInstanceConfig config = new RubyInstanceConfig();
        config.setLoader(RubyObjectPool.class.getClassLoader());
        config.setClassCache(cache);
        Ruby runtime = JavaEmbedUtils.initialize(libs, config);
        return setupRails(runtime);
    }

    /**
     * Gets JRUBY_LIB directory.
     *
     * @return JRUBY_LIB directory.
     */
    public String getJrubyLib() {
        return jrubyLib;
    }

    /**
     * Gets the number of directory.
     *
     * @return the number of directory;
     */
    public int getNumberOfRuntime() {
        return numberOfRuntime;
    }

    protected BlockingQueue getRubyRuntimeQueue() {
        return queue;
    }

    public boolean isAsyncEnabled() {
        return asyncEnabled;
    }

    private String getDispatcherString() {
        String str;
        StringBuffer completeText = new StringBuffer();
        try {
            InputStream is = getClass().getResourceAsStream("/dispatch.rb");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            str = br.readLine();
            while (str != null) {
                completeText.append(str);
                completeText.append("\n");
                str = br.readLine();
            }
        } catch (Exception e) {
            SelectorThread.logger().log(Level.WARNING, "Exception when trying to read the dispatch.rb script", e);
        }

        return completeText.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy