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;
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations