Search in sources :

Example 16 with Changed

use of org.jvnet.hk2.config.Changed in project Payara by payara.

the class CombinedJavaConfigSystemPropertyListener method changed.

/* force serial behavior; don't allow more than one thread to make a mess here */
@Override
public synchronized UnprocessedChangeEvents changed(PropertyChangeEvent[] events) {
    // ignore a REMOVE and an ADD of the same value
    final UnprocessedChangeEvents unp = ConfigSupport.sortAndDispatch(events, new Changed() {

        @Override
        public <T extends ConfigBeanProxy> NotProcessed changed(TYPE type, Class<T> tc, T t) {
            NotProcessed result = null;
            if (tc == Profiler.class) {
                result = new NotProcessed("Creation or changes to a profiler require restart");
            } else if (tc == Property.class && t.getParent().getClass() == JavaConfig.class) {
                result = new NotProcessed("Addition of properties to JavaConfig requires restart");
            } else if (tc == JavaConfig.class && t instanceof JavaConfig) {
                final JavaConfig njc = (JavaConfig) t;
                logFine(type, njc);
                // we must *always* check the jvm options, no way to know except by comparing,
                // plus we should send an appropriate message back for each removed/added item
                final List<String> curProps = new ArrayList<String>(njc.getJvmOptions());
                final boolean jvmOptionsWereChanged = !oldProps.equals(curProps);
                final List<String> reasons = handle(oldProps, curProps);
                oldProps = curProps;
                // something in the JavaConfig itself changed
                // to do this well, we ought to keep a list of attributes, so we can make a good message
                // saying exactly which attribute what changed
                final Map<String, String> curAttrs = collectAttrs(njc);
                reasons.addAll(handleAttrs(oldAttrs, curAttrs));
                oldAttrs = curAttrs;
                result = reasons.isEmpty() ? null : new NotProcessed(CombinedJavaConfigSystemPropertyListener.toString(reasons));
            } else if (tc == SystemProperty.class && t instanceof SystemProperty) {
                final SystemProperty sp = (SystemProperty) t;
                // check to see if this system property is for this instance
                ConfigBeanProxy proxy = sp.getParent();
                ConfigView p = ConfigSupport.getImpl(proxy);
                if (p == ConfigSupport.getImpl(server) || p == ConfigSupport.getImpl(config) || (cluster != null && p == ConfigSupport.getImpl(cluster)) || p == ConfigSupport.getImpl(domain)) {
                    // check to see if this system property is referenced by any of the options
                    String pname = sp.getName();
                    if (referencesProperty(pname, oldProps) || referencesProperty(pname, oldAttrs.values())) {
                        result = new NotProcessed("The system-property, " + pname + ", that is referenced by the Java configuration, was modified");
                    }
                }
                if (type == TYPE.ADD || type == TYPE.CHANGE) {
                    // create-system-properties
                    if (proxy instanceof Domain) {
                        return addToDomain(sp);
                    } else if (proxy instanceof Config && p == ConfigSupport.getImpl(config)) {
                        return addToConfig(sp);
                    } else if (cluster != null && proxy instanceof Cluster && p == ConfigSupport.getImpl(cluster)) {
                        return addToCluster(sp);
                    } else if (proxy instanceof Server && p == ConfigSupport.getImpl(server)) {
                        return addToServer(sp);
                    }
                } else if (type == TYPE.REMOVE) {
                    if (proxy instanceof Domain) {
                        return removeFromDomain(sp);
                    } else if (proxy instanceof Config && p == ConfigSupport.getImpl(config)) {
                        return removeFromConfig(sp);
                    } else if (cluster != null && proxy instanceof Cluster && p == ConfigSupport.getImpl(cluster)) {
                        return removeFromCluster(sp);
                    } else if (proxy instanceof Server && p == ConfigSupport.getImpl(server)) {
                        return removeFromServer(sp);
                    }
                }
            } else {
            // ignore other changes that are reported
            }
            return result;
        }
    }, logger);
    return unp;
}
Also used : UnprocessedChangeEvents(org.jvnet.hk2.config.UnprocessedChangeEvents) Server(com.sun.enterprise.config.serverbeans.Server) TranslatedConfigView(org.glassfish.config.support.TranslatedConfigView) ConfigView(org.jvnet.hk2.config.ConfigView) JavaConfig(com.sun.enterprise.config.serverbeans.JavaConfig) Config(com.sun.enterprise.config.serverbeans.Config) Cluster(com.sun.enterprise.config.serverbeans.Cluster) SystemProperty(com.sun.enterprise.config.serverbeans.SystemProperty) JavaConfig(com.sun.enterprise.config.serverbeans.JavaConfig) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) Profiler(com.sun.enterprise.config.serverbeans.Profiler) Changed(org.jvnet.hk2.config.Changed) NotProcessed(org.jvnet.hk2.config.NotProcessed) ArrayList(java.util.ArrayList) List(java.util.List) Domain(com.sun.enterprise.config.serverbeans.Domain) TYPE(org.jvnet.hk2.config.Changed.TYPE) Property(org.jvnet.hk2.config.types.Property) SystemProperty(com.sun.enterprise.config.serverbeans.SystemProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with Changed

use of org.jvnet.hk2.config.Changed 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 18 with Changed

use of org.jvnet.hk2.config.Changed in project Payara by payara.

the class UnprocessedEventsTest method changed.

public UnprocessedChangeEvents changed(PropertyChangeEvent[] propertyChangeEvents) {
    assertEquals("Array size", propertyChangeEvents.length, 1);
    final UnprocessedChangeEvent unp = new UnprocessedChangeEvent(propertyChangeEvents[0], "Java NIO port listener cannot reconfigure its port dynamically");
    unprocessed = new UnprocessedChangeEvents(unp);
    return unprocessed;
}
Also used : UnprocessedChangeEvents(org.jvnet.hk2.config.UnprocessedChangeEvents) UnprocessedChangeEvent(org.jvnet.hk2.config.UnprocessedChangeEvent)

Example 19 with Changed

use of org.jvnet.hk2.config.Changed in project Payara by payara.

the class ConfigConfigBeanListener method changed.

/* force serial behavior; don't allow more than one thread to make a mess here */
@Override
public synchronized UnprocessedChangeEvents changed(PropertyChangeEvent[] events) {
    for (PropertyChangeEvent e : events) {
        // ignore all events for which the source isn't the Config
        if (e.getSource().getClass() != config.getClass()) {
            continue;
        }
        // remove the DEFAULT_INSTANCE_NAME entry for an old value
        Object ov = e.getOldValue();
        if (ov instanceof ConfigBeanProxy) {
            ConfigBeanProxy ovbp = (ConfigBeanProxy) ov;
            logger.log(Level.FINE, removingDefaultInstanceIndexFor, ConfigSupport.getImpl(ovbp).getProxyType().getName());
            ServiceLocatorUtilities.removeFilter(habitat, BuilderHelper.createNameAndContractFilter(ConfigSupport.getImpl(ovbp).getProxyType().getName(), ServerEnvironment.DEFAULT_INSTANCE_NAME));
        }
        // add the DEFAULT_INSTANCE_NAME entry for a new value
        Object nv = e.getNewValue();
        if (nv instanceof ConfigBean) {
            ConfigBean nvb = (ConfigBean) nv;
            ConfigBeanProxy nvbp = nvb.getProxy(nvb.getProxyType());
            logger.log(Level.FINE, AddingDefaultInstanceIndexFor, nvb.getProxyType().getName());
            ServiceLocatorUtilities.addOneConstant(habitat, nvbp, ServerEnvironment.DEFAULT_INSTANCE_NAME, nvb.getProxyType());
        }
    }
    return null;
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigBean(org.jvnet.hk2.config.ConfigBean)

Example 20 with Changed

use of org.jvnet.hk2.config.Changed in project Payara by payara.

the class AddPropertyTest method transactionEvents.

@Test
public void transactionEvents() throws TransactionFailure {
    final Domain domain = getHabitat().getService(Domain.class);
    final TransactionListener listener = new TransactionListener() {

        public void transactionCommited(List<PropertyChangeEvent> changes) {
            events = changes;
        }

        public void unprocessedTransactedEvents(List<UnprocessedChangeEvents> changes) {
        }
    };
    Transactions transactions = getHabitat().getService(Transactions.class);
    try {
        transactions.addTransactionsListener(listener);
        assertTrue(domain != null);
        ConfigSupport.apply(new SingleConfigCode<Domain>() {

            public Object run(Domain domain) throws PropertyVetoException, TransactionFailure {
                Property prop = domain.createChild(Property.class);
                domain.getProperty().add(prop);
                prop.setName("Jerome");
                prop.setValue("was here");
                return prop;
            }
        }, domain);
        transactions.waitForDrain();
        assertTrue(events != null);
        logger.fine("Number of events " + events.size());
        assertTrue(events.size() == 3);
        for (PropertyChangeEvent event : events) {
            logger.fine(event.toString());
        }
        Map<String, String> configChanges = new HashMap<String, String>();
        configChanges.put("name", "julien");
        configChanges.put("value", "petit clown");
        ConfigBean domainBean = (ConfigBean) Dom.unwrap(domain);
        ConfigSupport.createAndSet(domainBean, Property.class, configChanges);
        transactions.waitForDrain();
        assertTrue(events != null);
        logger.fine("Number of events " + events.size());
        assertTrue(events.size() == 3);
        for (PropertyChangeEvent event : events) {
            logger.fine(event.toString());
        }
        final UnprocessedChangeEvents unprocessed = ConfigSupport.sortAndDispatch(events.toArray(new PropertyChangeEvent[0]), new Changed() {

            /**
             * Notification of a change on a configuration object
             *
             * @param type            type of change : ADD mean the changedInstance was added to the parent
             *                        REMOVE means the changedInstance was removed from the parent, CHANGE means the
             *                        changedInstance has mutated.
             * @param changedType     type of the configuration object
             * @param changedInstance changed instance.
             */
            public <T extends ConfigBeanProxy> NotProcessed changed(TYPE type, Class<T> changedType, T changedInstance) {
                return new NotProcessed("unimplemented by AddPropertyTest");
            }
        }, logger);
    } finally {
        transactions.removeTransactionsListener(listener);
    }
}
Also used : TransactionListener(org.jvnet.hk2.config.TransactionListener) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) UnprocessedChangeEvents(org.jvnet.hk2.config.UnprocessedChangeEvents) PropertyChangeEvent(java.beans.PropertyChangeEvent) HashMap(java.util.HashMap) ConfigBean(org.jvnet.hk2.config.ConfigBean) PropertyVetoException(java.beans.PropertyVetoException) Transactions(org.jvnet.hk2.config.Transactions) Changed(org.jvnet.hk2.config.Changed) NotProcessed(org.jvnet.hk2.config.NotProcessed) List(java.util.List) Domain(com.sun.enterprise.config.serverbeans.Domain) Property(org.jvnet.hk2.config.types.Property) Test(org.junit.Test)

Aggregations

PropertyChangeEvent (java.beans.PropertyChangeEvent)12 UnprocessedChangeEvents (org.jvnet.hk2.config.UnprocessedChangeEvents)12 UnprocessedChangeEvent (org.jvnet.hk2.config.UnprocessedChangeEvent)10 ArrayList (java.util.ArrayList)8 Property (org.jvnet.hk2.config.types.Property)7 SystemProperty (com.sun.enterprise.config.serverbeans.SystemProperty)5 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)5 List (java.util.List)4 Cluster (com.sun.enterprise.config.serverbeans.Cluster)3 Domain (com.sun.enterprise.config.serverbeans.Domain)3 NotProcessed (org.jvnet.hk2.config.NotProcessed)3 Config (com.sun.enterprise.config.serverbeans.Config)2 DasConfig (com.sun.enterprise.config.serverbeans.DasConfig)2 HttpService (com.sun.enterprise.config.serverbeans.HttpService)2 JavaConfig (com.sun.enterprise.config.serverbeans.JavaConfig)2 MonitoringService (com.sun.enterprise.config.serverbeans.MonitoringService)2 Server (com.sun.enterprise.config.serverbeans.Server)2 VirtualServer (com.sun.enterprise.config.serverbeans.VirtualServer)2 JmsService (com.sun.enterprise.connectors.jms.config.JmsService)2 PropertyVetoException (java.beans.PropertyVetoException)2