Search in sources :

Example 41 with Element

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

the class ApplicationLifecycle method setRestAppAttributes.

// set the rest of the application attributes at the end of the
// deployment
private void setRestAppAttributes(Application app, Properties appProps) throws PropertyVetoException, TransactionFailure {
    // context-root element
    if (appProps.getProperty(ServerTags.CONTEXT_ROOT) != null) {
        app.setContextRoot(appProps.getProperty(ServerTags.CONTEXT_ROOT));
    }
    // the rest properties will be written as property element
    for (Iterator itr = appProps.keySet().iterator(); itr.hasNext(); ) {
        String propName = (String) itr.next();
        if (!propName.equals(ServerTags.LOCATION) && !propName.equals(ServerTags.CONTEXT_ROOT) && !propName.equals(ServerTags.OBJECT_TYPE) && !propName.equals(ServerTags.DIRECTORY_DEPLOYED) && !propName.startsWith(DeploymentProperties.APP_CONFIG)) {
            if (appProps.getProperty(propName) != null) {
                Property prop = app.createChild(Property.class);
                app.getProperty().add(prop);
                prop.setName(propName);
                prop.setValue(appProps.getProperty(propName));
            }
        }
    }
}
Also used : Property(org.jvnet.hk2.config.types.Property)

Example 42 with Element

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

the class ApplicationLifecycle method prepareModule.

@Override
public ModuleInfo prepareModule(List<EngineInfo> sortedEngineInfos, String moduleName, DeploymentContext context, ProgressTracker tracker) throws Exception {
    ActionReport report = context.getActionReport();
    List<EngineRef> addedEngines = new ArrayList<EngineRef>();
    DeploymentTracing tracing = context.getModuleMetaData(DeploymentTracing.class);
    if (tracing != null) {
        tracing.addModuleMark(DeploymentTracing.ModuleMark.PREPARE, moduleName);
    }
    for (EngineInfo engineInfo : sortedEngineInfos) {
        // get the deployer
        Deployer deployer = engineInfo.getDeployer();
        try {
            if (tracing != null) {
                tracing.addContainerMark(DeploymentTracing.ContainerMark.PREPARE, engineInfo.getSniffer().getModuleType());
            }
            deployer.prepare(context);
            if (tracing != null) {
                tracing.addContainerMark(DeploymentTracing.ContainerMark.PREPARED, engineInfo.getSniffer().getModuleType());
            }
            // construct an incomplete EngineRef which will be later
            // filled in at loading time
            EngineRef engineRef = new EngineRef(engineInfo, null);
            addedEngines.add(engineRef);
            tracker.add("prepared", EngineRef.class, engineRef);
            tracker.add(Deployer.class, deployer);
        } catch (Exception e) {
            report.failure(logger, "Exception while invoking " + deployer.getClass() + " prepare method", e);
            throw e;
        }
    }
    if (tracing != null) {
        tracing.addModuleMark(DeploymentTracing.ModuleMark.PREPARE_EVENTS, moduleName);
    }
    if (events != null) {
        events.send(new Event<DeploymentContext>(Deployment.MODULE_PREPARED, context), false);
    }
    if (tracing != null) {
        tracing.addModuleMark(DeploymentTracing.ModuleMark.PREPARED, moduleName);
    }
    // I need to create the application info here from the context, or something like this.
    // and return the application info from this method for automatic registration in the caller.
    // set isComposite property on module props so we know whether to persist
    // module level properties inside ModuleInfo
    String isComposite = context.getAppProps().getProperty(ServerTags.IS_COMPOSITE);
    if (isComposite != null) {
        context.getModuleProps().setProperty(ServerTags.IS_COMPOSITE, isComposite);
    }
    ModuleInfo mi = new ModuleInfo(events, moduleName, addedEngines, context.getModuleProps());
    /*
         * Save the application config that is potentially attached to each
         * engine in the corresponding EngineRefs that have already created.
         * 
         * Later, in registerAppInDomainXML, the appInfo is saved, which in
         * turn saves the moduleInfo children and their engineRef children.
         * Saving the engineRef assigns the application config to the Engine
         * which corresponds directly to the <engine> element in the XML.
         * A long way to get this done.
         */
    // Application existingApp = applications.getModule(Application.class, moduleName);
    // if (existingApp != null) {
    ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(context.getAppProps());
    for (EngineRef er : mi.getEngineRefs()) {
        ApplicationConfig c = savedAppConfig.get(mi.getName(), er.getContainerInfo().getSniffer().getModuleType());
        if (c != null) {
            er.setApplicationConfig(c);
        }
    }
    // }
    return mi;
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) RetryableException(org.jvnet.hk2.config.RetryableException) MultiException(org.glassfish.hk2.api.MultiException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) DeploymentTracing(org.glassfish.internal.deployment.DeploymentTracing)

Example 43 with Element

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

the class ApplicationLifecycle method registerAppInDomainXML.

// register application information in domain.xml
@Override
public void registerAppInDomainXML(final ApplicationInfo applicationInfo, final DeploymentContext context, Transaction t, boolean appRefOnly) throws TransactionFailure {
    final Properties appProps = context.getAppProps();
    final DeployCommandParameters deployParams = context.getCommandParameters(DeployCommandParameters.class);
    if (t != null) {
        try {
            if (!appRefOnly) {
                Application app_w = context.getTransientAppMetaData(ServerTags.APPLICATION, Application.class);
                // adding the application element
                setRestAppAttributes(app_w, appProps);
                Applications apps_w = t.enroll(applications);
                apps_w.getModules().add(app_w);
                if (applicationInfo != null) {
                    applicationInfo.save(app_w);
                }
            }
            List<String> targets = new ArrayList<String>();
            if (!DeploymentUtils.isDomainTarget(deployParams.target)) {
                targets.add(deployParams.target);
            } else {
                List<String> previousTargets = context.getTransientAppMetaData(DeploymentProperties.PREVIOUS_TARGETS, List.class);
                if (previousTargets == null) {
                    previousTargets = domain.getAllReferencedTargetsForApplication(deployParams.name);
                }
                targets = previousTargets;
            }
            String origVS = deployParams.virtualservers;
            Boolean origEnabled = deployParams.enabled;
            Properties previousVirtualServers = context.getTransientAppMetaData(DeploymentProperties.PREVIOUS_VIRTUAL_SERVERS, Properties.class);
            Properties previousEnabledAttributes = context.getTransientAppMetaData(DeploymentProperties.PREVIOUS_ENABLED_ATTRIBUTES, Properties.class);
            for (String target : targets) {
                // first reset the virtualservers, enabled attribute
                deployParams.virtualservers = origVS;
                deployParams.enabled = origEnabled;
                // applicable
                if (DeploymentUtils.isDomainTarget(deployParams.target)) {
                    String vs = previousVirtualServers.getProperty(target);
                    if (vs != null) {
                        deployParams.virtualservers = vs;
                    }
                    String enabledAttr = previousEnabledAttributes.getProperty(target);
                    if (enabledAttr != null) {
                        deployParams.enabled = Boolean.valueOf(enabledAttr);
                    }
                }
                if (deployParams.enabled == null) {
                    deployParams.enabled = Boolean.TRUE;
                }
                Server servr = domain.getServerNamed(target);
                if (servr != null) {
                    ApplicationRef instanceApplicationRef = domain.getApplicationRefInTarget(deployParams.name, servr.getName());
                    if (instanceApplicationRef == null) {
                        // adding the application-ref element to the standalone
                        // server instance
                        ConfigBeanProxy servr_w = t.enroll(servr);
                        // adding the application-ref element to the standalone
                        // server instance
                        ApplicationRef appRef = servr_w.createChild(ApplicationRef.class);
                        setAppRefAttributes(appRef, deployParams);
                        ((Server) servr_w).getApplicationRef().add(appRef);
                    }
                }
                Cluster cluster = domain.getClusterNamed(target);
                if (cluster != null) {
                    // adding the application-ref element to the cluster
                    // and instances
                    ConfigBeanProxy cluster_w = t.enroll(cluster);
                    ApplicationRef appRef = cluster_w.createChild(ApplicationRef.class);
                    setAppRefAttributes(appRef, deployParams);
                    ((Cluster) cluster_w).getApplicationRef().add(appRef);
                    for (Server svr : cluster.getInstances()) {
                        ConfigBeanProxy svr_w = t.enroll(svr);
                        ApplicationRef appRef2 = svr_w.createChild(ApplicationRef.class);
                        setAppRefAttributes(appRef2, deployParams);
                        ((Server) svr_w).getApplicationRef().add(appRef2);
                    }
                }
                DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
                if (dg != null) {
                    ConfigBeanProxy dg_w = t.enroll(dg);
                    ApplicationRef appRef = dg_w.createChild(ApplicationRef.class);
                    setAppRefAttributes(appRef, deployParams);
                    ((DeploymentGroup) dg_w).getApplicationRef().add(appRef);
                    for (Server svr : dg.getInstances()) {
                        ApplicationRef instanceApplicationRef = domain.getApplicationRefInTarget(deployParams.name, svr.getName());
                        if (instanceApplicationRef == null) {
                            ConfigBeanProxy svr_w = t.enroll(svr);
                            ApplicationRef appRef2 = svr_w.createChild(ApplicationRef.class);
                            setAppRefAttributes(appRef2, deployParams);
                            ((Server) svr_w).getApplicationRef().add(appRef2);
                        }
                    }
                }
            }
        } catch (TransactionFailure e) {
            t.rollback();
            throw e;
        } catch (Exception e) {
            t.rollback();
            throw new TransactionFailure(e.getMessage(), e);
        }
        try {
            t.commit();
        } catch (RetryableException e) {
            System.out.println("Retryable...");
            // TODO : do something meaninful here
            t.rollback();
        } catch (TransactionFailure e) {
            t.rollback();
            throw e;
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) PropertyVetoException(java.beans.PropertyVetoException) RetryableException(org.jvnet.hk2.config.RetryableException) MultiException(org.glassfish.hk2.api.MultiException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) RetryableException(org.jvnet.hk2.config.RetryableException) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 44 with Element

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

the class NodeAgentConfigUpgrade method postConstruct.

public void postConstruct() {
    final NodeAgents nodeAgents = domain.getNodeAgents();
    if (nodeAgents == null) {
        createDefaultNodeList();
        return;
    }
    final List<NodeAgent> agList = nodeAgents.getNodeAgent();
    if (agList.size() == 0) {
        createDefaultNodeList();
        return;
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<Domain>() {

            public Object run(Domain d) throws PropertyVetoException, TransactionFailure {
                Nodes nodes = d.createChild(Nodes.class);
                Transaction t = Transaction.getTransaction(d);
                if (t == null)
                    return null;
                for (NodeAgent na : agList) {
                    String host = null;
                    Node node = nodes.createChild(Node.class);
                    node.setName(na.getName());
                    node.setType("CONFIG");
                    JmxConnector jc = na.getJmxConnector();
                    if (jc != null) {
                        // get the properties and see if host name is specified
                        List<Property> agentProp = jc.getProperty();
                        for (Property p : agentProp) {
                            String name = p.getName();
                            if (name.equals("client-hostname")) {
                                // create the node with a host name
                                node.setNodeHost(p.getValue());
                                node.setInstallDir("${com.sun.aas.productRoot}");
                            }
                        }
                    }
                    nodes.getNode().add(node);
                }
                // Now add the builtin localhost node
                createDefaultNode(d, nodes);
                d.setNodes(nodes);
                List<Server> serverList = servers.getServer();
                if (serverList.size() <= 0)
                    return null;
                for (Server s : serverList) {
                    s = t.enroll(s);
                    s.setNodeRef(s.getNodeAgentRef());
                    s.setNodeAgentRef(null);
                }
                // remove the node-agent element
                // d.getNodeAgents().getNodeAgent().clear();
                d.setNodeAgents(null);
                return null;
            }
        }, domain);
    } catch (Exception e) {
        Logger.getAnonymousLogger().log(Level.SEVERE, "Failure while upgrading node-agent from V2 to V3", e);
        throw new RuntimeException(e);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) Node(com.sun.enterprise.config.serverbeans.Node) NodeAgent(com.sun.enterprise.config.serverbeans.NodeAgent) JmxConnector(com.sun.enterprise.config.serverbeans.JmxConnector) NodeAgents(com.sun.enterprise.config.serverbeans.NodeAgents) Nodes(com.sun.enterprise.config.serverbeans.Nodes) PropertyVetoException(java.beans.PropertyVetoException) PropertyVetoException(java.beans.PropertyVetoException) Domain(com.sun.enterprise.config.serverbeans.Domain) Property(org.jvnet.hk2.config.types.Property)

Example 45 with Element

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

the class SetCommand method setLeafElement.

public static void setLeafElement(final ConfigBean node, final String elementName, final String values) throws TransactionFailure {
    ConfigBeanProxy readableView = node.getProxy(node.getProxyType());
    ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {

        /**
         * Runs the following command passing the configuration object. The
         * code will be run within a transaction, returning true will commit
         * the transaction, false will abort it.
         *
         * @param param is the configuration object protected by the
         * transaction
         * @return any object that should be returned from within the
         * transaction code
         * @throws java.beans.PropertyVetoException if the changes cannot be
         * applied to the configuration
         */
        @Override
        public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
            WriteableView writeableParent = (WriteableView) Proxy.getInvocationHandler(param);
            StringTokenizer st = new StringTokenizer(values, ",");
            List<String> valList = new ArrayList<>();
            while (st.hasMoreTokens()) {
                valList.add(st.nextToken());
            }
            ConfigBean bean = writeableParent.getMasterView();
            for (Method m : writeableParent.getProxyType().getMethods()) {
                // Check to see if the method is a setter for the element
                // An element setter has to have the right name, take a single
                // collection parameter that parameterized with the right type
                Class[] argClasses = m.getParameterTypes();
                Type[] argTypes = m.getGenericParameterTypes();
                ConfigModel.Property prop = bean.model.toProperty(m);
                if (prop == null || !prop.xmlName().equals(elementName) || argClasses.length != 1 || !Collection.class.isAssignableFrom(argClasses[0]) || argTypes.length != 1 || !(argTypes[0] instanceof ParameterizedType) || !Types.erasure(Types.getTypeArgument(argTypes[0], 0)).isAssignableFrom(values.getClass())) {
                    continue;
                }
                // we have the right method.  Now call it
                try {
                    m.invoke(writeableParent.getProxy(writeableParent.<ConfigBeanProxy>getProxyType()), valList);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new TransactionFailure("Exception while setting element", e);
                }
                return node;
            }
            throw new TransactionFailure("No method found for setting element");
        }
    }, readableView);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigBean(org.jvnet.hk2.config.ConfigBean) Method(java.lang.reflect.Method) PropertyVetoException(java.beans.PropertyVetoException) ParameterizedType(java.lang.reflect.ParameterizedType) WriteableView(org.jvnet.hk2.config.WriteableView) StringTokenizer(java.util.StringTokenizer) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.jvnet.hk2.config.types.Property)

Aggregations

Property (org.jvnet.hk2.config.types.Property)14 PropertyVetoException (java.beans.PropertyVetoException)13 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)13 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 List (java.util.List)4 TreeMap (java.util.TreeMap)4 ActionReport (org.glassfish.api.ActionReport)4 MultiException (org.glassfish.hk2.api.MultiException)4 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)4 ConfigModel (org.jvnet.hk2.config.ConfigModel)4 Dom (org.jvnet.hk2.config.Dom)4 Application (com.sun.enterprise.config.serverbeans.Application)3 Domain (com.sun.enterprise.config.serverbeans.Domain)3 Server (com.sun.enterprise.config.serverbeans.Server)3 SystemProperty (com.sun.enterprise.config.serverbeans.SystemProperty)3 File (java.io.File)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3