Search in sources :

Example 1 with ApplicationClass

use of play.classloading.ApplicationClasses.ApplicationClass in project Japid by branaway.

the class JapidController method template.

protected static String template() {
    // String japidControllerInvoker =
    // threadData.get().remove(JapidPlugin.ACTION_METHOD);
    // if (japidControllerInvoker != null) {
    // return japidControllerInvoker;
    // }
    // System.out.println("[japid] still using stacktrace to determine caller?");
    // the super.template() class uses current request object to determine
    // the caller and method to find the matching template
    // this won't work if the current method is called from another action.
    // let's fall back to use the stack trace to deduce the template.
    // String caller2 = StackTraceUtils.getCaller2();
    final StackTraceElement[] stes = new Throwable().getStackTrace();
    // let's iterate back in the stacktrace to find the recent action calls.
    for (StackTraceElement st : stes) {
        String controller = st.getClassName();
        String action = st.getMethodName();
        ApplicationClass conAppClass = Play.classes.getApplicationClass(controller);
        if (conAppClass != null) {
            Class controllerClass = conAppClass.javaClass;
            if (JapidController.class.isAssignableFrom(controllerClass)) {
                Method actionMethod = /* Java. */
                findActionMethod(action, controllerClass);
                if (actionMethod != null) {
                    String expr = controller + "." + action;
                    // content negotiation
                    String format = Request.current().format;
                    if (format == null || "html".equals(format)) {
                        return expr;
                    } else {
                        String expr_format = expr + "_" + format;
                        if (expr_format.startsWith("controllers.")) {
                            expr_format = "japidviews" + expr_format.substring(expr_format.indexOf('.'));
                        }
                        if (searchForTemplateClass(expr_format) != null)
                            return expr_format;
                        else {
                            // fall back
                            return expr;
                        }
                    }
                }
            }
        }
    }
    throw new RuntimeException("The calling stack does not contain a valid controller. Should not have happended...");
}
Also used : ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) RendererClass(cn.bran.japid.rendererloader.RendererClass) ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) Method(java.lang.reflect.Method)

Example 2 with ApplicationClass

use of play.classloading.ApplicationClasses.ApplicationClass in project Japid by branaway.

the class JapidController method searchForTemplateClass.

private static Class<? extends JapidTemplateBaseWithoutPlay> searchForTemplateClass(String template) {
    String templateClassName = getTemapletClassName(template);
    Class<? extends JapidTemplateBaseWithoutPlay> tClass = null;
    ApplicationClass appClass = Play.classes.getApplicationClass(templateClassName);
    if (appClass == null) {
        // let's try the stand-alone japid pool
        tClass = JapidPlayRenderer.getTemplateClass(templateClassName);
    } else {
        tClass = (Class<? extends JapidTemplateBaseWithoutPlay>) appClass.javaClass;
    }
    return tClass;
}
Also used : ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass)

Example 3 with ApplicationClass

use of play.classloading.ApplicationClasses.ApplicationClass in project Japid by branaway.

the class JapidController2 method template.

public static String template() {
    // the super.template() class uses current request object to determine
    // the caller and method to find the matching template
    // this won't work if the current method is called from another action.
    // let's fall back to use the stack trace to deduce the template.
    // String caller2 = StackTraceUtils.getCaller2();
    final StackTraceElement[] stes = new Throwable().getStackTrace();
    // let's iterate back in the stacktrace to find the recent action calls.
    for (StackTraceElement st : stes) {
        String controller = st.getClassName();
        String action = st.getMethodName();
        ApplicationClass conAppClass = Play.classes.getApplicationClass(controller);
        if (conAppClass != null) {
            Class controllerClass = conAppClass.javaClass;
            if (JapidController2.class.isAssignableFrom(controllerClass)) {
                Method actionMethod = /* Java. */
                findActionMethod(action, controllerClass);
                if (actionMethod != null) {
                    String expr = controller + "." + action;
                    // content negotiation
                    String format = Request.current().format;
                    if ("html".equals(format)) {
                        return expr;
                    } else {
                        String expr_format = expr + "_" + format;
                        if (expr_format.startsWith("controllers.")) {
                            expr_format = "japidviews" + expr_format.substring(expr_format.indexOf('.'));
                        }
                        RendererClass rc = JapidPlayRenderer.japidClasses.get(expr_format);
                        if (rc != null)
                            return expr_format;
                        else {
                            // fall back
                            return expr;
                        }
                    }
                }
            }
        }
    }
    throw new RuntimeException("The calling stack does not contain a valid controller. Should not have happended...");
}
Also used : ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) RendererClass(cn.bran.japid.rendererloader.RendererClass) ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) Method(java.lang.reflect.Method) RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 4 with ApplicationClass

