Example usage for org.apache.commons.lang ClassUtils getShortClassName

List of usage examples for org.apache.commons.lang ClassUtils getShortClassName

Introduction

In this page you can find the example usage for org.apache.commons.lang ClassUtils getShortClassName.

Prototype

public static String getShortClassName(String className) 

Source Link

Document

Gets the class name minus the package name from a String.

The string passed in is assumed to be a class name - it is not checked.

Usage

From source file:org.apache.click.ClickServlet.java

/**
 * Perform onPost or onGet event callback for the specified page.
 *
 * @param page the page for which the onGet or onPost is performed
 * @param context the request context/*www .  jav a2  s  . co  m*/
 * @param isPost specifies whether the request is a post or a get
 */
protected void performOnPostOrGet(Page page, Context context, boolean isPost) {
    if (isPost) {
        page.onPost();

        if (logger.isTraceEnabled()) {
            logger.trace("   invoked: " + ClassUtils.getShortClassName(page.getClass()) + ".onPost()");
        }

    } else {
        page.onGet();

        if (logger.isTraceEnabled()) {
            logger.trace("   invoked: " + ClassUtils.getShortClassName(page.getClass()) + ".onGet()");
        }
    }
}

From source file:org.apache.click.ClickServlet.java

/**
 * Perform onRender event callback for the specified page.
 *
 * @param page page to render//from w  w  w. j  a  va  2  s .c  o m
 * @param context the request context
 */
protected void performOnRender(Page page, Context context) {
    page.onRender();

    if (logger.isTraceEnabled()) {
        logger.trace("   invoked: " + ClassUtils.getShortClassName(page.getClass()) + ".onRender()");
    }

    if (page.hasControls()) {
        List<Control> controls = page.getControls();

        for (int i = 0, size = controls.size(); i < size; i++) {
            Control control = controls.get(i);
            control.onRender();

            if (logger.isTraceEnabled()) {
                String controlClassName = control.getClass().getName();
                controlClassName = controlClassName.substring(controlClassName.lastIndexOf('.') + 1);
                String msg = "   invoked: '" + control.getName() + "' " + controlClassName + ".onRender()";
                logger.trace(msg);
            }
        }
    }
}

From source file:org.apache.click.ClickServlet.java

/**
 * Process all Ajax target controls and return true if the page should continue
 * processing, false otherwise./*  w w w .ja  v a2s  . c  o  m*/
 *
 * @param context the request context
 * @param eventDispatcher the event dispatcher
 * @param controlRegistry the control registry
 * @return true if the page should continue processing, false otherwise
 */
protected boolean processAjaxTargetControls(Context context, ActionEventDispatcher eventDispatcher,
        ControlRegistry controlRegistry) {

    boolean continueProcessing = true;

    // Resolve the Ajax target control for this request
    Control ajaxTarget = resolveAjaxTargetControl(context, controlRegistry);

    if (ajaxTarget != null) {

        // Process the control
        if (!ajaxTarget.onProcess()) {
            continueProcessing = false;
        }

        // Log a trace if no behavior was registered after processing the control
        if (logger.isTraceEnabled()) {

            HtmlStringBuffer buffer = new HtmlStringBuffer();
            String controlClassName = ClassUtils.getShortClassName(ajaxTarget.getClass());
            buffer.append("   invoked: '");
            buffer.append(ajaxTarget.getName());
            buffer.append("' ").append(controlClassName);
            buffer.append(".onProcess() : ").append(continueProcessing);
            logger.trace(buffer.toString());

            if (!eventDispatcher.hasAjaxBehaviorSourceSet()) {
                logger.trace("   *no* AjaxBehavior was registered while processing the control");
            }
        }
    }

    return continueProcessing;
}

From source file:org.apache.click.ClickServlet.java

/**
 * Resolve and return the Ajax target control for this request or null if no
 * Ajax target was found.//from   ww  w . j a  v a  2  s . c  om
 *
 * @param context the request context
 * @param controlRegistry the control registry
 * @return the target Ajax target control or null if no Ajax target was found
 */
