use of org.glassfish.hk2.xml.internal.ParentedModel in project glassfish-hk2 by eclipse-ee4j.
the class Utilities method internalAdd.
@SuppressWarnings("unchecked")
public static BaseHK2JAXBBean internalAdd(BaseHK2JAXBBean myParent, String childPropertyNamespace, String childProperty, Object rawChild, String childKey, int index, DynamicChangeInfo<?> changeInformation, XmlDynamicChange xmlDynamicChange, List<ActiveDescriptor<?>> addedServices, boolean changeList) {
if (index < -1) {
throw new IllegalArgumentException("Unknown index " + index);
}
if (childKey != null && myParent._lookupChild(childPropertyNamespace, childProperty, childKey) != null) {
throw new IllegalStateException("There is already a child with name " + childKey + " for child " + childProperty);
}
if (rawChild != null && !(rawChild instanceof BaseHK2JAXBBean)) {
throw new IllegalArgumentException("The child added must be from XmlService.createBean");
}
ParentedModel childNode = myParent._getModel().getChild(childPropertyNamespace, childProperty);
if (childNode == null) {
throw new IllegalArgumentException("There is no child with xmlTag " + childProperty + " of " + myParent);
}
Object allMyChildren = myParent._getProperty(childPropertyNamespace, childProperty);
List<Object> multiChildren = null;
if (!ChildType.DIRECT.equals(childNode.getChildType())) {
if (allMyChildren == null) {
multiChildren = new ArrayList<Object>(10);
} else {
if (ChildType.LIST.equals(childNode.getChildType())) {
multiChildren = new ArrayList<Object>((List<Object>) allMyChildren);
} else {
multiChildren = new ArrayList<Object>(Arrays.asList((Object[]) allMyChildren));
}
}
if (changeList && index > multiChildren.size()) {
throw new IllegalArgumentException("The index given to add child " + childProperty + " to " + myParent + " is not in range (" + index + "," + multiChildren.size() + ")");
}
if (index == -1) {
index = multiChildren.size();
}
}
BaseHK2JAXBBean child = createBean(childNode.getChildModel().getProxyAsClass());
child._setClassReflectionHelper(myParent._getClassReflectionHelper());
if (rawChild != null) {
// Handling of children will be handled once the real child is better setup
BaseHK2JAXBBean childToCopy = (BaseHK2JAXBBean) rawChild;
for (QName nonChildProperty : childToCopy._getModel().getNonChildProperties().keySet()) {
String nonChildPropNamespace = QNameUtilities.getNamespace(nonChildProperty);
String nonChildPropKey = nonChildProperty.getLocalPart();
Object value = childToCopy._getProperty(nonChildPropNamespace, nonChildPropKey);
if (value == null)
continue;
child._setProperty(nonChildPropNamespace, nonChildPropKey, value, false, true);
}
}
if (childKey == null) {
if (childNode.getChildModel().getKeyProperty() != null) {
if (rawChild != null) {
String keyPropNamespace = QNameUtilities.getNamespace(childNode.getChildModel().getKeyProperty());
String keyPropKey = childNode.getChildModel().getKeyProperty().getLocalPart();
childKey = (String) child._getProperty(keyPropNamespace, keyPropKey);
}
if (childKey == null) {
throw new IllegalArgumentException("Attempted to create child with xmlTag " + childProperty + " with no key field in " + myParent);
}
child._setKeyValue(childKey);
} else if (!ChildType.DIRECT.equals(childNode.getChildType())) {
// This is a multi-child with no key and no key property, must generate a key
if (myParent._getChangeControl() == null) {
childKey = NOT_UNIQUE_UNIQUE_ID;
child._setKeyValue(NOT_UNIQUE_UNIQUE_ID);
} else {
childKey = myParent._getChangeControl().getGeneratedId();
child._setKeyValue(childKey);
}
}
} else {
/* childKey != null */
if (childNode.getChildModel().getKeyProperty() == null) {
throw new IllegalArgumentException("Attempted to add an unkeyed child with key " + childKey + " in " + myParent);
}
QName keyProp = childNode.getChildModel().getKeyProperty();
child._setProperty(QNameUtilities.getNamespace(keyProp), keyProp.getLocalPart(), childKey, false, true);
child._setKeyValue(childKey);
}
child._setParent(myParent);
child._setSelfXmlTag(childNode.getChildXmlNamespace(), constructXmlTag(childNode.getXmlWrapperTag(), childNode.getChildXmlTag()));
child._setKeyValue(childKey);
if (childKey != null) {
child._setInstanceName(composeInstanceName(myParent._getInstanceName(), child._getKeyValue(), childNode.getXmlWrapperTag()));
} else {
child._setInstanceName(composeInstanceName(myParent._getInstanceName(), childNode.getChildXmlTag(), childNode.getXmlWrapperTag()));
}
// Now freeze it
child._setDynamicChangeInfo((XmlRootHandleImpl<?>) myParent._getRoot(), changeInformation);
externalAdd(child, xmlDynamicChange.getDynamicConfiguration(), xmlDynamicChange.getBeanDatabase(), addedServices);
Utilities.invokeVetoableChangeListeners(changeInformation, child, null, child, EMPTY_STRING, myParent._getClassReflectionHelper());
if (rawChild != null) {
// Now we handle the children
handleChildren(child, (BaseHK2JAXBBean) rawChild, changeInformation, addedServices, xmlDynamicChange);
}
if (!changeList) {
return child;
}
// Now modify the actual list
if (multiChildren != null) {
// List or Array child
multiChildren.add(index, child);
Object finalChildList;
if (ChildType.LIST.equals(childNode.getChildType())) {
finalChildList = Collections.unmodifiableList(multiChildren);
} else {
// ARRAY
finalChildList = Array.newInstance(childNode.getChildModel().getOriginalInterfaceAsClass(), multiChildren.size());
for (int lcv = 0; lcv < multiChildren.size(); lcv++) {
Array.set(finalChildList, lcv, multiChildren.get(lcv));
}
}
if (xmlDynamicChange.getBeanDatabase() != null) {
myParent._changeInHub(childPropertyNamespace, childProperty, finalChildList, xmlDynamicChange.getBeanDatabase());
}
myParent._setProperty(childPropertyNamespace, childProperty, finalChildList, false, true);
} else {
// Direct child
if (xmlDynamicChange.getBeanDatabase() != null) {
myParent._changeInHub(childPropertyNamespace, childProperty, child, xmlDynamicChange.getBeanDatabase());
}
myParent._setProperty(childPropertyNamespace, childProperty, child, false, true);
}
return child;
}
use of org.glassfish.hk2.xml.internal.ParentedModel in project glassfish-hk2 by eclipse-ee4j.
the class Utilities method getDescriptorsToRemove.
@SuppressWarnings("unchecked")
private static void getDescriptorsToRemove(BaseHK2JAXBBean fromMe, HashSet<ActiveDescriptor<?>> descriptorsToRemove) {
ActiveDescriptor<?> fromMeDescriptor = fromMe._getSelfDescriptor();
if (fromMeDescriptor == null)
return;
descriptorsToRemove.add(fromMeDescriptor);
ModelImpl model = fromMe._getModel();
if (model == null)
return;
for (ParentedModel parentedChild : model.getAllChildren()) {
String childPropertyNamespace = parentedChild.getChildXmlNamespace();
String childPropertyName = parentedChild.getChildXmlTag();
switch(parentedChild.getChildType()) {
case LIST:
List<BaseHK2JAXBBean> listChildren = (List<BaseHK2JAXBBean>) fromMe._getProperty(childPropertyNamespace, childPropertyName);
if (listChildren != null) {
for (BaseHK2JAXBBean listChild : listChildren) {
getDescriptorsToRemove(listChild, descriptorsToRemove);
}
}
break;
case ARRAY:
Object arrayChildren = fromMe._getProperty(childPropertyNamespace, childPropertyName);
if (arrayChildren != null) {
int arrayLength = Array.getLength(arrayChildren);
for (int lcv = 0; lcv < arrayLength; lcv++) {
BaseHK2JAXBBean bean = (BaseHK2JAXBBean) Array.get(arrayChildren, lcv);
getDescriptorsToRemove(bean, descriptorsToRemove);
}
}
break;
case DIRECT:
BaseHK2JAXBBean bean = (BaseHK2JAXBBean) fromMe._getProperty(childPropertyNamespace, childPropertyName);
if (bean != null) {
getDescriptorsToRemove(bean, descriptorsToRemove);
}
break;
default:
throw new AssertionError("Unknown child type " + parentedChild.getChildType());
}
}
}
use of org.glassfish.hk2.xml.internal.ParentedModel in project glassfish-hk2 by eclipse-ee4j.
the class Utilities method getListDifferences.
@SuppressWarnings("unchecked")
private static void getListDifferences(ParentedModel pModel, Object sourceValue, Object otherValue, Differences differences, String xmlTag, BaseHK2JAXBBean source) {
Difference localDifference = new Difference(source);
QName keyProperty = pModel.getChildModel().getKeyProperty();
List<BaseHK2JAXBBean> sourceValueList = (List<BaseHK2JAXBBean>) sourceValue;
List<BaseHK2JAXBBean> otherValueList = (List<BaseHK2JAXBBean>) otherValue;
if (sourceValueList == null)
sourceValueList = Collections.emptyList();
if (otherValueList == null)
otherValueList = Collections.emptyList();
if (keyProperty != null) {
String keyPropertyNamespace = QNameUtilities.getNamespace(keyProperty);
String keyPropertyKey = keyProperty.getLocalPart();
Map<String, Integer> sourceIndexMap = getIndexMap(sourceValueList, keyPropertyNamespace, keyPropertyKey);
Map<String, Integer> otherIndexMap = getIndexMap(otherValueList, keyPropertyNamespace, keyPropertyKey);
for (BaseHK2JAXBBean sourceBean : sourceValueList) {
String sourceKeyValue = sourceBean._getKeyValue();
if (!otherIndexMap.containsKey(sourceKeyValue)) {
localDifference.addRemove(xmlTag, new RemoveData(xmlTag, sourceKeyValue, sourceBean));
} else {
int sourceIndex = sourceIndexMap.get(sourceKeyValue);
int otherIndex = otherIndexMap.get(sourceKeyValue);
Object otherBean = otherValueList.get(otherIndex);
if (otherIndex != sourceIndex) {
localDifference.addMove(xmlTag, new MoveData(sourceIndex, otherIndex));
}
// Need to know sub-differences
getAllDifferences(sourceBean, (BaseHK2JAXBBean) otherBean, differences);
}
}
for (BaseHK2JAXBBean otherBean : otherValueList) {
String otherKeyValue = otherBean._getKeyValue();
if (otherKeyValue == null) {
otherKeyValue = (String) otherBean._getProperty(keyPropertyNamespace, keyPropertyKey);
}
if (!sourceIndexMap.containsKey(otherKeyValue)) {
int addedIndex = otherIndexMap.get(otherKeyValue);
localDifference.addAdd(xmlTag, otherBean, addedIndex);
}
}
} else {
// Both lists are there, this is an unkeyed list, we go *purely* on list size
UnkeyedDiff unkeyedDiff = new UnkeyedDiff(sourceValueList, otherValueList, source, pModel);
Differences unkeyedDiffs = unkeyedDiff.compute();
differences.merge(unkeyedDiffs);
}
if (localDifference.isDirty()) {
differences.addDifference(localDifference);
}
}
use of org.glassfish.hk2.xml.internal.ParentedModel in project glassfish-hk2 by eclipse-ee4j.
the class Utilities method handleChildren.
@SuppressWarnings("unchecked")
private static void handleChildren(BaseHK2JAXBBean child, BaseHK2JAXBBean childToCopy, DynamicChangeInfo<?> changeInformation, List<ActiveDescriptor<?>> addedServices, XmlDynamicChange xmlDynamicChange) {
Map<QName, ParentedModel> childrenMap = childToCopy._getModel().getChildrenProperties();
for (Map.Entry<QName, ParentedModel> childsChildrenEntry : childrenMap.entrySet()) {
QName childsChildProperty = childsChildrenEntry.getKey();
ParentedModel childsChildParentNode = childsChildrenEntry.getValue();
String childsChildPropertyNamespace = QNameUtilities.getNamespace(childsChildProperty);
String childsChildPropertyKey = childsChildProperty.getLocalPart();
if (!ChildType.DIRECT.equals(childsChildParentNode.getChildType())) {
List<BaseHK2JAXBBean> childsChildren = null;
if (ChildType.LIST.equals(childsChildParentNode.getChildType())) {
childsChildren = (List<BaseHK2JAXBBean>) childToCopy._getProperty(childsChildPropertyNamespace, childsChildPropertyKey);
} else {
Object arrayChildsChildren = childToCopy._getProperty(childsChildPropertyNamespace, childsChildPropertyKey);
if (arrayChildsChildren != null) {
// This object is an array
childsChildren = new ArrayList<BaseHK2JAXBBean>(Array.getLength(arrayChildsChildren));
for (int lcv = 0; lcv < Array.getLength(arrayChildsChildren); lcv++) {
childsChildren.add(lcv, (BaseHK2JAXBBean) Array.get(arrayChildsChildren, lcv));
}
}
}
if (childsChildren == null)
continue;
if (childsChildren.size() <= 0)
continue;
ArrayList<BaseHK2JAXBBean> copiedChildArray = new ArrayList<BaseHK2JAXBBean>(childsChildren.size());
Object asArray = Array.newInstance(childsChildParentNode.getChildModel().getOriginalInterfaceAsClass(), childsChildren.size());
int lcv = 0;
for (BaseHK2JAXBBean childsChild : childsChildren) {
BaseHK2JAXBBean grandchild = internalAdd(child, childsChildPropertyNamespace, childsChildPropertyKey, childsChild, null, -1, changeInformation, xmlDynamicChange, addedServices, false);
copiedChildArray.add(grandchild);
Array.set(asArray, lcv++, grandchild);
}
if (ChildType.LIST.equals(childsChildParentNode.getChildType())) {
child._setProperty(childsChildPropertyNamespace, childsChildPropertyKey, copiedChildArray, false, true);
} else {
child._setProperty(childsChildPropertyNamespace, childsChildPropertyKey, asArray, false, true);
}
} else {
BaseHK2JAXBBean childsChild = (BaseHK2JAXBBean) childToCopy._getProperty(childsChildPropertyNamespace, childsChildPropertyKey);
if (childsChild == null)
continue;
BaseHK2JAXBBean grandchild = internalAdd(child, childsChildPropertyNamespace, childsChildPropertyKey, childsChild, null, -1, changeInformation, xmlDynamicChange, addedServices, false);
child._setProperty(childsChildPropertyNamespace, childsChildPropertyKey, grandchild, false, true);
}
}
}
use of org.glassfish.hk2.xml.internal.ParentedModel in project glassfish-hk2 by eclipse-ee4j.
the class Utilities method invokeAllDeletedChangeListeners.
@SuppressWarnings("unchecked")
private static void invokeAllDeletedChangeListeners(DynamicChangeInfo<?> control, BaseHK2JAXBBean rootBean, ClassReflectionHelper helper) {
ModelImpl model = rootBean._getModel();
Map<QName, ParentedModel> childrenByName = model.getChildrenByName();
for (Map.Entry<QName, ParentedModel> entry : childrenByName.entrySet()) {
QName propertyName = entry.getKey();
ParentedModel parentModel = entry.getValue();
String propertyNameNamespace = QNameUtilities.getNamespace(propertyName);
String propertyNameKey = propertyName.getLocalPart();
Object child = rootBean._getProperty(propertyNameNamespace, propertyNameKey);
if (child == null)
continue;
if (ChildType.LIST.equals(parentModel.getChildType())) {
List<BaseHK2JAXBBean> listChildren = (List<BaseHK2JAXBBean>) child;
for (BaseHK2JAXBBean grandchild : listChildren) {
invokeAllDeletedChangeListeners(control, grandchild, helper);
}
} else if (ChildType.ARRAY.equals(parentModel.getChildType())) {
int length = Array.getLength(child);
for (int lcv = 0; lcv < length; lcv++) {
BaseHK2JAXBBean grandchild = (BaseHK2JAXBBean) Array.get(child, lcv);
invokeAllDeletedChangeListeners(control, grandchild, helper);
}
} else if (ChildType.DIRECT.equals(parentModel.getChildType())) {
BaseHK2JAXBBean grandchild = (BaseHK2JAXBBean) child;
invokeAllDeletedChangeListeners(control, grandchild, helper);
}
}
invokeVetoableChangeListeners(control, rootBean, rootBean, null, EMPTY_STRING, helper);
}
Aggregations