Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java play framework 2.1.3 How retrive a routes object

i would like to know how is possible retrive a route object from a string in the global.java class because im trying to make a dynamic module routing: i don't want to edit my main routes each time i add a module (sub-project) as

-> /mymodule mymodule.Routes

So to avoid this,i'm tring to load the target routes depending of the URI path. /module/mymodule

I tryed to write some code as follow inside onRouteRequest()

Class.forName("mymodule.Routes").routes.lift(request);

but it fail, any suggestions?

edit 1: in play 1 was possible something like that:

/{controller}/{action} {controller}.{action}

But in play2 seems dont work as well

edit 2: my current Global.java is

import play.GlobalSettings;
import play.Play;
import play.api.mvc.Handler;
import play.mvc.Http;

public class Global extends GlobalSettings
{

@Override
public Handler onRouteRequest(Http.RequestHeader request)
{
    String path = request.path();
    if (path.startsWith("/module/"))
    {
        String[] paths = path.split("/");
        String router = paths[2];
        try
        {
            return (Handler) Class.forName(router + ".Routes", true, Play.application().classloader()).newInstance();
        }
        catch (InstantiationException | IllegalAccessException | ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
    return super.onRouteRequest(request);
}
}

And he found the correct routes but Instantation Exception is thrown

like image 206
user2054758 Avatar asked Nov 11 '22 23:11

user2054758


1 Answers

it's a bit demoralizing the java inferiority against scala in this framework. After some days i decided to perform the automatic handling writing new contents in the main routes file each time server is started.

import java.io.BufferedReader;
import java.io.BufferedWriter; 
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import play.Application;
import play.GlobalSettings;

public class Global extends GlobalSettings
{
@Override
public void onStart(Application app)
{
    String newline = System.getProperty("line.separator");
    File route = app.getFile("/conf/routes");
    File[] modules = app.getFile("/modules").listFiles();

    String newContents = "# start of autogenerated code" + newline;
    for (File module : modules)
    {
        String moduleLow = module.getName().toLowerCase();
        newContents += "-> /module    " + moduleLow + ".Routes " + newline;
    }
    newContents += "# end of autogenerated code" + newline;
    editRoute(route, newContents, newline);

}

private void editRoute(File route, String newContents, String newline)
{
    try
    {
        FileReader f = new FileReader(route);
        BufferedReader br = new BufferedReader(f);
        String contents = "";
        while (true)
        {
            String s = br.readLine();
            if (s == null)
                break;
            contents += s + newline;
        }
        br.close();

        FileWriter w = new FileWriter(route);
        BufferedWriter b = new BufferedWriter(w);
        b.write(newContents + contents);
        b.flush();
        b.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

@Override
public void onStop(Application app)
{
    String newline = System.getProperty("line.separator");
    File route = app.getFile("/conf/routes");
    try
    {
        FileReader f = new FileReader(route);
        BufferedReader br = new BufferedReader(f);
        String contents = "";
        boolean startAutoCode = false;
        boolean endAutoCode = false;
        while (true)
        {
            String s = br.readLine();
            if (s == null)
                break;
            if (s.contains("# start of autogenerated code"))
            {
                startAutoCode = true;
            }
            else if (s.contains("# end of autogenerated code"))
            {
                endAutoCode = true;
                continue;
            }

            if (!startAutoCode || endAutoCode)
            {
                contents += s + newline;
            }
        }
        br.close();

        FileWriter w = new FileWriter(route);
        BufferedWriter b = new BufferedWriter(w);
        b.write(contents);
        b.flush();
        b.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
 }
like image 185
user2054758 Avatar answered Jan 04 '23 02:01

user2054758