All the code below resolves to one simple em.persist(entity) call that fails, when everything I read says this should work. Everything behaves as expected until I call em.persist().
Can anyone see where I'm going wrong?
My database is MySql.
The relevant MySql database is "godb".
It contains a table named "loggedins".
I used PK INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY to Create the table's primary key.
I used the Glassfish Admin Console to:
Configure a Glassfish JDBC Connection Pool named "GoSQLPool". I gave it additional properties so that when I PING it I get "Ping Successful".
And I Configured a JDBC Resource named "jdbc/go". It uses "GoSqlPool". Its JNDI name is "jdbc/go"
This is my persistance.xml file:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name ="goDataBase">
<jta-data-source>jdbc/go</jta-data-source>
</persistence-unit>
</persistence>
persistence.xml is deployed in an ear package, which is where I read it's supposed to be:
go.ear
META-INF
application.xml
persistence.xml
go.war
go-server.jar
My entity bean is annotated as follows:
#Entity(name="LoggedIn")
#Table(name="loggedins")
public class LoggedIn extends EntityParent implements Serializable, DataItemsValues {
//constructors
public LoggedIn() {
this.id = ""; this.pw = ""; this.hash = "";
}
public LoggedIn(String id, String pw, String hash) {
this.id = id; this.pw = pw; this.hash = hash;
}
In case it matters, EntityParent is just an abstract class that reminds me I need to have get id, pw and hash methods in all entities whether they are for persisted fields or transient ones.
public abstract class EntityParent {
abstract public String getId ();
abstract public String getPw ();
abstract public String getHash();
}
My stateless java bean is annotated as follows:
#Stateless
public class LoggerBean {
#PersistenceContext(unitName = "goDataBase")
private EntityManager em;
This is the method in LoggerBean that throws the exception:
private Integer login (LoggedIn entity, String hash, String id,
String pw, StringBuffer sb)
throws GoExceptionServer {
U.upMarginS();
String iAm = U.getIAmS (Thread.currentThread ().getStackTrace ());
sb.append("\r\n" + iAm + "beg");
try {
sb.append("\r\n " + iAm + "persiting entity: " + entity);
///////////THIS IS THE ONLY NON PRINTING CODE///////////////////
this.em.persist(entity);
sb.append("\r\n" + iAm + "end");
U.downMarginS();
return null;
} catch (Exception e) {
sb.append("\r\n " + iAm + "caught: " + e.getClass().getName());
sb.append("\r\n " + iAm + "msg: " + e.getMessage());
sb.append("\r\n " + iAm + "cause : " + e.getCause());
sb.append("\r\n " + iAm + "Throwing a GoExceptionServer.");
sb.append("\r\n" + iAm + "end");
U.downMarginS();
throw new GoExceptionServer(MessageValues.MSG_KEY_UNEXPECTED_EXCEPTION);
}
This is the log output from the above system.out statements:
LoggerBean.login.............................beg
LoggerBean.login.............................persisting entity: LoggedInBean: PK(null) MBR_ID(G) MBR_PW(G)
LoggerBean.login.............................caught: java.lang.IllegalStateException
LoggerBean.login.............................msg: Unable to retrieve EntityManagerFactory for unitName goDataBase
LoggerBean.login.............................cause : null
LoggerBean.login.............................Throwing a GoExceptionServer.
LoggerBean.login.............................end
I'm lost. Can anyone suggest a way to make this work?
The fact that the LoggedIn entity has a null PK (primary key field) bothers me, but since the table auto-generates them I can't assign one before it is persisted.
Any suggestions or corrections are appreciated.
Everything seems to be OK, the error is that it is not possible to find configuration for goDataBase persistence unit. The file persistence.xml should be in folder META-INF, but according to your listing, the folder is named META_INF, which is not correct.
Related
I'm studying JavaWeb now,but not enough experience to solve the problem.
I use MySql 8.0+, "mysql-connector-java" version by maven impot is 8.0.12,JDBC connect MySql Driver use "com.mysql.cj.jdbc.Driver";DataBase table date time field type is " datetime",the field can be read successful by invoked set in JavaBean attribute "orderDate",then let attribute "orderDate" through 'HttpSession session' to set in session.
When JavaBean attribute type is " java.sql.Timestamp ", from session read variable values,the thymeleaf format work is fine;
There is the source code:
<td th:text="${#dates.format(orderBean.getOrderDate(),'yyyy-MM-dd HH:mm:ss')}"></td>
But when JavaBean attribute type is " java.time.LocalDateTime ",the "orderBean.java" part source code:
public class OrderBean {
....
private LocalDateTime orderDate;
.....
public LocalDateTime getOrderDate() {
return orderDate;
}
public void setOrderDate(LocalDateTime orderDate) {
this.orderDate = orderDate;
}
}
There is a method to set orderBean to HttpSeesion part source code:
public String checkout(HttpSession session){
........
OrderBean orderBean = new OrderBean();
LocalDateTime localDateTime = LocalDateTime.now();
orderBean.setOrderDate(localDateTime);
session.setAttribute("orderBean",orderBean);
........
return "cart/cart";
}
There is the cart.html source code:
<td th:text="${#temporals.format(orderBean.getOrderDate(),'yyyy-MM-dd HH:mm:ss')}"></td>
from session read variable values,the thymeleaf format not in effect,throw the erro:
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "#temporals.format(orderBean.getOrderDate(),'yyyy-MM-dd HH:mm:ss')" (template: "order/order" - line 48, col 19)
at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:191)
at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:95)
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205)
at org.thymeleaf.engine.Model.process(Model.java:282)
at org.thymeleaf.engine.Model.process(Model.java:290)
at org.thymeleaf.engine.IteratedGatheringModelProcessable.processIterationModel(IteratedGatheringModelProcessable.java:367)
at org.thymeleaf.engine.IteratedGatheringModelProcessable.process(IteratedGatheringModelProcessable.java:221)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640)
at org.thymeleaf.engine.CloseElementTag.beHandled(CloseElementTag.java:139)
at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136)
I try to solve it ,but i search in google,they write code like the same that.But the different is that they use SpringMVC, through "model.setAttribute()" method ,
model.addAttribute("localDate", LocalDate.now());
then through thymeleaf expression:
<p th:text="${#temporals.format(localDateTime, 'dd-MM-yyyy HH:mm')}"></p>
In addition,i also import dependency in maven artifactId "thymeleaf-extras-java8time ";
I just use javax-Servlet-api ,without spring,springMVC stc...,I'm devastated that I don't know how to solve it.
I hope someone can help me....
Currently using spring integration 4.2.8.
I have managed quite a lot of my previous question about this but I have 1 piece of xml configuration I can't work out how to replace in the new configuration class: it is the logging-channel-adapter there doesn't seem to be a matching class.
The only class I can find is the LoggingChannelAdapterParser but that is just designed to read the xml and output something ( AbstractBeanDefinition)
How do I specify a logging output in the recipientListRouter?
<int:logging-channel-adapter id="dlq-logger" level="ERROR" expression="'Unknown action type ['
.concat(headers.actionType)
.concat('] for message with payload ')
.concat(payload)"/>
<int:recipient-list-router input-channel="jms-inbound" id="action-type-router">
<int:recipient channel="inbound1" selector-expression="headers.actionType == 'CREATE'"/>
<int:recipient channel="inbound2" selector-expression="headers.actionType == 'UPDATE'"/>
<int:recipient channel="dlq-logger" selector-expression="headers.actionType != 'UPDATE' and headers.actionType != 'CREATE' "/>
</int:recipient-list-router>
Here is the recipinetListRouter constructor
#ServiceActivator(inputChannel = "routingChannel")
#Bean
RecipientListRouter actionTypeRouter(){
RecipientListRouter router = new RecipientListRouter();
router.setChannels()
router.addRecipient("Inbound1", "headers.actionType == 'CREATE'")
router.addRecipient("Inbound2", "headers.actionType == 'UPDATE'")
router.addRecipient("dlqLogger", "headers.actionType != 'UPDATE' and headers.actionType != 'CREATE' ")
}
Edit - from gary's answer
If as does seem sensible this is the most likely answer which way do I wire it, can a logging handler be a recipient? if so do i still need the ServiceActivator annotation?
Or is it a 2 way relationship?
#Bean
#ServiceActivator(inputChannel = "logChannel")
public LoggingHandler logging() {
LoggingHandler adapter = new LoggingHandler(LoggingHandler.Level.DEBUG);
adapter.setLoggerName("TEST_LOGGER");
adapter.setLogExpressionString("headers.id + ': ' + payload");
return adapter;
}
It's called LoggingHandler - see the documentation.
#Bean
#ServiceActivator(inputChannel = "logChannel")
public LoggingHandler logging() {
LoggingHandler adapter = new LoggingHandler(LoggingHandler.Level.DEBUG);
adapter.setLoggerName("TEST_LOGGER");
adapter.setLogExpressionString("headers.id + ': ' + payload");
return adapter;
}
EDIT from Artem Bilan
Also pay attention to the docs of the <int:logging-channel-adapter>:
<xsd:element name="logging-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Defines a Message Producing Endpoint for the
'org.springframework.integration.handler.LoggingHandler'.
</xsd:documentation>
</xsd:annotation>
And then go to this Docs for more understanding the model: https://docs.spring.io/spring-integration/reference/html/overview.html#_finding_class_names_for_java_and_dsl_configuration
Comment From Gary:
Consuming endpoints consist of 2 beans; a consumer (with the input channel) and message handler; the XML generates both; with Java Configuration, the #Bean creates the handler and the #ServiceActivator defines the consumer. So in your case, it would be #ServiceActivator(inputChannel="dlqLogger"). The router has the selector expressions. –
** FIXED **
All I had to do is add an apostrophe before and after each argument index,
i.e, change:
#Query(value = "{'type': 'Application','name': ?0,'organizationId': ?1}", fields = "{_id:1}")
To:
#Query(value = "{'type': 'Application','name': '?0','organizationId': '?1'}", fields = "{_id:1}")
===================
I recently upgraded my MongoDB and my Spring-Data-MongoDB Driver.
I used to access my MongoDB through mongoRepository using this code:
#Query(value = "{'type': 'Application','name': ?0,'organizationId': ?1}", fields = "{_id:1}")
Policies findPolicyByNameAndOrganizationId(String name, String organizationId);
Where Policies is the object I want to consume.
After performing an update to Spring, I get the following Error now when accessing the method above:
org.bson.json.JsonParseException: Invalid JSON number
I fear this is because I use Spring's MongoCoverter (in the case of this specific object only) to map documents to object.
Here's is my Reader Converter:
public class ApplicationPolicyReadConverotor implements Converter<Document, ApplicationPolicy > {
private MongoConverter mongoConverter;
public ApplicationPolicyReadConverotor(MongoConverter mongoConverter) {
this.mongoConverter = mongoConverter;
}
//#Override
public ApplicationPolicy convert(Document source) {
ApplicationPolicyEntity entity = mongoConverter.read(ApplicationPolicyEntity.class, source);
ApplicationPolicy policy = new ApplicationPolicy();
addFields(policy, entity);
addPackages(policy, entity);
return policy;
}
And here's is my Writer Converter:
public class ApplicationPolicyWriteConvertor implements Converter<ApplicationPolicy, Document>{
private MongoConverter mongoConverter;
public ApplicationPolicyWriteConvertor(MongoConverter mongoConverter) {
this.mongoConverter = mongoConverter;
}
#Override
public Document convert(ApplicationPolicy source) {
System.out.println("mashuWrite");
ApplicationPolicyEntity target = new ApplicationPolicyEntity();
copyFields(source, target);
copyPackages(source, target);
Document Doc = new Document();
mongoConverter.write(target, Doc);
return Doc;
}
I checked Spring reference (2.0.2) regarding MongoConverter and how it works and at this stage I think I'm doing it correctly.
Other object who do not use mapping/conversions suffer no problems.
Same did this Object (ApplicationPolicy) untill I upgraded my mongo and my spring driver.
My mongodb is 3.4.10 and Spring data mongo driver is 2.0.2.
Here's the code that initializes the MappingMongoCoverter Object:
(Adds my custom Converters).
SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(client, dbName);
DefaultDbRefResolver defaultDbRefResolver = new DefaultDbRefResolver(simpleMongoDbFactory);
MongoMappingContext mongoMappingContext = new MongoMappingContext();
MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(defaultDbRefResolver,
mongoMappingContext);
mappingMongoConverter.setMapKeyDotReplacement("_dot_");
// Adding custom read and write converters for permission policy.
mappingMongoConverter.setCustomConversions(new MongoCustomConversions(Arrays.asList(
new ApplicationPolicyWriteConvertor(mappingMongoConverter), new ApplicationPolicyReadConverotor(
mappingMongoConverter))));
mappingMongoConverter.afterPropertiesSet();
final MongoTemplate template = new MongoTemplate(simpleMongoDbFactory, mappingMongoConverter);
return template;
I know for sure that ReaderConverter WORKS legit (at least in some cases) since other aspects of the software use the custom ReaderConverter I've written and it works as expected.
Also when using debug mode (Intellij) I do not reach to the conversion code block when invoking the following:
#Query(value = "{'type': 'Application','name': ?0,'organizationId': ?1}", fields = "{_id:1}")
Policies findPolicyByNameAndOrganizationId(String name, String organizationId);
So basically I'm kinda clueless. I have a sense my converter Implementation is messy but couldn't fix it..
We have upgrade our application to use CDI beans. This change was very smooth when we are deploying our application on Wildfly 10.x, but when we tried to deploy the same application on Websphere Classic and Liberty some problems came up.
We have look for several questions already posted here, like this, this, this or this, but none of the answers were able to solve our problem.
On my localhost I am using Websphere Liberty Profile with webProfile-7.0, meaning CDI-1.2, EL-3.0, JSF-2.2 and servlet-3.1.
Our application also uses Primefaces 6.0.
The problem occurs on a phaseListener. On it, we are injecting a Bean annotated with both #Named (javax.inject.Named) and #SessionScoped (javax.enterprise.context.SessionScoped).
When the injected variable is called on the phaseListener the following error is thrown.
[err] 2017-05-10 09:45:06 ERROR MWExceptionHandler:139 - A server exception occurred
org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.SessionScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:691)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:89)
at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:164)
at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
at web.frmwrk.mgbean.WebSession$Proxy$_$$_WeldClientProxy.getLocale(Unknown Source)
at web.frmwrk.application.LocaleFaceletViewHandler.calculateLocale(LocaleFaceletViewHandler.java:43)
at javax.faces.application.ViewHandlerWrapper.calculateLocale(ViewHandlerWrapper.java:76)
at org.apache.myfaces.application.ResourceHandlerImpl.getLocalePrefixForLocateResource(ResourceHandlerImpl.java:715)
at org.apache.myfaces.application.ResourceHandlerImpl.createViewResource(ResourceHandlerImpl.java:1609)
at org.apache.myfaces.application.ResourceHandlerImpl.createViewResource(ResourceHandlerImpl.java:62)
at javax.faces.application.ResourceHandlerWrapper.createViewResource(ResourceHandlerWrapper.java:83)
at javax.faces.application.ResourceHandlerWrapper.createViewResource(ResourceHandlerWrapper.java:83)
at javax.faces.application.ResourceHandlerWrapper.createViewResource(ResourceHandlerWrapper.java:83)
at javax.faces.application.ResourceHandlerWrapper.createViewResource(ResourceHandlerWrapper.java:83)
at org.apache.myfaces.view.facelets.impl.DefaultResourceResolver.resolveUrl(DefaultResourceResolver.java:53)
at org.apache.myfaces.view.facelets.impl.DefaultResourceResolver.resolveUrl(DefaultResourceResolver.java:39)
at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.viewExists(FaceletViewDeclarationLanguage.java:325)
at org.apache.myfaces.shared.application.DefaultViewHandlerSupport.checkResourceExists(DefaultViewHandlerSupport.java:573)
at org.apache.myfaces.shared.application.DefaultViewHandlerSupport.handleSuffixMapping(DefaultViewHandlerSupport.java:507)
at org.apache.myfaces.shared.application.DefaultViewHandlerSupport.calculateViewId(DefaultViewHandlerSupport.java:113)
at org.apache.myfaces.application.ViewHandlerImpl.deriveLogicalViewId(ViewHandlerImpl.java:122)
at javax.faces.application.ViewHandlerWrapper.deriveLogicalViewId(ViewHandlerWrapper.java:112)
at javax.faces.application.ViewHandlerWrapper.deriveLogicalViewId(ViewHandlerWrapper.java:112)
at javax.faces.application.ViewHandlerWrapper.deriveLogicalViewId(ViewHandlerWrapper.java:112)
at javax.faces.application.ViewHandlerWrapper.deriveLogicalViewId(ViewHandlerWrapper.java:112)
at org.apache.myfaces.lifecycle.RestoreViewExecutor.execute(RestoreViewExecutor.java:225)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:196)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:143)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1290)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:778)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:475)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:148)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:79)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:1021)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1143)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:1381)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.include(WebAppRequestDispatcher.java:541)
at com.ibm.ws.webcontainer.webapp.WebApp.sendError(WebApp.java:4265)
at com.ibm.ws.webcontainer.webapp.WebApp.handleException(WebApp.java:5031)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:5011)
at com.ibm.ws.webcontainer31.osgi.webapp.WebApp31.handleRequest(WebApp31.java:525)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.handleRequest(DynamicVirtualHost.java:315)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1014)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.run(DynamicVirtualHost.java:280)
at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper.run(HttpDispatcherLink.java:967)
at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.wrapHandlerAndExecute(HttpDispatcherLink.java:359)
at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.ready(HttpDispatcherLink.java:318)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:471)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleNewRequest(HttpInboundLink.java:405)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.processRequest(HttpInboundLink.java:285)
at com.ibm.ws.http.channel.internal.inbound.HttpICLReadCallback.complete(HttpICLReadCallback.java:66)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.requestComplete(WorkQueueManager.java:504)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.attemptIO(WorkQueueManager.java:574)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.workerRun(WorkQueueManager.java:929)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager$Worker.run(WorkQueueManager.java:1018)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
[ERROR ] SRVE0777E: Exception thrown by application class 'javax.faces.webapp.FacesServlet.service:230'
javax.servlet.ServletException: WELD-001303: No active contexts for scope type javax.enterprise.context.SessionScoped
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:230)
at [internal classes]
Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.SessionScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:691)
at [internal classes]
at web.frmwrk.mgbean.WebSession$Proxy$_$$_WeldClientProxy.getLocale(Unknown Source)
at web.frmwrk.application.LocaleFaceletViewHandler.calculateLocale(LocaleFaceletViewHandler.java:43)
at javax.faces.application.ViewHandlerWrapper.calculateLocale(ViewHandlerWrapper.java:76)
... 1 more
Here is Session Scoped bean we wish to inject
#Named("ws")
#SessionScoped
public class WebSession extends LoggableBean {
private static final long serialVersionUID = 5L;
#Inject
protected WebApplication wa;
/** True if session originates from a trusted logon */
private boolean trusted = false;
/**
* Current user, null if not logged in (this may be a simulated user token if {#link #simulateUser(int)} was called
* before.
*/
private ISofTokenType userToken;
/**
* Original login user (identical to userToken if not simulating another user
*/
private ISofTokenType loginUserToken;
/** Current locale of the websession. */
private Locale locale;
/** The policy rules resolver for this session */
private transient PolicyResolver policy;
#Inject
protected Config config;
#Inject
protected WebPaths path;
#Inject
protected WebApplicationStore waStore;
#PostConstruct
protected void init() {
try {
setLocale(LocaleUtils.getDefaultLanguage().getCode());
} catch (ConfigurationException ex) {
// Fallback to default language in config.xml
getLog().error(ex);
locale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
}
}
/**
* Check if the currentRelease session is linked with a logged in user or if the visitor is a guest.
*
* #return True if the user is logged in, false otherwise.
*/
public boolean isLoggedIn() {
return userToken != null;
}
}
And here the phaseListener
public class PolicyController implements PhaseListener {
private static final long serialVersionUID = 2189917635371117541L;
private static final Log log = LogFactory.getLog(PolicyController.class);
private static final String VALIDATION_ERROR_DEFAULT_KEY = "validation_error_default";
private static final String COMPONENT_ATTRIBUTE_RENDERED_MODIFIED_BY_RULE = "rendered-modified-by-policy";
private static enum PhaseMoment {
BEFORE, AFTER
};
#Inject
private WebSession ws;
public void beforePhase(PhaseEvent event) {
if (!FacesHelper.getConfig().getBoolean(Properties.POLICY_CONTROLLER_ENABLED, true)) {
if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) log.debug("Policy is disabled");
return;
}
if ((event.getPhaseId() == PhaseId.RENDER_RESPONSE || event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS)
&& ws.isLoggedIn()) { // <- Error happens here
// Apply the rules...
FacesContext fc = event.getFacesContext();
log.debug("Run PolicyController before " + event.getPhaseId() + " (viewroot has "
+ fc.getViewRoot().getChildCount() + " direct children)");
traverseComponent(fc, fc.getViewRoot(), event.getPhaseId(), PhaseMoment.BEFORE);
}
}
public void afterPhase(PhaseEvent event) {
if (!FacesHelper.getConfig().getBoolean(Properties.POLICY_CONTROLLER_ENABLED, true)) {
return;
}
if ((event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS || event.getPhaseId() == PhaseId.RESTORE_VIEW)
&& ws.isLoggedIn()) { // <- Error happens here
FacesContext fc = event.getFacesContext();
log.debug("Run PolicyController after " + event.getPhaseId() + " (viewroot has "
+ fc.getViewRoot().getChildCount() + " direct children)");
traverseComponent(fc, fc.getViewRoot(), event.getPhaseId(), PhaseMoment.AFTER);
}
}
}
I've also tried adding
FacesContext context = event.getFacesContext();
WebSession webSession = context.getApplication().evaluateExpressionGet(context, "#{ws}", WebSession.class);
before the if statement and use webSession instead of ws, but got the same error.
Once again, I would like to point that this is working fine in Wildfly, which lead us to assume we are implementing the code correctly. Also, we are sure we are using Java 8 and the server supports JEE7, so, from all the places we've looked we were assuming this should be something to be supported on our version of Websphere.
We have the exact same problem when deploying the app on Websphere Classic 9.0.
The only difference we find so far is that Websphere uses Myfaces while Wildfly uses Mojarra. Can this be some kind of bug in Myfaces? Is there any specific configuration or code we need to use to support this kind of things?
If you need more info about our implementation that may help to figure out the cause of this, just let me know what.
Following up on this issue 4 years too late, but, in case others find it, the solution should be to set deferServletRequestListenerDestroyOnError as true on WebSphere.
Or just add this to the server.xml if you're on Liberty:
<webContainer deferServletRequestListenerDestroyOnError="true" />
https://www.ibm.com/support/pages/apar/PI26908
A explanation of this property can be found here: https://github.com/OpenLiberty/open-liberty/issues/18281#issuecomment-1353399402
I have the following dependency chain:
IUserAppService
IUserDomainService
IUserRepository
IUserDataContext - UserDataContextImpl(string conn)
All interfaces above and implementations are registered in a Windsor Castle container. When I use one connection string, everything works fine.
Now we want to support multiple databases, In UserAppServiceImpl.cs, we want to get different IUserRepository (different IUserDatabaseContext) according to userId as below:
// UserAppServiceImpl.cs
public UserInfo GetUserInfo(long userId)
{
var connStr = userId % 2 == 0 ? "conn1" : "conn2";
//var repo = container.Resolve<IUserRepository>(....)
}
How can I pass the argument connStr to UserDataContextImpl?
Since the connection string is runtime data in your case, it should not be injected directly into the constructor of your components, as explained here. Since however the connection string is contextual data, it would be awkward to pass it along all public methods in your object graph.
Instead, you should hide it behind an abstraction that allows you to retrieve the proper value for the current request. For instance:
public interface ISqlConnectionFactory
{
SqlConnection Open();
}
An implementation of the ISqlConnectionFactory itself could depend on a dependency that allows retrieving the current user id:
public interface IUserContext
{
int UserId { get; }
}
Such connection factory might therefore look like this:
public class SqlConnectionFactory : ISqlConnectionFactory
{
private readonly IUserContext userContext;
private readonly string con1;
private readonly string con2;
public SqlConnectionFactory(IUserContext userContext,
string con1, string con2) {
...
}
public SqlConnection Open() {
var connStr = userContext.UserId % 2 == 0 ? "conn1" : "conn2";
var con = new SqlConnection(connStr);
con.Open();
return con;
}
}
This leaves us with an IUserContext implementation. Such implementation will depend on the type of application we are building. For ASP.NET it might look like this:
public class AspNetUserContext : IUserContext
{
public string UserId => int.Parse(HttpContext.Current.Session["UserId"]);
}
You have to start from the beginning of your dependency resolver and resolve all of your derived dependencies to a "named" resolution.
Github code link:https://github.com/castleproject/Windsor/blob/master/docs/inline-dependencies.md
Example:
I have my IDataContext for MSSQL and another for MySQL.
This example is in Unity, but I am sure Windsor can do this.
container.RegisterType(Of IDataContextAsync, dbEntities)("db", New InjectionConstructor())
container.RegisterType(Of IUnitOfWorkAsync, UnitOfWork)("UnitOfWork", New InjectionConstructor(New ResolvedParameter(Of IDataContextAsync)("db")))
'Exceptions example
container.RegisterType(Of IRepositoryAsync(Of Exception), Repository(Of Exception))("iExceptionRepository",
New InjectionConstructor(New ResolvedParameter(Of IDataContextAsync)("db"),
New ResolvedParameter(Of IUnitOfWorkAsync)("UnitOfWork")))
sql container
container.RegisterType(Of IDataContextAsync, DataMart)(New HierarchicalLifetimeManager)
container.RegisterType(Of IUnitOfWorkAsync, UnitOfWork)(New HierarchicalLifetimeManager)
'brands
container.RegisterType(Of IRepositoryAsync(Of Brand), Repository(Of Brand))
controller code:
No changes required at the controller level.
results:
I can now have my MSSQL context do its work and MySQL do its work without any developer having to understand my container configuration. The developer simply consumes the correct service and everything is implemented.