org.opentestsystem.shared.mna.client.service.AbstractMnaClient.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.shared.mna.client.service.AbstractMnaClient.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.shared.mna.client.service;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.opentestsystem.shared.mna.client.domain.MnaBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestOperations;
import org.springframework.web.context.ServletContextAware;

/**
 * Abstract client that contains base fields and behavior for all clients to inherit from
 */
public abstract class AbstractMnaClient implements ServletContextAware {

    /**
     * Logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMnaClient.class);

    /**
     * Spring Rest Template
     */
    @Autowired
    protected RestOperations mnaRestTemplate;

    /**
     * Servlet context this client is in
     */
    private ServletContext servletContext;

    /**
     * Base M&A URI
     */
    @Value("${mna.mnaUrl}")
    protected String mnaUri;

    /**
     * A cached value of static attributes needed for communications with Monitoring and Alerting
     * optionally this base can wired and injected via spring
     * if not injected, we will use the servlet context to derive the required values.
     **/
    @Autowired(required = false)
    @Qualifier("baseInfo")
    private MnaBase baseInfo;

    /**
     * Server name this client is running on, usually pulled from an environment variable
     */
    @Value("${mnaServerName:}")
    private String mnaServerName;

    /**
     * Node name this client is running on, usually pulled from an environment variable
     */
    @Value("${mnaNodeName:}")
    private String mnaNodeName;

    /**
     * Populate server, node, component from an MnaBase object
     * 
     * @param baseMna
     */
    protected void populateMnaData(final MnaBase baseMna) {
        baseMna.setServer(getBaseInfo().getServer());
        baseMna.setComponent(getBaseInfo().getComponent());
        baseMna.setNode(getBaseInfo().getNode());
    }

    /**
     * Get the base server, node, component info using system data if need be
     * 
     * @return MnaBase
     */
    protected MnaBase getBaseInfo() {
        if (this.baseInfo == null) {
            try {
                final MnaBase temp = new MnaBase();
                if (StringUtils.isBlank(this.mnaNodeName)) {
                    // if not injected, we will use the servlet context to derive the required values.
                    String nodePath = this.servletContext.getRealPath("");
                    if (nodePath != null) {
                        final String[] separated = nodePath.split("\\" + File.separator);
                        // using the context and deploy directory because you could have one server with 2 jvms running the same app.
                        if (separated.length > 2) {
                            nodePath = separated[separated.length - 2] + "_" + separated[separated.length - 1];
                        } else {
                            nodePath = separated[0];
                        }
                    }
                    temp.setNode(nodePath);
                } else {
                    temp.setNode(this.mnaNodeName);
                }

                if (StringUtils.isBlank(this.mnaServerName)) {
                    final InetAddress address = InetAddress.getLocalHost();
                    final String host = address.getHostName();
                    temp.setServer(host);
                } else {
                    temp.setServer(this.mnaServerName);
                }

                String compName = this.servletContext.getServletContextName();
                if (compName != null) {
                    compName = compName.replaceAll(" ", "_");
                }
                temp.setComponent(compName);

                this.baseInfo = temp;
            } catch (final UnknownHostException e) {
                LOGGER.error("cannot find localHost", e);
            }
        }

        return this.baseInfo;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setServletContext(final ServletContext inServletContext) {
        this.servletContext = inServletContext;
    }

}