private Control resolveAjaxTargetControl(Context context, ControlRegistry controlRegistry) {

    Control ajaxTarget = null;

    if (logger.isTraceEnabled()) {
        logger.trace("   the following controls have been registered as potential Ajax targets:");
        if (controlRegistry.hasAjaxTargetControls()) {
            for (Control control : controlRegistry.getAjaxTargetControls()) {
                HtmlStringBuffer buffer = new HtmlStringBuffer();
                String controlClassName = ClassUtils.getShortClassName(control.getClass());
                buffer.append("      ").append(controlClassName);
                buffer.append(": name='").append(control.getName()).append("'");
                logger.trace(buffer.toString());
            }
        } else {
            logger.trace("      *no* control has been registered");
        }
    }

    for (Control control : controlRegistry.getAjaxTargetControls()) {

        if (control.isAjaxTarget(context)) {
            ajaxTarget = control;
            // The first matching control will be processed. Multiple matching
            // controls are not supported
            break;
        }
    }

    if (logger.isTraceEnabled()) {
        if (ajaxTarget == null) {
            String msg = "   *no* target control was found for the Ajax request";
            logger.trace(msg);

        } else {
            HtmlStringBuffer buffer = new HtmlStringBuffer();
            buffer.append("   invoked: '");
            buffer.append(ajaxTarget.getName()).append("' ");
            String className = ClassUtils.getShortClassName(ajaxTarget.getClass());
            buffer.append(className);
            buffer.append(".isAjaxTarget() : true (Ajax target control found)");
            logger.trace(buffer.toString());
        }
    }

    return ajaxTarget;
}

From source file:org.apache.click.util.ClickUtils.java

/**
 * Deploys required files (from a file list) for a control that repsects a specific convention.
 * <p/>/* www  . ja v a  2s.c om*/
 * <b>Convention:</b>
 * <p/>
 * There's a descriptor file generated by the <code>tools/standalone/dev-tasks/ListFilesTask</code>.
 * The files to deploy are all in a subdirectory placed in the same directory with the control.
 * See documentation for more details. <p/>
 *
 * <b>Usage:</b><p/>
 * In your Control simply use the code below, and everything should work automatically.
 * <pre class="prettyprint">
 * public void onDeploy(ServletContext servletContext) {
 *    ClickUtils.deployFileList(servletContext, HeavyControl.class, "click");
 * } </pre>
 *
 * @param servletContext the web applications servlet context
 * @param controlClass the class of the Control that has files for deployment
 * @param targetDir target directory where to deploy the files to. In most cases this
 * is only the reserved directory <code>click</code>
 */
public static void deployFileList(ServletContext servletContext, Class<? extends Control> controlClass,
        String targetDir) {

    String packageName = ClassUtils.getPackageName(controlClass);
    packageName = StringUtils.replaceChars(packageName, '.', '/');
    packageName = "/" + packageName;
    String controlName = ClassUtils.getShortClassName(controlClass);

    ConfigService configService = getConfigService(servletContext);
    LogService logService = configService.getLogService();
    String descriptorFile = packageName + "/" + controlName + ".files";
    logService.debug("Use deployment descriptor file:" + descriptorFile);

    try {
        InputStream is = getResourceAsStream(descriptorFile, ClickUtils.class);
        List fileList = IOUtils.readLines(is);
        if (fileList == null || fileList.isEmpty()) {
            logService.info("there are no files to deploy for control " + controlClass.getName());
            return;
        }

        // a target dir list is required cause the ClickUtils.deployFile() is too inflexible to autodetect
        // required subdirectories.
        List<String> targetDirList = new ArrayList<String>(fileList.size());
        for (int i = 0; i < fileList.size(); i++) {
            String filePath = (String) fileList.get(i);
            String destination = "";
            int index = filePath.lastIndexOf('/');
            if (index != -1) {
                destination = filePath.substring(0, index + 1);
            }
            targetDirList.add(i, targetDir + "/" + destination);
            fileList.set(i, packageName + "/" + filePath);
        }

        for (int i = 0; i < fileList.size(); i++) {
            String source = (String) fileList.get(i);
            String targetDirName = targetDirList.get(i);
            ClickUtils.deployFile(servletContext, source, targetDirName);
        }

    } catch (IOException e) {
        String msg = "error occurred getting resource " + descriptorFile + ", error " + e;
        logService.warn(msg);
    }
}

From source file:org.apache.click.util.ClickUtils.java

/**
 * Remove the control state from the session for the given stateful control,
 * control name and request context.//from w  w  w.  j  ava 2 s  . co m
 *
 * @param control the stateful control which state to remove
 * @param controlName the name of the control which state to remove
 * @param context the request context
 */
public static void removeState(Stateful control, String controlName, Context context) {
    if (control == null) {
        throw new IllegalStateException("Control cannot be null.");
    }
    if (controlName == null) {
        throw new IllegalStateException(ClassUtils.getShortClassName(control.getClass())
                + " name has not been set. State cannot be removed until the name is set");
    }
    if (context == null) {
        throw new IllegalStateException("Context cannot be null.");
    }

    String resourcePath = context.getResourcePath();
    Map pageMap = ClickUtils.getPageState(resourcePath, context);
    if (pageMap != null) {
        Object pop = pageMap.remove(controlName);

        if (pageMap.isEmpty()) {
            // If this was the last state for the page, remove the page state map
            context.removeSessionAttribute(resourcePath);
        } else {
            // Check if control state was emoved
            if (pop != null) {
                // If control state was removed, set session attribute to force
                // session replication in a cluster
                context.setSessionAttribute(resourcePath, pageMap);
            }
        }
    }
}

From source file:org.apache.click.util.ClickUtils.java

/**
 * Restore the control state from the session for the given stateful control,
 * control name and request context./*from   w w w.java  2s.c  o m*/
 * <p/>
 * This method delegates to {@link org.apache.click.Stateful#setState(java.lang.Object)}
 * to restore the control state.
 *
 * @param control the stateful control which state to restore
 * @param controlName the name of the control which state to restore
 * @param context the request context
 */
public static void restoreState(Stateful control, String controlName, Context context) {
    if (control == null) {
        throw new IllegalStateException("Control cannot be null.");
    }
    if (controlName == null) {
        throw new IllegalStateException(ClassUtils.getShortClassName(control.getClass())
                + " name has not been set. State cannot be restored until the name is set");
    }
    if (context == null) {
        throw new IllegalStateException("Context cannot be null.");
    }

    String resourcePath = context.getResourcePath();
    Map pageMap = ClickUtils.getPageState(resourcePath, context);
    if (pageMap != null) {
        control.setState(pageMap.get(controlName));
    }
}

From source file:org.apache.click.util.ClickUtils.java

/**
 * Save the control state in the session for the given stateful control,
 * control name and request context.//from   w w w. java2 s .  c  o  m
 * <p/>
 * * This method delegates to {@link org.apache.click.Stateful#getState()}
 * to retrieve the control state to save.
 *
 * @param control the stateful control which state to save
 * @param controlName the name of the control control which state to save
 * @param context the request context
 */
public static void saveState(Stateful control, String controlName, Context context) {
    if (control == null) {
        throw new IllegalStateException("Control cannot be null.");
    }
    if (controlName == null) {
        throw new IllegalStateException(ClassUtils.getShortClassName(control.getClass())
                + " name has not been set. State cannot be saved until the name is set");
    }
    if (context == null) {
        throw new IllegalStateException("Context cannot be null.");
    }

    String resourcePath = context.getResourcePath();
    Map pageMap = getOrCreatePageState(resourcePath, context);
    Object state = control.getState();
    if (state == null) {
        // Set null state to see if it differs from previous state
        Object pop = pageMap.put(controlName, state);
        if (pop != null) {
            // Previous state differs from current state, so set the
            // session attribute to force session replication in a cluster
            context.setSessionAttribute(resourcePath, pageMap);
        }
    } else {
        pageMap.put(controlName, state);
        // After control state has been added to the page state, set the
        // session attribute to force session replication in a cluster
        context.setSessionAttribute(resourcePath, pageMap);
    }
}

From source file:org.apache.click.util.ContainerUtils.java

/**
 * Populate the given object attributes from the Containers field values.
 * <p/>/* w w w.j av  a 2 s .c om*/
 * If a Field and object attribute matches, the object attribute is set to
 * the Object returned from the method
 * {@link org.apache.click.control.Field#getValueObject()}. If an object
 * attribute is a primitive, the Object returned from
 * {@link org.apache.click.control.Field#getValueObject()} will be converted
 * into the specific primitive e.g. Integer will become int and Boolean will
 * become boolean.
 * <p/>
 * The fieldList specifies which fields to copy to the object. This allows
 * one to include or exclude certain Container fields before populating the
 * object.
 * <p/>
 * The following example shows how to exclude disabled fields from
 * populating a customer object:
 * <pre class="prettyprint">
 * public void onInit() {
 *     List formFields = new ArrayList();
 *     for(Iterator it = form.getFieldList().iterator(); it.hasNext(); ) {
 *         Field field = (Field) formFields.next();
 *         // Exclude disabled fields
 *         if (!field.isDisabled()) {
 *             formFields.add(field);
 *         }
 *     }
 *     Customer customer = new Customer();
 *     ContainerUtils.copyContainerToObject(form, customer, formFields);
 * }
 * </pre>
 *
 * The specified Object can either be a POJO (plain old java object) or
 * a {@link java.util.Map}. If a POJO is specified, its attributes are
 * populated from  matching container fields. If a map is specified, its
 * key/value pairs are populated from matching container fields.
 *
 * @param container the fieldList Container
 * @param object the object to populate with field values
 * @param fieldList the list of fields to obtain values from
 *
 * @throws IllegalArgumentException if container, object or fieldList is
 * null
 */
public static void copyContainerToObject(Container container, Object object, List<Field> fieldList) {

    if (container == null) {
        throw new IllegalArgumentException("Null container parameter");
    }

    if (object == null) {
        throw new IllegalArgumentException("Null object parameter");
    }

    if (fieldList == null) {
        throw new IllegalArgumentException("Null fieldList parameter");
    }

    if (fieldList.isEmpty()) {
        LogService logService = ClickUtils.getLogService();
        if (logService.isDebugEnabled()) {
            String containerClassName = ClassUtils.getShortClassName(container.getClass());
            logService.debug("   " + containerClassName + " has no fields to copy from");
        }
        //Exit early.
        return;
    }

    String objectClassname = object.getClass().getName();
    objectClassname = objectClassname.substring(objectClassname.lastIndexOf(".") + 1);

    // If the given object is a map, its key/value pair is populated from
    // the fields name/value pair.
    if (object instanceof Map<?, ?>) {
        copyFieldsToMap(fieldList, (Map) object);
        // Exit after populating the map.
        return;
    }

    LogService logService = ClickUtils.getLogService();

    Set<String> properties = getObjectPropertyNames(object);
    Map<?, ?> ognlContext = Ognl.createDefaultContext(object, null, new FixBigDecimalTypeConverter(), null);

    for (Field field : fieldList) {

        // Ignore disabled field as their values are not submitted in HTML
        // forms
        if (field.isDisabled()) {
            continue;
        }

        if (!hasMatchingProperty(field, properties)) {
            continue;
        }

        String fieldName = field.getName();

        ensureObjectPathNotNull(object, fieldName);

        try {
            PropertyUtils.setValueOgnl(object, fieldName, field.getValueObject(), ognlContext);

            if (logService.isDebugEnabled()) {
                String containerClassName = ClassUtils.getShortClassName(container.getClass());
                String msg = "    " + containerClassName + " -> " + objectClassname + "." + fieldName + " : "
                        + field.getValueObject();

                logService.debug(msg);
            }

        } catch (Exception e) {
            String msg = "Error incurred invoking " + objectClassname + "." + fieldName + " with "
                    + field.getValueObject() + " error: " + e.toString();

            logService.debug(msg);
        }
    }
}

From source file:org.apache.click.util.ContainerUtils.java

/**
 * Populate the given Container field values from the object attributes.
 * <p/>//from  w  w w.  j  av  a  2 s.  c o  m
 * If a Field and object attribute matches, the Field value is set to the
 * object attribute using the method
 * {@link org.apache.click.control.Field#setValueObject(java.lang.Object)}. If
 * an object attribute is a primitive it is first converted to its proper
 * wrapper class e.g. int will become Integer and boolean will become
 * Boolean.
 * <p/>
 * The fieldList specifies which fields to populate from the object. This
 * allows one to exclude or include specific fields.
 * <p/>
 * The specified Object can either be a POJO (plain old java object) or
 * a {@link java.util.Map}. If a POJO is specified, its attributes are
 * copied to matching container fields. If a map is specified, its key/value
 * pairs are copied to matching container fields.
 *
 * @param object the object to obtain attribute values from
 * @param container the Container to populate
 * @param fieldList the list of fields to populate from the object
 * attributes
 */
public static void copyObjectToContainer(Object object, Container container, List<Field> fieldList) {
    if (object == null) {
        throw new IllegalArgumentException("Null object parameter");
    }

    if (container == null) {
        throw new IllegalArgumentException("Null container parameter");
    }

    if (container == null) {
        throw new IllegalArgumentException("Null fieldList parameter");
    }

    if (fieldList.isEmpty()) {
        LogService logService = ClickUtils.getLogService();
        if (logService.isDebugEnabled()) {
            String containerClassName = ClassUtils.getShortClassName(container.getClass());
            logService.debug("   " + containerClassName + " has no fields to copy to");
        }
        //Exit early.
        return;
    }

    String objectClassname = object.getClass().getName();
    objectClassname = objectClassname.substring(objectClassname.lastIndexOf(".") + 1);

    //If the given object is a map, populate the fields name/value from
    //the maps key/value pair.
    if (object instanceof Map<?, ?>) {

        copyMapToFields((Map) object, fieldList);
        //Exit after populating the fields.
        return;
    }

    Set<String> properties = getObjectPropertyNames(object);

    LogService logService = ClickUtils.getLogService();

    for (Field field : fieldList) {

        if (!hasMatchingProperty(field, properties)) {
            continue;
        }

        String fieldName = field.getName();
        try {
            Object result = PropertyUtils.getValue(object, fieldName);

            field.setValueObject(result);

            if (logService.isDebugEnabled()) {
                String containerClassName = ClassUtils.getShortClassName(container.getClass());
                String msg = "    " + containerClassName + " <- " + objectClassname + "." + fieldName + " : "
                        + result;
                logService.debug(msg);
            }

        } catch (Exception e) {
            String msg = "Error incurred invoking " + objectClassname + "." + fieldName + " error: "
                    + e.toString();

            logService.debug(msg);
        }
    }
}