Example usage for org.apache.commons.jxpath JXPathContext newContext

List of usage examples for org.apache.commons.jxpath JXPathContext newContext

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext newContext.

Prototype

public static JXPathContext newContext(Object contextBean) 

Source Link

Document

Creates a new JXPathContext with the specified object as the root node.

Usage

From source file:org.jboss.dashboard.factory.Component.java

protected Object makeAndSetNewInstance() {
    LookupHelper helper = ComponentsContextManager.getLookupHelper();
    synchronized (helper.getSynchronizationObject(getScope())) {
        Object object = null;/*w w  w.  java 2s . c  om*/
        if (status == STATUS_VALID) {
            if (log.isDebugEnabled())
                log.debug("Making and initializing new " + clazz);

            if (scope.equals(SCOPE_GLOBAL)) {
                try {
                    object = componentClass.getMethod("getInstance", new Class[0]).invoke(null, new Object[0]);
                } catch (Exception e) {
                    if (log.isDebugEnabled())
                        log.debug("Error using getInstance() method on " + clazz);
                }
            }

            if (object == null)
                try {
                    object = componentClass.getConstructor(new Class[0]).newInstance(new Object[0]);
                } catch (Exception e) {
                    log.error("Error creating instance for component " + name + " :", e);
                }
            //End. Object should be created now
            if (object == null) {
                status = STATUS_INVALID;
                log.error("Make sure the component class has a default public constructor"
                        + (scope.equals(SCOPE_GLOBAL) ? ", or a static getInstance() method." : "."));
            } else {
                if (object instanceof FactoryLifecycle) {
                    try {
                        if (object instanceof BasicFactoryElement) {
                            ((BasicFactoryElement) object).setComponentName(name);
                            ((BasicFactoryElement) object).setComponentScope(scope);
                            ((BasicFactoryElement) object).setComponentDescription(description);
                            ((BasicFactoryElement) object).setComponentAlias(alias);
                        }
                        ((FactoryLifecycle) object).init();
                        ((FactoryLifecycle) object).stop();
                    } catch (Exception e) {
                        log.error("Error in component lifecycle ", e);
                    }
                }
                setTheInstance(object);
                JXPathContext ctx = JXPathContext.newContext(object);
                for (Iterator it = componentConfiguredProperties.keySet().iterator(); it.hasNext();) {
                    String propertyName = (String) it.next();
                    List propertyValue = (List) componentConfiguredProperties.get(propertyName);
                    try {
                        setObjectProperty(object, propertyName, propertyValue, ctx);
                    } catch (Exception e) {
                        log.error("Error. Cannot set property " + getName() + "." + propertyName
                                + " with configured values " + propertyValue, e);
                    }
                }
                status = STATUS_VALID;
                if (object instanceof FactoryLifecycle) {
                    try {
                        ((FactoryLifecycle) object).start();
                    } catch (Exception e) {
                        log.error("Error in component lifecycle ", e);
                    }
                }
                setCreationOrderNumber(getTree().getNewOrderCounter());
            }
        } else {
            log.error("Infinite loop detected. Caused by component " + name
                    + " or some other component used by it.");
        }
        return object;
    }
}

From source file:org.jboss.dashboard.factory.Component.java

protected Object setObjectProperty(Object obj, String propertyName, List propertyValue, JXPathContext ctx)
        throws Exception {
    if (log.isDebugEnabled())
        log.debug("Setting in " + clazz + " property " + propertyName + " with values " + propertyValue);
    if (obj instanceof Map) {
        ((Map) obj).put(propertyName, getValueForProperty(propertyValue, String.class)); //Map of Strings
    } else if (obj instanceof List) {
        try {//from   w  w w  .  j a v  a2 s  . c om
            int index = Integer.parseInt(propertyName);
            while (((List) obj).size() <= index)
                ((List) obj).add(null);
            ((List) obj).set(index, getValueForProperty(propertyValue, String.class)); //List of Strings
        } catch (Exception e) {
            log.error("Error setting position " + propertyName + " in List " + name + ". ", e);
        }
    } else if (propertyName.indexOf('.') != -1) { //Set it by JXPath, valid only for strings
        ctx = ctx != null ? ctx : JXPathContext.newContext(obj);
        ctx.setValue(propertyName.replace('.', '/'), getValueForProperty(propertyValue, String.class));
    } else {
        //Find a Field
        Field field = getField(obj, propertyName);
        if (field != null) {
            Class fieldClass = field.getType();
            Object value = getValueForProperty(propertyValue, fieldClass);
            field.set(obj, value);
            return obj;
        }

        // Find a getter and setter.
        Method getter = getGetter(obj, propertyName);
        if (getter == null) {
            log.error("Cannot find a getter for property " + propertyName + " in class " + clazz
                    + ". Ignoring property.");
            return obj;
        }
        Method setter = getSetter(obj, getter, propertyName);
        if (setter == null) {
            log.error("Cannot find a setter for property " + propertyName + " in class " + clazz
                    + ". Ignoring property.");
            return obj;
        }
        Object value = getValueForProperty(propertyValue, getter.getReturnType());
        if (log.isDebugEnabled())
            log.debug("Invoking " + setter + " with value=" + value);
        setter.invoke(obj, new Object[] { value });
    }
    return obj;
}

