How do I get an EAR to read a properties file from a WAR? - wildfly

I'm using Wildfly 11 and Java 8. I'm trying to deploy an EAR file that contains multiple WAR files. One of my WAR files contains this in its web.xml ...
<context-param>
<param-name>Owasp.CsrfGuard.Config</param-name>
<param-value>csrfguard.properties</param-value>
</context-param>
The file in question is within one of my WARs at
myapp.war/WEB-INF/classes/csrfguard.properties
When I deploy the WAR by itself, everything deploys fine. However when I deploy the EAR containing the WAR I get an error complaining about not being able to locate the properties file ...
Caused by: java.io.IOException: unable to locate resource - csrfguard.properties
at org.owasp.csrfguard.CsrfGuardServletContextListener.getResourceStream(CsrfGuardServletContextListener.java:85)
at org.owasp.csrfguard.CsrfGuardServletContextListener.contextInitialized(CsrfGuardServletContextListener.java:36)
... 10 more
I sense that there is a class loader issue happening that I'm not figuring out how to work-around. How do I tell my EAR file where to find the properties file in question?

I suspect that the wrong class loader is being used to search the classpath for csrfguard.properties, which would cause getResourceAsStream to fail. In the .ear file, where does the CSRFGuard library get packaged?
You can try using the context.getRealPath fallback by switching to a path relative to the .war file:
<context-param>
<param-name>Owasp.CsrfGuard.Config</param-name>
<param-value>WEB-INF/classes/csrfguard.properties</param-value>
</context-param>

I had the same problem. Fixed it by adding the csrfguard.jar inside the WEB-INF/lib directory of the WAR file and not the lib directory of the EAR. Added the csrfguard.properties file to WEB-INF/classes directory and used the following context param in the web.xml
<context-param>
<param-name>Owasp.CsrfGuard.Config</param-name>
<param-value>csrfguard.properties</param-value>
</context-param>

Related

Reference a jar file in the persistence.xml within a Spring Boot project [duplicate]

persistence.xml looks like this:
<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/test</non-jta-data-source>
<jar-file>../../lib/app-services-1.0.jar</jar-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
It is a web project, so the deployment unit is a war file.
The jar file I tried to refer is in WEB-INF/lib/ folder , persistence.xml is in WEB-INF/classes/META-INF folder. When being deployed, it simply tells me
"WARNING: Unable to find file (ignored): file:.../../lib/app-services-1.0.jar".
I also tried every possible path I could think of, i.e. ../lib/app-services-1.0.jar, lib/app-services-1.0.jar.
What is the right path to do this?
Taking a look at jsr always works!
8.2.1.6.3 Jar Files
One or more JAR files may be specified using the jar-file elements instead of, or in addition to the mapping files specified in the mapping-file elements. If specified, these JAR files will >be searched for managed persistence classes, and any mapping metadata annotations found on them will be pro-cessed, or they will be mapped using the mapping annotation defaults defined by this specification.
Such JAR files are specified relative to the directory or jar file that contains the root of the persis-tence unit.
The following examples illustrate the use of the jar-file element to reference additional persistence classes. These examples use the convention that a jar file with a name terminating in “PUnit” contains the persistence.xml file and that a jar file with a name terminating in “Entities” contains additional persistence classes.
Example 1:
app.ear
lib/earEntities.jar
earRootPUnit.jar (with META-INF/persistence.xml )
persistence.xml contains:
<jar-file>lib/earEntities.jar</jar-file>
Example 2:
app.ear
lib/earEntities.jar
lib/earLibPUnit.jar (with META-INF/persistence.xml )
persistence.xml contains:
<jar-file>earEntities.jar</jar-file>
Example 3:
app.ear
lib/earEntities.jar
ejbjar.jar (with META-INF/persistence.xml )
persistence.xml contains:
<jar-file>lib/earEntities.jar</jar-file>
Example 4:
app.ear
war1.war
WEB-INF/lib/warEntities.jar
WEB-INF/lib/warPUnit.jar (with META-INF/persistence.xml )
persistence.xml contains:
<jar-file>warEntities.jar</jar-file>
Example 5:
app.ear
war2.war
WEB-INF/lib/warEntities.jar
WEB-INF/classes/META-INF/persistence.xml
persistence.xml contains:
<jar-file>lib/warEntities.jar</jar-file>
Example 6:
app.ear
lib/earEntities.jar
war2.war
WEB-INF/classes/META-INF/persistence.xml
persistence.xml contains:
<jar-file>../../lib/earEntities.jar</jar-file>
Example 7:
app.ear
lib/earEntities.jar
war1.war
WEB-INF/lib/warPUnit.jar (with META-INF/persistence.xml )
persistence.xml contains:
<jar-file>../../../lib/earEntities.jar</jar-file>
As you see there is no example for war files, all war files in the examples above are inside ear files!
But I tested in war files and it works just when I specify the absolute path of jar files and it is not a good approach for production environment!
just in case someone else stumbles upon this: the jar-file-statement is only valid. when the persistence-unit is deployed as part of an Enterprise Archives (.ear) - in every other case (.war), the persistence.xml must reside in /META-INF/ and cannot refrence classes that live outside the persistence-unit (see: http://javahowto.blogspot.com/2007/06/where-to-put-persistencexml-in-web-app.html). So, as far as I know, there is no way to have a persistence.xml living in WEB-INF/classes/META-INF that references classes that do not live in WEB-INF/classes.
war2.war
WEB-INF/lib/warEntities.jar
WEB-INF/classes/META-INF/persistence.xml
persistence.xml contains:
<jar-file>lib/warEntities.jar</jar-file>
this format works for war file. Im using Wildlfy 8.2.0 and JPA 2.1
Not sure this is related to the fact that you are deploying as a WAR, but the path should be simply "app-sevices-1.0.jar" and the jar should be in the lib of the Java EE application. The thing is: I'm not sure if this is available for the simplified "war" Java EE application. I suspect this is available only for the traditional Java EE deployment file (ear). I would test making an EAR, containing a WAR for the webapp, JAR for the PU and your other JAR for app-services, like a traditional Java EE deployment would be.
Another thing to note is that relative paths are not supported, and usage of this in SE environment is not supported by all vendors.

Wildfly 10 can't find my jar resources

i have a project that works very well in IntelliJ + WildFly 10. I have a WAR file that uses a JAR file, this jar file contains XHTML composite components.
When i put this WAR file inside deployments folder in WildFly 10 and try to start with standalone.bat i got the following error:
Caused by: javax.faces.el.EvaluationException: javax.el.PropertyNotFoundException: //D:/jee-ronaldo/wildfly-10.1.0.Final/bin/content/easyrec-adm.war/WEB-INF/lib/myjar-web-1.0.jar/META-INF/resources/myjar/listPage.xhtml #49,93 action="#{cc.attrs.managedBean.goTo(cc.attrs.targetPage,'action','ADD')}": Target Unreachable, 'null' returned null
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:94)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 50 more
I discovery that all beans inside my WAR file are not managed by CDI. I have a beans.xml file inside WEB-INF folder but i believe that CDI didn't work.

Maven web project servlet class not found

Just setup an eclipse project using Maven webapp architype. I created a servlet file and that got added to the resources folder under src/main/
When I run the app tomcat gives an error:
Error instantiating servlet class com.ABCompany.Demo.SampleDemo. java.lang.ClassNotFoundException: com.ABCompany.Demo.SampleDemo
The file is under the folder src/main/resources/com/ABCompany/Demo/SampleDemo.java
My web.xml is below.
<web-app>
<display-name>Demo Example</display-name>
<servlet>
<servlet-name>SampleDemo</servlet-name>
<display-name>SampleDemo</display-name>
<description></description>
<servlet-class>com.ABCompany.Demo.SampleDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleDemo</servlet-name>
<url-pattern>/SampleDemo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Reading in the question:
The file is under the folder src/main/resources/com/ABCompany/Demo/SampleDemo.java
Java source files, should go under src/main/java
src/main/java/com/ABCompany/Demo/SampleDemo.java
Other files, like properties, HTML/CSS files, images, templates, etc. Are normally placed in the src/main/resources. As they do not need to be compiled, just copied over into the build directory. So they can be used for running the application, or for packaging (creating a jar).
TIP: Package names, should not contains capitals, by confention. So prevered is com.abcompany.demo.
TIP: Also read How to create a Web Application Project with Maven which uses a Maven archetype, for setting it all up!
I had the same problem.
My solution is:
When the project is created please create the java folder under src/main manually, it is because the maven-archetype-webapp doesn't create the src/main/java by default.
Before: src/main
After: src/main/java
image:java folder manually created
When the first step is ready then you can create all the servlets that you need, please make sure that you create all your classes and servlets under src/main/java.
The problem is because the servlet was created under src/main/resources that means that all your .java files was packaged as a resource and not as .class compiled.

nullPointerException when deploy webLogic 12c application

