Search in sources :

Example 1 with RuntimeEnv

use of org.b3log.latke.RuntimeEnv in project solo by b3log.

the class BlogProcessor method getBlogInfo.

/**
     * Gets blog information.
     *
     * <ul>
     * <li>Time of the recent updated article</li>
     * <li>Article count</li>
     * <li>Comment count</li>
     * <li>Tag count</li>
     * <li>Serve path</li>
     * <li>Static serve path</li>
     * <li>Solo version</li>
     * <li>Runtime environment (LOCAL)</li>
     * <li>Locale</li>
     * </ul>
     *
     * @param context the specified context
     * @throws Exception exception
     */
@RequestProcessing(value = "/blog/info", method = HTTPRequestMethod.GET)
public void getBlogInfo(final HTTPRequestContext context) throws Exception {
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    jsonObject.put("recentArticleTime", articleQueryService.getRecentArticleTime());
    final JSONObject statistic = statisticQueryService.getStatistic();
    jsonObject.put("articleCount", statistic.getLong(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT));
    jsonObject.put("commentCount", statistic.getLong(Statistic.STATISTIC_PUBLISHED_BLOG_COMMENT_COUNT));
    jsonObject.put("tagCount", tagQueryService.getTagCount());
    jsonObject.put("servePath", Latkes.getServePath());
    jsonObject.put("staticServePath", Latkes.getStaticServePath());
    jsonObject.put("version", SoloServletListener.VERSION);
    jsonObject.put("locale", Latkes.getLocale());
    jsonObject.put("runtimeMode", Latkes.getRuntimeMode());
    final RuntimeEnv runtimeEnv = Latkes.getRuntimeEnv();
    jsonObject.put("runtimeEnv", runtimeEnv);
    if (RuntimeEnv.LOCAL == runtimeEnv) {
        jsonObject.put("runtimeDatabase", Latkes.getRuntimeDatabase());
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) RuntimeEnv(org.b3log.latke.RuntimeEnv) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 2 with RuntimeEnv

use of org.b3log.latke.RuntimeEnv in project solo by b3log.

the class InitService method init.

/**
     * Initializes Solo.
     *
     * <p>
     * Initializes the followings in sequence:
     * <ol>
     * <li>Statistic</li>
     * <li>Preference</li>
     * <li>Administrator</li>
     * </ol>
     * </p>
     *
     * <p>
     * We will try to initialize Solo 3 times at most.
     * </p>
     *
     * <p>
     * Posts "Hello World!" article and its comment while Solo initialized.
     * </p>
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "userName": "",
     *     "userEmail": "",
     *     "userPassword": "", // Unhashed
     * }
     * </pre>
     *
     * @throws ServiceException service exception
     */
public void init(final JSONObject requestJSONObject) throws ServiceException {
    if (isInited()) {
        return;
    }
    final RuntimeEnv runtimeEnv = Latkes.getRuntimeEnv();
    if (RuntimeEnv.LOCAL == runtimeEnv) {
        LOGGER.log(Level.INFO, "Solo is running on [" + runtimeEnv + "] environment, database [{0}], creates " + "all tables", Latkes.getRuntimeDatabase());
        if (RuntimeDatabase.H2 == Latkes.getRuntimeDatabase()) {
            String dataDir = Latkes.getLocalProperty("jdbc.URL");
            dataDir = dataDir.replace("~", System.getProperty("user.home"));
            LOGGER.log(Level.INFO, "YOUR DATA will be stored in directory [" + dataDir + "], " + "please pay more attention to it~");
        }
        final List<CreateTableResult> createTableResults = JdbcRepositories.initAllTables();
        for (final CreateTableResult createTableResult : createTableResults) {
            LOGGER.log(Level.DEBUG, "Create table result[tableName={0}, isSuccess={1}]", new Object[] { createTableResult.getName(), createTableResult.isSuccess() });
        }
    }
    int retries = MAX_RETRIES_CNT;
    while (true) {
        final Transaction transaction = userRepository.beginTransaction();
        try {
            final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
            if (null == statistic) {
                initStatistic();
                initPreference(requestJSONObject);
                initReplyNotificationTemplate();
                initAdmin(requestJSONObject);
                initLink();
            }
            transaction.commit();
            break;
        } catch (final Exception e) {
            if (0 == retries) {
                LOGGER.log(Level.ERROR, "Initialize Solo error", e);
                throw new ServiceException("Initailize Solo error: " + e.getMessage());
            }
            // Allow retry to occur
            --retries;
            LOGGER.log(Level.WARN, "Retrying to init Solo[retries={0}]", retries);
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }
    }
    final Transaction transaction = userRepository.beginTransaction();
    try {
        helloWorld();
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Hello World error?!", e);
    }
    try {
        final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
        final HTTPRequest req = new HTTPRequest();
        req.setURL(new URL(Latkes.getServePath() + "/blog/symphony/user"));
        urlFetchService.fetch(req);
    } catch (final Exception e) {
        LOGGER.log(Level.TRACE, "Sync account failed");
    }
    pluginManager.load();
}
Also used : HTTPRequest(org.b3log.latke.urlfetch.HTTPRequest) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) URLFetchService(org.b3log.latke.urlfetch.URLFetchService) RuntimeEnv(org.b3log.latke.RuntimeEnv) CreateTableResult(org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult) JSONException(org.json.JSONException) ParseException(java.text.ParseException) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException) URL(java.net.URL)

Aggregations

RuntimeEnv (org.b3log.latke.RuntimeEnv)2 JSONObject (org.json.JSONObject)2 URL (java.net.URL)1 ParseException (java.text.ParseException)1 RepositoryException (org.b3log.latke.repository.RepositoryException)1 Transaction (org.b3log.latke.repository.Transaction)1 CreateTableResult (org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult)1 ServiceException (org.b3log.latke.service.ServiceException)1 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)1 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)1 HTTPRequest (org.b3log.latke.urlfetch.HTTPRequest)1 URLFetchService (org.b3log.latke.urlfetch.URLFetchService)1 JSONException (org.json.JSONException)1