<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.geronimo.commands

import org.apache.geronimo.gshell.command.CommandSupport
import org.apache.geronimo.gshell.command.CommandException
import org.apache.geronimo.gshell.command.annotation.CommandComponent

import org.apache.geronimo.gshell.clp.Option

import org.apache.tools.ant.ExitStatusException

// Make sure we use our custom builder
import org.apache.geronimo.commands.AntBuilder

/**
 * Starts a new Geronimo server instance.
 *
 * @version $Rev$ $Date$
 */
@CommandComponent(id='start-server')
class StartServerCommand
    extends CommandSupport
{
    private AntBuilder ant
    
    @Option(name='-H', aliases=['--home'], metaVar='DIR', description='Use a specific Geronimo home directory')
    File geronimoHome
    
    @Option(name='-j', aliases=['--jvm'], metaVar='DIR', description='Use a specific Java Virtual Machine for server process')
    File javaVirtualMachine
    
    @Option(name='-A', aliases=['--javaagent'], metaVar='JAR', description='Use a specific Java Agent, set to \'none\' to disable')
    String javaAgent
    
    @Option(name='-l', aliases=['--logfile'], description='Capture console output to file')
    File logFile
    
    @Option(name='-b', aliases=['--background'], description='Run the server process in the background')
    boolean background = false
    
    @Option(name='-q', aliases=['--quiet'], description='Suppress informative and warning messages')
    boolean quiet = false
    
    int verbose = 0
    
    @Option(name='-v', aliases=['--verbose'], description='Enable verbose output; specify multipule times to increase verbosity')
    private void increaseVerbosity(boolean flag) {
        if (flag) {
            verbose++
        }
        else {
            verbose--
        }
    }
    
    @Option(name='-t', aliases=['--timeout'], description='Specify the timeout for the server process in seconds')
    int timeout = -1
    
    Map properties = [:]
    
    private void addPropertyFrom(final String nameValue, final String prefix) {
        assert nameValue

        String name, value
        int i = nameValue.indexOf('=')

        if (i == -1) {
            name = nameValue
            value = Boolean.TRUE.toString()
        }
        else {
            name = nameValue.substring(0, i)
            value = nameValue.substring(i + 1, nameValue.length())
        }
        name = name.trim()
        
        if (prefix) {
            name = "${prefix}.$name"
        }
        
        properties[name] = value
    }
    
    @Option(name='-D', aliases=['--property'], metaVar='NAME=VALUE', description='Define system properties')
    private void setPropertyFrom(final String nameValue) {
        addPropertyFrom(nameValue, null)
    }
    
    @Option(name='-G', aliases=['--gproperty'], metaVar='NAME=VALUE', description='Define an org.apache.geronimo property')
    private void setGeronimoPropertyFrom(final String nameValue) {
        addPropertyFrom(nameValue, 'org.apache.geronimo')
    }
    
    @Option(name='-J', aliases=['--javaopt'], metaVar='FLAG', description='Set a JVM flag')
    List<String> javaFlags = []
    
    @Option(name='-m', aliases=['--module'], metaVar='NAME', description='Start up a specific module by name')
    List<String> startModules = []
    
    //
    // TODO: Expose as options, maybe expose a single URI-ish thingy?
    //
    
    String hostname = 'localhost'
    
    int port = 1099
    
    String username = 'system'
    
    String password = '<PASSWORD>'
    
    private File getJavaAgentJar() {
        def file = new File(geronimoHome, 'bin/jpa.jar')
        
        if (javaAgent) {
            if (javaAgent.toLowerCase() == 'none') {
                file = null
            }
            else {
                file = new File(javaAgent)
                
                if (!file.exists()) {
                    log.warn("Disabling Java Agent support; missing jar: $file")
                    file = null
                }
            }
        }
        
        return file
    }
    
    protected Object doExecute() throws Exception {
        ant = new AntBuilder(log, io)
        
        if (!geronimoHome) {
            geronimoHome = new File(System.properties['gshell.home'])
        }
        log.debug("Geronimo home: $geronimoHome")
        
        // Setup default java flags
        if (javaAgentJar) {
            javaFlags << "-javaagent:${javaAgentJar.canonicalPath}"
        }
        
        // Setup the default properties required to boot the server
        properties['org.apache.geronimo.base.dir'] = geronimoHome
        properties['java.io.tmpdir'] = 'var/temp' // Use relative path
        properties['java.endorsed.dirs'] = prefixSystemPath('java.endorsed.dirs', new File(geronimoHome, 'lib/endorsed'))
        properties['java.ext.dirs'] = prefixSystemPath('java.ext.dirs', new File(geronimoHome, 'lib/ext'))<commit_msg>Use ShellInfo to get the homeDir<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.geronimo.commands

import org.apache.geronimo.gshell.command.CommandSupport
import org.apache.geronimo.gshell.command.CommandException
import org.apache.geronimo.gshell.command.annotation.CommandComponent

import org.apache.geronimo.gshell.clp.Option

import org.apache.tools.ant.ExitStatusException

// Make sure we use our custom builder
import org.apache.geronimo.commands.AntBuilder
import org.codehaus.plexus.component.annotations.Requirement
import org.apache.geronimo.gshell.shell.ShellInfo

/**
 * Starts a new Geronimo server instance.
 *
 * @version $Rev$ $Date$
 */
@CommandComponent(id='start-server')
class StartServerCommand
    extends CommandSupport
{
    private AntBuilder ant

    @Requirement
    ShellInfo shellInfo
    
    @Option(name='-H', aliases=['--home'], metaVar='DIR', description='Use a specific Geronimo home directory')
    File geronimoHome
    
    @Option(name='-j', aliases=['--jvm'], metaVar='DIR', description='Use a specific Java Virtual Machine for server process')
    File javaVirtualMachine
    
    @Option(name='-A', aliases=['--javaagent'], metaVar='JAR', description='Use a specific Java Agent, set to \'none\' to disable')
    String javaAgent
    
    @Option(name='-l', aliases=['--logfile'], description='Capture console output to file')
    File logFile
    
    @Option(name='-b', aliases=['--background'], description='Run the server process in the background')
    boolean background = false
    
    @Option(name='-q', aliases=['--quiet'], description='Suppress informative and warning messages')
    boolean quiet = false
    
    int verbose = 0
    
    @Option(name='-v', aliases=['--verbose'], description='Enable verbose output; specify multipule times to increase verbosity')
    private void increaseVerbosity(boolean flag) {
        if (flag) {
            verbose++
        }
        else {
            verbose--
        }
    }
    
    @Option(name='-t', aliases=['--timeout'], description='Specify the timeout for the server process in seconds')
    int timeout = -1
    
    Map properties = [:]
    
    private void addPropertyFrom(final String nameValue, final String prefix) {
        assert nameValue

        String name, value
        int i = nameValue.indexOf('=')

        if (i == -1) {
            name = nameValue
            value = Boolean.TRUE.toString()
        }
        else {
            name = nameValue.substring(0, i)
            value = nameValue.substring(i + 1, nameValue.length())
        }
        name = name.trim()
        
        if (prefix) {
            name = "${prefix}.$name"
        }
        
        properties[name] = value
    }
    
    @Option(name='-D', aliases=['--property'], metaVar='NAME=VALUE', description='Define system properties')
    private void setPropertyFrom(final String nameValue) {
        addPropertyFrom(nameValue, null)
    }
    
    @Option(name='-G', aliases=['--gproperty'], metaVar='NAME=VALUE', description='Define an org.apache.geronimo property')
    private void setGeronimoPropertyFrom(final String nameValue) {
        addPropertyFrom(nameValue, 'org.apache.geronimo')
    }
    
    @Option(name='-J', aliases=['--javaopt'], metaVar='FLAG', description='Set a JVM flag')
    List<String> javaFlags = []
    
    @Option(name='-m', aliases=['--module'], metaVar='NAME', description='Start up a specific module by name')
    List<String> startModules = []
    
    //
    // TODO: Expose as options, maybe expose a single URI-ish thingy?
    //
    
    String hostname = 'localhost'
    
    int port = 1099
    
    String username = 'system'
    
    String password = '<PASSWORD>'
    
    private File getJavaAgentJar() {
        def file = new File(geronimoHome, 'bin/jpa.jar')
        
        if (javaAgent) {
            if (javaAgent.toLowerCase() == 'none') {
                file = null
            }
            else {
                file = new File(javaAgent)
                
                if (!file.exists()) {
                    log.warn("Disabling Java Agent support; missing jar: $file")
                    file = null
                }
            }
        }
        
        return file
    }
    
    protected Object doExecute() throws Exception {
        ant = new AntBuilder(log, io)
        
        if (!geronimoHome) {
            geronimoHome = shellInfo.homeDir
        }
        
        log.debug("Geronimo home: $geronimoHome")
        
        // Setup default java flags
        if (javaAgentJar) {
            javaFlags << "-javaagent:${javaAgentJar.canonicalPath}"
        }
        
        // Setup the default properties required to boot the server
        properties['org.apache.geronimo.base.dir'] = geronimoHome
        properties['java.io.tmpdir'] = 'var/temp' // Use relative path
        properties['java.endorsed.dirs'] = prefixSystemPath('java.endorsed.dirs', new File(geronimoHome, 'lib/endorsed'))
        properties['java.ext.dirs'] = prefixSystemPath('java.ext.dirs', new File(geronimoHome, 'lib/ext'))
{'fraction_non_alphanumeric': 0.11480779176004137, 'fraction_numerical': 0.0022409929322530597, 'mean_word_length': 2.5407384803173634, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '381802', 'n_tokens_mistral': 3230, 'n_tokens_neox': 3186, 'n_words': 916}