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;
}
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;
}
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;
}
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();
}
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;
}
Aggregations