How to Inject SessionContext in arquillian embedded container for wildfly 8.2 - wildfly-8

My wildfly server not running after adding the line
#Resource
private SessionContext sessionContext;
How to inject Session Context in embeded arquillian with wildfly 8.2 version???
I even tried to add the line
.addAsWebInfResource(new File("D:\wildfly-.2.0.Final\domain\configuration\domain.xml"))
mentioning the domain.xml of my wildfly server where I have crated users. but again not able to start the server. Also when I remove the line of injecting SessionContext my server starts normally.
#RunWith(Arquillian.class)
public class CRLManagerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CRLManagerTest.class);
#Deployment
public static WebArchive createDeployment() {
WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test.war")
.addClass(CrManagerFacade1.class)
.addClass(SessionContext.class)
.addClass(CrManagerFacade.class)
.addClass(CrManager.class)
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(new File("D:\\wildfly-.2.0.Final\\domain\\configuration\\domain.xml"))
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") .addAsManifestResource("META-INF/persistence.xml", "persistence.xml") .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
;
return webArchive;
}
#Resource
private SessionContext sessionContext;
#Test
public void testUpdateReceipt1() throws Exception {
LOGGER.info(">>>>>>>>>>>>> This is a test");
Assert.assertEquals("hello","hello");
}
Here is my arquillian.xml
<container qualifier="jboss-managed" default="true">
<configuration>
<property name="jbossHome">${jbossHome}</property>
</configuration>
</container>

Looks like you're not using the Servlet protocol, so Arquillian isn't doing an http request, which would mean nothing that relies on a servlet or anything in the Request / Session would be available. Wildfly does not use the servlet protocol by default so you need to enable it.
Add this to your arquillian.xml:
<!-- Sets the protocol which is how Arquillian talks and executes the tests inside the container -->
<defaultProtocol type="Servlet 3.0" />
And this to your pom.xml:
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
</dependency>

The correct answer is to add classess to ShrinkWrat.create method.
.addClasses(CRLManagerTest.class, CrManagerFacade.class,
CrManager.class)

Related

.war on tomcat on docker, 404 on servlet

I finally found the motivation to work with Docker : I tried to deploy a basic "hello-world" servlet, on a tomcat running on a docker container.
This servlet works perfectly when I run it on the Tomcat started by intelliJ.
But when I use it with Docker, using this Dockerfile
FROM tomcat:latest
ADD example.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
And I build/start the image/container:
docker build -t example .
docker run -p 8090:8080 example
The index.jsp is displayed correctly at localhost:8090/example/, but I get a 404 when trying to access the servlet at localhost:8090/example/hello-servlet
At the same time, I can access localhost:8080/example/hello-servlet, when my non dockerized tomcat runs, and it works well.
Here is the servlet code :
package io.bananahammock.bananahammock_backend;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
#WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
What am I missing?
Since August 31, 2021 (this commit) the Docker image tomcat:latest uses Tomcat 10 (see the list of available tags).
As you are probably aware, software which uses the javax.* namespace does not work on Jakarta EE 9 servers such as Tomcat 10 (see e.g. this question). Therefore:
if it is a new project, migrate to the jakarta.* namespace and test everything on Tomcat 10 or higher,
if it is a legacy project, use another Docker image, e.g. the tomcat:9 tag.

Log4J2 StatusLogger messages are not logged in ODL

I have recently upgraded log4j2 in my application. It is running on top of the open day light controller. The pax-logging-log4j2 (version 1.11.3) bundle is added to the karaf start up bundles. With Log4J2, I have enabled the asynchronous logging as well. The below VM argument is added in the karaf start up script for enabling asynchronous logging.
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
-DAsyncLogger.WaitStrategy=Sleep
Application logging is working as expected but sometimes, the logs are not in order (timestamps are overlapped). To resolve this, I have tried to set AsyncQueueFullPolicy to ENQUEUE and RingBufferSize to 4098*1024 but no luck.
-Dlog4j2.AsyncQueueFullPolicy=Enqueue
-DAysncLogger.RingBufferSize=4194304
Tried to enable Log4J2 status logger (as like below) but the logs are not getting logged.
status=debug is added the "org.ops4j.apache.pax.logging.cfg" file.
Couple of issues, log4j2 status logger is not logged and application logs are not in order.
Any help here would be of great help.
EDIT:
Further analysis of the status logger shows asynchronous logging is reverted to synchronous with reason "com.lmax.disruptor.EventFactory cannot be found".
org.ops4j.pax.logging.pax-logging-api [log4j2] WARN : Asynchronous loggers defined, but the disruptor library is not available. Reverting to synchronous loggers. Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
java.lang.ClassNotFoundException: com.lmax.disruptor.EventFactory cannot be found by org.ops4j.pax.logging.pax-logging-log4j2_1.11.3
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl.updated(PaxLoggingServiceImpl.java:263)
at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl$1ManagedPaxLoggingService.updated(PaxLoggingServiceImpl.java:581)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85)
at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.provide(ConfigurationManager.java:1463)
at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.run(ConfigurationManager.java:1419)
at org.apache.felix.cm.impl.UpdateThread.run0(UpdateThread.java:141)
at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:109)
at java.lang.Thread.run(Thread.java:748)
Added "pax-logging-log4j2-extra" in startup.properties and the dependency issues are resolved.
AsyncLoggerContext is created now.
org.ops4j.pax.logging.pax-logging-api [log4j2] DEBUG : Starting LoggerContext[name=pax-logging, org.apache.logging.log4j.core.async.AsyncLoggerContext#1eb9a021] with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#3810215b... Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
but it is not picking the custom AsyncQueueFullPolicy added in the VM argument.
-Dlog4j2.AsyncQueueFullPolicy=a.b.c.d.BlockingQueueFullPolicy
Logs:
org.ops4j.pax.logging.pax-logging-api [log4j2] DEBUG : Using DefaultAsyncQueueFullPolicy. Could not create custom AsyncQueueFullPolicy 'a.b.c.d.BlockingQueueFullPolicy': java.lang.ClassNotFoundException: com.fujitsu.fnc.sdnfw.async.BlockingQueueFullPolicy cannot be found by org.ops4j.pax.logging.pax-logging-api_1.11.3 Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
Created a fragment bundle with the below custom policy and installed it in karaf.
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicy;
import org.apache.logging.log4j.core.async.EventRoute;
public class BlockingQueueFullPolicy implements AsyncQueueFullPolicy {
public BlockingQueueFullPolicy() {
// Don't Delete. Default constructor is required
}
#Override
public EventRoute getRoute(long backgroundThreadId, Level level) {
return EventRoute.ENQUEUE;
}
}
and pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>a.b.c.d.fw-async-policy-api</Bundle-SymbolicName>
<Bundle-Name>Custom Async Queue Full Policy API</Bundle-Name>
<Bundle-Vendor>XYZ</Bundle-Vendor>
<Bundle-ActivationPolicy>eager</Bundle-ActivationPolicy>
<Embed-Transitive>
false
</Embed-Transitive>
<Export-Package>
a.b.c.d
</Export-Package>
<Fragment-Host>system.bundle; extension:=framework</Fragment-Host>
<Import-Package>*</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.slf4j.impl.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.slf4j.impl.version}</version>
</dependency>
</dependencies>

Using Jasypt to secure spring boot database password, but received error

This is what i did with Jasypt for springboot:
i have confiured my maven dependency as follows:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
then i have configured my application.properties
(java -cp jasypt-1.9.3.jar
org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="mypassword"
password=123 algorithm=PBEWithMD5AndDES):
spring.datasource.password=ENC(YZ2Lt/juEKhtYIIGNNdm7gmy9p0xrFM7)
jasypt.encryptor.password=123
jasypt.encryptor.algorithm=PBEWithMD5AndDES
when i run my application i've got this:
Failed to bind properties under 'spring.datasource.password' to
java.lang.String:
Reason: Failed to bind properties under 'spring.datasource.password'
to java.lang.String
Action: Update your application's configuration
Anyone have any idea why this is happening? did i miss anything? Thanks in advance!

Integrating Jenkins with phantomjs for selenium webdriver test

I am integrating jenkins with phantomjs to run my selenium test scripts. Phantomjs is installed in my jenkins server and ghost driver is running in port 8090. But, still my tests were skipped and it throws an exception as
The path to the driver executable must be set by the
phantomjs.binary.path capability/system property/PATH variable; for
more information, see https://github.com/ariya/phantomjs/wiki. The
latest version can be downloaded from
http://phantomjs.org/download.html"
My jenkins runs in centos.
My code looks like below,
#BeforeClass
public void setUp() throws Exception {
dCaps = new DesiredCapabilities();
driver = new PhantomJSDriver(dCaps);
baseUrl = "";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
The path to the driver executable must be set by the
phantomjs.binary.path capability/system property/PATH variable;
You should explicitly point where you are placing phantomJm exe on the machine where tests asre supposed to be executed. So I've found out 2 ways how to resolve it:
1) possibility #1 ( point to it in code explicitly)
#BeforeClass
public void seleniumGrridUponGhostDriver() throws MalformedURLException {
File phantomjs = new File(System.getProperty("java.io.tmpdir")+File.separator+"phantomjs-1.9.7");
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability("takesScreenshot", true);
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
this.driver = new RemoteWebDriver(new URL("http://162.243.175.134:8080"), dcaps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
//page instances init()
loginPage = PageFactory.initElements(driver, LoginPage.class);
homePage = PageFactory.initElements(driver, FacebookUserPage.class);
}
2) possibility #2,using PhantomJS Windows/Mac OS X/Linux native binary embedder
pom.xml dependecies:
<!--substituting phanbedder with local Phanbedder implementation-->
<dependency>
<groupId>net.anthavio</groupId>
<artifactId>phanbedder-1.9.7</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.1.0</version>
</dependency>
code:
import net.anthavio.phanbedder.Phanbedder;
#BeforeClass
public void seleniumGrridUponGhostDriver() throws MalformedURLException {
File phantomjs = Phanbedder.unpack(); //Phanbedder to the rescue!
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability("takesScreenshot", true);
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
this.driver = new RemoteWebDriver(new URL("http://162.243.175.134:8080"), dcaps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
//page instances init()
loginPage = PageFactory.initElements(driver, LoginPage.class);
homePage = PageFactory.initElements(driver, FacebookUserPage.class);
}
Hope this helps you
I struggled with this too, and had already built Jenkins on Linux and wanted to use that installation. Using System Properties didn't work (for the service, or even the global Maven properties in Jenkins).
I didn't like the programmatic approach above, as that tied the solution to platform specific configuration.
In the end, what did work was putting it in the POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<phantomjs.binary.path>/usr/local/phantomjs/bin/phantomjs</phantomjs.binary.path>
</systemProperties>
</configuration>
</plugin>
Here is a more detailed description of what worked for me (not my article):
http://balamaci.ro/continous-integration-with-jenkins-docker-ansible-webdriver/

Exception on JNDI lookup of a resource adapter in jboss 7.1

I deployed a resource adapter (XADisk) in jboss 7.1.0.final and now get an exception on JNDI lookup.
I configured the ra.xml and deployed XADisk.rar.
Then I added the following to my standalone.xml:
<subsystem xmlns="urn:jboss:domain:resource-adapters:1.0">
<resource-adapters>
<resource-adapter>
<archive>
XADisk.rar
</archive>
<transaction-support>XATransaction</transaction-support>
<connection-definitions>
<connection-definition
class-name="org.xadisk.connector.outbound.XADiskConnectionFactory"
jndi-name="java:/XADiskCF"
pool-name="XADiskConnectionFactory">
<xa-pool>
<min-pool-size>2</min-pool-size>
<max-pool-size>10</max-pool-size>
</xa-pool>
<security>
<application/>
</security>
</connection-definition>
</connection-definitions>
</resource-adapter>
</resource-adapters>
</subsystem>
Now I tried to use it from a simple servlet:
XADiskConnectionFactory cf1 =
(XADiskConnectionFactory) new InitialContext().lookup("java:/XADiskCF");
if I invoke the servlet the following is raised:
javax.naming.NameNotFoundException: XADiskCF -- service jboss.naming.context.java.XADiskCF at
org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97) at
org.jboss.as.naming.NamingContext.lookup(NamingContext.java:177) at
org.jboss.as.naming.InitialContext.lookup(InitialContext.java:113) at
org.jboss.as.naming.NamingContext.lookup(NamingContext.java:213) at
...
I hope someone with a better understanding of jndi and jboss can help me out.
the described problem occured due an incompatibility between xadisk and jboss 7

Resources