when i deploy my first web app on webLogic i get this error:
weblogic.application.ModuleException: java.lang.NullPointerException
at weblogic.application.internal.ExtensibleModuleWrapper.prepare(ExtensibleModuleWrapper.java:114)
at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:100)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:172)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:167)
at weblogic.application.utils.StateMachineDriver$ParallelChange.run(StateMachineDriver.java:80)
Truncated. see log file for complete stacktrace
Caused By: java.lang.NullPointerException
at weblogic.servlet.internal.WebAnnotationProcessor.processMultipartConfigAnnotation(WebAnnotationProcessor.java:286)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotationForClasses(AnnotationProcessingManager.java:169)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotations(AnnotationProcessingManager.java:114)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotationsOutsideWebFragment(AnnotationProcessingManager.java:141)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotations(AnnotationProcessingManager.java:102)
Truncated. see log file for complete stacktrace
I had this problem and resolved it by removing JSF 2.2 Mojarra 2.2.0 from the application libraries. It only needs to be in the classpath.
In Eclipse, edit properties for the project, go to Project Facets-->Java Server Faces and uncheck "Include libraries with this application" and Apply change.
I deploy from Eclipse (OEPE), same problem here without JSF. Here is what solved my problem:
Quit Eclipse
Delete the workspace's .metadata folder
Launch Eclipse
The same solution worked on NetBeans8. From projects, go to your war project, right click over Libraries > JSF2.2 and click Remove. Rebuild the project and deploy. Remember to deploy as shared library the JSF on WLS.
This seems to be a duplicate of Maven generated ear file fails during deploying weblogic server. Some details about the solution can be found in this post https://roundwheeltech.wordpress.com/2016/06/15/deploying-a-jsf-2-2-to-weblogic-12-1-3/.
Because WebLogic 12.1.3 ships with JSF 2.1, the required JSF jars have to be bundles within the WAR file (specified as dependencies in pom.xml). WebLogic must also be instructed to use the libraries packaged in the WAR file instead of the ones that are provided by the container.
I had the exact same NPE. It was started by an instantiation of an factory that went wrong. I had to remote debug in to find this. An exception in the initialisation of the factory is swallowed and we don't get the real exception.
In my case the problem was antlr.Token and antlr.CommonToken not arriving from the same source. This gave me an ClassCastException. I solved it by moving all libraries from the ear to war to get more control over them.
Edit the file web.xml and put:
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>

Spring context:component-scan fails to find components in another Eclipse project dependency referenced in POM

I am working on a GWT web application split across two Eclipse Projects (myclient & myservice).
The myclient project references the myservice project via a dependency in the POM.
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myservices</artifactId>
<version>1.0.0</version>
</dependency>
The myclient project has a WAR directory src/main/webapp. The output folder for the myclient project is src/main/webapp/WEB-INF/classes.
The myclient project has a Spring descriptor application-context.xml with the following
<context:component-scan base-package="com.myproject.myclient, com.myproject.myservices"/>
and the web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
I have several files in the myservices project annotated as spring #Component, #Service, #Configuration but these are not picked up by the component scan when I run the GWT application in Eclipse. As a test I experimented with placing an #Component in the myclient project and this was successfully created.
I believe the following log entry during application startup indicates the source of the problem
org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:com/myproject/myservices/**/*.class] to resources []
the location pattern for the myclient project resolves to all the resources on the classpath but for myservices no resources are found.
I experimented with building the myservices project JAR and placing this JAR into the src/main/webapp/WEB-INF/lib folder of the myclient project. When I do this the component scanning works. However for development I don't want to have to build and copy a JAR everytime I make changes to the myservices project. I imagine that the component scanning should work on a project referenced through the POM without having to the build that project but after much experimenting I have been unable to get this working.
Be sure that in the deployment assembly (right click your web project and select "deployment assembly" of your myclient project it is configured to deploy the jar that is outputted by the myservices project. If you are using maven, the m2e, m2e-wtp project configurators should do this deployment assembly setup automatically.
Once you have deployment assembly settings properly configured, now when you deploy a project to your server using the Eclispe server adapter publish mechanism, everything should get deployed and the myservices jar would get placed in the right spot for your myclient project.
But make sure you the latest version of m2e-wtp installed. This way your configuration in your pom.xml and deployment assembly will get correctly configured.
Try splitting your application-context.xml into 2 separate files:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:service-context.xml,classpath:client-context.xml</param-value>
</context-param>
myservices/src/main/resources/service-context.xml:
<context:component-scan base-package="com.myproject.myservices"/>
myclient/src/main/resources/client-context.xml:
<context:component-scan base-package="com.myproject.myclient"/>
Try this :
<context:component-scan base-package="com.myproject"/>
Please check the following things:
Is your serviceproject present "only" as jar or is the whole project available via workspace resolution. This can be checked via maven-context-menue (Disable/Enable Workspaceresolution)
The layout of the webapp under src/main/webapp is okay. BUT do i get you right that all classes are copied there? If so, you should make sure everything is under target directory. So please check whether a maven call "clean package" generates a webappstructure under the target folder and all required libs (e.g. myservice) exist under target\$your-webarchivename\WEB-INF\lib
Check that packaging in myservice pom.xml is set to jar (you probably have this, right?)
it's obvious that your service jar is not included in your client project's build path. this is the only root cause.
Make sure 3 points:
you have run mvn clean install under your service project which has correct pom.xml.
you have run mvn eclipse:eclipse under your client project. this will pull out all your dependency project.
check your client eclipse project's build path dialog. is there your service jar in the list? Make sure this
You better once look this tutorial
http://fusesource.com/docs/framework/2.2/deploy_guide/CXFServletDeploySpring.html