use of org.jvnet.hk2.annotations.Service in project Payara by payara.
the class TemplateRestResource method setParentAndTagName.
public void setParentAndTagName(Dom parent, String tagName) {
if (parent == null) {
// prevent https://glassfish.dev.java.net/issues/show_bug.cgi?id=14125
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
this.parent = parent;
this.tagName = tagName;
synchronized (parent) {
entity = parent.nodeElement(tagName);
}
if (entity == null) {
// In some cases, the tagName requested is not found in the DOM tree. This is true,
// for example, for the various ZeroConf elements (e.g., transaction-service). If
// the zero conf element is not in domain.xml, then it won't be in the Dom tree
// returned by HK2. If that's the case, we can use ConfigModularityUtils.getOwningObject()
// to find the ConfigBean matching the path requested, which will add the node to
// the Dom tree. Once that's done, we can return that node and proceed as normal
String location = buildPath(parent) + "/" + tagName;
if (location.startsWith("domain/configs")) {
final ConfigModularityUtils cmu = locatorBridge.getRemoteLocator().<ConfigModularityUtils>getService(ConfigModularityUtils.class);
ConfigBeanProxy cbp = cmu.getOwningObject(location);
if (cbp == null) {
cbp = cmu.getConfigBeanInstanceFor(cmu.getOwningClassForLocation(location));
}
if (cbp != null) {
entity = Dom.unwrap(cbp);
childModel = entity.model;
}
}
// throw new WebApplicationException(new Exception("Trying to create an entity using generic create"),Response.Status.INTERNAL_SERVER_ERROR);
} else {
childModel = entity.model;
}
}
use of org.jvnet.hk2.annotations.Service in project Payara by payara.
the class DynamicConfigListener method processNetworkListener.
private <T extends ConfigBeanProxy> NotProcessed processNetworkListener(Changed.TYPE type, NetworkListener listener, final PropertyChangeEvent[] changedProperties) {
if (findConfigName(listener).equals(findConfigName(config))) {
boolean isAdminListener = ADMIN_LISTENER.equals(listener.getName());
Lock portLock = null;
try {
portLock = acquirePortLock(listener);
if (type == Changed.TYPE.ADD) {
final int[] ports = portLock.getPorts();
if (isAdminListener && ports[ports.length - 1] == -1) {
return null;
}
final Future future = grizzlyService.createNetworkProxy(listener);
if (future != null) {
future.get(RECONFIG_LOCK_TIMEOUT_SEC, TimeUnit.SECONDS);
grizzlyService.registerContainerAdapters();
} else {
logger.log(Level.FINE, "Skipping proxy registration for the listener {0}", listener.getName());
}
} else if (type == Changed.TYPE.REMOVE) {
if (!isAdminListener) {
grizzlyService.removeNetworkProxy(listener);
}
} else if (type == Changed.TYPE.CHANGE) {
// If the listener is the admin listener
if (isAdminListener) {
final boolean dynamic = isAdminDynamic(changedProperties);
// If configuration is dynamic then make the change
if (dynamic) {
GrizzlyProxy proxy = (GrizzlyProxy) grizzlyService.lookupNetworkProxy(listener);
if (proxy != null) {
GrizzlyListener netListener = proxy.getUnderlyingListener();
netListener.processDynamicConfigurationChange(grizzlyService.getHabitat(), changedProperties);
return null;
}
}
// Otherwise return the unprocessed event, describing the changed values.
if (!isRedundantChange(changedProperties)) {
StringBuilder eventsMessage = new StringBuilder();
/* Add list of changed events to an events message.
* Usually only one message is sent to this method at a time, so the for loop should only run once,
* but it's there just in case.
*/
for (PropertyChangeEvent event : changedProperties) {
eventsMessage.append("\"" + event.getPropertyName() + "\" changed from \"" + event.getOldValue() + "\" to \"" + event.getNewValue() + "\".\n");
}
return new NotProcessed(listener.getThreadPool() + " attribute " + eventsMessage.toString());
}
return null;
}
// Only restart the network listener if something hasn't changed
if (!isRedundantChange(changedProperties)) {
MonitoringService ms = config.getMonitoringService();
String level = ms.getModuleMonitoringLevels().getHttpService();
// We only need to throw an unprocessed change event if monitoring is enabled for the HTTP service
if (level != null && (!level.equals(OFF))) {
String eventsMessage = "Monitoring statistics will be incorrect for " + listener.findHttpProtocolName() + " until restart due to changed attribute(s): ";
// so the for loop should only run once, but it's there just in case.
for (PropertyChangeEvent event : changedProperties) {
eventsMessage += ("\"" + event.getPropertyName() + "\" changed from \"" + event.getOldValue() + "\" to \"" + event.getNewValue() + "\".\n");
}
// Still restart the network listener, as it's only the monitoring that breaks.
grizzlyService.restartNetworkListener(listener, RECONFIG_LOCK_TIMEOUT_SEC, TimeUnit.SECONDS);
return new NotProcessed(eventsMessage);
} else {
// Restart the network listener without throwing an unprocessed change
grizzlyService.restartNetworkListener(listener, RECONFIG_LOCK_TIMEOUT_SEC, TimeUnit.SECONDS);
}
}
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Network listener configuration error. Type: " + type, e);
} finally {
if (portLock != null) {
releaseListenerLock(portLock);
}
}
}
return null;
}
use of org.jvnet.hk2.annotations.Service in project Payara by payara.
the class DeleteModuleConfigCommand method deleteTopLevelExtensionByType.
private void deleteTopLevelExtensionByType(Config config, final String className, Class configBeanType) {
if (ConfigExtension.class.isAssignableFrom(configBeanType)) {
if (config.checkIfExtensionExists(configBeanType)) {
try {
ConfigSupport.apply(new SingleConfigCode<Config>() {
@Override
public Object run(Config param) throws PropertyVetoException, TransactionFailure {
List<ConfigExtension> configExtensions;
configExtensions = param.getExtensions();
for (ConfigExtension ext : configExtensions) {
String configExtensionClass = GlassFishConfigBean.unwrap(ext).getProxyType().getSimpleName();
if (configExtensionClass.equals(className)) {
configExtensions.remove(ext);
break;
}
}
return configExtensions;
}
}, config);
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
} catch (TransactionFailure e) {
String actual = e.getMessage();
String msg = localStrings.getLocalString("delete.module.config.failed.to.delete.config", DEFAULT_FORMAT, serviceName, actual);
report.setMessage(msg);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
}
} else {
report.setMessage(localStrings.getLocalString("delete.module.config.no.configuration", "No customized configuration exist for this service nor the default configuration has been added to the domain.xml."));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
}
} else if (DomainExtension.class.isAssignableFrom(configBeanType)) {
if (domain.checkIfExtensionExists(configBeanType)) {
try {
ConfigSupport.apply(new SingleConfigCode<Domain>() {
@Override
public Object run(Domain param) throws PropertyVetoException, TransactionFailure {
List<DomainExtension> domainExtensions;
domainExtensions = param.getExtensions();
for (DomainExtension ext : domainExtensions) {
String configExtensionClass = GlassFishConfigBean.unwrap(ext).getProxyType().getSimpleName();
if (configExtensionClass.equals(className)) {
domainExtensions.remove(ext);
break;
}
}
return domainExtensions;
}
}, domain);
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
} catch (TransactionFailure e) {
String actual = e.getMessage();
String msg = localStrings.getLocalString("delete.module.config.failed.to.delete.config", DEFAULT_FORMAT, serviceName, actual);
report.setMessage(msg);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
}
} else {
report.setMessage(localStrings.getLocalString("delete.module.config.no.configuration", "No customized configuration exist for this service nor the default configuration has been added to the domain.xml."));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
}
}
}
use of org.jvnet.hk2.annotations.Service in project Payara by payara.
the class DeleteModuleConfigCommand method deleteDependentConfigElement.
private void deleteDependentConfigElement(final ConfigBeanDefaultValue defaultValue) {
Class parentClass = configModularityUtils.getOwningClassForLocation(defaultValue.getLocation());
final Class configBeanClass = configModularityUtils.getClassForFullName(defaultValue.getConfigBeanClassName());
final Method m = configModularityUtils.findSuitableCollectionGetter(parentClass, configBeanClass);
if (m != null) {
try {
final ConfigBeanProxy parent = configModularityUtils.getOwningObject(defaultValue.getLocation());
ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {
@Override
public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
List col = null;
ConfigBeanProxy configBean = null;
try {
col = (List) m.invoke(param);
if (col != null) {
configBean = configModularityUtils.getCurrentConfigBeanForDefaultValue(defaultValue);
}
} catch (Exception e) {
String message = localStrings.getLocalString("delete.module.config.failed.deleting.dependant", "Failed to remove all configuration elements related to your service form domain.xml. You can use create-module-config --dryRun with your module name to see all relevant configurations and try removing the config elements ");
report.setMessage(message);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
LOG.log(Level.INFO, DELETE_MODULE_CONFIG_FAILED_DELETING_DEPENDENT, e);
}
if (configBean != null) {
boolean deleted = configModularityUtils.deleteConfigurationForConfigBean(configBean, col, defaultValue);
if (!deleted) {
for (int i = 0; i < col.size(); i++) {
if (configBeanClass.isAssignableFrom(col.get(i).getClass())) {
col.remove(col.get(i));
removeCustomTokens(defaultValue, configBean, parent);
return param;
}
}
}
}
return param;
}
}, parent);
} catch (Exception e) {
String message = localStrings.getLocalString("delete.module.config.failed.deleting.dependant", "Failed to remove all configuration elements related to your service form domain.xml. You can use create-module-config --dryRun with your module name to see all relevant configurations and try removing the config elements ");
report.setMessage(message);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
LOG.log(Level.INFO, DELETE_MODULE_CONFIG_FAILED_DELETING_DEPENDENT, e);
}
} else {
report.setMessage(localStrings.getLocalString("delete.module.config.failed.deleting.dependant", "Failed to remove all configuration elements related to your service form domain.xml. You can use create-module-config --dryRun with your module name to see all relevant configurations and try removing the config elements "));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
}
}
use of org.jvnet.hk2.annotations.Service in project Payara by payara.
the class UnprocessedEventsTest method unprocessedEventsTest.
@Test
public void unprocessedEventsTest() throws TransactionFailure {
// let's find our target
NetworkConfig service = habitat.getService(NetworkConfig.class);
NetworkListener listener = service.getNetworkListener("http-listener-1");
assertNotNull(listener);
// Let's register a listener
ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(listener);
bean.addListener(this);
Transactions transactions = getHabitat().getService(Transactions.class);
try {
transactions.addTransactionsListener(this);
String originalPort = listener.getPort();
try {
ConfigSupport.apply(param -> {
param.setPort("8908");
return null;
}, listener);
// Check the result.
assertEquals(listener.getPort(), "8908");
} finally {
// Restore the original port
ConfigSupport.apply(param -> {
param.setPort(originalPort);
return null;
}, listener);
}
// ensure events are delivered.
transactions.waitForDrain();
assertNotNull(unprocessed);
// finally
bean.removeListener(this);
} finally {
// check we recevied the event
transactions.removeTransactionsListener(this);
}
}
Aggregations