it.scoppelletti.programmerpower.wui.WebApplicationRegistry.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.programmerpower.wui.WebApplicationRegistry.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.programmerpower.wui;

import java.util.*;
import javax.persistence.*;
import javax.servlet.*;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.transaction.annotation.*;
import it.scoppelletti.programmerpower.*;
import it.scoppelletti.programmerpower.data.*;
import it.scoppelletti.programmerpower.reflect.*;
import it.scoppelletti.programmerpower.types.*;

/**
 * Registro delle applicazioni Web in esecuzione.
 *
 * @since 1.0.0
 */
@Final
@Transactional
public class WebApplicationRegistry implements WebApplicationManager, ServletContextListener {
    private static final Logger myLogger = LoggerFactory.getLogger(WebApplicationRegistry.class);

    private String myBaseUrl;

    @PersistenceContext(unitName = DataUtils.PERSISTENCE_UNIT)
    private EntityManager myEntityMgr;

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

    @Transactional(propagation = Propagation.SUPPORTS)
    public String getBaseUrl() {
        if (Strings.isNullOrEmpty(myBaseUrl)) {
            throw new PropertyNotSetException(toString(), "baseUrl");
        }

        return myBaseUrl;
    }

    /**
     * Imposta l&rsquo;indirizzo HTTP dell&rsquo;application server.
     * 
     * @param value Valore.
     */
    @Required
    public void setBaseUrl(String value) {
        myBaseUrl = value;
    }

    @Transactional(readOnly = true)
    public List<String> listRunningApplications() {
        String ctxPath;
        Query query;
        List<String> list;

        list = new ArrayList<String>();
        query = myEntityMgr
                .createQuery("select contextPath from WebApplication " + "where startTime <= CURRENT_TIMESTAMP and "
                        + "(endTime is null or endTime > CURRENT_TIMESTAMP) " + "order by contextPath");
        for (Object item : query.getResultList()) {
            ctxPath = (String) item;
            list.add(ctxPath);
        }

        return list;
    }

    /**
     * Inizializzazione di un&rsquo;applicazione Web.
     *
     * <P>Questa implementazione del metodo {@code contextInitialized} imposta
     * l&rsquo;istante di avvio dell&rsquo;applicazione Web nel registro delle
     * applicazioni (se l&rsquo;applicazione non esiste nel registro la 
     * inserisce).</P>
     * 
     * @param event Evento.
     */
    public void contextInitialized(ServletContextEvent event) {
        registerApplication(event.getServletContext().getContextPath());
    }

    /**
     * Registra un&rsquo;applicazione Web.
     * 
     * @param ctxPath contesto dell&rsquo;applicazione.
     */
    private void registerApplication(String ctxPath) {
        WebApplication appl;

        if (ctxPath == null) {
            throw new ArgumentNullException("ctxPath");
        }

        appl = loadApplication(ctxPath);
        if (appl == null) {
            appl = new WebApplication(ctxPath);
            appl.setStartTime(SimpleTimestamp.getNow());
            myEntityMgr.persist(appl);
        } else {
            appl.setStartTime(SimpleTimestamp.getNow());
            appl.setEndTime(null);
        }
    }

    /**
     * Termine di un&rsquo;applicazione Web.
     * 
     * <P>Questa implementazione del metodo {@code contextDestroyed} imposta
     * l&rsquo;istante di termine dell&rsquo;applicazione Web nel registro delle
     * applicazioni.</P>
     *  
     * @param event Evento.
     */
    public void contextDestroyed(ServletContextEvent event) {
        unregisterApplication(event.getServletContext().getContextPath());
    }

    /**
     * Deregistra un&rsquo;applicazione Web.
     * 
     * @param ctxPath Contesto dell&rsquo;applicazione.
     */
    private void unregisterApplication(String ctxPath) {
        WebApplication appl;

        if (ctxPath == null) {
            throw new ArgumentNullException("ctxPath");
        }

        appl = loadApplication(ctxPath);
        if (appl == null) {
            myLogger.warn("Application {} not registered.", ctxPath);
            return;
        }

        appl.setEndTime(SimpleTimestamp.getNow());
    }

    /**
     * Legge un&rsquo;applicazione Web.
     * 
     * @param  ctxPath Contesto dell&rsquo;applicazione.
     * @return         Oggetto. Se l&rsquo;applicazione non esiste, restituisce
     *                 {@code null}.
     */
    private WebApplication loadApplication(String ctxPath) {
        Query query;
        WebApplication appl;

        query = myEntityMgr.createQuery("from WebApplication where contextPath = :contextPath")
                .setParameter("contextPath", ctxPath);
        try {
            appl = (WebApplication) query.getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }

        return appl;
    }
}