use of org.jvnet.hk2.config.ConfigParser in project Payara by payara.
the class ConfigModularityUtils method getCurrentConfigBeanForDefaultValue.
public <T extends ConfigBeanProxy> T getCurrentConfigBeanForDefaultValue(ConfigBeanDefaultValue defaultValue) throws InvocationTargetException, IllegalAccessException {
// TODO make this method target aware!
Class parentClass = getOwningClassForLocation(defaultValue.getLocation());
Class configBeanClass = getClassForFullName(defaultValue.getConfigBeanClassName());
Method m = findSuitableCollectionGetter(parentClass, configBeanClass);
if (m != null) {
ConfigParser configParser = new ConfigParser(serviceLocator);
// I don't use the GlassFish document here as I don't need persistence
final DomDocument doc = new DomDocument<GlassFishConfigBean>(serviceLocator) {
@Override
public Dom make(final ServiceLocator serviceLocator, XMLStreamReader xmlStreamReader, GlassFishConfigBean dom, ConfigModel configModel) {
// by default, people get the translated view.
return new GlassFishConfigBean(serviceLocator, this, dom, configModel, xmlStreamReader);
}
};
ConfigBeanProxy parent = getOwningObject(defaultValue.getLocation());
ConfigurationPopulator populator = new ConfigurationPopulator(defaultValue.getXmlConfiguration(), doc, parent);
populator.run(configParser);
ConfigBeanProxy configBean = doc.getRoot().createProxy(configBeanClass);
Collection col = (Collection) m.invoke(parent);
return (T) getConfigBeanFromCollection(col, configBean, configBeanClass);
}
return null;
}
use of org.jvnet.hk2.config.ConfigParser in project Payara by payara.
the class ConfigurationParser method parseAndSetConfigBean.
/**
* @param <T> the ConfigBeanProxy type we are looking for
*/
public <T extends ConfigBeanProxy> void parseAndSetConfigBean(List<ConfigBeanDefaultValue> values) {
ConfigParser configParser = new ConfigParser(serviceLocator);
// I don't use the GlassFish document here as I don't need persistence
final DomDocument doc = new DomDocument<GlassFishConfigBean>(serviceLocator) {
@Override
public Dom make(final ServiceLocator serviceLocator, XMLStreamReader xmlStreamReader, GlassFishConfigBean dom, ConfigModel configModel) {
return new GlassFishConfigBean(serviceLocator, this, dom, configModel, xmlStreamReader);
}
};
// the solution is to put the loop into the apply method... But it would be some fine amount of work
for (final ConfigBeanDefaultValue configBeanDefaultValue : values) {
final ConfigBeanProxy parent = configModularityUtils.getOwningObject(configBeanDefaultValue.getLocation());
if (parent == null)
continue;
ConfigurationPopulator populator = null;
if (replaceSystemProperties)
try {
populator = new ConfigurationPopulator(configModularityUtils.replacePropertiesWithCurrentValue(configBeanDefaultValue.getXmlConfiguration(), configBeanDefaultValue), doc, parent);
} catch (Exception e) {
LOG.log(Level.SEVERE, ConfigApiLoggerInfo.CFG_EXT_ADD_FAILED, e);
}
else {
// Check that parent is not null!
populator = new ConfigurationPopulator(configBeanDefaultValue.getXmlConfiguration(), doc, parent);
}
populator.run(configParser);
synchronized (configModularityUtils) {
boolean oldValue = configModularityUtils.isIgnorePersisting();
try {
Class configBeanClass = configModularityUtils.getClassForFullName(configBeanDefaultValue.getConfigBeanClassName());
final ConfigBeanProxy pr = doc.getRoot().createProxy(configBeanClass);
configModularityUtils.setIgnorePersisting(true);
ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {
public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
configModularityUtils.setConfigBean(pr, configBeanDefaultValue, param);
return param;
}
}, parent);
} catch (TransactionFailure e) {
LOG.log(Level.SEVERE, ConfigApiLoggerInfo.CFG_EXT_ADD_FAILED, e);
} finally {
configModularityUtils.setIgnorePersisting(oldValue);
}
}
}
}
use of org.jvnet.hk2.config.ConfigParser in project Payara by payara.
the class ConfigurationPopulator method run.
public void run(ConfigParser parser) {
try {
InputStream is = new ByteArrayInputStream(xmlContent.getBytes());
XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is, "utf-8");
parser.parse(reader, doc, Dom.unwrap((ConfigBeanProxy) parent));
} catch (XMLStreamException e) {
LOG.log(Level.SEVERE, ConfigApiLoggerInfo.DEFAULT_CFG_READ_FAILED, e);
}
}
use of org.jvnet.hk2.config.ConfigParser in project Payara by payara.
the class DomainXml method parseDomainXml.
/**
* Parses <tt>domain.xml</tt>
* @param parser
* @param domainXml
* @param serverName
*/
protected void parseDomainXml(ConfigParser parser, final URL domainXml, final String serverName) {
long startNano = System.nanoTime();
try {
ServerReaderFilter xsr = null;
// Set the resolver so that any external entity references, such
// as a reference to a DTD, return an empty file. The domain.xml
// file doesn't support entity references.
xif.setXMLResolver(new XMLResolver() {
@Override
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
return new ByteArrayInputStream(new byte[0]);
}
});
if (null == env.getRuntimeType()) {
throw new RuntimeException("Internal Error: Unknown server type: " + env.getRuntimeType());
} else {
switch(env.getRuntimeType()) {
case DAS:
case EMBEDDED:
case MICRO:
xsr = new DasReaderFilter(domainXml, xif);
break;
case INSTANCE:
xsr = new InstanceReaderFilter(env.getInstanceName(), domainXml, xif);
break;
default:
throw new RuntimeException("Internal Error: Unknown server type: " + env.getRuntimeType());
}
}
Lock lock = null;
try {
// lock the domain.xml for reading if not embedded
try {
lock = configAccess.accessRead();
} catch (Exception e) {
// ignore
}
parser.parse(xsr, getDomDocument());
xsr.close();
} finally {
if (lock != null) {
lock.unlock();
}
}
String errorMessage = xsr.configWasFound();
if (errorMessage != null) {
LogRecord lr = new LogRecord(Level.WARNING, errorMessage);
lr.setLoggerName(getClass().getName());
EarlyLogHandler.earlyMessages.add(lr);
}
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException("Fatal Error. Unable to parse " + domainXml, e);
}
}
Long l = System.nanoTime() - startNano;
LogRecord lr = new LogRecord(Level.FINE, totalTimeToParseDomain + l.toString());
lr.setLoggerName(getClass().getName());
EarlyLogHandler.earlyMessages.add(lr);
}
use of org.jvnet.hk2.config.ConfigParser in project Payara by payara.
the class ConfigTest method parseDomainXml.
// @Test
public void parseDomainXml() {
ConfigParser parser = new ConfigParser(habitat);
URL url = this.getClass().getResource("/domain.xml");
System.out.println("URL : " + url);
try {
DomDocument doc = parser.parse(url);
System.out.println("[parseDomainXml] ==> Successfully parsed");
assert (doc != null);
} catch (Exception ex) {
ex.printStackTrace();
assert (false);
}
}
Aggregations