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

org.glassfish.scripting.rails.RailsDeployer Maven / Gradle / Ivy

There is a newer version: 1.0-b18
Show newest version
/*
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 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 org.glassfish.scripting.rails;

import org.glassfish.api.admin.ParameterNames;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.container.EndpointRegistrationException;
import org.glassfish.api.container.RequestDispatcher;
import org.glassfish.api.deployment.Deployer;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.deployment.MetaData;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.annotations.Service;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Properties;
import java.io.File;

import com.sun.enterprise.module.bootstrap.StartupContext;

/**
 * Deployer for Rails applications
 *
 * @author Jerome Dochez
 * @author Vivek Pandey
 */
@Service
public class RailsDeployer implements Deployer {

    @Inject
    ServerEnvironment env;

    @Inject
    RequestDispatcher dispatcher;

    private JRubyEnvironment jrubyEnv;
    private Logger logger;


    public boolean prepare(DeploymentContext context) {
        return true;
    }

    /**
     * Loads the meta date associated with the application.
     *
     * @parameters type type of metadata that this deployer has declared providing.
     */
    public  T loadMetaData(Class type, DeploymentContext context) {
        return null;
    }

    /**
     * Returns the meta data assocated with this Deployer
     *
     * @return the meta data for this Deployer
     */
    public MetaData getMetaData() {
        return new MetaData(false, null, null);
    }

    public RailsApplication load(RailsContainer container, DeploymentContext context) {
        String contextRoot = getContextRoot(context);
        String railsRoot = context.getSourceDir().getAbsolutePath();
        this.logger = context.getLogger();
        this.jrubyEnv = new JRubyEnvironment(env.getStartupContext(), context.getProps());
        RailsApplication railsApplication = new RailsApplication(jrubyEnv, railsRoot, context.getLogger(), contextRoot);

        context.getLogger().info(Messages.format(Messages.INFO_LOADING_APP,
                context.getCommandParameters().getProperty(ParameterNames.NAME),
                contextRoot));
        try {
            //Apparently grizzly can not register adapters at '/' but it can at "", related to issue#6609
            if(contextRoot.equals("/"))
                contextRoot = "";
            dispatcher.registerEndpoint(contextRoot, railsApplication.getAdapter(), railsApplication);
        } catch (EndpointRegistrationException e) {
            context.getLogger().log(Level.SEVERE,
                    Messages.format(Messages.ERR_REGISTRY_RAILSAPP,
                            context.getCommandParameters().getProperty(ParameterNames.NAME)), e);
        }
        return railsApplication;
    }

    public void unload(RailsApplication container, DeploymentContext context) {
        try {
            dispatcher.unregisterEndpoint(container.getContextRoot());
        } catch (EndpointRegistrationException e) {
            context.getLogger().log(Level.SEVERE,
                    Messages.format(Messages.ERR_UNLOAD_APP, container.getContextRoot()), e);
        }
    }

    public void clean(DeploymentContext context) {
    }

    private String getContextRoot(DeploymentContext context) {
        String contextRoot = context.getCommandParameters().getProperty(ParameterNames.CONTEXT_ROOT);
        if (contextRoot == null) {
            contextRoot = (String) env.getStartupContext().getArguments().get("--contextroot");
        }
        if (contextRoot == null || contextRoot.length() == 0) {
            contextRoot = "/" + context.getCommandParameters().getProperty(ParameterNames.NAME);
        }
        return contextRoot;
    }

    public final class JRubyEnvironment {
        final String jrubyHome = System.getProperty("jruby.home");
        private final String jrubyMinRuntime = System.getProperty("jruby.runtime.min");
        private final String jrubyMaxRuntime = System.getProperty("jruby.runtime.max");
        final int numberOfRuntime;
        final String jrubyLib;
        final int minRuntime;
        final int maxRuntime;
        final File rootDir;

        private static final String JRUBY_RUNTIME = "jruby-runtime";
        private static final String JRUBY_RUNTIME_MIN = "jruby-runtime-min";
        private static final String JRUBY_RUNTIME_MAX = "jruby-runtime-max";
        private static final String RAILS_ENV = "rails-env";
        
        private final String jrubyRuntime = System.getProperty("jruby.runtime");
        final String railsEnv;

        public JRubyEnvironment(StartupContext startupContext, Properties props) {
            if (jrubyHome == null) {
                logger.severe(Messages.format(Messages.ERR_JRUBYHOME_NULL));
                throw new IllegalArgumentException(Messages.format(Messages.ERR_JRUBYHOME_NULL));
            }

            if(System.getenv("RAILS_ENV") != null){
                this.railsEnv = System.getenv("RAILS_ENV");
                props.put(RAILS_ENV, this.railsEnv);
            }else{
                this.railsEnv = (props.getProperty(RAILS_ENV)==null)?"development":props.getProperty(RAILS_ENV);
            }

            this.rootDir = startupContext.getRootDirectory();

            int numRt = (props.getProperty(JRUBY_RUNTIME) == null)?1:Integer.valueOf(props.getProperty(JRUBY_RUNTIME));
            int numMinRt = (props.getProperty(JRUBY_RUNTIME_MIN) == null)?-1:Integer.valueOf(props.getProperty(JRUBY_RUNTIME_MIN));
            int numMaxRt = (props.getProperty(JRUBY_RUNTIME_MAX) == null)?-1:Integer.valueOf(props.getProperty(JRUBY_RUNTIME_MAX));

            if (jrubyRuntime != null) {
                try {
                    numRt = Integer.parseInt(jrubyRuntime);
                    props.setProperty(JRUBY_RUNTIME, String.valueOf(numRt));
                } catch (NumberFormatException ex) {
                    logger.log(Level.WARNING, Messages.format(Messages.ERR_INVALID_RUNTIMES, jrubyRuntime), ex);
                }
            }

            if (jrubyMinRuntime != null) {
                try {
                    numMinRt = Integer.parseInt(jrubyMinRuntime);
                    props.setProperty(JRUBY_RUNTIME_MIN, String.valueOf(numMinRt));
                } catch (NumberFormatException ex) {
                    logger.log(Level.WARNING, Messages.format(Messages.ERR_INVALID_RUNTIMES_MIN, jrubyMinRuntime), ex);
                }
            }


            if (jrubyMaxRuntime != null) {
                try {
                    numMaxRt = Integer.parseInt(jrubyMaxRuntime);
                    props.setProperty(JRUBY_RUNTIME_MAX, String.valueOf(numMaxRt));
                } catch (NumberFormatException ex) {
                    logger.log(Level.WARNING, Messages.format(Messages.ERR_INVALID_RUNTIMES_MAX, jrubyMaxRuntime), ex);
                }
            }

            minRuntime = numMinRt;
            maxRuntime = numMaxRt;

            //check the commandline, above whatever we do is for legacy
            if(startupContext.getArguments().get("--runtimes") != null){
                String nrt = (String) startupContext.getArguments().get("--runtimes");
                try {
                    numRt = Integer.parseInt(nrt);
                    props.put(JRUBY_RUNTIME, numRt);
                } catch (NumberFormatException ex) {
                    logger.log(Level.WARNING, Messages.format(Messages.ERR_INVALID_RUNTIMES, nrt));
                }
            }

            numberOfRuntime = numRt;
            jrubyLib = jrubyHome + "/lib";
        }
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy