My question is could this be implemented with Drools, Mule, etc?
This is a pretty open ended question and is probably more suited to Programmers than SO. Check out the very good answers to this similar question.
One idea that I have is to use Spring and use
applicationContext.getBean("beanName" + user.getkey()). This would
allow loading a bean dynamically at the point where differences may
occur in the business logic. The only bad thing here is that the main
code is tied to Spring which I really don't think is that big of a
deal.
You can make the logic pluggable without tying yourself to Spring. Using applicationContext.getBean()
defeats the purpose of dependency injection, so I don't recommend that. Try something like this:
- Define an interface that encapsulate the user specific logic
- Create an implementation of the interface for each type of user
- Autowire all implementations of the interface into whatever bean needs to use them.
- When needed, pick the implementation based on the user's type.
I don't know either framework very well, but could use some advice on
which avenue to pursue.
Start by hiding the logic behind an interface and encapsulating the user specific stuff into separate classes. Once everything depends on the interface, if you later need to move the user-specific stuff to a rules engine, it should be easy to do so without breaking the rest of the app.
Code example:
public enum UserType {
FOO, BAR
}
public interface UserStrategy {
public void doStuff();
public UserType getUserType();
}
@Component
public class FooUserStrategy implements UserStrategy {
public void doStuff() {
// foo specific stuff
}
public UserType getUserType() {
return UserType.FOO;
}
}
@Component
public BarUserStrategy implements UserStrategy {
public void doStuff() {
// bar specific stuff
}
public UserType getUserType() {
return UserType.BAR;
}
}
@Service
public class ClassThatUsesStrategies {
private Map<UserType, UserStrategy> map = new HashMap<>();
public void doUserTypeSpecificStuffForUser(User user) {
UserStrategy s = map.get(user.getUserType);
s.doStuff();
}
@Autowired
public void setUserStrategies(List<UserStrategy> strategies) {
for (UserStrategy current : strategies) {
map.put(current.getUserType(), current);
}
}
}