<commit_before>/**
 *
 * Copyright 2003-2004 The Apache Software Foundation
 *
 *  Licensed 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.system.serverinfo;

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URL;

import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;

/**
 * Contains information about the server and functions for resolving
 * pathnames.
 *
 * @version $Rev$ $Date$
 */
public class ServerInfo {
    private final String baseDirectory;
    private final File base;
    private final URI baseURI;

    public ServerInfo() {
        baseDirectory = null;
        base = null;
        baseURI = null;
    }

    public ServerInfo(String baseDirectory) throws Exception {
        this.baseDirectory = baseDirectory;

        // force load of server constants
        ServerConstants.getVersion();

        // Before we try the persistent value, we always check the
        // system properties first.  This lets an admin override this
        // on the command line.
        baseDirectory = System.getProperty("geronimo.base.dir", baseDirectory);
        if (baseDirectory == null || baseDirectory.length() == 0) {
            // guess from the location of the jar
            URL url = getClass().getClassLoader().getResource("META-INF/startup-jar");
            if (url == null) {
                throw new IllegalArgumentException("Unable to determine location of startup jar");
            }
            try {
                JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
                url = jarConnection.getJarFileURL();
            } catch (IOException e) {
                throw new IllegalArgumentException("Unable to extract base URL from location");
            }
            baseURI = new URI(url.toString()).resolve("..");
            base = new File(baseURI);
        } else {
            base = new File(baseDirectory);
            baseURI = base.toURI();
        }
        if (!base.isDirectory()) {
            throw new IllegalArgumentException("Base directory is not a directory: " + baseDirectory);
        }
        //System.setProperty("geronimo.base.dir", base.getAbsolutePath());
    }

    /**<commit_msg>Re-enable setting the system property<commit_after>/**
 *
 * Copyright 2003-2004 The Apache Software Foundation
 *
 *  Licensed 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.system.serverinfo;

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URL;

import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;

/**
 * Contains information about the server and functions for resolving
 * pathnames.
 *
 * @version $Rev$ $Date$
 */
public class ServerInfo {
    private final String baseDirectory;
    private final File base;
    private final URI baseURI;

    public ServerInfo() {
        baseDirectory = null;
        base = null;
        baseURI = null;
    }

    public ServerInfo(String baseDirectory) throws Exception {
        this.baseDirectory = baseDirectory;

        // force load of server constants
        ServerConstants.getVersion();

        // Before we try the persistent value, we always check the
        // system properties first.  This lets an admin override this
        // on the command line.
        baseDirectory = System.getProperty("geronimo.base.dir", baseDirectory);
        if (baseDirectory == null || baseDirectory.length() == 0) {
            // guess from the location of the jar
            URL url = getClass().getClassLoader().getResource("META-INF/startup-jar");
            if (url == null) {
                throw new IllegalArgumentException("Unable to determine location of startup jar");
            }
            try {
                JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
                url = jarConnection.getJarFileURL();
            } catch (IOException e) {
                throw new IllegalArgumentException("Unable to extract base URL from location");
            }
            baseURI = new URI(url.toString()).resolve("..");
            base = new File(baseURI);
        } else {
            base = new File(baseDirectory);
            baseURI = base.toURI();
        }
        if (!base.isDirectory()) {
            throw new IllegalArgumentException("Base directory is not a directory: " + baseDirectory);
        }
        System.setProperty("geronimo.base.dir", base.getAbsolutePath());
    }

    /**
{'fraction_non_alphanumeric': 0.09265795975997176, 'fraction_numerical': 0.004588775150017649, 'mean_word_length': 2.550751879699248, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4743231', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1419, 'n_words': 498}