/** * Call a class constructor with an String parameter * @param className the name of the class to construct * @param parameter to be used for the construction of the class instance * @return an instance of the class * @throws JMeterException if class cannot be created */ public static Object construct(String className, String parameter) throws JMeterException { Object instance = null; try { Class<?> clazz = Class.forName(className); Constructor<?> constructor = clazz.getConstructor(String.class); instance = constructor.newInstance(parameter); } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InstantiationException e) { throw new JMeterException(e); } return instance; }
@Override public void threadStarted() { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.evalNoLog("threadStarted()"); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
/** * {@inheritDoc} */ @Override public long delay() { String ret="0"; final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter(); if (bshInterpreter == null) { log.error("BeanShell not found"); return 0; } try { Object o = processFileOrScript(bshInterpreter); if (o != null) { ret=o.toString(); } } catch (JMeterException e) { if (log.isWarnEnabled()) { log.warn("Problem in BeanShell script. {}", e.toString()); } } try { return Long.decode(ret).longValue(); } catch (NumberFormatException e){ log.warn("Number format exception while decoding number: '{}'", ret); return 0; } }
@Override public void process(){ final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter(); if (bshInterpreter == null) { log.error("BeanShell not found"); return; } JMeterContext jmctx = JMeterContextService.getContext(); Sampler sam = jmctx.getCurrentSampler(); try { // Add variables for access to context and variables bshInterpreter.set("sampler", sam);//$NON-NLS-1$ processFileOrScript(bshInterpreter); } catch (JMeterException e) { if (log.isWarnEnabled()) { log.warn("Problem in BeanShell script. {}", e.toString()); } } }
/** * Call no-args constructor for a class. * * @param className name of the class to be constructed * @return an instance of the class * @throws JMeterException if class cannot be created */ public static Object construct(String className) throws JMeterException { Object instance = null; try { instance = ClassUtils.getClass(className).newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { throw new JMeterException(e); } return instance; }
@Override public void testStarted() { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.evalNoLog("testStarted()"); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
@Override public void sampleOccurred(SampleEvent se) { final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter(); if (bshInterpreter == null) { log.error("BeanShell not found"); return; } SampleResult samp=se.getResult(); try { bshInterpreter.set("sampleEvent", se);//$NON-NLS-1$ bshInterpreter.set("sampleResult", samp);//$NON-NLS-1$ processFileOrScript(bshInterpreter); } catch (JMeterException e) { if (log.isWarnEnabled()) { log.warn("Problem in BeanShell script. {}", e.toString()); } } }
/** * Call a class constructor with an integer parameter * @param className name of the class to be constructed * @param parameter the value to be used in the constructor * @return an instance of the class * @throws JMeterException if class cannot be created */ public static Object construct(String className, int parameter) throws JMeterException { Object instance = null; try { Class<?> clazz = ClassUtils.getClass(className); Constructor<?> constructor = clazz.getConstructor(Integer.TYPE); instance = constructor.newInstance(Integer.valueOf(parameter)); } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | NoSuchMethodException | SecurityException | IllegalAccessException | InstantiationException e) { throw new JMeterException(e); } return instance; }
@Override public void testEnded() { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.evalNoLog("testEnded()"); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
@Override public void process() { JMeterContext jmctx = JMeterContextService.getContext(); SampleResult prev = jmctx.getPreviousResult(); if (prev == null) { return; // TODO - should we skip processing here? } final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter(); if (bshInterpreter == null) { log.error("BeanShell not found"); return; } try { // Add variables for access to context and variables bshInterpreter.set("data", prev.getResponseData());//$NON-NLS-1$ processFileOrScript(bshInterpreter); } catch (JMeterException e) { if (log.isWarnEnabled()) { log.warn("Problem in BeanShell script: {}", e.toString()); } } }
private Object bshInvoke(Method m, Object[] o, boolean shouldLog) throws JMeterException { Object r = null; final String errorString = "Error invoking bsh method: "; try { r = m.invoke(bshInstance, o); } catch (IllegalArgumentException | IllegalAccessException e) { // Programming error final String message = errorString + m.getName(); log.error(message); throw new JMeterError(message, e); } catch (InvocationTargetException e) { // Can occur at run-time // could be caused by the bsh Exceptions: // EvalError, ParseException or TargetError String message = errorString + m.getName(); Throwable cause = e.getCause(); if (cause != null) { message += "\t" + cause.getLocalizedMessage(); } if (shouldLog) { log.error(message); } throw new JMeterException(message, e); } return r; }
@Override public void threadFinished() { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.evalNoLog("threadFinished()"); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
/** * Invoke a public method on a class instance * * @param instance * object on which the method should be called * @param methodName * name of the method to be called * @throws SecurityException * if a security violation occurred while looking for the method * @throws IllegalArgumentException * if the method parameters (none given) do not match the * signature of the method * @throws JMeterException * if something went wrong in the invoked method */ public static void invoke(Object instance, String methodName) throws SecurityException, IllegalArgumentException, JMeterException { Method m; try { m = ClassUtils.getPublicMethod(instance.getClass(), methodName, new Class [] {}); m.invoke(instance, (Object [])null); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new JMeterException(e); } } }
@Override public void testEnded(String host) { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.eval((new StringBuilder("testEnded(\"")) // $NON-NLS-1$ .append(host) .append("\")") // $NON-NLS-1$ .toString()); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
if (ba.length == 0) { log.debug("{} Empty request, ignored", port); throw new JMeterException(); // hack to skip processing throw new JMeterException(); // hack to skip processing throw new JMeterException(); // hack to skip processing log.warn("{} Empty response to http over SSL. Probably waiting for user to authorize the certificate for {}", port, request.getUrl()); throw new JMeterException(); // hack to skip processing
@Override public void testStarted(String host) { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.eval((new StringBuilder("testStarted(\"")) // $NON-NLS-1$ .append(host) .append("\")") // $NON-NLS-1$ .toString()); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
throw new JMeterException("No Parser available");
@Override public boolean interrupt() { if (savedBsh != null) { try { savedBsh.evalNoLog("interrupt()"); // $NON-NLS-1$ } catch (JMeterException ignored) { if (log.isDebugEnabled()) { log.debug("{} : {}", getClass(), ignored.getLocalizedMessage()); // $NON-NLS-1$ } } return true; } return false; }