Why we need Preparable Interface in Struts2? - java

We have Interceptor, we have custom interceptor where we can do all that we want to do before or after our action executes.
Then what is the need to use Preparable interface and implement prepare method for it?
Is this another option or there is some specific aim to do like that?

Well Preparable interface act in conjunction with Prepare Interceptor.This interface has one method defined prepare() and as its name suggest this method is responsible to allow the action to prepare itself.
Prepare interceptor calls prepare() on actions which implement Preparable. This interceptor is very useful for any situation where you need to ensure some logic runs before the actual execute method runs.So if you see this is some kind if init for your action class and it makes sure that before the Action's execute or any other method get called, this method prepare your execute method to work fine.
If you see the default-stack define in core, you will come to know that this interceptor is being called before params or workflow interceptor which indicates its purpose.
A typical use of this is to run some logic to load an object from the database so that when parameters are set they can be set on this object. For details read the doc of Prepare interceptor for details how it work closely with Preparable interface.In short Prepare interceptor will come in to act only when action implements Preparable.
Prepare-Interceptor

Prepare interface assures that if object in use is already on value stack then no need to query database it populates form properties using existing object on value stack.

Related

Is there a design pattern to write a dispatcher logic that calls specific methods based on given path?

We have a use case where we need to write a dispatcher class that takes the rest request object and should call the specific methods based on the request. For example we have a class Users that has methods to get, put, delete and post user data. We have another class Ledger that has methods related to ledgers add money, send money, create, delete etc. The dispatcher should take the request object and if the path is /users/* it should call the specific method in Users class. If the path is /ledger/* it should call the specific method in Ledger class. Is there a design pattern for such use cases?
According to your scenario you should use facade design pattern and provide a wrapper interface on top of the existing interface to help client application.
you can combine factory and strategy pattern.
you can create a class for each scenario and a factory class or method to decide which class should initialize..also you can create a facade class to hide implemention complexity.

Struts2 prepare interceptor - How to skip certain methods

I have prepare interceptor in use. In one action class, I have an action method named testSomething(), and I also have an action method named prepareTestSomething().
The problem I'm facing here is that the prepare interceptor would invoke the prepareTestSomething() action method as if it was a preparing method for testSomething(), in which case it is not.
Is there a way to make the prepare interceptor to skip the invocation for certain action methods? Like for validation interceptor, we can use "excludeMethods" parameter.
Prepending the prepare word to an Xxx method is the Struts2 convention to tell the framework that it is the prepare() method for the Xxx action method. From the docs:
if the action class have prepare{MethodName}(), it will be invoked
Instead of doing other voodoos (like excluding methods) in order to make this voodoo work, simply change the method name, it is the only right way to go.
Call it initTestSomething(), initializeTestSomething(), preparzTestSomething()... whatever; but please, don't use a convention trying to make it work for something else. It is just... wrong.

Using AsyncTask With a Observer based Controller class (MVC Arch.)

I've been following this tutorial.Now, I have to fetch and parse some JSON from a web URL. For that I am thinking to use an AsyncTask for now (might try something else as well. Such as a separate thread. advice..?)
But am not able to decide and approach things for now as to how the AsyncTask can inform back to the controller about the state of execution (I thought of using a handler but that'll defeat the purpose of already used controller acting as a Observer for button click(s)..starting the AsyncTask as well) and where can I put the AsyncTask. Certainly not in the controller itself. As the controller has the sole responsibility of giving out commands/judging + it'll be a cluster f* :D
Any help in form of sample code clearly showing me how/what to accomplish is appreciated. Thanks
You can apply a simplified Observer pattern to your AsyncTask also. Here, your Controller becomes the Observer and AsyncTask will be the notifier. In your onPostExeccute of the AsyncTask, you notify your observers with the result. In your Controller, implement the update method and take action on the result.
Since usually AsyncTask needs to notify only one object, I follow the simpler Listener interface. Define a simple interface with a method called handleResult which takes appropriate data structure. My activity implements this interface (but in your case, it would be the controller) and adds code to handle the result. My AsyncTask stores this listener as a field and on completion of the task, calls the handleResult method.
But as you can see, it is very similar.

Struts2 letting an interceptor not run for certain classes

I have a custom interceptor. I would like this interceptor to run on all action invocations except a few. I would like to program this (for extendibility/clarity) rather than using if/else statements checking the action's name inside the interceptor's intercept() method itself.
I think it might be done with the "exclude method" capacities of Struts2, but I'm stuck with the exact details. I think my interceptor needs to extend the MethodFilterInterceptor, but it has 2 intercept methods and the API is not very helpful in saying what each should do:
protected abstract String doIntercept(ActionInvocation invocation)
Subclasses must override to implement the interceptor logic.
String intercept(ActionInvocation invocation)
Override to handle interception
You are thinking it the other way around:
instead of checking the Action name (or better, the instanceOf to check for a specific Action Interface) to see if it should do some business, simply tell that Action to use a different Interceptor Stack.
For example, you can say that your Custom Stack (the Stack containing your Interceptor) is the default (then applied to all actions), but that ActionA, ActionB and ActionX run with the DefaultStack...
Interceptors shouldn't extend ActionSupport, yuck. They're not actions.
Mark the actions it should (or should not) run for with an interface or annotation and check for that in the interceptor.

With hessian in Java, how do you control instantiation?

I have a cache of objects (not the HTTP session attributes) and I want to be able to get an object from this cache when a Hessian request comes in and have Hessian execute the call on this object instead of the servlet.
I can control the class that the request is executed on by setting the service-class and api-class init parameters on the HessianServlet. However, it is performing the instantiation of objects itself and it does not look like I can control this.
I've tried to override the execute() method of HessianServlet and calling setService() or setObject() but it does not use the object I've passed in. Instead it seems to instantiate its own.
A simple hack is create a service class that has the same interface on your object, which delegates to an instance of your object it fetches to the pool, expose this service through Hessian.

Categories

Resources