org.glassfish.hk2.xml.internal.XmlRootHandleImpl Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2014-2017 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.hk2.xml.internal;
import java.beans.VetoableChangeListener;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.DynamicConfiguration;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.configuration.hub.api.Hub;
import org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase;
import org.glassfish.hk2.xml.api.XmlHandleTransaction;
import org.glassfish.hk2.xml.api.XmlHubCommitMessage;
import org.glassfish.hk2.xml.api.XmlRootCopy;
import org.glassfish.hk2.xml.api.XmlRootHandle;
import org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean;
import org.glassfish.hk2.xml.spi.XmlServiceParser;
/**
* @author jwells
*
*/
public class XmlRootHandleImpl implements XmlRootHandle {
private final XmlServiceImpl parent;
private final Hub hub;
private T root;
private final ModelImpl rootNode;
private URI rootURI;
private final boolean advertised;
private final boolean advertisedInHub;
private final DynamicChangeInfo changeControl;
/* package */ XmlRootHandleImpl(
XmlServiceImpl parent,
Hub hub,
T root,
ModelImpl rootNode,
URI rootURI,
boolean advertised,
boolean inHub,
DynamicChangeInfo changes) {
this.parent = parent;
this.hub = hub;
this.root = root;
this.rootNode = rootNode;
this.rootURI = rootURI;
this.advertised = advertised;
this.advertisedInHub = inHub;
this.changeControl = changes;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlRootHandle#getRoot()
*/
@Override
public T getRoot() {
return root;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlRootHandle#getRootClass()
*/
@SuppressWarnings("unchecked")
@Override
public Class getRootClass() {
return (Class) rootNode.getOriginalInterfaceAsClass();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlRootHandle#getURI()
*/
@Override
public URI getURI() {
return rootURI;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlRootHandle#isAdvertisedInLocator()
*/
@Override
public boolean isAdvertisedInLocator() {
return advertised;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlRootHandle#isAdvertisedInHub()
*/
@Override
public boolean isAdvertisedInHub() {
return advertisedInHub;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlRootHandle#overlay(org.glassfish.hk2.xml.api.XmlRootHandle)
*/
@Override
public void overlay(XmlRootHandle newRoot) {
if (!(newRoot instanceof XmlRootHandle)) {
throw new IllegalArgumentException("newRoot must have been created by the same XmlService as this one");
}
XmlRootHandleImpl newRootImpl = (XmlRootHandleImpl) newRoot;
if (newRootImpl.isAdvertisedInHub()) {
throw new IllegalArgumentException("The newRoot must not be advertised in the Hub");
}
if (newRootImpl.isAdvertisedInLocator()) {
throw new IllegalArgumentException("The newRoot must not be advertised as hk2 services");
}
if (root == null) {
throw new IllegalArgumentException("This XmlRootHandle must have a root to be overlayed");
}
T newRootRoot = newRootImpl.getRoot();
if (newRootRoot == null) {
throw new IllegalArgumentException("The newRoot must have a root to overlay onto this root");
}
if (!(newRootRoot instanceof BaseHK2JAXBBean)) {
throw new IllegalArgumentException("The newRoot has a root of an unknown type: " + newRootRoot.getClass().getName());
}
if (!newRootRoot.getClass().equals(root.getClass())) {
throw new IllegalArgumentException("The two roots must have the same class as this root, instead it is of type " + newRootRoot.getClass().getName());
}
if (newRootRoot.equals(root)) {
throw new IllegalArgumentException("Cannot overlay the same bean on top of itself");
}
BaseHK2JAXBBean newRootBase = (BaseHK2JAXBBean) newRootRoot;
BaseHK2JAXBBean oldRootBase = (BaseHK2JAXBBean) root;
boolean success = false;
XmlHandleTransaction handle = lockForTransaction();
try {
Differences differences = Utilities.getDiff(oldRootBase, newRootBase);
if (!differences.getDifferences().isEmpty()) {
Utilities.applyDiff(differences, changeControl);
}
success = true;
}
finally {
if (success) {
handle.commit();
}
else {
handle.abandon();
}
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlRootHandle#getXmlRootCopy()
*/
@SuppressWarnings("unchecked")
@Override
public XmlRootCopy getXmlRootCopy() {
// In any case, the child should not be directly given the hub, as
// it is not reflected in the hub
DynamicChangeInfo copyController =
new DynamicChangeInfo(changeControl.getJAUtilities(),
null,
false,
changeControl.getIdGenerator(),
null,
false,
changeControl.getServiceLocator());
changeControl.getReadLock().lock();
try {
BaseHK2JAXBBean bean = (BaseHK2JAXBBean) root;
if (bean == null) {
return new XmlRootCopyImpl(this, changeControl.getChangeNumber(), null);
}
BaseHK2JAXBBean copy;
try {
Map referenceMap = new HashMap();
List unresolved = new LinkedList();
copy = doCopy(bean, copyController, null, referenceMap, unresolved);
Utilities.fillInUnfinishedReferences(referenceMap, unresolved);
}
catch (RuntimeException re) {
throw re;
}
catch (Throwable th) {
throw new RuntimeException(th);
}
return new XmlRootCopyImpl(this, changeControl.getChangeNumber(), (T) copy);
}
finally {
changeControl.getReadLock().unlock();
}
}
private BaseHK2JAXBBean doCopy(BaseHK2JAXBBean copyMe,
DynamicChangeInfo copyController,
BaseHK2JAXBBean theCopiedParent,
Map referenceMap,
List unresolved) throws Throwable {
if (copyMe == null) return null;
BaseHK2JAXBBean retVal = Utilities.createBean(copyMe.getClass());
retVal._shallowCopyFrom(copyMe);
ModelImpl myModel = retVal._getModel();
Set childrenProps = copyMe._getChildrenXmlTags();
for (String childProp : childrenProps) {
Object child = copyMe._getProperty(childProp);
if (child == null) continue;
if (child instanceof List) {
List> childList = (List>) child;
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy