How to reset a Spring managed Bean - java

NOTE: This issue may or may not be Vaadin related, depending on wether there is a "better" solution to "resetting" the bean or not.
Background scenario
I am building a wizard for entering some values that, when finished, is sent to a table (using Vaadin and the add-on "Wizards for Vaadin").
The add-on does not provide a way to reset the Wizard (i.e. go back to step 1) without a forced call to the current steps (overridden) onAdvance() and onBack() methods, which will return false in some of my steps because I'm using logic in those methods in case the use for example haven't filled in all required data.
I cannot simple create a new instance of the Wizard, because I'm using Spring to manage this #Component.
So, this leaves me with actually resetting the bean in order to reset the wizard correctly.
My question is
How do I "reset" a Spring managed Bean (#Component)? I should add that this Bean also has some dependencies injected to it.
or... (for the Vaadin people):
Is there another way of resetting this wizard other than creating a new Wizard?
Some code
#Component
#Scope("session")
public class MyWizard extends Wizard {
#Inject
private MyWizardContainer myWizardContainer;
#Inject
private MyService myService;
#Inject
private MyWizardStep1 myWizardStep1;
#Inject
private MyWizardStep2 myWizardStep2;
#Inject
private MyWizardStep3 myWizardStep3;
#Inject
private MyMainTableContainer myMainTableContainer;
final static Logger logger = LoggerFactory.getLogger(MyWizard.class);
private static final long serialVersionUID = 1L;
public MyWizard() {
setupListener();
}
public void addSteps() {
this.addStep(myWizardStep1);
this.addStep(myWizardStep2);
this.addStep(myWizardStep3);
this.mainLayout.setComponentAlignment(this.footer, Alignment.BOTTOM_LEFT);
}
private void setupListener() {
this.addListener(new WizardProgressListener() {
#Override
public void wizardCompleted(WizardCompletedEvent event) {
endWizard("Wizard Finished Successfully!");
}
#Override
public void wizardCancelled(WizardCancelledEvent event) {
endWizard("Wizard Cancelled!");
}
#Override
public void stepSetChanged(WizardStepSetChangedEvent event) {
// TODO Auto-generated method stub
}
#Override
public void activeStepChanged(WizardStepActivationEvent event) {
// TODO Auto-generated method stub
}
});
}
private void resetWizard() {
myWizardContainer.removeAll(); //here I'm simply resetting all data that the user generated thus far in the wizard
this.activateStep(myWizardStep1); //this will not work, as some steps will not always return true on onBack() and/or onAdvance()
}
private void endWizard(String message) {
resetWizard();
this.setVisible(false);
Notification.show(message);
}
}

SpringVaadinIntegration, that you probably use, does not require all elements to be #Components, only UI has to be annotated.
Your wizard and it's steps should not be components in this case, if you really need to inject dependencies into them you can use #Configurable annotation, which allowes to inject dependencies to classes not managed by Spring (some more configuration needed to make it work). Just create wizard and steps as new objects when you need them.

Related

Calling Spring controller method without going to internet

tldr: Is there a way to make an internal request (using the method's path) without going to the internet?
--
Why do I need it? I have a project which receives many events. The decision of who will handle each event is made by a Controller. So I have something similar to this:
#RestController
#RequestMapping("/events")
public class EventHandlerAPI {
#Autowired
private EventAHandler eventAhandler;
#Autowired
private EventBHandler eventBhandler;
#PostMapping("/a")
public void handleEventA(#RequestBody EventA event) {
eventAhandler.handle(id, event);
}
#PostMapping("/b")
public void handleEventB(#RequestBody EventB event) {
eventBhandler.handle(id, event);
}
}
We recently added support to receive events through a Queue service. It sends to us the payload and the event class. Our decision is to let both interfaces working (rest and queue). The solution to avoid code duplication was to keep the Controller choosing which handler will take care of the event. The code nowadays is similar to this:
#Configuration
public class EventHandlerQueueConsumer {
#Autowired
private EventHandlerAPI eventHandlerAPI;
private Map<Class, EventHandler> eventHandlers;
#PostConstruct
public void init() {
/* start listen queue */
declareEventHandlers();
}
private void declareEventHandlers() {
eventHandlers = new HashMap<>();
eventHandlers.put(EventAHandler.class, (EventHandler<EventAHandler>) eventHandlerAPI::handleEventA);
eventHandlers.put(EventBHandler.class, (EventHandler<EventBHandler>) eventHandlerAPI::handleEventB);
}
private void onEventReceived(AbstractEvent event) {
EventHandler eventHandler = eventHandlers.get(event.getClass());
eventHandler.handle(event);
}
private interface EventHandler<T extends AbstractEvent> {
void handle(T event);
}
}
This code works, but it doesn't let the controller choose who will handle the event (our intention). The decision is actually being made by the map.
What I would like to do was to invoke the controller method through it's request mapping without going to the internet. Something like this:
#Configuration
public class EventHandlerQueueConsumer {
// MADE UP CLASS TO SHOW WHAT I WANT
#Autowired
private ControllerInkover controllerInvoker;
#PostConstruct
public void init() { /* start listen queue */ }
private void onEventReceived(AbstractEvent event) {
controllerInvoker.post(event.getPath(), new Object[] { event });
}
}
This way is much cleaner and let all the decisions be made by the controller.
I've researched a lot and didn't found a way to implement it. Debugging spring, I found how he routes the request after the DispatcherServlet, but all the spring internals uses HttpServletRequest and HttpServletResponse :(
Is there a way to make an internal request (using the method's path) without going to the internet?
They are classes of the same application
Then it should easy enough.
1) You can call your own API on http(s)://localhost:{port}/api/{path} using RestTemplate utility class. This is preferred way, since you'll follow standard MVC pattern. Something like:
restTemplate.exchange(uri, HttpMethod.POST, httpEntity, ResponseClass.class);
2) If you don't want to invoke network connection at all, then you can either use Spring's internal to find the mapping/method map or use some reflection to build custom
map upon controller's startup. Then you can pass your event/object to the method from the map in a way shown in your mock-up class. Something like:
#RequestMapping("foo")
public void fooMethod() {
System.out.println("mapping = " + getMapping("fooMethod")); // you can get all methods/mapping in #PostContruct initialization phase
}
private String getMapping(String methodName) {
Method methods[] = this.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName() == methodName) {
String mapping[] = methods[i].getAnnotation(RequestMapping.class).value();
if (mapping.length > 0) {
return mapping[mapping.length - 1];
}
}
}
return null;
}

