Search in sources :

Example 56 with Service

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;
    }
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) WebApplicationException(javax.ws.rs.WebApplicationException) ConfigModularityUtils(com.sun.enterprise.config.modularity.ConfigModularityUtils)

Example 57 with Service

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;
}
Also used : GrizzlyListener(org.glassfish.grizzly.config.GrizzlyListener) PropertyChangeEvent(java.beans.PropertyChangeEvent) Future(java.util.concurrent.Future) NotProcessed(org.jvnet.hk2.config.NotProcessed) MonitoringService(com.sun.enterprise.config.serverbeans.MonitoringService) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 58 with Service

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);
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) SingleConfigCode(org.jvnet.hk2.config.SingleConfigCode) Config(com.sun.enterprise.config.serverbeans.Config) ConfigExtension(org.glassfish.api.admin.config.ConfigExtension) DomainExtension(com.sun.enterprise.config.serverbeans.DomainExtension) PropertyVetoException(java.beans.PropertyVetoException) List(java.util.List) Domain(com.sun.enterprise.config.serverbeans.Domain)

Example 59 with Service

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);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) List(java.util.List) Method(java.lang.reflect.Method) PropertyVetoException(java.beans.PropertyVetoException)

Example 60 with Service

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);
    }
}
Also used : Transactions(org.jvnet.hk2.config.Transactions) NetworkConfig(org.glassfish.grizzly.config.dom.NetworkConfig) ObservableBean(org.jvnet.hk2.config.ObservableBean) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)25 PropertyVetoException (java.beans.PropertyVetoException)21 ActionReport (org.glassfish.api.ActionReport)16 Property (org.jvnet.hk2.config.types.Property)16 Config (com.sun.enterprise.config.serverbeans.Config)14 ArrayList (java.util.ArrayList)8 File (java.io.File)7 PropertyChangeEvent (java.beans.PropertyChangeEvent)5 HashMap (java.util.HashMap)5 Properties (java.util.Properties)5 Service (org.jvnet.hk2.annotations.Service)5 Resources (com.sun.enterprise.config.serverbeans.Resources)4 List (java.util.List)4 Test (org.junit.Test)4 UnprocessedChangeEvent (org.jvnet.hk2.config.UnprocessedChangeEvent)4 UnprocessedChangeEvents (org.jvnet.hk2.config.UnprocessedChangeEvents)4 Cluster (com.sun.enterprise.config.serverbeans.Cluster)3 Domain (com.sun.enterprise.config.serverbeans.Domain)3 HttpService (com.sun.enterprise.config.serverbeans.HttpService)3 Resource (com.sun.enterprise.config.serverbeans.Resource)3