All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.PlayPlugin Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package play;

import com.google.gson.JsonObject;
import play.classloading.ApplicationClasses.ApplicationClass;
import play.data.binding.RootParamNode;
import play.db.Model;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Router.Route;
import play.mvc.results.Result;
import play.templates.BaseTemplate;
import play.templates.Template;
import play.test.BaseTest;
import play.test.TestEngine.TestResults;
import play.vfs.VirtualFile;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;

/**
 * A framework plugin
 */
public abstract class PlayPlugin implements Comparable {

    /**
     * Plugin priority (0 for highest priority)
     */
    public int index;

    /**
     * Called at plugin loading
     */
    public void onLoad() {
    }

    public boolean compileSources() {
        return false;
    }

    /**
     * Run a test class
     */
    public TestResults runTest(Class clazz) {
        return null;
    }

    /**
     * Use method using RootParamNode instead
     * @return
     */
    @Deprecated
    public Object bind(String name, Class clazz, Type type, Annotation[] annotations, Map params) {
        return null;
    }

    /**
     * Called when play need to bind a Java object from HTTP params.
     *
     * When overriding this method, do not call super impl.. super impl is calling old bind method
     * to be backward compatible.
     */
    public Object bind( RootParamNode rootParamNode, String name, Class clazz, Type type, Annotation[] annotations) {
        // call old method to be backward compatible
        return bind(name, clazz, type, annotations, rootParamNode.originalParams);
    }

    /**
     * Use bindBean instead
     */
    @Deprecated
    public Object bind(String name, Object o, Map params) {
        return null;
    }

    /**
     * Called when play need to bind an existing Java object from HTTP params.
     * When overriding this method, DO NOT call the super method, since its default impl is to
     * call the old bind method to be backward compatible.
     */
    public Object bindBean(RootParamNode rootParamNode, String name, Object bean) {
        // call old method to be backward compatible.
        return bind(name, bean, rootParamNode.originalParams);
    }

    public Map unBind(Object src, String name) {
        return null;
    }
    
    /**
     * Translate the given key for the given locale and arguments.
     * If null is returned, Play's normal message translation mechanism will be
     * used.
     */
    public String getMessage(String locale, Object key, Object... args) {
        return null;
    }

    /**
     * Return the plugin status
     */
    public String getStatus() {
        return null;
    }

    /**
     * Return the plugin status in JSON format
     */
    public JsonObject getJsonStatus() {
        return null;
    }

    /**
     * Enhance this class
     * @param applicationClass
     * @throws java.lang.Exception
     */
    public void enhance(ApplicationClass applicationClass) throws Exception {
    }

    /**
     * This hook is not plugged, don't implement it
     * @param template
     */
    @Deprecated
    public void onTemplateCompilation(Template template) {
    }

    /**
     * Give a chance to this plugin to fully manage this request
     * @param request The Play request
     * @param response The Play response
     * @return true if this plugin has managed this request
     */
    public boolean rawInvocation(Request request, Response response) throws Exception {
        return false;
    }

    /**
     * Let a chance to this plugin to manage a static resource
     * @param request The Play request
     * @param response The Play response
     * @return true if this plugin has managed this request
     */
    public boolean serveStatic(VirtualFile file, Request request, Response response) {
        return false;
    }

    public void beforeDetectingChanges() {
    }

    public Template loadTemplate(VirtualFile file) {
        return null;
    }

    /**
     * It's time for the plugin to detect changes.
     * Throw an exception is the application must be reloaded.
     */
    public void detectChange() {
    }

    /**
     * It's time for the plugin to detect changes.
     * Throw an exception is the application must be reloaded.
     */
    public boolean detectClassesChange() {
        return false;
    }

    /**
     * Called at application start (and at each reloading)
     * Time to start stateful things.
     */
    public void onApplicationStart() {
    }

    /**
     * Called after the application start.
     */
    public void afterApplicationStart() {
    }

    /**
     * Called at application stop (and before each reloading)
     * Time to shutdown stateful things.
     */
    public void onApplicationStop() {
    }

    /**
     * Called before a Play! invocation.
     * Time to prepare request specific things.
     */
    public void beforeInvocation() {
    }

    /**
     * Called after an invocation.
     * (unless an excetion has been thrown).
     * Time to close request specific things.
     */
    public void afterInvocation() {
    }

    /**
     * Called if an exception occured during the invocation.
     * @param e The catched exception.
     */
    public void onInvocationException(Throwable e) {
    }

    /**
     * Called at the end of the invocation.
     * (even if an exception occured).
     * Time to close request specific things.
     */
    public void invocationFinally() {
    }

    /**
     * Called before an 'action' invocation,
     * ie an HTTP request processing.
     */
    public void beforeActionInvocation(Method actionMethod) {
    }

    /**
     * Called when the action method has thrown a result.
     * @param result The result object for the request.
     */
    public void onActionInvocationResult(Result result) {
    }

    public void onInvocationSuccess() {
    }

    /**
     * Called when the request has been routed.
     * @param route The route selected.
     */
    public void onRequestRouting(Route route) {
    }

    /**
     * Called at the end of the action invocation.
     */
    public void afterActionInvocation() {
    }

    /**
     * Called at the end of the action invocation (either in case of success or any failure).
     */
    public void onActionInvocationFinally() {
    }

    /**
     * Called when the application.conf has been read.
     */
    public void onConfigurationRead() {
    }

    /**
     * Called after routes loading.
     */
    public void onRoutesLoaded() {
    }

    /** 
     * Event may be sent by plugins or other components
     * @param message convention: pluginClassShortName.message
     * @param context depends on the plugin
     */
    public void onEvent(String message, Object context) {
    }

    public List onClassesChange(List modified) {
        return new ArrayList();
    }

    public List addTemplateExtensions() {
        return new ArrayList();
    }

    /**
     * Override to provide additional mime types from your plugin. These mimetypes get priority over
     * the default framework mimetypes but not over the application's configuration.
     * @return a Map from extensions (without dot) to mimetypes
     */
    public Map addMimeTypes() {
        return new HashMap();
    }

    /**
     * Let a chance to the plugin to compile it owns classes.
     * Must be added to the mutable list.
     */
    @Deprecated
    public void compileAll(List classes) {
    }

    /**
     * Let some plugins route themself
     * @param request
     */
    public void routeRequest(Request request) {
    }

    public Model.Factory modelFactory(Class modelClass) {
        return null;
    }

    public void afterFixtureLoad() {
    }

    /**
     * Inter-plugin communication.
     */
    public static void postEvent(String message, Object context) {
        Play.pluginCollection.onEvent(message, context);
    }

    public void onApplicationReady() {
    }

    // ~~~~~
    public int compareTo(PlayPlugin o) {
        int res = index < o.index ? -1 : (index == o.index ? 0 : 1);
        if (res != 0) {
            return res;
        }

        // index is equal in both plugins.
        // Sort on classtype to get consistent order
        res = this.getClass().getName().compareTo(o.getClass().getName());
        if (res != 0) {
            // classnames where different
            return res;
        }

        // Identical classnames.
        // Sort on instance to get consistent order.
        // We only return 0 (equal) if both identityHashCode are identical
        // which is only the case if both this and other are the same object instance.
        // This is consistent with equals() when no special equals-method is implemented.
        int thisHashCode = System.identityHashCode(this);
        int otherHashCode = System.identityHashCode(o);
        return (thisHashCode < otherHashCode ? -1 : (thisHashCode == otherHashCode ? 0 : 1));
    }

    public String overrideTemplateSource(BaseTemplate template, String source) {
        return null;
    }

    public Object willBeValidated(Object value) {
        return null;
    }

    /**
     * Implement to add some classes that should be considered unit tests but do not extend
     * {@link org.junit.Assert} to tests that can be executed by test runner (will be visible in test UI).
     * 

* Note:You probably will also need to override {@link PlayPlugin#runTest(java.lang.Class)} method * to handle unsupported tests execution properly. *

* Keep in mind that this method can only add tests to currently loaded ones. * You cannot disable tests this way. You should also make sure you do not duplicate already loaded tests. * * @return list of plugin supported unit test classes (empty list in default implementation) */ public Collection getUnitTests() { return Collections.emptyList(); } /** * Implement to add some classes that should be considered functional tests but do not extend * {@link play.test.FunctionalTest} to tests that can be executed by test runner (will be visible in test UI). *

* Note:You probably will also need to override {@link PlayPlugin#runTest(java.lang.Class)} method * to handle unsupported tests execution properly. *

* Keep in mind that this method can only add tests to currently loaded ones. * You cannot disable tests this way. You should also make sure you do not duplicate already loaded tests. * * @return list of plugin supported functional test classes (empty list in default implementation) */ public Collection getFunctionalTests() { return Collections.emptyList(); } /** * Class that define a filter. A filter is a class that wrap a certain behavior around an action. * You can access your Request and Response object within the filter. See the JPA plugin for an example. * The JPA plugin wraps a transaction around an action. The filter applies a transaction to the current Action. */ public abstract class Filter { String name; public Filter(String name) { this.name = name; } public abstract T withinFilter(play.libs.F.Function0 fct) throws Throwable; public String getName() { return name; } } /** * Return the filter implementation for this plugin. */ public Filter getFilter() { return null; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy