Search in sources :

Example 1 with Attribute

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

the class WebContainer method updateHost.

/**
 * Updates a virtual-server element.
 *
 * @param vsBean the virtual-server config bean.
 */
public void updateHost(com.sun.enterprise.config.serverbeans.VirtualServer vsBean) throws LifecycleException {
    if (org.glassfish.api.web.Constants.ADMIN_VS.equals(vsBean.getId())) {
        return;
    }
    final VirtualServer vs = (VirtualServer) getEngine().findChild(vsBean.getId());
    if (vs == null) {
        logger.log(Level.WARNING, LogFacade.CANNOT_UPDATE_NON_EXISTENCE_VS, vsBean.getId());
        return;
    }
    boolean updateListeners = false;
    // Only update connectors if virtual-server.http-listeners is changed dynamically
    if (vs.getNetworkListeners() == null) {
        if (vsBean.getNetworkListeners() == null) {
            updateListeners = false;
        } else {
            updateListeners = true;
        }
    } else if (vs.getNetworkListeners().equals(vsBean.getNetworkListeners())) {
        updateListeners = false;
    } else {
        List<String> vsList = StringUtils.parseStringList(vs.getNetworkListeners(), ",");
        List<String> vsBeanList = StringUtils.parseStringList(vsBean.getNetworkListeners(), ",");
        for (String vsBeanName : vsBeanList) {
            if (!vsList.contains(vsBeanName)) {
                updateListeners = true;
                if (logger.isLoggable(Level.FINE)) {
                    logger.log(Level.FINE, LogFacade.UPDATE_LISTENER, new Object[] { vsBeanName, vs.getNetworkListeners() });
                }
                break;
            }
        }
    }
    // Must retrieve the old default-web-module before updating the
    // virtual server with the new vsBean, because default-web-module is
    // read from vsBean
    String oldDefaultWebModule = vs.getDefaultWebModuleID();
    vs.setBean(vsBean);
    String vsLogFile = vsBean.getLogFile();
    vs.setLogFile(vsLogFile, logLevel, logServiceFile);
    vs.configureState();
    vs.clearAliases();
    vs.configureAliases();
    // support both docroot property and attribute
    String docroot = vsBean.getPropertyValue("docroot");
    if (docroot == null) {
        docroot = vsBean.getDocroot();
    }
    if (docroot != null) {
        // Only update docroot if it is modified
        if (!vs.getDocRoot().getAbsolutePath().equals(docroot)) {
            updateDocroot(docroot, vs, vsBean);
        }
    }
    List<Property> props = vs.getProperties();
    for (Property prop : props) {
        updateHostProperties(vsBean, prop.getName(), prop.getValue(), securityService, vs);
    }
    vs.configureSingleSignOn(globalSSOEnabled, webContainerFeatureFactory, isSsoFailoverEnabled());
    vs.reconfigureAccessLog(globalAccessLogBufferSize, globalAccessLogWriteInterval, habitat, domain, globalAccessLoggingEnabled);
    // old listener names
    List<String> oldListenerList = StringUtils.parseStringList(vsBean.getNetworkListeners(), ",");
    String[] oldListeners = (oldListenerList != null) ? oldListenerList.toArray(new String[oldListenerList.size()]) : new String[0];
    // new listener config
    HashSet<NetworkListener> networkListeners = new HashSet<NetworkListener>();
    if (oldListenerList != null) {
        for (String listener : oldListeners) {
            boolean found = false;
            for (NetworkListener httpListener : serverConfig.getNetworkConfig().getNetworkListeners().getNetworkListener()) {
                if (httpListener.getName().equals(listener)) {
                    networkListeners.add(httpListener);
                    found = true;
                    break;
                }
            }
            if (!found) {
                String msg = rb.getString(LogFacade.LISTENER_REFERENCED_BY_HOST_NOT_EXIST);
                msg = MessageFormat.format(msg, listener, vs.getName());
                logger.log(Level.SEVERE, msg);
            }
        }
        // Update the port numbers with which the virtual server is
        // associated
        configureHostPortNumbers(vs, networkListeners);
    } else {
        // The virtual server is not associated with any http listeners
        vs.setNetworkListenerNames(new String[0]);
    }
    // have been removed from its http-listeners attribute
    for (String oldListener : oldListeners) {
        boolean found = false;
        for (NetworkListener httpListener : networkListeners) {
            if (httpListener.getName().equals(oldListener)) {
                found = true;
            }
        }
        if (!found) {
            // http listener was removed
            Connector[] connectors = _embedded.findConnectors();
            for (Connector connector : connectors) {
                WebConnector conn = (WebConnector) connector;
                if (oldListener.equals(conn.getName())) {
                    try {
                        conn.getMapperListener().unregisterHost(vs.getJmxName());
                    } catch (Exception e) {
                        throw new LifecycleException(e);
                    }
                }
            }
        }
    }
    // have been added to its http-listeners attribute
    for (NetworkListener httpListener : networkListeners) {
        boolean found = false;
        for (String oldListener : oldListeners) {
            if (httpListener.getName().equals(oldListener)) {
                found = true;
            }
        }
        if (!found) {
            // http listener was added
            Connector[] connectors = _embedded.findConnectors();
            for (Connector connector : connectors) {
                WebConnector conn = (WebConnector) connector;
                if (httpListener.getName().equals(conn.getName())) {
                    if (!conn.isAvailable()) {
                        conn.start();
                    }
                    try {
                        conn.getMapperListener().registerHost(vs);
                    } catch (Exception e) {
                        throw new LifecycleException(e);
                    }
                }
            }
        }
    }
    // passing in "null" as the default context path
    if (oldDefaultWebModule != null) {
        updateDefaultWebModule(vs, oldListeners, null);
    }
    /*
         * Add default web module if one has been configured for the
         * virtual server. If the module declared as the default web module
         * has already been deployed at the root context, we don't have
         * to do anything.
         */
    WebModuleConfig wmInfo = vs.getDefaultWebModule(domain, habitat.<WebArchivist>getService(WebArchivist.class), appRegistry);
    if ((wmInfo != null) && (wmInfo.getContextPath() != null) && !"".equals(wmInfo.getContextPath()) && !"/".equals(wmInfo.getContextPath())) {
        // Remove dummy context that was created off of docroot, if such
        // a context exists
        removeDummyModule(vs);
        updateDefaultWebModule(vs, vs.getNetworkListenerNames(), wmInfo);
    } else {
        WebModuleConfig wmc = vs.createSystemDefaultWebModuleIfNecessary(habitat.<WebArchivist>getService(WebArchivist.class));
        if (wmc != null) {
            loadStandaloneWebModule(vs, wmc);
        }
    }
    if (updateListeners) {
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, LogFacade.VS_UPDATED_NETWORK_LISTENERS, new Object[] { vs.getName(), vs.getNetworkListeners(), vsBean.getNetworkListeners() });
        }
        /*
             * Need to update connector and mapper restart is required
             * when virtual-server.http-listeners is changed dynamically
             */
        List<NetworkListener> httpListeners = serverConfig.getNetworkConfig().getNetworkListeners().getNetworkListener();
        if (httpListeners != null) {
            for (NetworkListener httpListener : httpListeners) {
                updateConnector(httpListener, habitat.<HttpService>getService(HttpService.class));
            }
        }
    }
}
Also used : PECoyoteConnector(com.sun.enterprise.web.connector.coyote.PECoyoteConnector) Connector(org.apache.catalina.Connector) LifecycleException(org.apache.catalina.LifecycleException) LifecycleException(org.apache.catalina.LifecycleException) NamingException(javax.naming.NamingException) BindException(java.net.BindException) MalformedURLException(java.net.MalformedURLException) WebArchivist(org.glassfish.web.deployment.archivist.WebArchivist) HttpService(com.sun.enterprise.config.serverbeans.HttpService) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.jvnet.hk2.config.types.Property) SystemProperty(com.sun.enterprise.config.serverbeans.SystemProperty) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) HashSet(java.util.HashSet)

Example 2 with Attribute

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

the class CompositeUtil method analyzeInterface.

private void analyzeInterface(Class<?> iface, Map<String, Map<String, Object>> properties) throws SecurityException {
    // find class level bean reference
    String defaultBean = null;
    if (iface.isAnnotationPresent(DefaultBeanReference.class)) {
        DefaultBeanReference beanRef = iface.getAnnotation(DefaultBeanReference.class);
        defaultBean = beanRef.bean();
    }
    for (Method method : iface.getMethods()) {
        String name = method.getName();
        final boolean isGetter = name.startsWith("get");
        if (isGetter || name.startsWith("set")) {
            name = name.substring(3);
            Map<String, Object> property = properties.get(name);
            if (property == null) {
                property = new HashMap<String, Object>();
                properties.put(name, property);
            }
            String bean = null;
            String attribute = null;
            AttributeReference ar = method.getAnnotation(AttributeReference.class);
            if (ar != null) {
                bean = ar.bean();
                attribute = ar.attribute();
            }
            if (!StringUtil.notEmpty(bean)) {
                bean = defaultBean;
            }
            if (!StringUtil.notEmpty(attribute)) {
                attribute = name;
            }
            if (StringUtil.notEmpty(bean) && StringUtil.notEmpty(attribute)) {
                property.put("annotations", gatherReferencedAttributes(bean, attribute));
            }
            Attribute attr = method.getAnnotation(Attribute.class);
            if (attr != null) {
                property.put("defaultValue", attr.defaultValue());
            }
            Class<?> type = isGetter ? method.getReturnType() : method.getParameterTypes()[0];
            property.put("type", type);
        }
    }
}
Also used : DefaultBeanReference(org.glassfish.admin.rest.composite.metadata.DefaultBeanReference) Attribute(org.jvnet.hk2.config.Attribute) AttributeReference(org.glassfish.admin.rest.composite.metadata.AttributeReference) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) Method(java.lang.reflect.Method)

Example 3 with Attribute

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

the class ParamMetadata method getDefaultValue.

/**
 * This method will process the annotations for a field to try to determine the default value, if one has been specified.
 * @param annos
 * @return
 */
private JsonValue getDefaultValue(Annotation[] annos) {
    Object defval = null;
    if (annos != null) {
        for (Annotation annotation : annos) {
            if (Default.class.isAssignableFrom(annotation.getClass())) {
                try {
                    Default def = (Default) annotation;
                    Class clazz = def.generator();
                    if (def.useContext()) {
                        defval = ((DefaultsGenerator) context).getDefaultValue(name);
                    } else if (clazz != null && clazz != Void.class) {
                        if (DefaultsGenerator.class.isAssignableFrom(clazz)) {
                            defval = ((DefaultsGenerator) clazz.newInstance()).getDefaultValue(name);
                        } else {
                            RestLogging.restLogger.log(Level.SEVERE, RestLogging.DOESNT_IMPLEMENT_DEFAULTS_GENERATOR);
                        }
                    } else {
                        defval = parseValue(def.value());
                    }
                    break;
                } catch (Exception ex) {
                    RestLogging.restLogger.log(Level.SEVERE, null, ex);
                }
            } else if (Attribute.class.isAssignableFrom(annotation.getClass())) {
                Attribute attr = (Attribute) annotation;
                defval = attr.defaultValue();
                break;
            }
        }
    }
    try {
        return JsonUtil.getJsonValue(defval);
    } catch (JsonException e) {
        return null;
    }
}
Also used : JsonException(javax.json.JsonException) Attribute(org.jvnet.hk2.config.Attribute) JsonObject(javax.json.JsonObject) Annotation(java.lang.annotation.Annotation) JsonException(javax.json.JsonException)

Example 4 with Attribute

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

the class ClientGenerator method processAttributes.

protected void processAttributes(ClientClassWriter writer, ConfigModel model, Set<String> processed) {
    Class clazz = model.getProxyType();
    for (Method method : clazz.getMethods()) {
        String methodName = method.getName();
        Attribute a = method.getAnnotation(Attribute.class);
        Param p = method.getAnnotation(Param.class);
        if ((a != null) || (p != null)) {
            String type = "String";
            if (a != null) {
                type = a.dataType().getName();
            }
            if (methodName.startsWith("get") || methodName.startsWith("set")) {
                methodName = methodName.substring(3);
            }
            String fieldName = Util.lowerCaseFirstLetter(methodName);
            if (processed.contains(fieldName)) {
                continue;
            }
            processed.add(fieldName);
            writer.generateGettersAndSetters(type, methodName, fieldName);
        }
    }
}
Also used : Attribute(org.jvnet.hk2.config.Attribute) Param(org.glassfish.api.Param) Method(java.lang.reflect.Method)

Example 5 with Attribute

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

the class DefaultValueTest method rawAttributeTest.

@Test
public void rawAttributeTest() throws NoSuchMethodException {
    String address = listener.getAddress();
    Dom raw = Dom.unwrap(listener);
    Attribute attr = raw.getProxyType().getMethod("getAddress").getAnnotation(Attribute.class);
    assertEquals(attr.defaultValue(), address);
    assertEquals(raw.attribute("address"), address);
    assertEquals(raw.rawAttribute("address"), address);
}
Also used : Dom(org.jvnet.hk2.config.Dom) Attribute(org.jvnet.hk2.config.Attribute) Test(org.junit.Test)

Aggregations

Attribute (org.jvnet.hk2.config.Attribute)11 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)7 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)6 PropertyChangeEvent (java.beans.PropertyChangeEvent)5 HashMap (java.util.HashMap)5 MultiException (org.glassfish.hk2.api.MultiException)5 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)5 Map (java.util.Map)4 Dom (org.jvnet.hk2.config.Dom)4 Property (org.jvnet.hk2.config.types.Property)4 Cluster (com.sun.enterprise.config.serverbeans.Cluster)3 SystemProperty (com.sun.enterprise.config.serverbeans.SystemProperty)3 PropertyVetoException (java.beans.PropertyVetoException)3 List (java.util.List)3 Param (org.glassfish.api.Param)3 ParameterMap (org.glassfish.api.admin.ParameterMap)3 Domain (com.sun.enterprise.config.serverbeans.Domain)2 MonitoringService (com.sun.enterprise.config.serverbeans.MonitoringService)2 Server (com.sun.enterprise.config.serverbeans.Server)2