Search in sources :

Example 6 with Converter

use of org.directwebremoting.extend.Converter 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 7 with Converter

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

the class BlabberConverterManager method getConverterAssignableFrom.

/**
 * @param paramType
 *            The type to find a converter for
 * @return The converter assignable for the given type, or null if one can't be found
 */
private Converter getConverterAssignableFrom(Class<?> paramType) {
    if (paramType == null) {
        return null;
    }
    String lookup = paramType.getName();
    // Can we find the converter for paramType in the converters HashMap?
    Converter converter = null;
    synchronized (this.converters) {
        converter = converters.get(lookup);
        if (converter != null)
            return converter;
        // Lookup all of the interfaces of this class for a match
        Class<?>[] interfaces = paramType.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            converter = getConverterAssignableFrom(interfaces[i]);
            if (converter != null) {
                converters.put(lookup, converter);
                return converter;
            }
        }
        // Let's search it in paramType superClass
        converter = getConverterAssignableFrom(paramType.getSuperclass());
        if (converter != null) {
            converters.put(lookup, converter);
        }
    }
    return converter;
}
Also used : NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter)

Example 8 with Converter

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

the class BlabberConverterManager method getConverterAssignableFromNoAdd.

/**
 * @param paramType
 *            The type to find a converter for
 * @return The converter assignable for the given type, or null if one can't be found
 */
public Converter getConverterAssignableFromNoAdd(Class<?> paramType) {
    String lookup = paramType.getName();
    Converter converter = null;
    synchronized (this.converters) {
        converter = converters.get(lookup);
        if (converter != null)
            return converter;
        // Lookup all of the interfaces of this class for a match
        Class<?>[] interfaces = paramType.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            converter = getConverterAssignableFrom(interfaces[i]);
            if (converter != null)
                return converter;
        }
        // Let's search it in paramType superClass
        converter = getConverterAssignableFrom(paramType.getSuperclass());
    }
    return converter;
}
Also used : NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter)

Example 9 with Converter

use of org.directwebremoting.extend.Converter 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 10 with Converter

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

the class BlabberConverterManager method convertInbound.

/*
     * (non-Javadoc)
     * 
     * @see org.directwebremoting.ConverterManager#convertInbound(java.lang.Class,
     * org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext,
     * org.directwebremoting.TypeHintContext)
     */
public Object convertInbound(@SuppressWarnings("rawtypes") Class paramType, InboundVariable iv, InboundContext inctx, TypeHintContext incc) throws MarshallException {
    Object converted = inctx.getConverted(iv, paramType);
    if (converted == null) {
        // Was the inbound variable marshalled as an Object in the client
        // (could mean that this is an instance of one of our generated
        // JavaScript classes)
        Converter converter = getNamedConverter(paramType, iv.getType());
        // didn't find anything above
        if (converter == null) {
            converter = getConverter(paramType);
        }
        if (converter == null) {
            throw new MarshallException(paramType, Messages.getString("DefaultConverterManager.MissingConverter", paramType));
        }
        // from passing null to things they are not allowed to convert
        if (iv.isNull()) {
            return null;
        }
        inctx.pushContext(incc);
        converted = converter.convertInbound(paramType, iv, inctx);
        inctx.popContext();
    }
    return converted;
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter)

Aggregations

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