io.datakernel.jmx.AttributeNodeForPojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-boot Show documentation
Show all versions of datakernel-boot Show documentation
An intelligent way of booting complex applications and services according to their dependencies
/*
* Copyright (C) 2015 SoftIndex LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datakernel.jmx;
import javax.management.openmbean.OpenType;
import java.util.*;
import static io.datakernel.util.JmxUtils.filterNulls;
import static io.datakernel.util.Preconditions.checkNotNull;
import static java.util.Collections.singletonList;
final class AttributeNodeForPojo implements AttributeNode {
private static final char ATTRIBUTE_NAME_SEPARATOR = '_';
private final String name;
private final String description;
private final ValueFetcher fetcher;
private final JmxReducer reducer;
private final Map fullNameToNode;
private final List extends AttributeNode> subNodes;
private boolean visible;
public AttributeNodeForPojo(String name, String description, boolean visible,
ValueFetcher fetcher, JmxReducer reducer,
List extends AttributeNode> subNodes) {
this.name = name;
this.description = description;
this.visible = visible;
this.fetcher = fetcher;
this.reducer = reducer;
this.fullNameToNode = createFullNameToNodeMapping(name, subNodes);
this.subNodes = subNodes;
}
private static Map createFullNameToNodeMapping(String name,
List extends AttributeNode> subNodes) {
Map fullNameToNodeMapping = new HashMap<>();
for (AttributeNode subNode : subNodes) {
Set currentSubAttrNames = subNode.getAllAttributes();
for (String currentSubAttrName : currentSubAttrNames) {
String currentAttrFullName = addPrefix(currentSubAttrName, name);
if (fullNameToNodeMapping.containsKey(currentAttrFullName)) {
throw new IllegalArgumentException(
"There are several attributes with same name: " + currentSubAttrName);
}
fullNameToNodeMapping.put(currentAttrFullName, subNode);
}
}
return fullNameToNodeMapping;
}
@Override
public String getName() {
return name;
}
@Override
public Set getAllAttributes() {
return fullNameToNode.keySet();
}
@Override
public Set getVisibleAttributes() {
if (!visible) {
return Collections.emptySet();
} else {
Set allVisibleAttrs = new HashSet<>();
for (AttributeNode subNode : subNodes) {
Set visibleSubAttrs = subNode.getVisibleAttributes();
for (String visibleSubAttr : visibleSubAttrs) {
String visibleAttr = addPrefix(visibleSubAttr);
allVisibleAttrs.add(visibleAttr);
}
}
return allVisibleAttrs;
}
}
@Override
public Map> getDescriptions() {
Map> nameToDescriptions = new HashMap<>();
for (AttributeNode subNode : subNodes) {
Map> currentSubNodeDescriptions = subNode.getDescriptions();
for (String subNodeAttrName : currentSubNodeDescriptions.keySet()) {
String resultAttrName = addPrefix(subNodeAttrName);
Map curDescriptions = new LinkedHashMap<>();
if (description != null) {
curDescriptions.put(name, description);
}
curDescriptions.putAll(currentSubNodeDescriptions.get(subNodeAttrName));
nameToDescriptions.put(resultAttrName, curDescriptions);
}
}
return nameToDescriptions;
}
@Override
public Map> getOpenTypes() {
Map> allTypes = new HashMap<>();
for (AttributeNode subNode : subNodes) {
Map> subAttrTypes = subNode.getOpenTypes();
for (String subAttrName : subAttrTypes.keySet()) {
OpenType> attrType = subAttrTypes.get(subAttrName);
String attrName = addPrefix(subAttrName);
allTypes.put(attrName, attrType);
}
}
return allTypes;
}
@Override
@SuppressWarnings("unchecked")
public Map aggregateAttributes(Set attrNames, List> sources) {
checkNotNull(sources);
checkNotNull(attrNames);
List> notNullSources = filterNulls(sources);
if (notNullSources.size() == 0 || attrNames.isEmpty()) {
Map nullMap = new HashMap<>();
for (String attrName : attrNames) {
nullMap.put(attrName, null);
}
return nullMap;
}
Map> groupedAttrs = groupBySubnode(attrNames);
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy