Search in sources :

Example 6 with Component

use of lucee.runtime.Component in project Lucee by lucee.

the class Caster method otherTypeToClass.

private static Class<?> otherTypeToClass(String type) throws PageException, ClassException {
    PageContext pc = ThreadLocalPageContext.get();
    PageException pe = null;
    // try to load as cfc
    if (pc != null) {
        try {
            Component c = pc.loadComponent(type);
            return ComponentUtil.getComponentPropertiesClass(pc, c);
        } catch (PageException e) {
            pe = e;
        }
    }
    // try to load as class
    try {
        return ClassUtil.loadClass(type);
    } catch (ClassException ce) {
        if (pe != null)
            throw pe;
        throw ce;
    }
}
Also used : PageException(lucee.runtime.exp.PageException) ClassException(lucee.commons.lang.ClassException) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) Component(lucee.runtime.Component)

Example 7 with Component

use of lucee.runtime.Component in project Lucee by lucee.

the class Caster method _castTo.

private static Object _castTo(PageContext pc, String strType, Object o) throws PageException {
    if (o instanceof Component) {
        Component comp = ((Component) o);
        if (comp.instanceOf(strType))
            return o;
        throw new ExpressionException("can't cast Component of Type [" + comp.getAbsName() + "] to [" + strType + "]");
    }
    if (o instanceof Pojo) {
        Component cfc = AxisCaster.toComponent(pc, ((Pojo) o), strType, null);
        if (cfc != null)
            return cfc;
        throw new ExpressionException("can't cast Pojo of Type [" + o.getClass().getName() + "] to [" + strType + "]");
    }
    if (strType.endsWith("[]") && Decision.isArray(o)) {
        String _strType = strType.substring(0, strType.length() - 2);
        short _type = CFTypes.toShort(_strType, false, (short) -1);
        Array arr = Caster.toArray(o, null);
        if (arr != null) {
            // convert the values
            Iterator<Entry<Key, Object>> it = arr.entryIterator();
            Array _arr = new ArrayImpl();
            Entry<Key, Object> e;
            Object src, trg;
            boolean hasChanged = false;
            while (it.hasNext()) {
                e = it.next();
                src = e.getValue();
                trg = castTo(pc, _type, _strType, src);
                _arr.setEL(e.getKey(), trg);
                if (src != trg)
                    hasChanged = true;
            }
            if (!hasChanged)
                return arr;
            return _arr;
        }
    }
    throw new CasterException(o, strType);
}
Also used : Pojo(lucee.runtime.net.rpc.Pojo) CasterException(lucee.runtime.exp.CasterException) ArrayImpl(lucee.runtime.type.ArrayImpl) ExpressionException(lucee.runtime.exp.ExpressionException) Array(lucee.runtime.type.Array) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ListAsArray(lucee.runtime.type.wrap.ListAsArray) Entry(java.util.Map.Entry) JavaObject(lucee.runtime.java.JavaObject) Component(lucee.runtime.Component) Key(lucee.runtime.type.Collection.Key)

Example 8 with Component

use of lucee.runtime.Component in project Lucee by lucee.

the class WDDXConverter method _deserializeComponent.

/**
 * Desirialize a Component Object
 *
 * @param elComp
 *            Component Object as XML Element
 * @return Component Object
 * @throws ConverterException
 * @throws ConverterException
 */
private Object _deserializeComponent(Element elComp) throws ConverterException {
    // String type=elStruct.getAttribute("type");
    String name = elComp.getAttribute("name");
    String md5 = elComp.getAttribute("md5");
    // TLPC
    PageContext pc = ThreadLocalPageContext.get();
    // Load comp
    Component comp = null;
    try {
        comp = pc.loadComponent(name);
        if (!ComponentUtil.md5(comp).equals(md5)) {
            throw new ConverterException("component [" + name + "] in this enviroment has not the same interface as the component to load, it is possible that one off the components has Functions added dynamicly.");
        }
    } catch (ConverterException e) {
        throw e;
    } catch (Exception e) {
        throw new ConverterException(e.getMessage());
    }
    NodeList list = elComp.getChildNodes();
    ComponentScope scope = comp.getComponentScope();
    int len = list.getLength();
    String scopeName;
    Element var, value;
    Collection.Key key;
    for (int i = 0; i < len; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            var = (Element) node;
            value = getChildElement((Element) node);
            scopeName = var.getAttribute("scope");
            if (value != null) {
                key = Caster.toKey(var.getAttribute("name"), null);
                if (key == null)
                    continue;
                if ("variables".equalsIgnoreCase(scopeName))
                    scope.setEL(key, _deserialize(value));
                else
                    comp.setEL(key, _deserialize(value));
            }
        }
    }
    return comp;
}
Also used : Key(lucee.runtime.type.Collection.Key) ComponentScope(lucee.runtime.ComponentScope) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Collection(lucee.runtime.type.Collection) PageContext(lucee.runtime.PageContext) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) Component(lucee.runtime.Component) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) CoderException(lucee.runtime.coder.CoderException)

Example 9 with Component

use of lucee.runtime.Component in project Lucee by lucee.

the class WDDXConverter method _serializeComponent.

/**
 * serialize a Component
 *
 * @param component
 *            Component to serialize
 * @param done
 * @return serialized component
 * @throws ConverterException
 */
private String _serializeComponent(Component component, Set<Object> done) throws ConverterException {
    StringBuilder sb = new StringBuilder();
    Component ca;
    component = new ComponentSpecificAccess(Component.ACCESS_PRIVATE, ca = component);
    boolean isPeristent = ca.isPersistent();
    deep++;
    Object member;
    Iterator<Key> it = component.keyIterator();
    Collection.Key key;
    while (it.hasNext()) {
        key = Caster.toKey(it.next(), null);
        member = component.get(key, null);
        if (member instanceof UDF)
            continue;
        sb.append(goIn() + "<var scope=\"this\" name=" + del + XMLUtil.escapeXMLString(key.toString()) + del + ">");
        sb.append(_serialize(member, done));
        sb.append(goIn() + "</var>");
    }
    Property p;
    Boolean remotingFetch;
    Struct props = ignoreRemotingFetch ? null : ComponentUtil.getPropertiesAsStruct(ca, false);
    ComponentScope scope = ca.getComponentScope();
    it = scope.keyIterator();
    while (it.hasNext()) {
        key = Caster.toKey(it.next(), null);
        if (!ignoreRemotingFetch) {
            p = (Property) props.get(key, null);
            if (p != null) {
                remotingFetch = Caster.toBoolean(p.getDynamicAttributes().get(REMOTING_FETCH, null), null);
                if (remotingFetch == null) {
                    if (isPeristent && ORMUtil.isRelated(p))
                        continue;
                } else if (!remotingFetch.booleanValue())
                    continue;
            }
        }
        member = scope.get(key, null);
        if (member instanceof UDF || key.equals(KeyConstants._this))
            continue;
        sb.append(goIn() + "<var scope=\"variables\" name=" + del + XMLUtil.escapeXMLString(key.toString()) + del + ">");
        sb.append(_serialize(member, done));
        sb.append(goIn() + "</var>");
    }
    deep--;
    try {
        // return goIn()+"<struct>"+sb+"</struct>";
        return goIn() + "<component md5=\"" + ComponentUtil.md5(component) + "\" name=\"" + XMLUtil.escapeXMLString(component.getAbsName()) + "\">" + sb + "</component>";
    } catch (Exception e) {
        throw toConverterException(e);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) CoderException(lucee.runtime.coder.CoderException) Struct(lucee.runtime.type.Struct) Key(lucee.runtime.type.Collection.Key) ComponentScope(lucee.runtime.ComponentScope) UDF(lucee.runtime.type.UDF) ComponentSpecificAccess(lucee.runtime.ComponentSpecificAccess) Collection(lucee.runtime.type.Collection) Component(lucee.runtime.Component) Property(lucee.runtime.component.Property) Key(lucee.runtime.type.Collection.Key)

Example 10 with Component

use of lucee.runtime.Component in project Lucee by lucee.

the class XMLConverter method _serializeComponent.

/**
 * serialize a Component
 * @param component Component to serialize
 * @param done
 * @return serialized component
 * @throws ConverterException
 */
private String _serializeComponent(Component component, Map<Object, String> done) throws ConverterException {
    StringBuilder sb = new StringBuilder();
    Component ca;
    component = new ComponentSpecificAccess(Component.ACCESS_PRIVATE, ca = component);
    boolean isPeristent = ca.isPersistent();
    deep++;
    Object member;
    Iterator<Key> it = component.keyIterator();
    Collection.Key key;
    while (it.hasNext()) {
        key = it.next();
        member = component.get(key, null);
        if (member instanceof UDF)
            continue;
        sb.append(goIn() + "<var scope=\"this\" name=" + del + key.toString() + del + ">");
        sb.append(_serialize(member, done));
        sb.append(goIn() + "</var>");
    }
    Property p;
    Boolean remotingFetch;
    Struct props = ignoreRemotingFetch ? null : ComponentUtil.getPropertiesAsStruct(ca, false);
    ComponentScope scope = ca.getComponentScope();
    it = scope.keyIterator();
    while (it.hasNext()) {
        key = Caster.toKey(it.next(), null);
        if (!ignoreRemotingFetch) {
            p = (Property) props.get(key, null);
            if (p != null) {
                remotingFetch = Caster.toBoolean(p.getDynamicAttributes().get(REMOTING_FETCH, null), null);
                if (remotingFetch == null) {
                    if (isPeristent && ORMUtil.isRelated(p))
                        continue;
                } else if (!remotingFetch.booleanValue())
                    continue;
            }
        }
        member = scope.get(key, null);
        if (member instanceof UDF || key.equals(KeyConstants._this))
            continue;
        sb.append(goIn() + "<var scope=\"variables\" name=" + del + key.toString() + del + ">");
        sb.append(_serialize(member, done));
        sb.append(goIn() + "</var>");
    }
    deep--;
    try {
        // return goIn()+"<struct>"+sb+"</struct>";
        return goIn() + "<component md5=\"" + ComponentUtil.md5(component) + "\" name=\"" + component.getAbsName() + "\">" + sb + "</component>";
    } catch (Exception e) {
        throw toConverterException(e);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) Struct(lucee.runtime.type.Struct) Key(lucee.runtime.type.Collection.Key) ComponentScope(lucee.runtime.ComponentScope) UDF(lucee.runtime.type.UDF) ComponentSpecificAccess(lucee.runtime.ComponentSpecificAccess) Collection(lucee.runtime.type.Collection) Component(lucee.runtime.Component) Property(lucee.runtime.component.Property) 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