Search in sources :

Example 1 with AbstractPlugin

use of org.b3log.latke.plugin.AbstractPlugin in project solo by b3log.

the class PluginMgmtService method setPluginStatus.

/**
     * Sets a plugin's status with the specified plugin id, status.
     * 
     * @param pluginId the specified plugin id
     * @param status the specified status, see {@link PluginStatus}
     * @return for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": "" 
     * }
     * </pre>
     */
public JSONObject setPluginStatus(final String pluginId, final String status) {
    final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
    final List<AbstractPlugin> plugins = pluginManager.getPlugins();
    final JSONObject ret = new JSONObject();
    for (final AbstractPlugin plugin : plugins) {
        if (plugin.getId().equals(pluginId)) {
            final Transaction transaction = pluginRepository.beginTransaction();
            try {
                plugin.setStatus(PluginStatus.valueOf(status));
                pluginRepository.update(pluginId, plugin.toJSONObject());
                transaction.commit();
                plugin.changeStatus();
                ret.put(Keys.STATUS_CODE, true);
                ret.put(Keys.MSG, langs.get("setSuccLabel"));
                return ret;
            } catch (final Exception e) {
                if (transaction.isActive()) {
                    transaction.rollback();
                }
                LOGGER.log(Level.ERROR, "Set plugin status error", e);
                ret.put(Keys.STATUS_CODE, false);
                ret.put(Keys.MSG, langs.get("setFailLabel"));
                return ret;
            }
        }
    }
    ret.put(Keys.STATUS_CODE, false);
    ret.put(Keys.MSG, langs.get("refreshAndRetryLabel"));
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) AbstractPlugin(org.b3log.latke.plugin.AbstractPlugin) JSONException(org.json.JSONException)

Example 2 with AbstractPlugin

use of org.b3log.latke.plugin.AbstractPlugin in project solo by b3log.

the class PluginRefresher method action.

@Override
public void action(final Event<List<AbstractPlugin>> event) throws EventException {
    final List<AbstractPlugin> plugins = event.getData();
    LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]", new Object[] { event.getType(), plugins, PluginRefresher.class.getName() });
    final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
    final PluginRepository pluginRepository = beanManager.getReference(PluginRepositoryImpl.class);
    final Transaction transaction = pluginRepository.beginTransaction();
    try {
        final PluginMgmtService pluginMgmtService = beanManager.getReference(PluginMgmtService.class);
        pluginMgmtService.refresh(plugins);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Processing plugin loaded event error", e);
        throw new EventException(e);
    }
}
Also used : PluginMgmtService(org.b3log.solo.service.PluginMgmtService) Transaction(org.b3log.latke.repository.Transaction) EventException(org.b3log.latke.event.EventException) PluginRepository(org.b3log.solo.repository.PluginRepository) AbstractPlugin(org.b3log.latke.plugin.AbstractPlugin) EventException(org.b3log.latke.event.EventException) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 3 with AbstractPlugin

use of org.b3log.latke.plugin.AbstractPlugin in project solo by b3log.

the class PluginMgmtService method refresh.

/**
     * Updates datastore plugin descriptions with the specified plugins.
     * 
     * @param plugins the specified plugins
     * @throws Exception exception 
     */
