How will you get the user identity? Unless this is to be hardcoded in configuration, or you have some other smart way of determining it, you will need to set this in your code. And there's no simple way to pass a parameter to an interceptor, unless you get into madness like "annotate the string argument that is the username with @ThisIsTheUsername"
My suggestion would be this:
You will need several co-operating classes.
A DoAsUserTemplate that puts the user into a ThreadLocal scoped bean.
The DoAsUserTemplate would be modelled after e.g. TransactionTemplate and be used like this:
Code:
DoAsUserTemplate temp = new DoAsUserTemplate();
temp.execute("SomeUserName",
new DoAsUserCallback() {
doAsUser() { ...};
}
)
It would first test whether a ThreadLocal bean already existed and throw an exception if it did and representeda different user. Then put the user into the threadLocal bean before calling doAsUser and, in a finally clause, remove it afterwards if it had been put in.
Then wrap your datasource inside a a UserSetupDataSource which extracts the user from the ThreadLocal class and does the Alter Session before passing on the DataSource. It should probably throw an exception, if no user has been set up.
Let us know, if this wuld work for you?