use of com.cosylab.cdb.jdal.hibernate.RootMap in project ACS by ACS-Community.
the class HibernateWDALImpl method getChannelsTableMap.
public Object getChannelsTableMap() {
final String keyField = "Name";
final Class type = alma.TMCDB.maci.EventChannel.class;
try {
Session session = hibernateUtil.getSession();
Method accessor = DOMJavaClassIntrospector.getAccessorMethod(type, keyField);
Field field = null;
if (accessor == null) {
try {
field = type.getField(keyField);
} catch (Throwable th) {
throw new IllegalStateException("failed to obtain key ");
}
}
boolean channelsExist = false;
Channels channels = new Channels();
alma.TMCDB.maci.NotificationServiceMapping mapping = (alma.TMCDB.maci.NotificationServiceMapping) session.createCriteria(alma.TMCDB.maci.NotificationServiceMapping.class).add(Restrictions.eq("ConfigurationId", configId)).uniqueResult();
if (mapping != null) {
channels.setNotificationServiceMapping(mapping);
channelsExist = true;
}
List list = getListForConfiguration(session, type);
if (list.size() > 0) {
channelsExist = true;
}
Map<String, EventChannelNode> map;
if (channelsExist) {
map = channels._;
} else {
//map = new RootMap<String, EventChannelNode>();
return null;
}
for (Object data : list) {
String baseKey;
if (accessor != null)
baseKey = accessor.invoke(data, (Object[]) null).toString();
else
//if (field != null)
baseKey = field.get(data).toString();
// baseKey should not be null
Map parentMap = map;
alma.TMCDB.maci.EventChannel channel = (alma.TMCDB.maci.EventChannel) data;
// now find its map
String path = getNormalizedPath(channel.Path);
while (path != null && path.length() > 0) {
// remove trailing slashes, to have unique curl (used for key)
if (path.charAt(0) == '/') {
path = path.substring(1);
continue;
}
int pos = path.indexOf('/');
String parentPath = (pos > 0) ? path.substring(0, pos) : path;
String subpath = (pos > 0) ? path.substring(pos + 1, path.length()) : null;
alma.TMCDB.maci.EventChannelNode parentChannel = (alma.TMCDB.maci.EventChannelNode) parentMap.get(parentPath);
if (parentChannel == null) {
parentChannel = new alma.TMCDB.maci.EventChannelNode();
parentMap.put(parentPath, parentChannel);
}
parentMap = parentChannel._;
path = subpath;
}
// unique key generation
int count = 0;
String key = baseKey;
while (parentMap.containsKey(key)) key = baseKey + String.valueOf(++count);
parentMap.put(key, data);
}
return channels;
} catch (Throwable th) {
th.printStackTrace();
}
return null;
}
use of com.cosylab.cdb.jdal.hibernate.RootMap in project ACS by ACS-Community.
the class HibernateWDALImpl method getManagersTableMap.
public Map<String, Object> getManagersTableMap() {
m_logger.info("clear_cache(curl): ManagersTable1");
Map<String, Object> managersMap = new RootMap<String, Object>();
try {
Session session = hibernateUtil.getSession();
m_logger.info("clear_cache(curl): ManagersTable2");
List managerList = getListForConfiguration(session, alma.TMCDB.maci.Manager.class);
m_logger.info("clear_cache(curl): ManagersTable3");
if (!managerList.isEmpty())
managersMap.put("Manager", managerList.get(0));
else
managersMap.put("Manager", getDefaultMangerConfig());
m_logger.info("clear_cache(curl): ManagersTable4");
String additionalServices = getAcsServices(session, config);
m_logger.info("clear_cache(curl): ManagersTable5");
if (additionalServices != null) {
alma.TMCDB.maci.Manager manager = (alma.TMCDB.maci.Manager) managersMap.get("Manager");
m_logger.info("clear_cache(curl): ManagersTable6");
String currentServices = manager.getServiceComponents();
m_logger.info("clear_cache(curl): ManagersTable7");
if (currentServices == null || currentServices.length() == 0)
manager.setServiceComponents(additionalServices);
else
// there might be duplicates, but they do not harm
manager.setServiceComponents(currentServices + "," + additionalServices);
m_logger.info("clear_cache(curl): ManagersTable8");
}
} catch (Throwable th) {
th.printStackTrace();
}
m_logger.info("clear_cache(curl): ManagersTable9");
return managersMap;
}
use of com.cosylab.cdb.jdal.hibernate.RootMap in project ACS by ACS-Community.
the class HibernateWDALImpl method updateComponentsTableMap.
protected void updateComponentsTableMap(String curl) {
m_logger.info("clear_cache(curl): ComponentsTable1");
final String keyField = "Name";
final Class type = alma.TMCDB.maci.Component.class;
Map<String, Object> rootMap = (Map<String, Object>) rootNode;
Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) rootMap.get("MACI")).get("Components");
m_logger.info("clear_cache(curl): ComponentsTable2");
try {
Session session = hibernateUtil.getSession();
Method accessor = DOMJavaClassIntrospector.getAccessorMethod(type, keyField);
m_logger.info("clear_cache(curl): ComponentsTable3");
Field field = null;
if (accessor == null) {
try {
field = type.getField(keyField);
} catch (Throwable th) {
throw new IllegalStateException("failed to obtain key ");
}
}
m_logger.info("clear_cache(curl): ComponentsTable4");
String[] els = curl.split("/");
String rpath = "^/*";
String rsubpath = "^/*";
String rcpath = "^/*";
String rcname = els[els.length - 1];
for (int i = 0; i < els.length; i++) {
rpath += els[i];
rsubpath += els[i];
if (i < els.length - 1) {
rpath += "/+";
rsubpath += "/+";
rcpath += els[i];
if (i < els.length - 2)
rcpath += "/+";
}
}
rpath += "/*$";
rsubpath += "/+.*";
rcpath += "/*$";
System.out.println(rpath);
System.out.println(rsubpath);
System.out.println(rcpath + "|" + rcname);
m_logger.info("clear_cache(curl): ComponentsTable5");
//Consider the cases where the curl matches exactly the Path, where
//it is part of the path and when it matches exactly the path and
//the component name.
Criterion cr = Restrictions.disjunction().add(getRegularExpressionRestriction("Path", rpath)).add(getRegularExpressionRestriction("Path", rsubpath)).add(Restrictions.and(getRegularExpressionRestriction("Path", rcpath), Restrictions.eq("Name", rcname)));
m_logger.info("clear_cache(curl): ComponentsTable6");
List list = getListForConfiguration(session, type, cr);
m_logger.info("clear_cache(curl): ComponentsTable7");
System.out.println("\nFound the following Components");
for (Object data : list) {
System.out.println(((alma.TMCDB.maci.Component) data).Path + "/" + ((alma.TMCDB.maci.Component) data).getName());
}
m_logger.info("clear_cache(curl): ComponentsTable8");
//Remove the entries from existing maps.
System.out.println("\nChecking maps to remove");
Map rParentMap = map;
for (int i = 0; i < els.length; i++) {
System.out.println("Checking path " + els[i] + ".");
System.out.println("Parent keys: " + rParentMap.keySet().toString());
Object data = rParentMap.get(els[i]);
if (data == null) {
System.out.println("No element found with the given curl");
break;
} else {
if (data instanceof alma.TMCDB.maci.Component) {
System.out.println("Instance of Component (Component!).");
} else if (data instanceof alma.TMCDB.maci.ComponentNode) {
System.out.println("Instance of ComponentNode (Path!).");
} else {
System.out.println("Unknown type! Details: " + data.toString());
}
if (i < els.length - 1) {
System.out.println("There are elements remaining, so we proceed to next element in path.");
rParentMap = ((alma.TMCDB.maci.ComponentNode) data)._;
} else {
System.out.println("There are no elements remaining, we remove all entries from this element in path and on.");
rParentMap.remove(els[i]);
}
}
}
m_logger.info("clear_cache(curl): ComponentsTable9");
// Sort the list by path + component to ensure that parent components are added before their children
Comparator<alma.TMCDB.maci.Component> comparator = new Comparator<alma.TMCDB.maci.Component>() {
public int compare(alma.TMCDB.maci.Component o1, alma.TMCDB.maci.Component o2) {
String fullName1 = ((o1.Path == null ? "" : o1.Path) + "/") + o1.getName();
String fullName2 = ((o2.Path == null ? "" : o2.Path) + "/") + o2.getName();
return fullName1.compareTo(fullName2);
}
};
Collections.sort(list, comparator);
m_logger.info("clear_cache(curl): ComponentsTable10");
for (Object data : list) {
String baseKey;
if (accessor != null)
baseKey = accessor.invoke(data, (Object[]) null).toString();
else
//if (field != null)
baseKey = field.get(data).toString();
// baseKey should not be null
Map parentMap = map;
alma.TMCDB.maci.Component component = (alma.TMCDB.maci.Component) data;
// some cleaning
if (component.getComponentLogger().getMinLogLevel() == -1 && component.getComponentLogger().getMinLogLevelLocal() == -1)
component.setComponentLogger(null);
// now find its map
String path = getNormalizedPath(component.Path);
while (path != null && path.length() > 0) {
// remove trailing slashes, to have unique curl (used for key)
if (path.charAt(0) == '/') {
path = path.substring(1);
continue;
}
int pos = path.indexOf('/');
String parentPath = (pos > 0) ? path.substring(0, pos) : path;
String subpath = (pos > 0) ? path.substring(pos + 1, path.length()) : null;
alma.TMCDB.maci.ComponentNode parentComponent = (alma.TMCDB.maci.ComponentNode) parentMap.get(parentPath);
if (parentComponent == null) {
parentComponent = new alma.TMCDB.maci.ComponentNode();
parentMap.put(parentPath, parentComponent);
}
parentMap = parentComponent._;
path = subpath;
}
// unique key generation
int count = 0;
String key = baseKey;
while (parentMap.containsKey(key)) key = baseKey + String.valueOf(++count);
parentMap.put(key, data);
if (data instanceof alma.TMCDB.maci.Component) {
alma.TMCDB.maci.Component comp = (alma.TMCDB.maci.Component) data;
m_logger.finer("Loaded component name=" + comp.Path + comp.getName() + ", type=" + comp.getType() + ", container=" + comp.getContainer() + ", implLang=" + comp.getImplLang());
} else {
m_logger.warning("Bad component class '" + data.getClass().getName() + "' read from TMCDB.");
}
m_logger.info("clear_cache(curl): ComponentsTable11");
}
m_logger.info("clear_cache(curl): ComponentsTable12");
} catch (Throwable th) {
th.printStackTrace();
}
m_logger.info("clear_cache(curl): ComponentsTable13");
}
use of com.cosylab.cdb.jdal.hibernate.RootMap in project ACS by ACS-Community.
the class HibernateWDALImpl method initializeRootNode.
protected synchronized void initializeRootNode() {
m_logger.info("Loading configuration from the database...");
Map<String, Object> rootMap = new RootMap<String, Object>();
rootNode = rootMap;
// useful in case of reload (when rootNode was not null)
System.gc();
try {
Session session = hibernateUtil.getSession();
// plugin loadPrologue()
if (plugin != null) {
try {
plugin.loadPrologue(session, config, rootMap);
} catch (Throwable th) {
this.m_logger.log(Level.SEVERE, "Failure in plugin " + plugin.getClass().getName(), th);
// TODO: or should we let HD fail completely?
}
}
// maci branch
Map<String, Object> maciMap = new RootMap<String, Object>();
maciMap.put("Components", getComponentsTableMap());
maciMap.put("Containers", getContainersTableMap());
Object channelsMap = getChannelsTableMap();
if (channelsMap != null)
maciMap.put("Channels", channelsMap);
Map<String, Object> managersMap = new RootMap<String, Object>();
List managerList = getListForConfiguration(session, alma.TMCDB.maci.Manager.class);
if (!managerList.isEmpty())
managersMap.put("Manager", managerList.get(0));
else
managersMap.put("Manager", getDefaultMangerConfig());
String additionalServices = getAcsServices(session, config);
if (additionalServices != null) {
alma.TMCDB.maci.Manager manager = (alma.TMCDB.maci.Manager) managersMap.get("Manager");
String currentServices = manager.getServiceComponents();
if (currentServices == null || currentServices.length() == 0)
manager.setServiceComponents(additionalServices);
else
// there might be duplicates, but they do not harm
manager.setServiceComponents(currentServices + "," + additionalServices);
}
maciMap.put("Managers", managersMap);
rootMap.put("MACI", maciMap);
// component, i.e. alma, branch
rootMap.put(COMPONENT_TREE_NAME, getAlmaBranch());
// plugin loadEpilogue()
if (plugin != null) {
try {
plugin.loadEpilogue(session, config, rootMap);
} catch (Throwable th) {
this.m_logger.log(Level.SEVERE, "Failure in plugin " + plugin.getClass().getName(), th);
// TODO: or should we let HD fail completely?
}
}
// session check
if (hibernateUtil.getSession() != mainSession) {
throw new RuntimeException("Unexpected change of session...");
}
m_logger.info("Configuration loaded.");
} catch (Throwable th) {
th.printStackTrace();
}
}
Aggregations