use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.
the class CobarDAOImple method getCobarList.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public List<CobarDO> getCobarList(long clusterId, String status) {
List<CobarDO> list = new ArrayList<CobarDO>();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Long, CobarDO> entry = (Entry<Long, CobarDO>) it.next();
CobarDO cobar = entry.getValue();
if (clusterId == cobar.getClusterId() && status.equals(cobar.getStatus())) {
list.add(cobar);
}
}
return list;
}
use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.
the class CobarDAOImple method read.
private boolean read() {
FileInputStream is = null;
lock.lock();
try {
map.clear();
is = new FileInputStream(xmlPath);
xpp.setInput(is, "UTF-8");
while (!(xpp.getEventType() == XmlPullParser.END_TAG && "cobars".equals(xpp.getName()))) {
if (xpp.getEventType() == XmlPullParser.START_TAG && "cobar".equals(xpp.getName())) {
CobarDO cobar = read(xpp);
if (null == cobar) {
throw new XmlPullParserException("Cobar read error");
}
maxId = (maxId < cobar.getId()) ? cobar.getId() : maxId;
map.put(cobar.getId(), cobar);
}
xpp.next();
}
is.close();
return true;
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
} catch (XmlPullParserException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
maxId = maxId < 0 ? 0 : maxId;
lock.unlock();
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
return false;
}
use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.
the class CobarDAOImple method read.
private CobarDO read(XmlPullParser xpp) {
CobarDO cobar = new CobarDO();
cobar.setServerPort(8066);
try {
while (!(xpp.getEventType() == XmlPullParser.END_TAG && "cobar".equals(xpp.getName()))) {
if (xpp.getEventType() == XmlPullParser.START_TAG && "property".equals(xpp.getName())) {
int type = typeMap.get(xpp.getAttributeValue(0).trim());
switch(type) {
case ID:
cobar.setId(Long.parseLong(xpp.nextText().trim()));
break;
case NAME:
cobar.setName(xpp.nextText().trim());
break;
case CLUSTER_ID:
cobar.setClusterId(Long.parseLong(xpp.nextText().trim()));
break;
case STATUS:
String value = xpp.nextText().trim();
if (ACTIVE.equals(value)) {
cobar.setStatus(ACTIVE);
} else if (DELETE.equals(value)) {
cobar.setStatus(DELETE);
} else {
cobar.setStatus(IN_ACTIVE);
}
break;
case HOST:
cobar.setHost(xpp.nextText().trim());
break;
case PORT:
cobar.setPort(Integer.parseInt(xpp.nextText().trim()));
break;
case SERVER_PORT:
cobar.setServerPort(Integer.parseInt(xpp.nextText().trim()));
break;
case USER:
cobar.setUser(xpp.nextText().trim());
break;
case PASSWORD:
String password = EncryptUtil.decrypt(xpp.nextText().trim());
cobar.setPassword(password);
break;
case TIME_DIFF:
cobar.setTime_diff(xpp.nextText().trim());
break;
default:
break;
}
}
xpp.next();
}
return cobar;
} catch (NumberFormatException e) {
logger.error(e.getMessage(), e);
} catch (XmlPullParserException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
}
use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.
the class PropertyReload method handleRequestInternal.
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
UserDO user = (UserDO) request.getSession().getAttribute("user");
String types = request.getParameter("type");
int type = typeMap.get(types);
String list = request.getParameter("list");
String[] temp = list.split(",");
int[] index = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
index[i] = Integer.parseInt(temp[i]);
}
if (logger.isWarnEnabled()) {
StringBuilder log = new StringBuilder(user.getUsername()).append(" | do ").append(types).append(" | cobar:");
for (int i = 0; i < index.length; i++) {
log.append(xmlAccesser.getCobarDAO().getCobarById(index[i])).append(" ");
}
logger.warn(log.toString());
}
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
lock.lock();
try {
switch(type) {
case CONFIG_RELOAD:
for (int i = 0; i < index.length; i++) {
CobarDO c = xmlAccesser.getCobarDAO().getCobarById(index[i]);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
if (c.getStatus().equals(ConstantDefine.ACTIVE)) {
CobarAdapterDAO perf = cobarAccesser.getAccesser(index[i]);
if (perf.checkConnection()) {
if (perf.reloadConfig()) {
map.put("result", "success");
} else {
map.put("result", "config reload error");
}
} else {
map.put("result", "connection error");
}
} else {
map.put("result", "cobar InActive");
}
resultList.add(map);
}
break;
case CONFIG_ROLLBACK:
for (int i = 0; i < index.length; i++) {
CobarDO c = xmlAccesser.getCobarDAO().getCobarById(index[i]);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
if (c.getStatus().equals(ConstantDefine.ACTIVE)) {
CobarAdapterDAO perf = cobarAccesser.getAccesser(index[i]);
if (perf.checkConnection()) {
if (perf.rollbackConfig()) {
map.put("result", "success");
} else {
map.put("result", "config reload error");
}
} else {
map.put("result", "connection error");
}
} else {
map.put("result", "cobar InActive");
}
resultList.add(map);
}
break;
default:
logger.error("Wrong property control type!");
break;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "UNKNOWN ERROR");
map.put("result", "unknown exception occurs when reloading");
resultList.clear();
resultList.add(map);
} finally {
lock.unlock();
}
return new ModelAndView("c_result", "resultList", resultList);
}
use of com.alibaba.cobar.manager.dataobject.xml.CobarDO in project cobar by alibaba.
the class PropertyReloadScreen method handleRequestInternal.
@SuppressWarnings("unchecked")
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
UserDO user = (UserDO) request.getSession().getAttribute("user");
String id = request.getParameter("clusterId");
long clusterId = -1;
if (null != id) {
clusterId = Long.parseLong(id);
}
List<ClusterDO> cList = xmlAccesser.getClusterDAO().listAllCluster();
List<Map<String, Object>> clusterList = new ArrayList<Map<String, Object>>();
ListSortUtil.sortClusterByName(cList);
for (ClusterDO e : cList) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", e.getId());
map.put("name", CobarStringUtil.htmlEscapedString(e.getName()));
clusterList.add(map);
}
List<CobarDO> cobarList = null;
if (null != cList && cList.size() > 0) {
if (-1 == clusterId) {
clusterId = cList.get(0).getId();
cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
} else {
cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
}
}
List<Map<String, Object>> cobarListMap = new ArrayList<Map<String, Object>>();
PropertyUtilsBean util = new PropertyUtilsBean();
if (null != cobarList) {
ListSortUtil.sortCobarByName(cobarList);
for (CobarDO c : cobarList) {
CobarAdapterDAO perf = cobarAccesser.getAccesser(c.getId());
Map<String, Object> map;
try {
map = util.describe(c);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
map.remove("class");
map.remove("name");
map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
if (ConstantDefine.ACTIVE.equals(c.getStatus())) {
if (!perf.checkConnection()) {
map.remove("status");
map.put("status", ConstantDefine.ERROR);
map.put("reloadTime", "");
map.put("rollbackTime", "");
} else {
ServerStatus ss = perf.getServerStatus();
String rollbackTime = "NO";
String reloadTime = FormatUtil.fromMilliseconds2String(ss.getReloadTime());
if (ss.getRollbackTime() != -1) {
rollbackTime = FormatUtil.fromMilliseconds2String(ss.getRollbackTime());
}
map.put("reloadTime", reloadTime);
map.put("rollbackTime", rollbackTime);
}
} else {
map.put("reloadTime", "");
map.put("rollbackTime", "");
}
cobarListMap.add(map);
}
}
return new ModelAndView("c_propertyReload", new FluenceHashMap<String, Object>().putKeyValue("cList", clusterList).putKeyValue("cobarList", cobarListMap).putKeyValue("clusterId", clusterId).putKeyValue("user", user));
}
Aggregations