org.glassfish.hk2.xml.internal.XmlServiceImpl Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2014 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.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.glassfish.hk2.api.DynamicConfiguration;
import org.glassfish.hk2.api.DynamicConfigurationService;
import org.glassfish.hk2.api.MultiException;
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.utilities.reflection.ClassReflectionHelper;
import org.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl;
import org.glassfish.hk2.xml.api.XmlHubCommitMessage;
import org.glassfish.hk2.xml.api.XmlRootHandle;
import org.glassfish.hk2.xml.api.XmlService;
import org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean;
/**
* @author jwells
*
*/
@Singleton
public class XmlServiceImpl implements XmlService {
private final static String ID_PREFIX = "XmlServiceUID-";
private final JAUtilities jaUtilities = new JAUtilities();
@Inject
private ServiceLocator serviceLocator;
@Inject
private DynamicConfigurationService dynamicConfigurationService;
@Inject
private Hub hub;
private final ClassReflectionHelper classReflectionHelper = new ClassReflectionHelperImpl();
private final AtomicLong idGenerator = new AtomicLong();
/**
* Gets the XmlService wide unique identifier
* @return A unique identifier for unkeyed multi-children
*/
public String getUniqueId() {
return ID_PREFIX + idGenerator.getAndAdd(1L);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlService#unmarshall(java.net.URI, java.lang.Class, boolean, boolean)
*/
@Override
public XmlRootHandle unmarshall(URI uri,
Class jaxbAnnotatedClassOrInterface) {
return unmarshall(uri, jaxbAnnotatedClassOrInterface, true, true);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlService#unmarshall(java.net.URI, java.lang.Class)
*/
@Override
public XmlRootHandle unmarshall(URI uri,
Class jaxbAnnotatedInterface,
boolean advertiseInRegistry, boolean advertiseInHub) {
if (uri == null || jaxbAnnotatedInterface == null) throw new IllegalArgumentException();
if (!jaxbAnnotatedInterface.isInterface()) {
throw new IllegalArgumentException("Only an interface can be given to unmarshall: " + jaxbAnnotatedInterface.getName());
}
try {
UnparentedNode parent = jaUtilities.convertRootAndLeaves(jaxbAnnotatedInterface);
return unmarshallClass(uri, parent, advertiseInRegistry, advertiseInHub);
}
catch (RuntimeException re) {
throw re;
}
catch (Exception e) {
throw new MultiException(e);
}
}
@SuppressWarnings("unchecked")
private XmlRootHandle unmarshallClass(URI uri, UnparentedNode node,
boolean advertise, boolean advertiseInHub) throws Exception {
JAXBContext context = JAXBContext.newInstance(node.getTranslatedClass());
Listener listener = new Listener();
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setListener(listener);
T root = (T) unmarshaller.unmarshal(uri.toURL());
DynamicChangeInfo changeControl = new DynamicChangeInfo(jaUtilities,
((advertiseInHub) ? hub : null),
this,
((advertise) ? dynamicConfigurationService : null),
serviceLocator);
for (BaseHK2JAXBBean base : listener.getAllBeans()) {
String instanceName = Utilities.createInstanceName(base);
base._setInstanceName(instanceName);
base._setDynamicChangeInfo(changeControl);
}
DynamicConfiguration config = (advertise) ? dynamicConfigurationService.createDynamicConfiguration() : null ;
WriteableBeanDatabase wdb = (advertiseInHub) ? hub.getWriteableDatabaseCopy() : null ;
for (BaseHK2JAXBBean bean : listener.getAllBeans()) {
Utilities.advertise(wdb, config, bean);
}
if (config != null) {
config.commit();
}
if (wdb != null) {
wdb.commit(new XmlHubCommitMessage() {});
}
return new XmlRootHandleImpl(this, hub, root, node, uri, advertise, advertiseInHub, changeControl);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlService#createEmptyHandle(java.lang.Class)
*/
@Override
public XmlRootHandle createEmptyHandle(
Class jaxbAnnotatedInterface, boolean advertiseInRegistry,
boolean advertiseInHub) {
if (!jaxbAnnotatedInterface.isInterface()) {
throw new IllegalArgumentException("Only an interface can be given to unmarshall: " + jaxbAnnotatedInterface.getName());
}
try {
UnparentedNode node = jaUtilities.convertRootAndLeaves(jaxbAnnotatedInterface);
return new XmlRootHandleImpl(this, hub, null, node, null, advertiseInRegistry, advertiseInHub,
new DynamicChangeInfo(jaUtilities,
hub,
this,
((advertiseInRegistry) ? dynamicConfigurationService : null),
serviceLocator));
}
catch (RuntimeException re) {
throw re;
}
catch (Exception e) {
throw new MultiException(e);
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlService#createEmptyHandle(java.lang.Class, boolean, boolean)
*/
@Override
public XmlRootHandle createEmptyHandle(
Class jaxbAnnotationInterface) {
return createEmptyHandle(jaxbAnnotationInterface, true, true);
}
private class Listener extends Unmarshaller.Listener {
private final LinkedList allBeans = new LinkedList();
private void setUserKey(BaseHK2JAXBBean bean, boolean listOrArray) {
UnparentedNode model = bean._getModel();
String keyProperty = model.getKeyProperty();
if (keyProperty == null && listOrArray) {
bean._setKeyValue(getUniqueId());
return;
}
if (keyProperty == null) return;
String key = (String) bean._getProperty(keyProperty);
if (key == null) return;
bean._setKeyValue(key);
}
@SuppressWarnings("unchecked")
private void setSelfXmlTagInAllChildren(BaseHK2JAXBBean targetBean) {
UnparentedNode model = targetBean._getModel();
for (ParentedNode parentedNode : model.getAllChildren()) {
Object children = targetBean._getProperty(parentedNode.getChildName());
if (children == null) continue;
if (children instanceof List) {
for (Object child : (List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy