Search in sources :

Example 1 with NamedConverter

use of org.directwebremoting.extend.NamedConverter in project ma-core-public by infiniteautomation.

the class BlabberConverterManager method getNamedConverter.

/**
 * When we are using typed Javascript names we sometimes want to get a specially named converter
 *
 * @param paramType
 *            The class that we are converting to
 * @param type
 *            The type name as passed in from the client
 * @return The Converter that matches this request (if any)
 * @throws MarshallException
 */
protected Converter getNamedConverter(Class<?> paramType, String type) throws MarshallException {
    if (type.startsWith("Object_")) {
        // Extract the JavaScript classname from the inbound type
        String javascriptClassName = type.substring("Object_".length());
        // Locate a converter for this JavaScript classname
        synchronized (converters) {
            Iterator<Map.Entry<String, Converter>> it = converters.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Converter> entry = it.next();
                String match = entry.getKey();
                Converter conv = entry.getValue();
                // JavaScript mapping is only applicable for compound converters
                if (conv instanceof NamedConverter) {
                    NamedConverter boConv = (NamedConverter) conv;
                    if (boConv.getJavascript() != null && boConv.getJavascript().equals(javascriptClassName)) {
                        // parameter type?
                        try {
                            Class<?> inboundClass = LocalUtil.classForName(match);
                            if (paramType.isAssignableFrom(inboundClass)) {
                                // Hack: We also want to make sure that the
                                // converter creates its object based on the
                                // inbound class instead of the parameter
                                // type, and we have to use the other ref
                                // for this:
                                boConv.setInstanceType(inboundClass);
                                return boConv;
                            }
                        } catch (ClassNotFoundException ex) {
                            throw new MarshallException(paramType, ex);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) NamedConverter(org.directwebremoting.extend.NamedConverter) NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with NamedConverter

use of org.directwebremoting.extend.NamedConverter in project ma-core-public by infiniteautomation.

the class DefaultRemoter method generateInterfaceScript.

/* (non-Javadoc)
     * @see org.directwebremoting.Remoter#generateInterfaceScript(java.lang.String, java.lang.String)
     */
public String generateInterfaceScript(String scriptName, String path) throws SecurityException {
    String actualPath = path;
    if (overridePath != null) {
        actualPath = overridePath;
    }
    StringBuffer buffer = new StringBuffer();
    // Output the class definitions for the converted objects
    Collection converterMatches = converterManager.getConverterMatchStrings();
    Iterator it = converterMatches.iterator();
    while (it.hasNext()) {
        String match = (String) it.next();
        try {
            StringBuffer paramBuffer = new StringBuffer();
            Converter conv = converterManager.getConverterByMatchString(match);
            // We will only generate JavaScript classes for compound objects/beans
            if (conv instanceof NamedConverter) {
                NamedConverter boConv = (NamedConverter) conv;
                String jsClassName = boConv.getJavascript();
                // We need a configured JavaScript class name
                if (jsClassName != null && !jsClassName.equals("")) {
                    // Wildcard match strings are currently not supported
                    if (match.indexOf("*") == -1) {
                        paramBuffer.append('\n');
                        // output: if (typeof <class> != "function") { var <class> = function() {
                        paramBuffer.append("if (typeof " + jsClassName + " != \"function\") {\n");
                        paramBuffer.append("  function " + jsClassName + "() {\n");
                        // output: this.<property> = <init-value>;
                        Class mappedType;
                        try {
                            mappedType = LocalUtil.classForName(match);
                        } catch (ClassNotFoundException ex) {
                            throw new IllegalArgumentException(ex.getMessage());
                        }
                        Map properties = boConv.getPropertyMapFromClass(mappedType, true, true);
                        for (Iterator pit = properties.entrySet().iterator(); pit.hasNext(); ) {
                            Map.Entry entry = (Map.Entry) pit.next();
                            String name = (String) entry.getKey();
                            Property property = (Property) entry.getValue();
                            Class propType = property.getPropertyType();
                            // Property name
                            paramBuffer.append("    this." + name + " = ");
                            // Default property values
                            if (propType.isArray()) {
                                paramBuffer.append("[]");
                            } else if (propType == boolean.class) {
                                paramBuffer.append("false");
                            } else if (propType.isPrimitive()) {
                                paramBuffer.append("0");
                            } else {
                                paramBuffer.append("null");
                            }
                            paramBuffer.append(";\n");
                        }
                        paramBuffer.append("  }\n");
                        paramBuffer.append("}\n");
                    }
                }
            }
            buffer.append(paramBuffer.toString());
        } catch (Exception ex) {
            log.warn("Failed to create parameter declaration for " + match, ex);
            buffer.append("// Missing parameter declaration for " + match + ". See the server logs for details.");
        }
    }
    Creator creator = creatorManager.getCreator(scriptName);
    buffer.append('\n');
    String init = EnginePrivate.getEngineInitScript();
    buffer.append(init);
    buffer.append("if (" + scriptName + " == null) var " + scriptName + " = {};\n");
    buffer.append(scriptName + "._path = '" + actualPath + "';\n");
    Method[] methods = creator.getType().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String methodName = method.getName();
        // check if we can display it
        try {
            accessControl.assertIsDisplayable(creator, scriptName, method);
        } catch (SecurityException ex) {
            if (!allowImpossibleTests) {
                continue;
            }
        }
        // Is it on the list of banned names
        if (JavascriptUtil.isReservedWord(methodName)) {
            continue;
        }
        // Check to see if the creator is reloadable
        // If it is, then do not cache the generated Javascript
        String script;
        if (!creator.isCacheable()) {
            script = getMethodJS(scriptName, method);
        } else {
            String key = scriptName + "." + method.getName();
            // For optimal performance we might use the Memoizer pattern
            // JCiP#108 however performance isn't a big issue and we are
            // prepared to cope with getMethodJS() being run more than once.
            script = (String) methodCache.get(key);
            if (script == null) {
                script = getMethodJS(scriptName, method);
                methodCache.put(key, script);
            }
        }
        buffer.append(script);
    }
    return buffer.toString();
}
Also used : NamedConverter(org.directwebremoting.extend.NamedConverter) Creator(org.directwebremoting.extend.Creator) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Iterator(java.util.Iterator) Collection(java.util.Collection) NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property)

Example 3 with NamedConverter

use of org.directwebremoting.extend.NamedConverter in project ma-core-public by infiniteautomation.

the class DefaultConverterManager method getNamedConverter.

/**
 * When we are using typed Javascript names we sometimes want to get a
 * specially named converter
 * @param paramType The class that we are converting to
 * @param type The type name as passed in from the client
 * @return The Converter that matches this request (if any)
 * @throws MarshallException
 */
protected Converter getNamedConverter(Class paramType, String type) throws MarshallException {
    if (type.startsWith("Object_")) {
        // Extract the JavaScript classname from the inbound type
        String javascriptClassName = type.substring("Object_".length());
        // Locate a converter for this JavaScript classname
        synchronized (converters) {
            Iterator it = converters.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                String match = (String) entry.getKey();
                Converter conv = (Converter) entry.getValue();
                // JavaScript mapping is only applicable for compound converters
                if (conv instanceof NamedConverter) {
                    NamedConverter boConv = (NamedConverter) conv;
                    if (boConv.getJavascript() != null && boConv.getJavascript().equals(javascriptClassName)) {
                        // parameter type?
                        try {
                            Class inboundClass = LocalUtil.classForName(match);
                            if (paramType.isAssignableFrom(inboundClass)) {
                                // Hack: We also want to make sure that the
                                // converter creates its object based on the
                                // inbound class instead of the parameter
                                // type, and we have to use the other ref
                                // for this:
                                boConv.setInstanceType(inboundClass);
                                return boConv;
                            }
                        } catch (ClassNotFoundException ex) {
                            throw new MarshallException(paramType, ex);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) NamedConverter(org.directwebremoting.extend.NamedConverter) Iterator(java.util.Iterator) NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)3 Map (java.util.Map)3 Converter (org.directwebremoting.extend.Converter)3 NamedConverter (org.directwebremoting.extend.NamedConverter)3 Iterator (java.util.Iterator)2 MarshallException (org.directwebremoting.extend.MarshallException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Creator (org.directwebremoting.extend.Creator)1 Property (org.directwebremoting.extend.Property)1