use of lucee.runtime.Component in project Lucee by lucee.
the class AxisCaster method toLuceeType.
public static Object toLuceeType(PageContext pc, String customType, Object value) throws PageException {
pc = ThreadLocalPageContext.get(pc);
if (pc != null && value instanceof Pojo) {
if (!StringUtil.isEmpty(customType)) {
Component cfc = toComponent(pc, (Pojo) value, customType, null);
if (cfc != null)
return cfc;
}
/*
// try package/class name as component name
String compPath=value.getClass().getName();
Component cfc = toComponent(pc, (Pojo)value, compPath, null);
if(cfc!=null) return cfc;
// try class name as component name
compPath=ListUtil.last(compPath, '.');
cfc = toComponent(pc, (Pojo)value, compPath, null);
if(cfc!=null) return cfc;
*/
}
if (value instanceof Date || value instanceof Calendar) {
// do not change to caster.isDate
return Caster.toDate(value, null);
}
if (value instanceof Object[]) {
Object[] arr = (Object[]) value;
if (!ArrayUtil.isEmpty(arr)) {
boolean allTheSame = true;
// byte
if (arr[0] instanceof Byte) {
for (int i = 1; i < arr.length; i++) {
if (!(arr[i] instanceof Byte)) {
allTheSame = false;
break;
}
}
if (allTheSame) {
byte[] bytes = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
bytes[i] = Caster.toByteValue(arr[i]);
}
return bytes;
}
}
}
}
if (value instanceof Byte[]) {
Byte[] arr = (Byte[]) value;
if (!ArrayUtil.isEmpty(arr)) {
byte[] bytes = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
bytes[i] = arr[i].byteValue();
}
return bytes;
}
}
if (value instanceof byte[]) {
return value;
}
if (Decision.isArray(value)) {
Array a = Caster.toArray(value);
int len = a.size();
Object o;
String ct;
for (int i = 1; i <= len; i++) {
o = a.get(i, null);
if (o != null) {
ct = customType != null && customType.endsWith("[]") ? customType.substring(0, customType.length() - 2) : null;
a.setEL(i, toLuceeType(pc, ct, o));
}
}
return a;
}
if (value instanceof Map) {
Struct sct = new StructImpl();
Iterator it = ((Map) value).entrySet().iterator();
Map.Entry entry;
while (it.hasNext()) {
entry = (Entry) it.next();
sct.setEL(Caster.toString(entry.getKey()), toLuceeType(pc, null, entry.getValue()));
}
return sct;
// return StructUtil.copyToStruct((Map)value);
}
if (isQueryBean(value)) {
QueryBean qb = (QueryBean) value;
String[] strColumns = qb.getColumnList();
Object[][] data = qb.getData();
int recorcount = data.length;
Query qry = new QueryImpl(strColumns, recorcount, "QueryBean");
QueryColumn[] columns = new QueryColumn[strColumns.length];
for (int i = 0; i < columns.length; i++) {
columns[i] = qry.getColumn(strColumns[i]);
}
int row;
for (row = 1; row <= recorcount; row++) {
for (int i = 0; i < columns.length; i++) {
columns[i].set(row, toLuceeType(pc, null, data[row - 1][i]));
}
}
return qry;
}
if (Decision.isQuery(value)) {
Query q = Caster.toQuery(value);
int recorcount = q.getRecordcount();
String[] strColumns = q.getColumns();
QueryColumn col;
int row;
for (int i = 0; i < strColumns.length; i++) {
col = q.getColumn(strColumns[i]);
for (row = 1; row <= recorcount; row++) {
col.set(row, toLuceeType(pc, null, col.get(row, null)));
}
}
return q;
}
return value;
}
use of lucee.runtime.Component in project Lucee by lucee.
the class Mapping method _init.
private static ArrayList<Source> _init(PageContext pc, Mapping mapping, Resource dir) throws PageException {
Resource[] children = dir.listResources(FILTER);
RestSettings settings = pc.getApplicationContext().getRestSettings();
ArrayList<Source> sources = new ArrayList<Source>();
PageSource ps;
Component cfc;
Struct meta;
String path;
for (int i = 0; i < children.length; i++) {
try {
ps = pc.toPageSource(children[i], null);
cfc = ComponentLoader.loadComponent(pc, ps, children[i].getName(), true, true);
meta = cfc.getMetaData(pc);
if (Caster.toBooleanValue(meta.get(KeyConstants._rest, null), false)) {
path = Caster.toString(meta.get(KeyConstants._restPath, null), null);
sources.add(new Source(mapping, cfc.getPageSource(), path));
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
if (!settings.getSkipCFCWithError())
throw Caster.toPageException(t);
}
}
return sources;
}
use of lucee.runtime.Component in project Lucee by lucee.
the class ComponentController method _invoke.
public static Object _invoke(String name, Object[] args) throws PageException {
Key key = Caster.toKey(name);
Component c = component.get();
PageContext p = pagecontext.get();
MessageContext mc = messageContext.get();
if (c == null)
throw new ApplicationException("missing component");
if (p == null)
throw new ApplicationException("missing pagecontext");
UDF udf = Caster.toFunction(c.get(p, key, null), null);
FunctionArgument[] fa = null;
if (udf != null)
fa = udf.getFunctionArguments();
for (int i = 0; i < args.length; i++) {
if (fa != null && i < fa.length && fa[i].getType() == CFTypes.TYPE_UNKNOW) {
args[i] = AxisCaster.toLuceeType(p, fa[i].getTypeAsString(), args[i]);
} else
args[i] = AxisCaster.toLuceeType(p, args[i]);
}
// return type
String rtnType = udf != null ? udf.getReturnTypeAsString() : "any";
Object rtn = c.call(p, key, args);
// cast return value to Axis type
try {
RPCServer server = RPCServer.getInstance(p.getId(), p, p.getServletContext());
TypeMapping tm = mc != null ? mc.getTypeMapping() : TypeMappingUtil.getServerTypeMapping(server.getEngine().getTypeMappingRegistry());
rtn = Caster.castTo(p, rtnType, rtn, false);
Class<?> clazz = Caster.cfTypeToClass(rtnType);
return AxisCaster.toAxisType(tm, rtn, clazz.getComponentType() != null ? clazz : null);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw Caster.toPageException(t);
}
}
use of lucee.runtime.Component in project Lucee by lucee.
the class SMTPClient method send.
public void send(PageContext pc, long sendTime) throws MailException {
if (plainText == null && htmlText == null)
throw new MailException("you must define plaintext or htmltext");
Server[] servers = ((PageContextImpl) pc).getMailServers();
ConfigWeb config = pc.getConfig();
if (ArrayUtil.isEmpty(servers) && ArrayUtil.isEmpty(host))
throw new MailException("no SMTP Server defined");
if (spool == SPOOL_YES || (spool == SPOOL_UNDEFINED && config.isMailSpoolEnable())) {
MailSpoolerTask mst = new MailSpoolerTask(this, servers, sendTime);
if (listener instanceof Component)
mst.setListener(new ComponentSpoolerTaskListener(SystemUtil.getCurrentContext(), mst, (Component) listener));
else if (listener instanceof UDF)
mst.setListener(new UDFSpoolerTaskListener(SystemUtil.getCurrentContext(), mst, (UDF) listener));
config.getSpoolerEngine().add(mst);
} else
_send(config, servers);
}
use of lucee.runtime.Component in project Lucee by lucee.
the class Caster method toArray.
/**
* cast a Object to a Array Object
* @param o Object to cast
* @return casted Array
* @throws PageException
*/
public static Array toArray(Object o) throws PageException {
if (o instanceof Array)
return (Array) o;
else if (o instanceof Object[]) {
return new ArrayImpl((Object[]) o);
} else if (o instanceof List) {
// new ArrayImpl(((List) o).toArray());
return ListAsArray.toArray((List) o);
} else if (o instanceof Set) {
// new ArrayImpl(((List) o).toArray());
return toArray(((Set) o).toArray());
} else if (o instanceof XMLStruct) {
XMLMultiElementStruct xmes;
if (o instanceof XMLMultiElementStruct) {
xmes = (XMLMultiElementStruct) o;
} else {
XMLStruct sct = (XMLStruct) o;
Array a = new ArrayImpl();
a.append(o);
xmes = new XMLMultiElementStruct(a, sct.getCaseSensitive());
}
return new XMLMultiElementArray(xmes);
} else if (o instanceof ObjectWrap) {
return toArray(((ObjectWrap) o).getEmbededObject());
} else if (o instanceof Struct) {
// function _toArray
if (o instanceof Component) {
Component c = (Component) o;
PageContext pc = ThreadLocalPageContext.get();
if (pc != null) {
Member member = c.getMember(Component.ACCESS_PRIVATE, KeyConstants.__toArray, false, false);
// Object o = get(pc,"_toString",null);
if (member instanceof UDFPlus) {
UDFPlus udf = (UDFPlus) member;
if (udf.getReturnType() == CFTypes.TYPE_ARRAY && udf.getFunctionArguments().length == 0) {
return Caster.toArray(c.call(pc, KeyConstants.__toArray, new Object[0]));
}
}
}
}
Struct sct = (Struct) o;
Array arr = new ArrayImpl();
Iterator<Entry<Key, Object>> it = sct.entryIterator();
Entry<Key, Object> e = null;
try {
while (it.hasNext()) {
e = it.next();
arr.setE(toIntValue(e.getKey().getString()), e.getValue());
}
} catch (ExpressionException ee) {
throw new ExpressionException("can't cast struct to a array, key [" + e.getKey().getString() + "] is not a number");
}
return arr;
} else if (o instanceof boolean[])
return new ArrayImpl(ArrayUtil.toReferenceType((boolean[]) o));
else if (o instanceof byte[])
return new ArrayImpl(ArrayUtil.toReferenceType((byte[]) o));
else if (o instanceof char[])
return new ArrayImpl(ArrayUtil.toReferenceType((char[]) o));
else if (o instanceof short[])
return new ArrayImpl(ArrayUtil.toReferenceType((short[]) o));
else if (o instanceof int[])
return new ArrayImpl(ArrayUtil.toReferenceType((int[]) o));
else if (o instanceof long[])
return new ArrayImpl(ArrayUtil.toReferenceType((long[]) o));
else if (o instanceof float[])
return new ArrayImpl(ArrayUtil.toReferenceType((float[]) o));
else if (o instanceof double[])
return new ArrayImpl(ArrayUtil.toReferenceType((double[]) o));
throw new CasterException(o, "Array");
}
Aggregations