use of play.classloading.ApplicationClasses.ApplicationClass in project Japid by branaway.

the class JapidPlugin method buildRoutes.

private void buildRoutes() {
    List<Route> oldRoutes = Router.routes;
    List<Route> newRoutes = new ArrayList<Route>(oldRoutes.size());
    if (this.lastApplicationClassloaderState == Play.classloader.currentState && recentAddedRoutes != null && this.ctxPath == Play.ctxPath) {
        JapidFlags.debug("classloader state not changed. Use cached auto-routes.");
        newRoutes = new ArrayList<Route>(recentAddedRoutes);
    } else {
        // rebuild the dynamic route table
        long start = System.nanoTime();
        boolean ctxChanged = this.ctxPath != Play.ctxPath;
        for (ApplicationClass ac : Play.classes.all()) {
            // check cache
            RouterClass r = routerCache.get(ac.name);
            if (r != null && !ctxChanged && r.matchHash(ac.sigChecksum)) {
            // no change
            // JapidFlags.debug(ac.name + " has not changed. ");
            } else {
                Class<?> javaClass = ac.javaClass;
                if (javaClass != null && javaClass.getName().startsWith("controllers.") && javaClass.getAnnotation(AutoPath.class) != null) {
                    JapidFlags.debug("generate route for: " + ac.name);
                    r = new RouterClass(javaClass, appPath, ac.sigChecksum);
                } else
                    continue;
            }
            this.routerCache.put(ac.name, r);
            newRoutes.addAll(r.buildRoutes());
        }
        //
        JapidFlags.debug("rebuilding auto paths took(/ms): " + StringUtils.durationInMsFromNanos(start, System.nanoTime()));
        this.recentAddedRoutes = new ArrayList<Route>(newRoutes);
        this.lastApplicationClassloaderState = Play.classloader.currentState;
    }
    // copy fixed routes from the old route
    for (Iterator<Route> iterator = oldRoutes.iterator(); iterator.hasNext(); ) {
        Route r = iterator.next();
        if (r.routesFileLine != RouterMethod.AUTO_ROUTE_LINE) {
            newRoutes.add(r);
        }
    }
    Router.routes = newRoutes;
}
Also used : RouterClass(cn.bran.play.routing.RouterClass) ArrayList(java.util.ArrayList) ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) Route(play.mvc.Router.Route)

Example 5 with ApplicationClass

use of play.classloading.ApplicationClasses.ApplicationClass in project Japid by branaway.

the class TemplateClassLoaderWithPlay method getClassDefinition.

@Override
protected byte[] getClassDefinition(String name) {
    ApplicationClass applicationClass = play.Play.classes.getApplicationClass(name);
    if (applicationClass != null) {
        if (applicationClass.javaByteCode != null) {
            return applicationClass.javaByteCode;
        } else {
            // let's see if we can recover it.
            if (Play.usePrecompiled) {
                //					JapidFlags.log("in pre-compiled mode. try to recover it.");
                try {
                    File file = Play.getFile("precompiled/java/" + name.replace(".", "/") + ".class");
                    if (!file.exists()) {
                        return null;
                    }
                    byte[] code = IO.readContent(file);
                    if (code != null && code.length > 0) {
                        applicationClass.javaByteCode = code;
                        return code;
                    }
                } catch (Exception e) {
                    JapidFlags.error("exception raised while trying to recover the bytecode: " + e);
                }
                JapidFlags.warn("could not recover it. It might be a bug.");
            }
            return null;
        }
    } else
        return super.getClassDefinition(name);
}
Also used : ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) File(java.io.File) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ApplicationClass (play.classloading.ApplicationClasses.ApplicationClass)13 ArrayList (java.util.ArrayList)4 RendererClass (cn.bran.japid.rendererloader.RendererClass)2 GsonBuilder (com.google.gson.GsonBuilder)2 Method (java.lang.reflect.Method)2 JAXBException (javax.xml.bind.JAXBException)2 RouterClass (cn.bran.play.routing.RouterClass)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 List (java.util.List)1 JMSException (javax.jms.JMSException)1 MessageListener (javax.jms.MessageListener)1 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)1 Route (play.mvc.Router.Route)1