use of org.jvnet.hk2.config.ConfigBeanProxy in project Payara by payara.
the class ConfigModularityUtils method getOwningObject.
public ConfigBeanProxy getOwningObject(String location) {
if (!location.startsWith("domain/configs")) {
if (!location.startsWith("domain")) {
// Sorry only know domain and below :D
return null;
}
StringTokenizer tokenizer = new StringTokenizer(location, "/", false);
// something directly inside the domain itself as we know one token is domain for sure
if (tokenizer.countTokens() == 1) {
return serviceLocator.getService(Domain.class);
}
location = location.substring(location.indexOf("/", "domain".length()) + 1);
tokenizer = new StringTokenizer(location, "/", false);
ConfigBeanProxy parent = serviceLocator.getService(Domain.class);
// skipping the domain itself as a token, we know it and took it away.
String parentElement = "domain";
String childElement = null;
while (tokenizer.hasMoreTokens()) {
try {
childElement = tokenizer.nextToken();
parent = getOwner(parent, parentElement, childElement);
parentElement = childElement;
} catch (Exception e) {
LogHelper.log(LOG, Level.INFO, cannotGetParentConfigBean, e, childElement);
}
}
return parent;
} else {
Class typeToFindGetter = getOwningClassForLocation(location);
if (typeToFindGetter == null) {
return null;
}
// Check if config object is where the location or it goes deeper in the config layers.
StringTokenizer tokenizer = new StringTokenizer(location, "/", false);
// something directly inside the config itself
if (tokenizer.countTokens() == 3) {
String expression = location.substring(location.lastIndexOf("[") + 1, location.length() - 1);
String configName = resolveExpression(expression);
return serviceLocator.<Domain>getService(Domain.class).getConfigNamed(configName);
}
location = location.substring(location.indexOf("/", "domain/configs".length()) + 1);
tokenizer = new StringTokenizer(location, "/", false);
String curLevel = tokenizer.nextToken();
String expression;
if (curLevel.contains("[")) {
expression = curLevel.substring(curLevel.lastIndexOf("[") + 1, curLevel.length() - 1);
} else {
expression = curLevel;
}
String configName = resolveExpression(expression);
ConfigBeanProxy parent = serviceLocator.<Domain>getService(Domain.class).getConfigNamed(configName);
String childElement;
String parentElement = "Config";
while (tokenizer.hasMoreTokens()) {
try {
childElement = tokenizer.nextToken();
parent = getOwner(parent, parentElement, childElement);
parentElement = childElement;
} catch (Exception e) {
LogHelper.log(LOG, Level.INFO, cannotGetParentConfigBean, e, configName);
}
}
return parent;
}
}
use of org.jvnet.hk2.config.ConfigBeanProxy in project Payara by payara.
the class ConfigModularityUtils method setConfigBean.
public <T extends ConfigBeanProxy> T setConfigBean(T finalConfigBean, ConfigBeanDefaultValue configBeanDefaultValue, ConfigBeanProxy parent) {
Class owningClassForLocation = getOwningClassForLocation(configBeanDefaultValue.getLocation());
Class configBeanClass = getClassForFullName(configBeanDefaultValue.getConfigBeanClassName());
try {
ConfigBeanProxy configBeanInstance = null;
if (getNameForConfigBean(finalConfigBean, configBeanClass) == null) {
List<ConfigBeanProxy> extensions = getExtensions(parent);
for (ConfigBeanProxy extension : extensions) {
try {
configBeanInstance = (ConfigBeanProxy) configBeanClass.cast(extension);
break;
} catch (Exception e) {
// ignore, not the right type.
}
}
if (!configBeanDefaultValue.replaceCurrentIfExists() || !stackPositionHigher(finalConfigBean, configBeanInstance)) {
if (configBeanInstance != null)
return (T) configBeanInstance;
}
if (configBeanInstance != null) {
extensions.remove(configBeanInstance);
}
}
} catch (InvocationTargetException e) {
LOG.log(Level.INFO, cannotSetConfigBean, e);
} catch (IllegalAccessException e) {
LOG.log(Level.INFO, cannotSetConfigBean, e);
}
Method m = getMatchingSetterMethod(owningClassForLocation, configBeanClass);
if (m != null) {
try {
if (configBeanClass.getAnnotation(HasCustomizationTokens.class) != null) {
applyCustomTokens(configBeanDefaultValue, finalConfigBean, parent);
}
m.invoke(parent, finalConfigBean);
} catch (Exception e) {
LogHelper.log(LOG, Level.INFO, cannotSetConfigBeanFor, e, finalConfigBean.getClass().getName());
}
return finalConfigBean;
}
m = findSuitableCollectionGetter(owningClassForLocation, configBeanClass);
if (m != null) {
try {
Collection col = (Collection) m.invoke(parent);
String name = getNameForConfigBean(finalConfigBean, configBeanClass);
ConfigBeanProxy itemToRemove = getNamedConfigBeanFromCollection(col, name, configBeanClass);
if (configBeanDefaultValue.replaceCurrentIfExists()) {
try {
if (itemToRemove != null) {
if (stackPositionHigher(finalConfigBean, itemToRemove)) {
col.remove(itemToRemove);
}
}
} catch (Exception ex) {
LogHelper.log(LOG, Level.INFO, cannotRemoveConfigBean, ex, finalConfigBean.getClass().getName());
}
}
if (configBeanClass.getAnnotation(HasCustomizationTokens.class) != null) {
applyCustomTokens(configBeanDefaultValue, finalConfigBean, parent);
}
if (itemToRemove != null && !configBeanDefaultValue.replaceCurrentIfExists()) {
// Check for duplication here.
if (((ConfigView) Proxy.getInvocationHandler(itemToRemove)).getProxyType().isAssignableFrom(configBeanClass)) {
return finalConfigBean;
}
}
col.add(finalConfigBean);
return finalConfigBean;
} catch (Exception e) {
LogHelper.log(LOG, Level.INFO, cannotSetConfigBeanFor, e, finalConfigBean.getClass().getName());
}
}
return null;
}
use of org.jvnet.hk2.config.ConfigBeanProxy 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.ConfigBeanProxy in project Payara by payara.
the class ConfigModularityUtils method getOwner.
public ConfigBeanProxy getOwner(ConfigBeanProxy parent, String parentElement, String childElement) throws InvocationTargetException, IllegalAccessException {
if (childElement.contains("CURRENT_INSTANCE_CONFIG_NAME")) {
return serviceLocator.<Config>getService(Config.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
}
if (childElement.contains("CURRENT_INSTANCE_SERVER_NAME")) {
return serviceLocator.<Server>getService(Server.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
}
if (childElement.endsWith("]")) {
String componentName;
String elementName;
elementName = childElement.substring(childElement.lastIndexOf("/") + 1, childElement.indexOf("["));
componentName = childElement.substring(childElement.lastIndexOf("[") + 1, childElement.indexOf("]"));
Class childClass = getClassFor(elementName);
Class parentClass = getClassFor(parentElement);
Method m = findSuitableCollectionGetter(parentClass, childClass);
if (m != null) {
try {
Collection col = (Collection) m.invoke(parent);
componentName = resolveExpression(componentName);
return getNamedConfigBeanFromCollection(col, componentName, childClass);
} catch (Exception e) {
LogHelper.log(LOG, Level.INFO, invalidPath, e, childElement, componentName);
}
}
return null;
} else {
Class clz = getClassFor(childElement);
if (parent == null)
return null;
Method m = getMatchingGetterMethod(parent.getClass(), clz);
if (m != null) {
return (ConfigBeanProxy) m.invoke(parent);
} else {
try {
m = parent.getClass().getMethod("getExtensionByType", java.lang.Class.class);
} catch (NoSuchMethodException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot find getExtensionByType", e);
}
}
if (m != null) {
return (ConfigBeanProxy) m.invoke(parent, clz);
}
return null;
}
}
}
use of org.jvnet.hk2.config.ConfigBeanProxy in project Payara by payara.
the class ConfigModularityUtils method serializeConfigBean.
public String serializeConfigBean(ConfigBeanProxy configBean) {
if (configBean == null)
return null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = null;
IndentingXMLStreamWriter indentingXMLStreamWriter = null;
String s = null;
try {
writer = xmlFactory.createXMLStreamWriter(new BufferedOutputStream(bos));
indentingXMLStreamWriter = new IndentingXMLStreamWriter(writer);
Dom configBeanDom = Dom.unwrap(configBean);
configBeanDom.writeTo(configBeanDom.model.getTagName(), indentingXMLStreamWriter);
indentingXMLStreamWriter.flush();
s = bos.toString();
} catch (XMLStreamException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
return null;
} finally {
try {
if (bos != null)
bos.close();
if (writer != null)
writer.close();
if (indentingXMLStreamWriter != null)
indentingXMLStreamWriter.close();
} catch (IOException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
} catch (XMLStreamException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
}
}
return s;
}
Aggregations