it.scoppelletti.wui.AboutAction.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.wui.AboutAction.java

Source

/*
 * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * 
 * 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 it.scoppelletti.wui;

import java.util.*;
import com.opensymphony.xwork2.*;
import org.restlet.resource.*;
import org.slf4j.*;
import org.springframework.web.util.*;
import it.scoppelletti.programmerpower.reflect.*;
import it.scoppelletti.programmerpower.resources.*;
import it.scoppelletti.programmerpower.web.control.*;
import it.scoppelletti.programmerpower.web.rest.*;
import it.scoppelletti.programmerpower.wui.*;

/**
 * Informazioni sulle applicazioni Web.
 */
@Final
public class AboutAction extends ActionBase {
    private static final long serialVersionUID = 1L;

    /**
     * Identificatore della vista. Il valore della costante &egrave;
     * <CODE>{@value}</CODE>.
     */
    public static final String VIEWID = "it-scoppelletti-wui-about";

    private static final Logger myLogger = LoggerFactory.getLogger(AboutAction.class);

    private transient List<ApplicationInfo> myApplList;

    @javax.annotation.Resource(name = WebApplicationManager.BEAN_NAME)
    private transient WebApplicationManager myApplMgr;

    /**
     * Costruttore.
     */
    public AboutAction() {
    }

    /**
     * Restituisce le informazioni sulle applicazioni in esecuzione.
     * 
     * @return Collezione.
     */
    public List<ApplicationInfo> getApplications() {
        return myApplList;
    }

    @Override
    protected String initViewId() {
        return AboutAction.VIEWID;
    }

    @Override
    public String execute() {
        String uri;
        UriComponentsBuilder uriBuilder;
        ApplicationInfo about;
        ApplicationInfoResource aboutRes;
        List<String> applList;
        List<ApplicationInfo> aboutList;

        aboutList = new ArrayList<ApplicationInfo>();
        applList = myApplMgr.listRunningApplications();
        for (String ctxPath : applList) {
            uriBuilder = UriComponentsBuilder.fromHttpUrl(myApplMgr.getBaseUrl());
            uriBuilder.path(ctxPath).path(ApplicationInfoResource.PATH);
            uriBuilder.queryParam(AbstractServerResource.QUERY_LOCALE, getLocale().toString());

            uri = uriBuilder.build().toUriString();
            aboutRes = ClientResource.create(uri, ApplicationInfoResource.class);
            try {
                about = aboutRes.getApplicationInfo();
            } catch (Exception ex) {
                myLogger.error(String.format("Failed to get %1$s.", uri), ex);
                about = new SimpleApplicationInfo(ctxPath);
            }
            if (about == null) {
                myLogger.error("Failed to get {}.", uri);
                about = new SimpleApplicationInfo(ctxPath);
            }

            aboutList.add(about);
        }

        Collections.sort(aboutList, new ApplicationInfoComparator());
        myApplList = aboutList;

        return Action.SUCCESS;
    }
}