public void refresh(final List<AbstractPlugin> plugins) throws Exception {
    if (!initService.isInited()) {
        return;
    }
    final JSONObject result = pluginRepository.get(new Query());
    final JSONArray pluginArray = result.getJSONArray(Keys.RESULTS);
    final List<JSONObject> persistedPlugins = CollectionUtils.jsonArrayToList(pluginArray);
    try {
        // Reads plugin status from datastore and clear plugin datastore
        for (final JSONObject oldPluginDesc : persistedPlugins) {
            final String descId = oldPluginDesc.getString(Keys.OBJECT_ID);
            final AbstractPlugin plugin = get(plugins, descId);
            pluginRepository.remove(descId);
            if (null != plugin) {
                final String status = oldPluginDesc.getString(Plugin.PLUGIN_STATUS);
                final String setting = oldPluginDesc.optString(Plugin.PLUGIN_SETTING);
                plugin.setStatus(PluginStatus.valueOf(status));
                try {
                    if (StringUtils.isNotBlank(setting)) {
                        plugin.setSetting(new JSONObject(setting));
                    }
                } catch (final JSONException e) {
                    LOGGER.log(Level.WARN, "the formatter of the old config failed to convert to json", e);
                }
            }
        }
        // Adds these plugins into datastore
        for (final AbstractPlugin plugin : plugins) {
            final JSONObject pluginDesc = plugin.toJSONObject();
            pluginRepository.add(pluginDesc);
            LOGGER.log(Level.TRACE, "Refreshed plugin[{0}]", pluginDesc);
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Refresh plugins failed", e);
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) AbstractPlugin(org.b3log.latke.plugin.AbstractPlugin) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 4 with AbstractPlugin

use of org.b3log.latke.plugin.AbstractPlugin in project solo by b3log.

the class PluginMgmtService method updatePluginSetting.

/**
     * updatePluginSetting.
     * 
     * @param pluginId the specified pluginoId
     * @param setting the specified setting
     * @return the ret json
     */
public JSONObject updatePluginSetting(final String pluginId, final String setting) {
    final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
    final List<AbstractPlugin> plugins = pluginManager.getPlugins();
    final JSONObject ret = new JSONObject();
    for (final AbstractPlugin plugin : plugins) {
        if (plugin.getId().equals(pluginId)) {
            final Transaction transaction = pluginRepository.beginTransaction();
            try {
                final JSONObject pluginJson = plugin.toJSONObject();
                pluginJson.put(Plugin.PLUGIN_SETTING, setting);
                pluginRepository.update(pluginId, pluginJson);
                transaction.commit();
                ret.put(Keys.STATUS_CODE, true);
                ret.put(Keys.MSG, langs.get("setSuccLabel"));
                return ret;
            } catch (final Exception e) {
                if (transaction.isActive()) {
                    transaction.rollback();
                }
                LOGGER.log(Level.ERROR, "Set plugin status error", e);
                ret.put(Keys.STATUS_CODE, false);
                ret.put(Keys.MSG, langs.get("setFailLabel"));
                return ret;
            }
        }
    }
    ret.put(Keys.STATUS_CODE, false);
    ret.put(Keys.MSG, langs.get("refreshAndRetryLabel"));
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) AbstractPlugin(org.b3log.latke.plugin.AbstractPlugin) JSONException(org.json.JSONException)

Example 5 with AbstractPlugin

use of org.b3log.latke.plugin.AbstractPlugin in project solo by b3log.

the class PluginQueryService method getPlugins.

/**
     * Gets plugins by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "paginationCurrentPageNum": 1,
     *     "paginationPageSize": 20,
     *     "paginationWindowSize": 10,
     * }, see {@link Pagination} for more details
     * </pre>
     * @return for example,
     * <pre>
     * {
     *     "pagination": {
     *         "paginationPageCount": 100,
     *         "paginationPageNums": [1, 2, 3, 4, 5]
     *     },
     *     "plugins": [{
     *         "name": "",
     *         "version": "",
     *         "author": "",
     *         "status": "", // Enumeration name of {@link org.b3log.latke.plugin.PluginStatus}
     *      }, ....]
     * }
     * </pre>
     * @throws ServiceException service exception
     * @see Pagination
     */
public JSONObject getPlugins(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    try {
        final int currentPageNum = requestJSONObject.getInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
        final int pageSize = requestJSONObject.getInt(Pagination.PAGINATION_PAGE_SIZE);
        final int windowSize = requestJSONObject.getInt(Pagination.PAGINATION_WINDOW_SIZE);
        final List<JSONObject> pluginJSONObjects = new ArrayList<JSONObject>();
        final List<AbstractPlugin> plugins = pluginManager.getPlugins();
        for (final AbstractPlugin plugin : plugins) {
            final JSONObject jsonObject = plugin.toJSONObject();
            pluginJSONObjects.add(jsonObject);
        }
        final int pageCount = (int) Math.ceil((double) pluginJSONObjects.size() / (double) pageSize);
        final JSONObject pagination = new JSONObject();
        ret.put(Pagination.PAGINATION, pagination);
        final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
        pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
        pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
        final int start = pageSize * (currentPageNum - 1);
        int end = start + pageSize;
        end = end > pluginJSONObjects.size() ? pluginJSONObjects.size() : end;
        ret.put(Plugin.PLUGINS, pluginJSONObjects.subList(start, end));
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets plugins failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ArrayList(java.util.ArrayList) AbstractPlugin(org.b3log.latke.plugin.AbstractPlugin) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Aggregations

AbstractPlugin (org.b3log.latke.plugin.AbstractPlugin)5 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 Transaction (org.b3log.latke.repository.Transaction)3 ArrayList (java.util.ArrayList)1 EventException (org.b3log.latke.event.EventException)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 Query (org.b3log.latke.repository.Query)1 RepositoryException (org.b3log.latke.repository.RepositoryException)1 ServiceException (org.b3log.latke.service.ServiceException)1 PluginRepository (org.b3log.solo.repository.PluginRepository)1 PluginMgmtService (org.b3log.solo.service.PluginMgmtService)1 JSONArray (org.json.JSONArray)1