<commit_before>package brooklyn.entity.webapp.jboss

import java.util.List;

import brooklyn.entity.basic.Attributes
import brooklyn.util.SshBasedJavaWebAppSetup
import brooklyn.location.basic.SshMachineLocation

public class JBoss6SshSetup extends SshBasedJavaWebAppSetup {
    public static final String DEFAULT_VERSION = "6.0.0.Final"
    public static final String DEFAULT_INSTALL_DIR = DEFAULT_INSTALL_BASEDIR+"jboss/"
    public static final String DEFAULT_DEPLOY_SUBDIR = "deploy"
    public static final int DEFAULT_HTTP_PORT = 8080;

    private String jbossVersion
    private int portIncrement
    private String serverProfile
    
    public static JBoss6SshSetup newInstance(JBossNode entity, SshMachineLocation machine) {
        Integer suggestedJbossVersion = entity.getConfig(JBossNode.SUGGESTED_VERSION)
        String suggestedInstallDir = entity.getConfig(JBossNode.SUGGESTED_INSTALL_DIR)
        String suggestedRunDir = entity.getConfig(JBossNode.SUGGESTED_RUN_DIR)
        Integer suggestedJmxPort = entity.getConfig(JBossNode.SUGGESTED_JMX_PORT)
        String suggestedJmxHost = entity.getConfig(JBossNode.SUGGESTED_JMX_HOST)
        Integer suggestedPortIncrement = entity.getConfig(JBossNode.SUGGESTED_PORT_INCREMENT)
        String suggestedServerProfile = entity.getConfig(JBossNode.SUGGESTED_SERVER_PROFILE)
        
        String jbossVersion = suggestedJbossVersion ?: DEFAULT_VERSION
        String installDir = suggestedInstallDir ?: (DEFAULT_INSTALL_DIR+"jboss-$jbossVersion")
        String runDir = suggestedRunDir ?: (DEFAULT_RUN_DIR+"/"+"app-"+entity.getApplication()?.id+"/jboss-"+entity.id)
        String deployDir = runDir+"/"+DEFAULT_DEPLOY_SUBDIR
        String serverProfile = suggestedServerProfile ?: "default"
        
        String jmxHost = suggestedJmxHost ?: machine.getAddress().getHostName()
        int jmxPort = machine.obtainPort(toDesiredPortRange(suggestedJmxPort, DEFAULT_FIRST_JMX_PORT))
        int portIncrement = suggestedPortIncrement ?: 0
        
        JBoss6SshSetup result = new JBoss6SshSetup(entity, machine)
        result.setJmxPort(jmxPort)
        result.setJmxHost(jmxHost)
        result.setJbossVersion(jbossVersion)
        result.setInstallDir(installDir)
        result.setDeployDir(deployDir)
        result.setRunDir(runDir)
        result.setPortIncrement(portIncrement)
        result.setServerProfile(serverProfile)
        
        return result
    }

    public JBoss6SshSetup(JBossNode entity, SshMachineLocation machine) {
        super(entity, machine)
    }
    
    public JBoss6SshSetup setJbossVersion(String val) {
        this.jbossVersion = val
        return this
    }
    
    public JBoss6SshSetup setPortIncrement(int val) {
        this.portIncrement = val
        return this
    }
    
    public JBoss6SshSetup setServerProfile(String val) {
        this.serverProfile = val
        return this
    }
    
    @Override
    protected void postStart() {
        entity.setAttribute(Attributes.JMX_PORT, jmxPort)
        entity.setAttribute(Attributes.JMX_HOST, jmxHost)
        entity.setAttribute(Attributes.HTTP_PORT, DEFAULT_HTTP_PORT+portIncrement)
    }
    
    public List<String> getInstallScript() {
        String url = "http://downloads.sourceforge.net/project/jboss/JBoss/JBoss-${jbossVersion}/jboss-as-distribution-${jbossVersion}.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjboss%2Ffiles%2FJBoss%2F${jbossVersion}%2F&ts=1307104229&use_mirror=kent"
        String saveAs  = "jboss-as-distribution-${jbossVersion}"
        
        // Note the -o option to unzip, to overwrite existing files without warning.
        // The JBoss zip file contains lgpl.txt (at least) twice and the prompt to
        // overwrite interrupts the installer.
        makeInstallScript([
	            "curl -L \"${url}\" -o ${saveAs}",
	            "unzip -o ${saveAs}"
            ])
    }

    public List<String> getRunScript() {
        def portGroupName = "ports-brooklyn"
        
        List<String> script = [
            "cd $serverProfile/conf/bindingservice.beans/META-INF/",
            "$installDir/bin/run.sh -Djboss.service.binding.set=$portGroupName -Djboss.server.base.dir=$runDir/server -Djboss.server.base.url=file://$runDir/server -c $serverProfile &",
            "sleep 30",
        ]
        return script
    }
    