Guice custom scope entry and exit in Java console application

I'm currently trying to implement injection in a Java console application using Guice. The application imports XML files in a database. Every import operation is done in an AbstractImporter, which can either be a UserImporter, a ScheduleImporter, etc.
public class ScheduleMigrator extends AbstractMigrator {
private final UserImporter userImporter;
private final ScheduleImporterFactory scheduleImporterFactory;
#Inject
public ScheduleMigrator(UserImporter userImporter,
ScheduleImporterFactory scheduleImporterFactory) {
this.userImporter = userImporter;
this.scheduleImporterFactory = scheduleImporterFactory;
}
public void migrate() {
// Migrate users
userImporter.run();
// Migrate schedules for each type
for (ScheduleType scheduleTypes : ScheduleType.values()) {
ScheduleImporter importer =
scheduleImporterFactory.create(scheduleTypes);
importer.run();
}
}
}
public class UserImporter extends AbstractImporter {
private final UserTransformer userTransformer;
private final ConfigurationService configurationService;
#Inject
public UserImporter(UserTransformer userTransformer,
ConfigurationService configurationService) {
this.userTransformer = userTransformer;
this.configurationService = configurationService;
}
public void run() {
// do stuff here
}
}
#Singleton
public class UserTransformer {
// ...code ommited...
}
#ImporterScoped
public class ConfigurationService {
// ...code ommited...
}
I have successfully created my own scope (#ImporterScoped) for classes that should only be available and instantiated only in an Importer. The scope was created by following the steps in the wiki. My problem is, how should I enter and exit the scope in ScheduleMigrator?
As you can see in ScheduleMigrator, each Importer is injected and its run() method is invoked. There are also factories (based on Guice's #AssistedInject feature). This is where I want each scope to start and end, UserImporter and ScheduleImporterFactory should run in their own scope.
This is a rough idea of what I'm trying to achieve:
importerScope.enter();
(new UserImporter()).run();
importerScope.exit();
Guice's documentation mentions the use of interceptors, but I'm a little lost on how it can be implemented.
Using AOP seems a very over-engineered approach and might introduce problems. When do I enter the scope? When do I exit? What happens if I instantiate two Importer objects?
Instead, I added a runScoped method in AbstractMigrator that takes a Runnable and executes it. Using injection I get the ImporterScope scope, enter and exit it appropriately.
protected void runScoped(Runnable function)
{
scenarioScope.enter();
try {
function.run();
}
finally {
scenarioScope.exit();
}
}
Usage:
runScoped(() -> {
ScheduleImporter importer =
scheduleImporterFactory.create(scheduleTypes);
importer.run();
});
This introduces one problem though. In ScheduleMigrator, I can't have Importers injected, because their instantiation would occur outside of a scope and Guice throws an OutOfScopeException. I had to wrap each Importer in a Provider.
private final Provider<UserImporter> userImporterProvider;
runScoped(() -> {
UserImporter importer = userImporterProvider.get();
importer.run();
});

Dynamic target for declarative service in OSGI

Given a consumer which uses a service, how can this consumer select a specific provider dynamically using declarative service ?
Example
Service.java
public interface Service {
public void do();
}
Provider1.java
public class Provider1 implements Service {
#Override
public void do(){
//a way
}
}
Provider2.java
public class Provider2 implements Service {
#Override
public void do(){
//another way
}
}
Consumer.java
public class Consumer {
private Service myService;
protected void bindService(Service s){ // Actually it's Provider1
myService = s;
}
protected void unbindService(Service s){
myService = null;
}
public void useThisKindOfService(String s){
// Do something crazy
}
}
So, what I would like it's instead of "Do something crazy", to find a way to reconfigure the consumer in order to release Provider1 and ask for Provider2.
Is it possible ?
Update related to "Duplicate Question"
OSGI/Felix Declarative services: How to filter the services to be bound
In my context I cannot use the declarative target because the value of the target has to be know at build time, in my case the target could be defined by a user at runtime.
Components of Declarative Services can be configured via ConfigurationAdmin. By doing that, the configuration of the component can be changed at runtime.
You can also change the configuration of myService.target via ConfigurationAdmin at runtime. If you do that, another reference will be bound to your component.
If the policy of the reference of your component is dynamic, the new reference will be bound without reactivating your component.
For more information, see the Declarative Services chapter of the OSGi Compendium specification.

How to call constructor of Eclipse 4 RCP part when application start?

I have a few Parts in one PartStack. When application start, only constructor of the first part is called. Other constructors are called when I click on these Parts.
I have something like this:
public class OneOfParts {
#Inject OneOfParts(final BorderPane pane) {
//some initialization stuff
}
//other methods
}
How can I call constructors of all Parts in this PartStack when application start?
E: Or is there another way to initialize final field from Parts when application start?
You could try to do your initializations during application start using a LifeCycleHandler. Lars Vogel describes how to implement one here.
Alternatively you could write a data class and annotate it with #Creatable and #Singleton which you then use in all parts sharing that data by DI.
#Singleton
#Creatable
public class SharedData() {
private SharedData() {
// initialize Data here - alternatively: Use #PostConstruct method in this class
}
}
public class PartShowingHex {
#PostConstruct
private void initializePart(SharedData pData) {
// do whatever you need
}
}
For a field based approach:
public class PartShowingTxt {
#Inject
private SharedData myData;
#PostConstruct
private void initializePart() {
// ...
}
}
I'd recommand using the Data-Singleton with method-DI approach (first code example).

wicket #SpringBean can not create bean

I have a project on Eclipse, Wicket, Spring, Hibernate. Every thing works normaly except : when I try
public class SortableContactDataProvider extends SortableDataProvider<User>
{
#SpringBean
private Service service;
public Iterator<User> iterator(int first, int count)
{
//SortParam sp = getSort();
return service.findAllUsers().subList(0, 15).iterator();
}
...
the service variable is null? In any another places when I use this constuction "service" is not null and working well. Please help me to solve this problem.
#SpringBean works only in any Subclass of Component.
You need to do the following in your Constructor
Wicket 1.4
InjectorHolder.getInjector().inject(this);
Wicket 1.5+
org.apache.wicket.injection.Injector.get().inject(this);
See 'generic IDataProvider implementation' # http://stronglytypedblog.blogspot.com/2009/03/wicket-patterns-and-pitfalls-1.html
Enjoy
A bit more of context for those who are newbies to Wicket/Spring environment - as bert, pointed out, #SpringBean works only in any Subclass of Component so you'll need to drive the injection manually. This is a 2 step process:
Drive the injection in your class, something as:
public class SortableContactDataProvider extends SortableDataProvider<User>
{
#SpringBean
private Service service;
public SortableContactDataProvider(){
Injector.get().inject(this); // set up the injection
}
public Iterator<User> iterator(int first, int count)
{
return service.findAllUsers().subList(0, 15).iterator();
}
}
And make sure the Injector is set up in Wicket application - something like:
public WicketApplication
#Override
protected void init() {
// make sure Spring injector is available and set up
getComponentInstantiationListeners().add(new SpringComponentInjector(this));
}
}

Categories

Resources