Search in sources :

Example 11 with Converter

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

the class BlabberConverterManager method getConverter.

/**
 * @param paramType
 *            The type to find a converter for
 * @return The converter for the given type, or null if one can't be found
 */
public Converter getConverter(Class<?> paramType) {
    // Can we find a converter assignable to paramType in the HashMap?
    Converter converter = getConverterAssignableFrom(paramType);
    if (converter != null) {
        return converter;
    }
    String lookup = paramType.getName();
    // dynamic proxies
    if (lookup.startsWith("$Proxy")) {
        converter = converters.get("$Proxy*");
        if (converter != null) {
            return converter;
        }
    }
    while (true) {
        // Can we find a converter using wildcards?
        converter = converters.get(lookup + ".*");
        if (converter != null) {
            return converter;
        }
        // Arrays can have wildcards like [L* so we don't require a '.'
        converter = converters.get(lookup + '*');
        if (converter != null) {
            return converter;
        }
        // Give up if the name is now empty
        if (lookup.length() == 0) {
            break;
        }
        // Strip of the component after the last .
        int lastdot = lookup.lastIndexOf('.');
        if (lastdot != -1) {
            lookup = lookup.substring(0, lastdot);
        } else {
            int arrayMarkers = 0;
            while (lookup.charAt(arrayMarkers) == '[') {
                arrayMarkers++;
            }
            if (arrayMarkers == 0) {
                // bail out.
                break;
            }
            // We want to keep the type marker too
            lookup = lookup.substring(arrayMarkers - 1, arrayMarkers + 1);
            // Now can we find it?
            converter = converters.get(lookup);
            if (converter != null) {
                return converter;
            }
        }
    }
    return null;
}
Also used : NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter)

Example 12 with Converter

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

the class BlabberConverterManager method addConverter.

/*
     * (non-Javadoc)
     * 
     * @see org.directwebremoting.ConverterManager#addConverter(java.lang.String, org.directwebremoting.Converter)
     */
public void addConverter(String match, Converter converter) throws IllegalArgumentException {
    // Check that we don't have this one already
    Converter other = converters.get(match);
    if (other != null) {
        log.warn("Clash of converters for " + match + ". Using " + converter.getClass().getName() + " in place of " + other.getClass().getName());
    }
    log.debug("- adding converter: " + LocalUtil.getShortClassName(converter.getClass()) + " for " + match);
    converters.put(match, converter);
}
Also used : NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter)

Example 13 with Converter

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

Example 14 with Converter

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

the class DefaultConverterManager method getConverter.

/**
 * @param paramType The type to find a converter for
 * @return The converter for the given type, or null if one can't be found
 */
private Converter getConverter(Class paramType) {
    // Can we find a converter assignable to paramType in the HashMap?
    Converter converter = getConverterAssignableFrom(paramType);
    if (converter != null) {
        return converter;
    }
    String lookup = paramType.getName();
    // dynamic proxies
    if (lookup.startsWith("$Proxy")) {
        converter = (Converter) converters.get("$Proxy*");
        if (converter != null) {
            return converter;
        }
    }
    while (true) {
        // Can we find a converter using wildcards?
        converter = (Converter) converters.get(lookup + ".*");
        if (converter != null) {
            return converter;
        }
        // Arrays can have wildcards like [L* so we don't require a '.'
        converter = (Converter) converters.get(lookup + '*');
        if (converter != null) {
            return converter;
        }
        // Give up if the name is now empty
        if (lookup.length() == 0) {
            break;
        }
        // Strip of the component after the last .
        int lastdot = lookup.lastIndexOf('.');
        if (lastdot != -1) {
            lookup = lookup.substring(0, lastdot);
        } else {
            int arrayMarkers = 0;
            while (lookup.charAt(arrayMarkers) == '[') {
                arrayMarkers++;
            }
            if (arrayMarkers == 0) {
                // bail out.
                break;
            }
            // We want to keep the type marker too
            lookup = lookup.substring(arrayMarkers - 1, arrayMarkers + 1);
            // Now can we find it?
            converter = (Converter) converters.get(lookup);
            if (converter != null) {
                return converter;
            }
        }
    }
    return null;
}
Also used : NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter)

Example 15 with Converter

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

the class DefaultConverterManager 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 = (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)

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