use of java.lang.reflect.InvocationTargetException in project sonarqube by SonarSource.
the class TagUtils method lookup.
/**
* Locate and return the specified property of the specified bean, from an
* optionally specified scope, in the specified page context. If an
* exception is thrown, it will have already been saved via a call to
* <code>saveException()</code>.
*
* @param pageContext Page context to be searched
* @param name Name of the bean to be retrieved
* @param property Name of the property to be retrieved, or
* <code>null</code> to retrieve the bean itself
* @param scope Scope to be searched (page, request, session,
* application) or <code>null</code> to use
* <code>findAttribute()</code> instead
* @return property of specified JavaBean
* @throws JspException if an invalid scope name is requested
* @throws JspException if the specified bean is not found
* @throws JspException if accessing this property causes an
* IllegalAccessException, IllegalArgumentException,
* InvocationTargetException, or NoSuchMethodException
*/
public Object lookup(PageContext pageContext, String name, String property, String scope) throws JspException {
// Look up the requested bean, and return if requested
Object bean = lookup(pageContext, name, scope);
if (bean == null) {
JspException e = null;
if (scope == null) {
e = new JspException(messages.getMessage("lookup.bean.any", name));
} else {
e = new JspException(messages.getMessage("lookup.bean", name, scope));
}
saveException(pageContext, e);
throw e;
}
if (property == null) {
return bean;
}
// Locate and return the specified property
try {
return PropertyUtils.getProperty(bean, property);
} catch (IllegalAccessException e) {
saveException(pageContext, e);
throw new JspException(messages.getMessage("lookup.access", property, name));
} catch (IllegalArgumentException e) {
saveException(pageContext, e);
throw new JspException(messages.getMessage("lookup.argument", property, name));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null) {
t = e;
}
saveException(pageContext, t);
throw new JspException(messages.getMessage("lookup.target", property, name));
} catch (NoSuchMethodException e) {
saveException(pageContext, e);
String beanName = name;
// its class name for the exception message.
if (Constants.BEAN_KEY.equals(name)) {
Object obj = pageContext.findAttribute(Constants.BEAN_KEY);
if (obj != null) {
beanName = obj.getClass().getName();
}
}
throw new JspException(messages.getMessage("lookup.method", property, beanName));
}
}
use of java.lang.reflect.InvocationTargetException in project sonarqube by SonarSource.
the class OptionsTag method doEndTag.
/**
* Process the end of this tag.
*
* @throws JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException {
// Acquire the select tag we are associated with
SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
if (selectTag == null) {
throw new JspException(messages.getMessage("optionsTag.select"));
}
StringBuffer sb = new StringBuffer();
// If a collection was specified, use that mode to render options
if (collection != null) {
Iterator collIterator = getIterator(collection, null);
while (collIterator.hasNext()) {
Object bean = collIterator.next();
Object value = null;
Object label = null;
try {
value = PropertyUtils.getProperty(bean, property);
if (value == null) {
value = "";
}
} catch (IllegalAccessException e) {
throw new JspException(messages.getMessage("getter.access", property, collection));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(messages.getMessage("getter.result", property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(messages.getMessage("getter.method", property, collection));
}
try {
if (labelProperty != null) {
label = PropertyUtils.getProperty(bean, labelProperty);
} else {
label = value;
}
if (label == null) {
label = "";
}
} catch (IllegalAccessException e) {
throw new JspException(messages.getMessage("getter.access", labelProperty, collection));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(messages.getMessage("getter.result", labelProperty, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(messages.getMessage("getter.method", labelProperty, collection));
}
String stringValue = value.toString();
addOption(sb, stringValue, label.toString(), selectTag.isMatched(stringValue));
}
} else // Otherwise, use the separate iterators mode to render options
{
// Construct iterators for the values and labels collections
Iterator valuesIterator = getIterator(name, property);
Iterator labelsIterator = null;
if ((labelName != null) || (labelProperty != null)) {
labelsIterator = getIterator(labelName, labelProperty);
}
// Render the options tags for each element of the values coll.
while (valuesIterator.hasNext()) {
Object valueObject = valuesIterator.next();
if (valueObject == null) {
valueObject = "";
}
String value = valueObject.toString();
String label = value;
if ((labelsIterator != null) && labelsIterator.hasNext()) {
Object labelObject = labelsIterator.next();
if (labelObject == null) {
labelObject = "";
}
label = labelObject.toString();
}
addOption(sb, value, label, selectTag.isMatched(value));
}
}
TagUtils.getInstance().write(pageContext, sb.toString());
return EVAL_PAGE;
}
use of java.lang.reflect.InvocationTargetException in project sonarqube by SonarSource.
the class OptionsTag method getIterator.
/**
* Return an iterator for the option labels or values, based on our
* configured properties.
*
* @param name Name of the bean attribute (if any)
* @param property Name of the bean property (if any)
* @throws JspException if an error occurs
*/
protected Iterator getIterator(String name, String property) throws JspException {
// Identify the bean containing our collection
String beanName = name;
if (beanName == null) {
beanName = Constants.BEAN_KEY;
}
Object bean = TagUtils.getInstance().lookup(pageContext, beanName, null);
if (bean == null) {
throw new JspException(messages.getMessage("getter.bean", beanName));
}
// Identify the collection itself
Object collection = bean;
if (property != null) {
try {
collection = PropertyUtils.getProperty(bean, property);
if (collection == null) {
throw new JspException(messages.getMessage("getter.property", property));
}
} catch (IllegalAccessException e) {
throw new JspException(messages.getMessage("getter.access", property, name));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(messages.getMessage("getter.result", property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(messages.getMessage("getter.method", property, name));
}
}
// Construct and return an appropriate iterator
if (collection.getClass().isArray()) {
collection = Arrays.asList((Object[]) collection);
}
if (collection instanceof Collection) {
return (((Collection) collection).iterator());
} else if (collection instanceof Iterator) {
return ((Iterator) collection);
} else if (collection instanceof Map) {
return (((Map) collection).entrySet().iterator());
} else if (collection instanceof Enumeration) {
return new IteratorAdapter((Enumeration) collection);
} else {
throw new JspException(messages.getMessage("optionsTag.iterator", collection.toString()));
}
}
use of java.lang.reflect.InvocationTargetException in project sonarqube by SonarSource.
the class SelectTag method calculateMatchValues.
/**
* Calculate the match values we will actually be using.
*
* @throws JspException
*/
private void calculateMatchValues() throws JspException {
if (this.value != null) {
this.match = new String[1];
this.match[0] = this.value;
} else {
Object bean = TagUtils.getInstance().lookup(pageContext, name, null);
if (bean == null) {
JspException e = new JspException(messages.getMessage("getter.bean", name));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
try {
this.match = BeanUtils.getArrayProperty(bean, property);
if (this.match == null) {
this.match = new String[0];
}
} catch (IllegalAccessException e) {
TagUtils.getInstance().saveException(pageContext, e);
throw new JspException(messages.getMessage("getter.access", property, name));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
TagUtils.getInstance().saveException(pageContext, t);
throw new JspException(messages.getMessage("getter.result", property, t.toString()));
} catch (NoSuchMethodException e) {
TagUtils.getInstance().saveException(pageContext, e);
throw new JspException(messages.getMessage("getter.method", property, name));
}
}
}
use of java.lang.reflect.InvocationTargetException in project sonarqube by SonarSource.
the class CompareTagBase method condition.
/**
* Evaluate the condition that is being tested by this particular tag, and
* return <code>true</code> if the nested body content of this tag should
* be evaluated, or <code>false</code> if it should be skipped. This
* method must be implemented by concrete subclasses.
*
* @param desired1 First desired value for a true result (-1, 0, +1)
* @param desired2 Second desired value for a true result (-1, 0, +1)
* @throws JspException if a JSP exception occurs
*/
protected boolean condition(int desired1, int desired2) throws JspException {
// Acquire the value and determine the test type
int type = -1;
double doubleValue = 0.0;
long longValue = 0;
if ((type < 0) && (value.length() > 0)) {
try {
doubleValue = Double.parseDouble(value);
type = DOUBLE_COMPARE;
} catch (NumberFormatException e) {
;
}
}
if ((type < 0) && (value.length() > 0)) {
try {
longValue = Long.parseLong(value);
type = LONG_COMPARE;
} catch (NumberFormatException e) {
;
}
}
if (type < 0) {
type = STRING_COMPARE;
}
// Acquire the unconverted variable value
Object variable = null;
if (cookie != null) {
Cookie[] cookies = ((HttpServletRequest) pageContext.getRequest()).getCookies();
if (cookies == null) {
cookies = new Cookie[0];
}
for (int i = 0; i < cookies.length; i++) {
if (cookie.equals(cookies[i].getName())) {
variable = cookies[i].getValue();
break;
}
}
} else if (header != null) {
variable = ((HttpServletRequest) pageContext.getRequest()).getHeader(header);
} else if (name != null) {
Object bean = TagUtils.getInstance().lookup(pageContext, name, scope);
if (property != null) {
if (bean == null) {
JspException e = new JspException(messages.getMessage("logic.bean", name));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
try {
variable = PropertyUtils.getProperty(bean, property);
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null) {
t = e;
}
TagUtils.getInstance().saveException(pageContext, t);
throw new JspException(messages.getMessage("logic.property", name, property, t.toString()));
} catch (Throwable t) {
TagUtils.getInstance().saveException(pageContext, t);
throw new JspException(messages.getMessage("logic.property", name, property, t.toString()));
}
} else {
variable = bean;
}
} else if (parameter != null) {
variable = pageContext.getRequest().getParameter(parameter);
} else {
JspException e = new JspException(messages.getMessage("logic.selector"));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
if (variable == null) {
// Coerce null to a zero-length String
variable = "";
}
// Perform the appropriate comparison
int result = 0;
if (type == DOUBLE_COMPARE) {
try {
double doubleVariable = Double.parseDouble(variable.toString());
if (doubleVariable < doubleValue) {
result = -1;
} else if (doubleVariable > doubleValue) {
result = +1;
}
} catch (NumberFormatException e) {
result = variable.toString().compareTo(value);
}
} else if (type == LONG_COMPARE) {
try {
long longVariable = Long.parseLong(variable.toString());
if (longVariable < longValue) {
result = -1;
} else if (longVariable > longValue) {
result = +1;
}
} catch (NumberFormatException e) {
result = variable.toString().compareTo(value);
}
} else {
result = variable.toString().compareTo(value);
}
// Normalize the result
if (result < 0) {
result = -1;
} else if (result > 0) {
result = +1;
}
// Return true if the result matches either desired value
return ((result == desired1) || (result == desired2));
}
Aggregations