    public Map<String, String> getRunEnvironment() {
        Map<String, String> env = [
	        "LAUNCH_JBOSS_IN_BACKGROUND" : "1",
	        "JBOSS_HOME" : "$installDir",
	        "JAVA_OPTS" : toJavaDefinesString(getJvmStartupProperties()) + " " +
		        "-Djboss.platform.mbeanserver " +
		        "-Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl " +
		        "-Djava.util.logging.manager=org.jboss.logmanager.LogManager " +
		        "-Dorg.jboss.logging.Logger.pluginClass=org.jboss.logging.logmanager.LoggerPluginImpl",
	        "JBOSS_CLASSPATH" : "$installDir/lib/jboss-logmanager.jar",
        ]<commit_msg>Updating JBoss startup script commands to use environment variables<commit_after>package brooklyn.entity.webapp.jboss

import java.util.List;

import brooklyn.entity.basic.Attributes
import brooklyn.util.SshBasedJavaWebAppSetup
import brooklyn.location.basic.SshMachineLocation

public class JBoss6SshSetup extends SshBasedJavaWebAppSetup {
    public static final String DEFAULT_VERSION = "6.0.0.Final"
    public static final String DEFAULT_INSTALL_DIR = DEFAULT_INSTALL_BASEDIR+"jboss/"
    public static final String DEFAULT_DEPLOY_SUBDIR = "deploy"
    public static final int DEFAULT_HTTP_PORT = 8080;

    private String jbossVersion
    private int portIncrement
    private String serverProfile
    
    public static JBoss6SshSetup newInstance(JBossNode entity, SshMachineLocation machine) {
        Integer suggestedJbossVersion = entity.getConfig(JBossNode.SUGGESTED_VERSION)
        String suggestedInstallDir = entity.getConfig(JBossNode.SUGGESTED_INSTALL_DIR)
        String suggestedRunDir = entity.getConfig(JBossNode.SUGGESTED_RUN_DIR)
        Integer suggestedJmxPort = entity.getConfig(JBossNode.SUGGESTED_JMX_PORT)
        String suggestedJmxHost = entity.getConfig(JBossNode.SUGGESTED_JMX_HOST)
        Integer suggestedPortIncrement = entity.getConfig(JBossNode.SUGGESTED_PORT_INCREMENT)
        String suggestedServerProfile = entity.getConfig(JBossNode.SUGGESTED_SERVER_PROFILE)
        
        String jbossVersion = suggestedJbossVersion ?: DEFAULT_VERSION
        String installDir = suggestedInstallDir ?: (DEFAULT_INSTALL_DIR+"jboss-$jbossVersion")
        String runDir = suggestedRunDir ?: (DEFAULT_RUN_DIR+"/"+"app-"+entity.getApplication()?.id+"/jboss-"+entity.id)
        String deployDir = runDir+"/"+DEFAULT_DEPLOY_SUBDIR
        String serverProfile = suggestedServerProfile ?: "default"
        
        String jmxHost = suggestedJmxHost ?: machine.getAddress().getHostName()
        int jmxPort = machine.obtainPort(toDesiredPortRange(suggestedJmxPort, DEFAULT_FIRST_JMX_PORT))
        int portIncrement = suggestedPortIncrement ?: 0
        
        JBoss6SshSetup result = new JBoss6SshSetup(entity, machine)
        result.setJmxPort(jmxPort)
        result.setJmxHost(jmxHost)
        result.setJbossVersion(jbossVersion)
        result.setInstallDir(installDir)
        result.setDeployDir(deployDir)
        result.setRunDir(runDir)
        result.setPortIncrement(portIncrement)
        result.setServerProfile(serverProfile)
        
        return result
    }

    public JBoss6SshSetup(JBossNode entity, SshMachineLocation machine) {
        super(entity, machine)
    }
    
    public JBoss6SshSetup setJbossVersion(String val) {
        this.jbossVersion = val
        return this
    }
    
    public JBoss6SshSetup setPortIncrement(int val) {
        this.portIncrement = val
        return this
    }
    
    public JBoss6SshSetup setServerProfile(String val) {
        this.serverProfile = val
        return this
    }
    
    @Override
    protected void postStart() {
        entity.setAttribute(Attributes.JMX_PORT, jmxPort)
        entity.setAttribute(Attributes.JMX_HOST, jmxHost)
        entity.setAttribute(Attributes.HTTP_PORT, DEFAULT_HTTP_PORT+portIncrement)
    }
    
    public List<String> getInstallScript() {
        String url = "http://downloads.sourceforge.net/project/jboss/JBoss/JBoss-${jbossVersion}/jboss-as-distribution-${jbossVersion}.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjboss%2Ffiles%2FJBoss%2F${jbossVersion}%2F&ts=1307104229&use_mirror=kent"
        String saveAs  = "jboss-as-distribution-${jbossVersion}"
        
        // Note the -o option to unzip, to overwrite existing files without warning.
        // The JBoss zip file contains lgpl.txt (at least) twice and the prompt to
        // overwrite interrupts the installer.
        makeInstallScript([
	            "curl -L \"${url}\" -o ${saveAs}",
	            "unzip -o ${saveAs}"
            ])
    }

    public List<String> getRunScript() {
        def portGroupName = "ports-brooklyn"
        
        List<String> script = [
            "\$JBOSS_HOME/bin/run.sh -Djboss.service.binding.set=$portGroupName -Djboss.server.base.dir=\$RUN/server -Djboss.server.base.url=file://\$RUN/server -c $serverProfile &",
            "sleep 30",
        ]
        return script
    }
    
    public Map<String, String> getRunEnvironment() {
        Map<String, String> env = [
	        "LAUNCH_JBOSS_IN_BACKGROUND" : "1",
	        "JBOSS_HOME" : "$installDir",
	        "JAVA_OPTS" : toJavaDefinesString(getJvmStartupProperties()) + " " +
		        "-Djboss.platform.mbeanserver " +
		        "-Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl " +
		        "-Djava.util.logging.manager=org.jboss.logmanager.LogManager " +
		        "-Dorg.jboss.logging.Logger.pluginClass=org.jboss.logging.logmanager.LoggerPluginImpl",
	        "JBOSS_CLASSPATH" : "$installDir/lib/jboss-logmanager.jar",
	        "RUN" : "$runDir",
        ]
{'fraction_non_alphanumeric': 0.09277733439744613, 'fraction_numerical': 0.007581803671189146, 'mean_word_length': 3.1805671392827355, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4824987', 'n_tokens_mistral': 3237, 'n_tokens_neox': 3055, 'n_words': 506}