Search in sources :

Example 1 with Component

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;
}
Also used : Entry(java.util.Map.Entry) Query(lucee.runtime.type.Query) Struct(lucee.runtime.type.Struct) QueryBean(coldfusion.xml.rpc.QueryBean) QueryImpl(lucee.runtime.type.QueryImpl) Iterator(java.util.Iterator) Component(lucee.runtime.Component) Calendar(java.util.Calendar) Date(java.util.Date) Array(lucee.runtime.type.Array) StructImpl(lucee.runtime.type.StructImpl) QueryColumn(lucee.runtime.type.QueryColumn) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Component

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;
}
Also used : Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) Component(lucee.runtime.Component) PageSource(lucee.runtime.PageSource) PageSource(lucee.runtime.PageSource) Struct(lucee.runtime.type.Struct)

Example 3 with Component

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);
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) UDF(lucee.runtime.type.UDF) TypeMapping(javax.xml.rpc.encoding.TypeMapping) PageContext(lucee.runtime.PageContext) MessageContext(org.apache.axis.MessageContext) Component(lucee.runtime.Component) FunctionArgument(lucee.runtime.type.FunctionArgument) Key(lucee.runtime.type.Collection.Key)

Example 4 with Component

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);
}
Also used : Server(lucee.runtime.net.mail.Server) UDFSpoolerTaskListener(lucee.runtime.spooler.UDFSpoolerTaskListener) UDF(lucee.runtime.type.UDF) MailException(lucee.runtime.net.mail.MailException) PageContextImpl(lucee.runtime.PageContextImpl) MailSpoolerTask(lucee.runtime.spooler.mail.MailSpoolerTask) Component(lucee.runtime.Component) ConfigWeb(lucee.runtime.config.ConfigWeb) ComponentSpoolerTaskListener(lucee.runtime.spooler.ComponentSpoolerTaskListener)

Example 5 with Component

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");
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) ArrayImpl(lucee.runtime.type.ArrayImpl) ExpressionException(lucee.runtime.exp.ExpressionException) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) ObjectStruct(lucee.runtime.type.scope.ObjectStruct) MapAsStruct(lucee.runtime.type.wrap.MapAsStruct) Struct(lucee.runtime.type.Struct) CollectionStruct(lucee.runtime.type.CollectionStruct) Entry(java.util.Map.Entry) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) ArrayList(java.util.ArrayList) ArrayAsList(lucee.runtime.type.wrap.ArrayAsList) List(java.util.List) NodeList(org.w3c.dom.NodeList) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) Component(lucee.runtime.Component) Member(lucee.runtime.component.Member) CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) UDFPlus(lucee.runtime.type.UDFPlus) Array(lucee.runtime.type.Array) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ListAsArray(lucee.runtime.type.wrap.ListAsArray) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) JavaObject(lucee.runtime.java.JavaObject) Key(lucee.runtime.type.Collection.Key)

Aggregations

Component (lucee.runtime.Component)36 PageException (lucee.runtime.exp.PageException)12 Key (lucee.runtime.type.Collection.Key)12 Struct (lucee.runtime.type.Struct)12 PageContextImpl (lucee.runtime.PageContextImpl)9 UDF (lucee.runtime.type.UDF)9 Entry (java.util.Map.Entry)7 ComponentScope (lucee.runtime.ComponentScope)7 PageContext (lucee.runtime.PageContext)7 Array (lucee.runtime.type.Array)7 JavaObject (lucee.runtime.java.JavaObject)6 IOException (java.io.IOException)5 PageSource (lucee.runtime.PageSource)5 ThreadLocalPageContext (lucee.runtime.engine.ThreadLocalPageContext)5 ExpressionException (lucee.runtime.exp.ExpressionException)5 Collection (lucee.runtime.type.Collection)5 ComponentSpecificAccess (lucee.runtime.ComponentSpecificAccess)4 Property (lucee.runtime.component.Property)4 ArrayImpl (lucee.runtime.type.ArrayImpl)4 StructImpl (lucee.runtime.type.StructImpl)4