Search in sources :

Example 31 with ConfigBeanProxy

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

the class CreateHTTPLoadBalancerCommand method addLoadBalancer.

private void addLoadBalancer(final String lbConfigName) {
    LoadBalancers loadBalancers = domain.getExtensionByType(LoadBalancers.class);
    // create load-balancers parent element if it does not exist
    if (loadBalancers == null) {
        Transaction transaction = new Transaction();
        try {
            ConfigBeanProxy domainProxy = transaction.enroll(domain);
            loadBalancers = domainProxy.createChild(LoadBalancers.class);
            ((Domain) domainProxy).getExtensions().add(loadBalancers);
            transaction.commit();
        } catch (TransactionFailure ex) {
            transaction.rollback();
            String msg = localStrings.getLocalString("FailedToUpdateLB", "Failed to update load-balancers");
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        } catch (RetryableException ex) {
            transaction.rollback();
            String msg = localStrings.getLocalString("FailedToUpdateLB", "Failed to update load-balancers");
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<LoadBalancers>() {

            @Override
            public Object run(LoadBalancers param) throws PropertyVetoException, TransactionFailure {
                LoadBalancer lb = param.createChild(LoadBalancer.class);
                lb.setDeviceHost(devicehost);
                lb.setDevicePort(deviceport);
                lb.setLbConfigName(lbConfigName);
                lb.setName(load_balancer_name);
                // add properties
                if (properties != null) {
                    for (Object propname : properties.keySet()) {
                        Property newprop = lb.createChild(Property.class);
                        newprop.setName((String) propname);
                        newprop.setValue(properties.getProperty((String) propname));
                        lb.getProperty().add(newprop);
                    }
                }
                if (sslproxyhost != null) {
                    Property newprop = lb.createChild(Property.class);
                    newprop.setName("ssl-proxy-host");
                    newprop.setValue(sslproxyhost);
                    lb.getProperty().add(newprop);
                }
                if (sslproxyport != null) {
                    Property newprop = lb.createChild(Property.class);
                    newprop.setName("ssl-proxy-port");
                    newprop.setValue(sslproxyport);
                    lb.getProperty().add(newprop);
                }
                param.getLoadBalancer().add(lb);
                return Boolean.TRUE;
            }
        }, loadBalancers);
    } catch (TransactionFailure ex) {
        String msg = localStrings.getLocalString("FailedToUpdateLB", "Failed to update load-balancers");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) LoadBalancers(org.glassfish.loadbalancer.config.LoadBalancers) LoadBalancer(org.glassfish.loadbalancer.config.LoadBalancer) Property(org.jvnet.hk2.config.types.Property)

Example 32 with ConfigBeanProxy

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

the class UpgradeService method addProperty.

/**
 * Adds a property with the specified name and value to a writable config
 * object.
 * @param <T> the type of the config object
 * @param propName name of the property to add
 * @param propValue value of the property to add
 * @param owner_w the owning config object
 * @return the added Property object
 * @throws TransactionFailure
 * @throws PropertyVetoException
 */
private <T extends PropertyBag & ConfigBeanProxy> Property addProperty(final String propName, final String propValue, final T owner_w) throws TransactionFailure, PropertyVetoException {
    final Property p = owner_w.createChild(Property.class);
    p.setName(propName);
    p.setValue(propValue);
    owner_w.getProperty().add(p);
    return p;
}
Also used : Property(org.jvnet.hk2.config.types.Property)

Example 33 with ConfigBeanProxy

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

the class DeploymentCommandUtils method getTargetResourceName.

private static StringBuilder getTargetResourceName(final Domain d, final String target) {
    final StringBuilder sb = new StringBuilder();
    ConfigBeanProxy p = d.getServerNamed(target);
    if (p == null) {
        p = d.getClusterNamed(target);
        if (p == null) {
            p = d.getDeploymentGroupNamed(target);
        }
    }
    if (p == null) {
        sb.append("domain/???/").append(target);
    } else {
        sb.append(AccessRequired.Util.resourceNameFromConfigBeanProxy(p));
    }
    return sb;
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy)

Example 34 with ConfigBeanProxy

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

the class UpdateApplicationRefCommand method execute.

/**
 * Execution method for updating the configuration of an ApplicationRef.
 * Will be replicated if the target is a cluster.
 *
 * @param context context for the command.
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // Make a list of all ApplicationRefs that need to change
    List<ApplicationRef> applicationRefsToChange = new ArrayList<>();
    // Add the ApplicationRef which is being immediately targetted
    {
        ApplicationRef primaryApplicationRef = domain.getApplicationRefInTarget(name, target);
        if (primaryApplicationRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("appref.not.exists", "Target {1} does not have a reference to application {0}.", name, target));
            return;
        }
        applicationRefsToChange.add(primaryApplicationRef);
    }
    // Add the implicitly targetted ApplicationRefs if the target is in a cluster or deployment group
    {
        Cluster cluster = domain.getClusterNamed(target);
        // if the target is a cluster
        if (cluster != null) {
            for (Server server : cluster.getInstances()) {
                ApplicationRef instanceAppRef = server.getApplicationRef(name);
                // if the server in the cluster contains the ApplicationRef
                if (instanceAppRef != null) {
                    applicationRefsToChange.add(instanceAppRef);
                }
            }
        }
        DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
        if (dg != null) {
            for (Server server : dg.getInstances()) {
                ApplicationRef instanceAppRef = server.getApplicationRef(name);
                // if the server in the dg contains the ApplicationRef
                if (instanceAppRef != null) {
                    applicationRefsToChange.add(instanceAppRef);
                }
            }
        }
    }
    // Apply the configuration to the listed ApplicationRefs
    try {
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                for (ConfigBeanProxy proxy : params) {
                    if (proxy instanceof ApplicationRef) {
                        ApplicationRef applicationRefProxy = (ApplicationRef) proxy;
                        if (enabled != null) {
                            applicationRefProxy.setEnabled(enabled.toString());
                        }
                        if (virtualservers != null) {
                            applicationRefProxy.setVirtualServers(virtualservers);
                        }
                        if (lbenabled != null) {
                            applicationRefProxy.setLbEnabled(lbenabled.toString());
                        }
                    }
                }
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return true;
            }
        }, applicationRefsToChange.toArray(new ApplicationRef[] {}));
    } catch (TransactionFailure ex) {
        report.failure(logger, ex.getLocalizedMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) ArrayList(java.util.ArrayList) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) PropertyVetoException(java.beans.PropertyVetoException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 35 with ConfigBeanProxy

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

the class WriteableView method commit.

/**
 * Commit this Transaction.
 *
 * @param t the transaction commiting.
 * @throws TransactionFailure
 *          if the transaction commit failed
 */
public synchronized List<PropertyChangeEvent> commit(Transaction t) throws TransactionFailure {
    if (currentTx == t) {
        currentTx = null;
    }
    // a key attribute must be non-null and have length >= 1
    final ConfigBean master = getMasterView();
    final String keyStr = master.model.key;
    if (keyStr != null) {
        final String key = stripMarkers(keyStr);
        final String value = getPropertyValue(key);
        if (value == null) {
            throw new TransactionFailure("Key value cannot be null: " + key);
        }
        if (value.length() == 0) {
            throw new TransactionFailure("Key value cannot be empty string: " + key);
        }
    }
    try {
        List<PropertyChangeEvent> appliedChanges = new ArrayList<PropertyChangeEvent>();
        for (PropertyChangeEvent event : changedAttributes.values()) {
            ConfigModel.Property property = bean.model.findIgnoreCase(event.getPropertyName());
            ConfigBeanInterceptor interceptor = bean.getOptionalFeature(ConfigBeanInterceptor.class);
            try {
                if (interceptor != null) {
                    interceptor.beforeChange(event);
                }
            } catch (PropertyVetoException e) {
                throw new TransactionFailure(e.getMessage(), e);
            }
            property.set(bean, event.getNewValue());
            if (interceptor != null) {
                interceptor.afterChange(event, System.currentTimeMillis());
            }
            appliedChanges.add(event);
        }
        for (ProtectedList entry : changedCollections.values()) {
            List<Object> originalList = entry.readOnly;
            for (PropertyChangeEvent event : entry.changeEvents) {
                if (event.getOldValue() == null) {
                    originalList.add(event.getNewValue());
                } else {
                    final Object toBeRemovedObj = event.getOldValue();
                    if (toBeRemovedObj instanceof ConfigBeanProxy) {
                        final Dom toBeRemoved = Dom.unwrap((ConfigBeanProxy) toBeRemovedObj);
                        for (int index = 0; index < originalList.size(); index++) {
                            Object element = originalList.get(index);
                            Dom dom = Dom.unwrap((ConfigBeanProxy) element);
                            if (dom == toBeRemoved) {
                                Object newValue = event.getNewValue();
                                if (newValue == null) {
                                    originalList.remove(index);
                                } else {
                                    originalList.set(index, newValue);
                                }
                            }
                        }
                    } else if (toBeRemovedObj instanceof String) {
                        final String toBeRemoved = (String) toBeRemovedObj;
                        for (int index = 0; index < originalList.size(); index++) {
                            final String item = (String) originalList.get(index);
                            if (item.equals(toBeRemoved)) {
                                originalList.remove(index);
                            }
                        }
                    } else {
                        throw new IllegalArgumentException();
                    }
                }
                appliedChanges.add(event);
            }
        }
        changedAttributes.clear();
        changedCollections.clear();
        return appliedChanges;
    } catch (TransactionFailure e) {
        throw e;
    } catch (Exception e) {
        throw new TransactionFailure(e.getMessage(), e);
    } finally {
        bean.getLock().unlock();
    }
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyVetoException(java.beans.PropertyVetoException) PropertyVetoException(java.beans.PropertyVetoException) Property(org.jvnet.hk2.config.ConfigModel.Property)

Aggregations

ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)41 PropertyVetoException (java.beans.PropertyVetoException)21 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)14 Method (java.lang.reflect.Method)11 ArrayList (java.util.ArrayList)11 ConfigCode (org.jvnet.hk2.config.ConfigCode)10 Config (com.sun.enterprise.config.serverbeans.Config)8 Server (com.sun.enterprise.config.serverbeans.Server)6 IOException (java.io.IOException)6 List (java.util.List)6 ActionReport (org.glassfish.api.ActionReport)6 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)6 Protocol (org.glassfish.grizzly.config.dom.Protocol)6 MultiException (org.glassfish.hk2.api.MultiException)6 ConfigModel (org.jvnet.hk2.config.ConfigModel)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)5 ConfigBean (org.jvnet.hk2.config.ConfigBean)5 Property (org.jvnet.hk2.config.types.Property)5