Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller is not reloaded when active_reload is false with custom routes so controller values has what was requested on previous request. #109

Closed
artdaw opened this issue May 30, 2013 · 11 comments

Comments

@artdaw
Copy link
Contributor

artdaw commented May 30, 2013

Original author: ezio.fer...@gmail.com (July 14, 2012 01:38:03)

What steps will reproduce the problem?

  1. Set active_reload=false or do not set it.
  2. Create a custom route. Ex: route("/{fiCode}/").to(HomeController.class);
  3. Set a view value in the controller action. Ex: view("test","not empty");
  4. Test the view parameter in another request before setting the parameter. values().get("test") == null

What is the expected output? What do you see instead?
Expected -> true
I see -> false

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue: http://code.google.com/p/activeweb/issues/detail?id=109

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From i...@polevoy.org on July 16, 2012 04:08:39
Not sure I fully understand this. The "active_reload" is for reloading controllers or not. Meaning, if set to false, controllers will not be reloaded. This is an expected bahavior, right?

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ezio.fer...@gmail.com on July 16, 2012 05:10:16
The problem is that when active_reload is false and you have custom routes the vivew values are not cleared on each request.

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From i...@polevoy.org on July 16, 2012 06:31:22
is there a chunk of code maybe in test that can reproduce this?

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ezio.fer...@gmail.com on July 16, 2012 06:43:35
I have to write one. But this is simple to understand looking at the code:

protected Route recognize(String uri, HttpMethod httpMethod) throws ClassLoadException {

    Route route = matchCustom(uri, httpMethod);
    if (route == null) { //proceed to built-in routes

        //DTO as map here
        Map<String, String> controllerPath = getControllerPath(uri);

        String controllerName = controllerPath.get(Router.CONTROLLER_NAME);
        String packageSuffix = controllerPath.get(Router.PACKAGE_SUFFIX);
        if (controllerName == null) {
            return null;
        }
        String controllerClassName = getControllerClassName(controllerName, packageSuffix);
        AppController controller = createControllerInstance(controllerClassName);

        if (uri.equals("/") && rootControllerName != null && httpMethod.equals(HttpMethod.GET)) {
            return new Route(controller, "index");
        }

        route = controller.restful() ? matchRestful(uri, controllerName, packageSuffix, httpMethod, controller) :
                matchStandard(uri, controllerName, packageSuffix, controller);
    }


    return route;
}

private Route matchCustom(String uri, HttpMethod httpMethod) throws ClassLoadException {

    for (Route route : routes) {
        if (route.matches(uri, httpMethod)) {
            return route;
        }
    }
    return null;
}

On match custom you are reusing the same route object instance (and using the same controller object associated with this route instance) on every request instead of creating a new route (like on matchRestful and matchStandard).

You should add route.getController().values().clear() before returning the route.

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ipolevoy@gmail.com on July 16, 2012 15:13:43
well, this is embarrassing, thanks for catching this bug. It is fixed now, please get the latest snapshot:
https://oss.sonatype.org/content/repositories/snapshots/org/javalite/activeweb/1.2-SNAPSHOT/

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ipolevoy@gmail.com on July 17, 2012 01:29:55
I also made a new release 1.2 and pushed it to Maven Central. It fixes this defect

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ezio.fer...@gmail.com on July 17, 2012 01:31:28
Sorry, I missed the point here too. At current implementation/architecture you MUST create a new Route for each request. If you do not the route will be the same for all concurrent requests and this is not thread safe. If you just clear the values all concurrent threads for the same route you have the values cleared (some at the middle of the request).

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ipolevoy@gmail.com on July 17, 2012 01:38:46
oh, man, you are right, :) I'm being soft focused these days, will fix

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ipolevoy@gmail.com on July 17, 2012 04:19:24
I made another fix for defect. Thanks for catching. Now custom routs, like any other routes are reloaded on each request.

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ipolevoy@gmail.com on July 17, 2012 04:20:23
Please, check out the latest snapshot: https://oss.sonatype.org/content/repositories/snapshots/org/javalite/activeweb/1.3-SNAPSHOT/
If you think this fixes the defect, I will cut a new version tomorrow.

@artdaw
Copy link
Contributor Author

artdaw commented May 30, 2013

From ezio.fer...@gmail.com on July 17, 2012 04:47:14
This should work. As future optimization route should be a controller factory and create a new controller instance at each request. Route should not have the controller instance as internal attribute and only the controller class. Thank you for the bug fixing!!!

@artdaw artdaw closed this as completed May 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant