private static String getMessagesFromBibiscoWebSite() {

    String lStrJsonMessages = null;

    mLog.debug("Start getMessagesFromBibiscoWebSite()");

    String lStrVersion = VersionManager.getInstance().getVersion();
    String lStrLanguage = LocaleManager.getInstance().getLocale().getLanguage();

    try {
      ClientConfig lClientConfig = new DefaultClientConfig();
      Client lClient = Client.create(lClientConfig);
      WebResource lWebResource = lClient.resource(ContextManager.getInstance().getURIWeb());
      WebResource.Builder lBuilder =
          lWebResource
              .path("rest")
              .path("messages")
              .path("get")
              .path(lStrVersion)
              .path(lStrLanguage)
              .accept(MediaType.APPLICATION_JSON);
      lStrJsonMessages = lBuilder.get(String.class);
    } catch (Throwable t) {
      // Maybe we are offline...
      mLog.error(t);
    }

    mLog.debug("End getMessagesFromBibiscoWebSite()");

    return lStrJsonMessages;
  }
  private SqlSessionFactory buildSqlSessionFactory(String pStrDBUrl) {

    SqlSessionFactory mSqlSessionFactory;

    mLog.debug("Start buildSqlSessionFactory(String)");
    mLog.debug("DB_URL: ", pStrDBUrl);

    try {
      Reader lReader = Resources.getResourceAsReader(RESOURCE_FILE_NAME);
      Properties lProperties = new Properties();
      lProperties.setProperty("url", pStrDBUrl);
      lProperties.setProperty("username", DB_USERNAME);
      lProperties.setProperty("password", DB_PASSWORD);

      mSqlSessionFactory = new SqlSessionFactoryBuilder().build(lReader, lProperties);
      mLog.debug("SqlSessionFactory built.");

    } catch (IOException e) {
      mLog.error(e);
      throw new BibiscoException(BibiscoException.IO_EXCEPTION);
    }

    mLog.debug("End buildSqlSessionFactory(String)");

    return mSqlSessionFactory;
  }
  public static WebMessage getMessageFromBibiscoWebSite() {

    WebMessage lWebMessage = null;

    mLog.debug("Start getMessageFromBibiscoWebSite()");

    String lStrJson = getMessagesFromBibiscoWebSite();
    if (StringUtils.isNotEmpty(lStrJson)) {
      Gson lGson = new Gson();
      WebMessage[] lWebMessages = lGson.fromJson(lStrJson, WebMessage[].class);

      for (int i = 0; i < lWebMessages.length; i++) {
        mLog.debug(lWebMessages[i].getIdMessage() + " " + lWebMessages[i].getTitle());
        if (showMessage(lWebMessages[i])) {
          lWebMessage = lWebMessages[i];
          break;
        }
      }
    }

    mLog.debug(
        "End getMessageFromBibiscoWebSite() return: ",
        lWebMessage != null ? lWebMessage.getMessage() : "null");

    return lWebMessage;
  }
  public void cleanSqlSessionFactoryProject() {

    mLog.debug("Start cleanSqlSessionFactoryProject()");

    PooledDataSource lPooledDataSource =
        (PooledDataSource)
            mSqlSessionFactoryProject.getConfiguration().getEnvironment().getDataSource();
    lPooledDataSource.forceCloseAll();
    mSqlSessionFactoryProject = null;

    mLog.debug("End cleanSqlSessionFactoryProject()");
  }
  public static void precompileLoadingPage(String lStrURL) {

    mLog.debug("Start precompileLoadingPage()");

    try {
      ClientConfig lClientConfig = new DefaultClientConfig();
      Client lClient = Client.create(lClientConfig);
      WebResource lWebResource = lClient.resource(lStrURL);
      WebResource.Builder lBuilder = lWebResource.accept(MediaType.TEXT_HTML);
      lBuilder.get(String.class);
    } catch (Throwable t) {
      // Maybe we Jetty is not started...
      mLog.error(t);
    }

    mLog.debug("End precompileLoadingPage()");
  }
  private static boolean showMessage(WebMessage pWebMessage) {

    boolean lBlnResult = false;

    mLog.debug("Start showMessage(WebMessage)");

    SqlSessionFactory lSqlSessionFactory =
        SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {

      MessagesMapper lMessagesMapper = lSqlSession.getMapper(MessagesMapper.class);
      Messages lMessages = lMessagesMapper.selectByPrimaryKey(pWebMessage.getIdMessage());

      if (lMessages != null) {
        // message already read: check if i can show another time
        if (lMessages.getNumberOfViews() < pWebMessage.getNumberOfViewsAllowed()) {
          lMessages.setNumberOfViews(lMessages.getNumberOfViews() + 1);
          lMessagesMapper.updateByPrimaryKey(lMessages);
          lBlnResult = true;
        } else {
          lBlnResult = false;
        }
      } else {
        // message not read
        lMessages = new Messages();
        lMessages.setIdMessage(pWebMessage.getIdMessage());
        lMessages.setNumberOfViews(1);
        lMessagesMapper.insert(lMessages);
        lBlnResult = true;
      }

      lSqlSession.commit();

    } catch (Throwable t) {
      mLog.error(t);
      lSqlSession.rollback();
      throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
      lSqlSession.close();
    }

    mLog.debug("End showMessage(WebMessage)");

    return lBlnResult;
  }
/**
 * Manager of http messages.
 *
 * @author Andrea Feccomandi
 */
public class HttpManager {

  private static Log mLog = Log.getInstance(HttpManager.class);

  public static void precompileLoadingPage(String lStrURL) {

    mLog.debug("Start precompileLoadingPage()");

    try {
      ClientConfig lClientConfig = new DefaultClientConfig();
      Client lClient = Client.create(lClientConfig);
      WebResource lWebResource = lClient.resource(lStrURL);
      WebResource.Builder lBuilder = lWebResource.accept(MediaType.TEXT_HTML);
      lBuilder.get(String.class);
    } catch (Throwable t) {
      // Maybe we Jetty is not started...
      mLog.error(t);
    }

    mLog.debug("End precompileLoadingPage()");
  }

  public static WebMessage getMessageFromBibiscoWebSite() {

    WebMessage lWebMessage = null;

    mLog.debug("Start getMessageFromBibiscoWebSite()");

    String lStrJson = getMessagesFromBibiscoWebSite();
    if (StringUtils.isNotEmpty(lStrJson)) {
      Gson lGson = new Gson();
      WebMessage[] lWebMessages = lGson.fromJson(lStrJson, WebMessage[].class);

      for (int i = 0; i < lWebMessages.length; i++) {
        mLog.debug(lWebMessages[i].getIdMessage() + " " + lWebMessages[i].getTitle());
        if (showMessage(lWebMessages[i])) {
          lWebMessage = lWebMessages[i];
          break;
        }
      }
    }

    mLog.debug(
        "End getMessageFromBibiscoWebSite() return: ",
        lWebMessage != null ? lWebMessage.getMessage() : "null");

    return lWebMessage;
  }

  private static String getMessagesFromBibiscoWebSite() {

    String lStrJsonMessages = null;

    mLog.debug("Start getMessagesFromBibiscoWebSite()");

    String lStrVersion = VersionManager.getInstance().getVersion();
    String lStrLanguage = LocaleManager.getInstance().getLocale().getLanguage();

    try {
      ClientConfig lClientConfig = new DefaultClientConfig();
      Client lClient = Client.create(lClientConfig);
      WebResource lWebResource = lClient.resource(ContextManager.getInstance().getURIWeb());
      WebResource.Builder lBuilder =
          lWebResource
              .path("rest")
              .path("messages")
              .path("get")
              .path(lStrVersion)
              .path(lStrLanguage)
              .accept(MediaType.APPLICATION_JSON);
      lStrJsonMessages = lBuilder.get(String.class);
    } catch (Throwable t) {
      // Maybe we are offline...
      mLog.error(t);
    }

    mLog.debug("End getMessagesFromBibiscoWebSite()");

    return lStrJsonMessages;
  }

  private static boolean showMessage(WebMessage pWebMessage) {

    boolean lBlnResult = false;

    mLog.debug("Start showMessage(WebMessage)");

    SqlSessionFactory lSqlSessionFactory =
        SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {

      MessagesMapper lMessagesMapper = lSqlSession.getMapper(MessagesMapper.class);
      Messages lMessages = lMessagesMapper.selectByPrimaryKey(pWebMessage.getIdMessage());

      if (lMessages != null) {
        // message already read: check if i can show another time
        if (lMessages.getNumberOfViews() < pWebMessage.getNumberOfViewsAllowed()) {
          lMessages.setNumberOfViews(lMessages.getNumberOfViews() + 1);
          lMessagesMapper.updateByPrimaryKey(lMessages);
          lBlnResult = true;
        } else {
          lBlnResult = false;
        }
      } else {
        // message not read
        lMessages = new Messages();
        lMessages.setIdMessage(pWebMessage.getIdMessage());
        lMessages.setNumberOfViews(1);
        lMessagesMapper.insert(lMessages);
        lBlnResult = true;
      }

      lSqlSession.commit();

    } catch (Throwable t) {
      mLog.error(t);
      lSqlSession.rollback();
      throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
      lSqlSession.close();
    }

    mLog.debug("End showMessage(WebMessage)");

    return lBlnResult;
  }
}
/**
 * SqlSessionFactory manager.
 *
 * @author Andrea Feccomandi
 */
public class SqlSessionFactoryManager {

  private static final String BIBISCO_DB_URL = "bibisco";

  private static final String DB_USERNAME = "******";
  private static final String DB_PASSWORD = "******";
  private static final String RESOURCE_FILE_NAME = "dbConfiguration.xml";

  private static Log mLog = Log.getInstance(SqlSessionFactoryManager.class);
  private SqlSessionFactory mSqlSessionFactoryBibisco;
  private SqlSessionFactory mSqlSessionFactoryProject;
  private static SqlSessionFactoryManager mSqlSessionFactoryManager;

  public static synchronized SqlSessionFactoryManager getInstance() {

    if (mSqlSessionFactoryManager == null) {
      mSqlSessionFactoryManager = new SqlSessionFactoryManager();
    }

    return mSqlSessionFactoryManager;
  }

  private SqlSessionFactoryManager() {}

  public Object clone() throws CloneNotSupportedException {
    return new CloneNotSupportedException();
  }

  private SqlSessionFactory buildSqlSessionFactory(String pStrDBUrl) {

    SqlSessionFactory mSqlSessionFactory;

    mLog.debug("Start buildSqlSessionFactory(String)");
    mLog.debug("DB_URL: ", pStrDBUrl);

    try {
      Reader lReader = Resources.getResourceAsReader(RESOURCE_FILE_NAME);
      Properties lProperties = new Properties();
      lProperties.setProperty("url", pStrDBUrl);
      lProperties.setProperty("username", DB_USERNAME);
      lProperties.setProperty("password", DB_PASSWORD);

      mSqlSessionFactory = new SqlSessionFactoryBuilder().build(lReader, lProperties);
      mLog.debug("SqlSessionFactory built.");

    } catch (IOException e) {
      mLog.error(e);
      throw new BibiscoException(BibiscoException.IO_EXCEPTION);
    }

    mLog.debug("End buildSqlSessionFactory(String)");

    return mSqlSessionFactory;
  }

  private static String getDBURL(String pStrDBName) {

    ContextManager lContextManager = ContextManager.getInstance();
    StringBuilder lStringBuilder = new StringBuilder();
    lStringBuilder.append("jdbc:h2:file:");

    lStringBuilder.append(ContextManager.getPathSeparator());
    lStringBuilder.append(lContextManager.getDbDirectoryPath());

    if (pStrDBName.equalsIgnoreCase(BIBISCO_DB_URL)) {
      lStringBuilder.append("bibisco");
    } else {
      lStringBuilder.append(pStrDBName);
      lStringBuilder.append(ContextManager.getPathSeparator());
      lStringBuilder.append(pStrDBName);
    }

    return lStringBuilder.toString();
  }

  public SqlSessionFactory getSqlSessionFactoryBibisco() {

    if (mSqlSessionFactoryBibisco == null) {
      mSqlSessionFactoryBibisco = buildSqlSessionFactory(getDBURL(BIBISCO_DB_URL));
    }

    return mSqlSessionFactoryBibisco;
  }

  public SqlSessionFactory getSqlSessionFactoryProject() {

    if (mSqlSessionFactoryProject == null) {
      mSqlSessionFactoryProject =
          buildSqlSessionFactory(getDBURL(ContextManager.getInstance().getIdProject()));
    }

    return mSqlSessionFactoryProject;
  }

  public void cleanSqlSessionFactoryProject() {

    mLog.debug("Start cleanSqlSessionFactoryProject()");

    PooledDataSource lPooledDataSource =
        (PooledDataSource)
            mSqlSessionFactoryProject.getConfiguration().getEnvironment().getDataSource();
    lPooledDataSource.forceCloseAll();
    mSqlSessionFactoryProject = null;

    mLog.debug("End cleanSqlSessionFactoryProject()");
  }
}