alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (ServerEnvironmentImpl.java)

This example Glassfish source code file (ServerEnvironmentImpl.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

asenvpropertyreader, das, default_admin_console_app_name, file, file, io, override, override, runtimetype, serverenvironmentimpl, serverenvironmentimpl, startupcontext, status, string, string, util

The Glassfish ServerEnvironmentImpl.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2006-2010 Oracle and/or its affiliates. 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_1_1.html
 * or packager/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 packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [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.server;

import com.sun.enterprise.module.bootstrap.StartupContext;
import com.sun.enterprise.universal.glassfish.ASenvPropertyReader;
import com.sun.enterprise.util.SystemPropertyConstants;

import java.io.*;
import java.util.*;

import org.glassfish.api.admin.RuntimeType;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.component.PostConstruct;
import org.glassfish.api.admin.ServerEnvironment;

/**
 * Defines various global configuration for the running GlassFish instance.
 *
 * <p>
 * This primarily replaces all the system variables in V2.
 *
 * @author Jerome Dochez
 * @author Byron Nevins
 */
@Service
public class ServerEnvironmentImpl implements ServerEnvironment, PostConstruct {
    @Inject
    StartupContext startupContext;

    /** folder where all generated code like compiled jsps, stubs is stored */
    public static final String kGeneratedDirName = "generated";
    public static final String kRepositoryDirName = "applications";
    public static final String kEJBStubDirName = "ejb";
    public static final String kGeneratedXMLDirName = "xml";
    public static final String kPolicyFileDirName = "policy";

    public static final String kConfigXMLFileName = "domain.xml";
    public static final String kConfigXMLFileNameBackup = "domain.xml.bak";
    public static final String kLoggingPropertiesFileName = "logging.properties";
    /** folder where the configuration of this instance is stored */
    public static final String kConfigDirName = "config";
    /** init file name */
    public static final String kInitFileName = "init.conf";

    public static final String DEFAULT_ADMIN_CONSOLE_CONTEXT_ROOT = "/admin";
    public static final String DEFAULT_ADMIN_CONSOLE_APP_NAME     = "__admingui"; //same as folder
    
    private /*almost final*/ File root;
    private /*almost final*/ boolean verbose;
    private /*almost final*/ boolean debug;
    private ASenvPropertyReader asenv;
    private /*almost final*/ String domainName;
    private /*almost final*/ String instanceName;
    private RuntimeType serverType = RuntimeType.DAS; //set to DAS to avoid null

    private final static String INSTANCE_ROOT_PROP_NAME = "com.sun.aas.instanceRoot";
    private static final String INSTALL_ROOT_PROP_NAME = "com.sun.aas.installRoot";

    /**
     * Compute all the values per default.
     */
    public ServerEnvironmentImpl() {
    }

    public ServerEnvironmentImpl(File root) {
        // the getParentFile() that we do later fails to work correctly if
        // root is for example "new File(".")
        this.root = root.getAbsoluteFile();
        asenv = new ASenvPropertyReader();
    }

    /**
     * This is where the real initialization happens.
     */
    @Override
    public void postConstruct() {

        // todo : dochez : this will need to be reworked...
        String installRoot = startupContext.getArguments().getProperty(INSTALL_ROOT_PROP_NAME);
        if (installRoot == null) {
            // Sahoo: This is truely a bad piece of code. During unit testing, we find an empty StartupContext.
            // To be consistent with earlier code (i.e., code that relied on StartupContext.getRootDirectory()),
            // I am setting user.dir as installRoot.
            installRoot = System.getProperty("user.dir");
        }
        asenv = new ASenvPropertyReader(new File(installRoot));

        // default
        if(this.root==null) {
            String envVar = System.getProperty(INSTANCE_ROOT_PROP_NAME);
            if (envVar!=null) {
                root = new File(envVar);
            } else {
                String instanceRoot = startupContext.getArguments().getProperty(INSTANCE_ROOT_PROP_NAME);
                if (instanceRoot == null) {
                    // In client container, instanceRoot is not set. It is a different question altogether as to why
                    // an object called ServerEnvironmentImpl is at all active in client runtime. To be consistent
                    // with earlier code, we use installRoot as instanceRoot.
                    instanceRoot = installRoot;
                }
                root = new File(instanceRoot);
            }
        }

        asenv.getProps().put(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, root.getAbsolutePath());
        for (Map.Entry<String, String> entry : asenv.getProps().entrySet()) {

            File location = new File(entry.getValue());
            if (!location.isAbsolute()) {
                location = new File(asenv.getProps().get(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY), entry.getValue());
            }
            System.setProperty(entry.getKey(), location.getAbsolutePath());
        }
        
        Properties args = startupContext.getArguments();

        verbose = Boolean.parseBoolean(args.getProperty("-verbose"));
        debug = Boolean.parseBoolean(args.getProperty("-debug"));

        // ugly code because domainName & instanceName are final...
        String s = args.getProperty("-domainname");

        if (!ok(s)) {
            s = root.getName();
        }
        domainName = s;

        s = args.getProperty("-instancename");

        if (!ok(s)) {
            instanceName = "server";
        }
        else {
            instanceName = s;
        }
        // bnevins IT 10209
        asenv.getProps().put(SystemPropertyConstants.SERVER_NAME, instanceName);
        System.setProperty(SystemPropertyConstants.SERVER_NAME, instanceName);

        // bnevins Apr 2010 adding clustering support...
        String typeString = args.getProperty("-type");
        serverType = RuntimeType.getDefault();

        try {
            if(typeString != null)
                serverType = RuntimeType.valueOf(typeString);
        }
        catch(Exception e) {
            // already handled above...
        }
    }

    // XXX - many of these methods should be on ServerEnvironment

    public String getDomainName() {
        return domainName;
    }

    @Override
    public File getInstanceRoot() {
        return root;
    }

    /**
     * @return the instance root
     * @deprecated  As of GlassFish 3.1 replaced with {@link #getInstanceRoot() }
     */
    @Deprecated
    @Override
    public File getDomainRoot() {
        return getInstanceRoot();
    }


    @Override
    public StartupContext getStartupContext() {
        return startupContext;
    }

    /**
     * Gets the directory to store configuration.
     * Normally {@code ROOT/config}
     */
    @Override
    public File getConfigDirPath() {
        return new File(root,kConfigDirName);
    }

    /**
     * Gets the directory to store deployed applications
     * Normally {@code ROOT/applications}
     */
    @Override
    public File getApplicationRepositoryPath() {
        return new File(root,kRepositoryDirName);
    }

    /**
     * Gets the directory to store generated stuff.
     * Normally {@code ROOT/generated}
     */
    @Override
    public File getApplicationStubPath() {
        return new File(root,kGeneratedDirName);
    }

    /**
     * Gets the <tt>init.conf file.
     */
    public File getInitFilePath() {
        return new File(getConfigDirPath(),kInitFileName);
    }

    /**
     * Gets the directory for hosting user-provided jar files.
     * Normally {@code ROOT/lib}
     */
    @Override
    public File getLibPath() {
        return new File(root,"lib");

    }

    @Override
    public File getApplicationEJBStubPath() {
        return new File(getApplicationStubPath(), kEJBStubDirName);
    }

    @Override
    public File getApplicationGeneratedXMLPath() {
        return new File(getApplicationStubPath(),kGeneratedXMLDirName);
    }

    /**
     * Returns the path for compiled JSP Pages from an application
     * that is deployed on this instance. By default all such compiled JSPs
     * should lie in the same folder.
     */
    @Override
    public File getApplicationCompileJspPath() {
        return new File(getApplicationStubPath(),kCompileJspDirName);
    }

    /**
     * Returns the path for policy files for applications
     * deployed on this instance.
     */
    @Override
    public File getApplicationPolicyFilePath() {
        return new File(getApplicationStubPath(),kPolicyFileDirName);
    }

    /*
     * XXX - no one is using these methods, so I'm commenting them out
     * for now.  When they're needed, they should probably be added to
     * the ServerEnvironment Interface.
     *
    public String getJavaWebStartPath() {
        return null;
    }

    public String getApplicationBackupRepositoryPath() {
        return null;
    }

    public String getInstanceClassPath() {
        return null;
    }
     */

    public Map<String, String> getProps() {
        return Collections.unmodifiableMap(asenv.getProps());
    }
    
    /** Returns the folder where the admin console application's folder (in the
     *  name of admin console application) should be found. Thus by default,
     *  it should be: [install-dir]/lib/install/applications. No attempt is made
     *  to check if this location is readable or writable.
     *  @return java.io.File representing parent folder for admin console application
     *   Never returns a null
     */
    public File getDefaultAdminConsoleFolderOnDisk() {
        File install = new File(asenv.getProps().get(SystemPropertyConstants.INSTALL_ROOT_PROPERTY));
        File agp = new File(new File(new File(install, "lib"), "install"), "applications");
        return (agp);
    }

    @Override
    public File getMasterPasswordFile() {
        return new File (getInstanceRoot(), "master-password");
    }

    @Override
    public File getJKS() {
        return new File (getConfigDirPath(), "keystore.jks");
    }

    @Override
    public File getTrustStore() {
        return new File(getConfigDirPath(), "cacerts.jks");
    }

    @Override
    public File getDomainXML() {
        return new File(getConfigDirPath(), "domain.xml");
    }

    private Status status=Status.starting;
    @Override
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public boolean isEmbedded() {
        return serverType == RuntimeType.EMBEDDED;
    }

    @Override
    public RuntimeType getRuntimeType() {
        return serverType;
    }
 /**
     * Every server has a name that can be found in the server element in domain.xml
     * @return the name of this server i.e. "my" name
     */
    @Override
    public String getInstanceName() {
        return instanceName;
    }

    /**
     * Am I a running GlassFish server instance?
     * @return true if we are an instance
     */
    @Override
    public boolean isInstance() {
        return serverType == RuntimeType.INSTANCE;
    }
    
    /**
     * Am I a running GlassFish DAS server?
     * @return true if we are a DAS
     */
    @Override
    public boolean isDas() {
        return serverType == RuntimeType.DAS;
    }

    private boolean ok(String s) {
        return s != null && s.length() > 0;
    }
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish ServerEnvironmentImpl.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.