From source file:org.jboss.dashboard.ui.formatters.ForComparator.java

private Object getObjectProperty(Object object, String property) {
    JXPathContext ctx = JXPathContext.newContext(object);
    try {/*from  ww w.j  a  va2 s .  c  o m*/
        return ctx.getValue(property);
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Invalid property '" + property + "' ", e);
    }
    return null;
}

From source file:org.jboss.dashboard.ui.formatters.ForFormatter.java

public void service(HttpServletRequest request, HttpServletResponse response) throws FormatterException {
    log.debug("Servicing ForFormatter.");
    Object array = getParameter("array");
    if (array == null) {
        Object componentName = getParameter("factoryElement");
        Object propertyName = getParameter("property");
        if (componentName != null) {
            Object component = CDIBeanLocator.getBeanByNameOrType((String) componentName);
            array = component;// w  w  w  .  j  av a 2 s.c om
            if (propertyName != null) {
                JXPathContext ctx = JXPathContext.newContext(component);
                try {
                    array = ctx.getValue((String) propertyName);
                } catch (Exception e) {
                    log.debug("Error:", e);
                }
            }
        }
    }
    String sortProperties = (String) getParameter("sortProperties");

    Iterator iterator = null;
    if (array == null) {
        renderFragment("empty");
        return;
    }

    if (array instanceof Collection) {
        iterator = ((Collection) array).iterator();
    } else if (array.getClass().isArray()) {
        final Object theArray = array;
        iterator = new Iterator() {
            int index = 0;

            public void remove() {
                throw new UnsupportedOperationException();
            }

            public boolean hasNext() {
                return Array.getLength(theArray) > index;
            }

            public Object next() {
                return Array.get(theArray, index++);
            }
        };
    } else if (array instanceof Iterator) {
        iterator = (Iterator) array;
    } else if (array instanceof Enumeration) {
        List l = new ArrayList();
        while (((Enumeration) array).hasMoreElements()) {
            l.add(((Enumeration) array).nextElement());
        }
        iterator = l.iterator();
    }

    if (sortProperties != null) {
        iterator = getSortedIterator(iterator, sortProperties);
    }

    if (iterator != null && iterator.hasNext()) {
        renderFragment("outputStart");
        int i = 0;
        while (iterator.hasNext()) {
            Object o = iterator.next();
            setAttribute("index", new Integer(i));
            setAttribute("count", new Integer(++i));
            if (o != null)
                setAttribute("element", o);
            else
                setAttribute("element", getParameter("nullValue"));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    } else {
        renderFragment("empty");
    }
}

From source file:org.jboss.dashboard.ui.taglib.factory.PropertyTag.java

protected Object getValue() {
    if (value != null)
        return value;

    Object beanObject = CDIBeanLocator.getBeanByNameOrType(getBeanName());
    if (beanObject != null) {
        JXPathContext ctx = JXPathContext.newContext(beanObject);
        try {/*from w ww .j av a2 s . co  m*/
            return value = ctx.getValue(getProperty());
        } catch (JXPathException jxpe) {
            log.warn("Can't read property " + getProperty() + " in " + getBeanName() + ": ", jxpe);
        }
    }
    return null;
}

From source file:org.jboss.dashboard.ui.taglib.formatter.ForFormatter.java

public void service(HttpServletRequest request, HttpServletResponse response) throws FormatterException {
    log.debug("Servicing ForFormatter.");
    Object array = getParameter("array");
    if (array == null) {
        Object componentName = getParameter("factoryElement");
        Object propertyName = getParameter("property");
        if (componentName != null) {
            Object component = Factory.lookup((String) componentName);
            array = component;/*from  www.  j a va 2s  . co m*/
            if (propertyName != null) {
                JXPathContext ctx = JXPathContext.newContext(component);
                try {
                    array = ctx.getValue((String) propertyName);
                } catch (Exception e) {
                    log.debug("Error:", e);
                }
            }
        }
    }
    String sortProperties = (String) getParameter("sortProperties");

    Iterator iterator = null;
    if (array == null) {
        renderFragment("empty");
        return;
    }

    if (array instanceof Collection) {
        iterator = ((Collection) array).iterator();
    } else if (array.getClass().isArray()) {
        final Object theArray = array;
        iterator = new Iterator() {
            int index = 0;

            public void remove() {
                throw new UnsupportedOperationException();
            }

            public boolean hasNext() {
                return Array.getLength(theArray) > index;
            }

            public Object next() {
                return Array.get(theArray, index++);
            }
        };
    } else if (array instanceof Iterator) {
        iterator = (Iterator) array;
    } else if (array instanceof Enumeration) {
        List l = new ArrayList();
        while (((Enumeration) array).hasMoreElements()) {
            l.add(((Enumeration) array).nextElement());
        }
        iterator = l.iterator();
    }

    if (sortProperties != null) {
        iterator = getSortedIterator(iterator, sortProperties);
    }

    if (iterator != null && iterator.hasNext()) {
        renderFragment("outputStart");
        int i = 0;
        while (iterator.hasNext()) {
            Object o = iterator.next();
            setAttribute("index", new Integer(i));
            setAttribute("count", new Integer(++i));
            if (o != null)
                setAttribute("element", o);
            else
                setAttribute("element", getParameter("nullValue"));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    } else {
        renderFragment("empty");
    }
}

From source file:org.jboss.dashboard.ui.taglib.formatter.FragmentTag.java

public Object getParam(String name) {
    FormatterTag parent = (FormatterTag) getParent();
    Object value = parent.getFragmentParams().get(name);
    if (value == null && parent.getFormaterTagDynamicAttributesInterpreter() != null) {
        value = parent.getFormaterTagDynamicAttributesInterpreter().getValueForParameter(name);
    }/*ww w .ja v a  2  s .  co m*/
    if (value == null && name.indexOf('/') != -1)
        try {
            log.debug("Attempt JXPath detection of param...");
            JXPathContext ctx = JXPathContext.newContext(parent.getFragmentParams());
            ctx.setLenient(false);
            value = ctx.getValue(name);
            if (value == null && parent.getFormaterTagDynamicAttributesInterpreter() != null) {
                String firstName = name.substring(0, name.indexOf('/'));
                Object firstValue = parent.getFormaterTagDynamicAttributesInterpreter()
                        .getValueForParameter(firstName);
                if (firstValue != null) {
                    ctx = JXPathContext.newContext(firstValue);
                    ctx.setLenient(false);
                    value = ctx.getValue(name.substring(name.indexOf('/') + 1));
                }
            }
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.warn("Error getting attribute " + value + " in params.");
        }
    return value;
}

From source file:org.jboss.dashboard.ui.taglib.PropertyReadTag.java

protected Object getPropertyValue() {
    log.debug("Getting property " + property + " from " + object);
    Object subjectOfTheGetter = null;
    if ("workspace".equalsIgnoreCase(object)) {
        subjectOfTheGetter = NavigationManager.lookup().getCurrentWorkspace();
    } else if ("section".equalsIgnoreCase(object)) {
        subjectOfTheGetter = NavigationManager.lookup().getCurrentSection();
    } else if ("panel".equalsIgnoreCase(object)) {
        subjectOfTheGetter = RequestContext.lookup().getActivePanel();
    } else if ("request".equalsIgnoreCase(object)) {
        subjectOfTheGetter = RequestContext.lookup().getRequest();
    } else if ("session".equalsIgnoreCase(object)) {
        subjectOfTheGetter = RequestContext.lookup().getRequest().getSessionObject();
    } else {//from  w w w  . j  a  v  a  2s .c o  m
        log.warn("Invalid object to get property from: " + object);
    }
    if (subjectOfTheGetter == null) {
        log.debug("Cannot get current " + object);
        return null;
    }
    try {
        Object val = JXPathContext.newContext(subjectOfTheGetter).getValue(property);
        if (localize != null && localize.booleanValue() && val instanceof Map) {
            return StringEscapeUtils.ESCAPE_ECMASCRIPT
                    .translate(StringEscapeUtils.ESCAPE_HTML4.translate((String) (localize((Map) val))));
        }
        return StringEscapeUtils.ESCAPE_ECMASCRIPT
                .translate(StringEscapeUtils.ESCAPE_HTML4.translate((String) val));
    } catch (Exception e) {
        log.warn("Error accessing property " + property + " in " + object + "." + e);
        return null;
    }
}

From source file:org.jbpm.formModeler.core.processing.impl.FormProcessorImpl.java

protected Object getUnbindedFieldValue(String bindingExpression, Map<String, Object> bindingData) {
    if (bindingExpression.indexOf("/") != -1) {
        try {// w ww  .j av a2  s .  c o  m
            String root = bindingExpression.substring(0, bindingExpression.indexOf("/"));
            String expression = bindingExpression.substring(root.length() + 1);

            Object object = bindingData.get(root);
            JXPathContext ctx = JXPathContext.newContext(object);
            return ctx.getValue(expression);
        } catch (Exception e) {
            log.warn("Error getting value for xpath xpression '{}': {}", bindingExpression, e);
        }
    }
    return bindingData.get(bindingExpression);
}

From source file:org.jbpm.formModeler.service.bb.mvc.taglib.factory.PropertyTag.java

protected Object getValue() throws Exception {
    if (value != null)
        return value;
    Object beanObject = CDIBeanLocator.getBeanByNameOrType(getBean());
    if (beanObject != null) {
        JXPathContext ctx = JXPathContext.newContext(beanObject);
        try {/*from  ww  w.jav  a  2  s . c om*/
            return value = ctx.getValue(getProperty());
        } catch (JXPathException jxpe) {
            if (log.isDebugEnabled()) {
                log.debug("Can't read property " + getProperty() + " in " + getBean() + ": ", jxpe);
            }
        }
    }
    return null;
}