How to use org.eclipse.jetty.server.Request in a webapp? - maven

In my ServletFilter, I want to use specific jetty API exposed in the HttpServletRequest implementation.
I launched it like that:
final Request jettyRequest = Request.getBaseRequest(request)
If I want to avoid ClassNotFoundException, I must add the jetty-server artifact to my maven dependencies. But if I do that, getBaseRequest returns null because 'request instanceof Request' returns false instead of true.
This is certainly due to conflict between jetty and application classloaders because both of them have loaded 'org.eclipse.jetty.server.Request' class. I tried several configurations, but I was not able to make the Request class exposed to my webapp without adding the dependency in WEB-INF/lib, which causes the classpath issue.
My application is launched with "mvn jetty:run-forked" and configured like this:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<webAppSourceDirectory>${project.build.directory}/${project.name}</webAppSourceDirectory>
<systemProperties>
<force>true</force>
</systemProperties>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
<jettyXml>../jetty.xml,../jetty-ssl.xml,../jetty-https.xml</jettyXml>
<jvmArgs>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xbootclasspath/p:${settings.localRepository}/org/mortbay/jetty/alpn/alpn-boot/${alpn-version}/alpn-boot-${alpn-version}.jar</jvmArgs>
</configuration>
</plugin>
Any help will be appreciated!

I fixed the issue by adding a WebAppContext configuration file that contains:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="parentLoaderPriority">true</Set>
</Configure>
The file must be referenced in jetty-maven-plugin like that:
<contextXml>../jetty-context.xml</contextXml>

Related

Websphere 9 maven remote deployment

My springboot application builds into a WAR file (using Jenkins). I want to automate the remote deployment to Websphere 9.
I have read around and it seems there is no maven plugin for deployment to websphere 9 but ant support is pretty good. So, I'm using maven ant plugin to help running those ant tasks. I started with attempt to list the applications installed, just to see if it works. However I'm running into an exception related to localization:
[ERROR] C:\DEV\ant-was-deploy.xml:81:
java.util.MissingResourceException: Can't find bundle for base name
com.ibm.ws.profile.resourcebundle.WSProfileResourceBundle, locale
en_US
My ant-was-deploy.xml is referenced from pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>id123</id>
<phase>clean</phase>
<configuration>
<locales>es</locales>
<target>
<ant antfile="${basedir}/ant-was-deploy.xml">
<target name="listApps"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
ant-was-deploy.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="websphere" default="listApps" basedir="." >
<target name="listApps" >
<taskdef name="wsListApps" classname="com.ibm.websphere.ant.tasks.ListApplications" classpath="${wasHome.dir}/plugins/com.ibm.ws.runtime.jar" />
<wsListApps
profileName="AppServ01"
wasHome="C:\\opt\\IBM\\WebSphere\\AppServer"
/>
</target>
</project>
I think the error comes from com.ibm.ws.runtime.jar. Inside it has WSProfileResourceBundle.class and WSProfileResourceBundle_en.class but not WSProfileResourceBundle_en_US.class (name is just an assumption - I have copied the bundle with this name inside the jar but it didn't work).
I also tried to set the locale for the entire plugin but it seems that localization for this plugin is not implemented properly (no impact in my case - I set the locale to 'es' but still got the error for en_US).
I also tried to pass system parameters to maven command: mvn clean -Duser.language=fr -Duser.country=FR
It didn't work either.
So, my question is if there is a way to change the locale before the ant script? If I can set it to 'en' probably it will find the right resource bundle.
I'm fairly new to Websphere, if there is another solution to automate the remote deployment to websphere 9 I would be happy to hear it. I would rather not use scripts on target server or Jenkins plugin but if there is no other way ...
I just had the same issue. In my case, i was using an AppServer name (AppSrv1 instead of AppSrv01) that did not exist anymore, in my maven settings.xml.
The right server name solved the issue.

Spring - show git commit ID on custom health endpoint

I am trying to show the Git version info (branch, commit etc) on my custom health endpoint.
I tried using management.info.git.mode=full + git-commit-id-plugin but there is no direct way to extract the git info into a Java class. If there is, this will be the ideal way.
I also tried the same git-commit-id-plugin with Value annotations in my Java class like so #Value("${git.commit.id}") but Spring can't find the property values. I see the git.properties file created in the target dir.
What am I missing here? thanks in advance
We have to configure PropertyPlaceHolderConfigurer bean so that we can able to access the property file generated by the plugin, Please use the below code for your reference,
#Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer propsConfig
= new PropertySourcesPlaceholderConfigurer();
propsConfig.setLocation(new ClassPathResource("git.properties"));
propsConfig.setIgnoreResourceNotFound(true);
propsConfig.setIgnoreUnresolvablePlaceholders(true);
return propsConfig;
}
then in your custom health check class, you can use,
#Value("${git.commit.id}")
private String commitId;
I hope this will resolve your problem.
With Spring Boot 2 you can get this information using git-commit-id-plugin in info endpoint. This is how you can configure it
POM File
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
</plugins>
</build>
Sample Response http://localhost:8080/actuator/info
{
"git":{
"branch":"some-name",
"commit":{
"id":"ef569c2",
"time":1579000598.000000000
}
},
"build":{
"artifact":"xxx",
"name":"xxxx",
"time":1579020527.139000000,
"version":"0.0.1-SNAPSHOT",
"group":"xxxx"
}
}
The easies way is to use the commit plugin:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.build.generate-git-info
It generates git.properties. Then Spring autoconfiguration jumps in. When git.properties is available in the classpath, it creates GitProperties bean:
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.info.git-commit-information
Simply inject GitProperties to your bean and use it.

Cannot import correctly maven properties with PropertiesFactoryBean and PropertyPlaceholderConfigurer in spring

I'm facing problems with a jdbc dynamic properties configurer. I try to explain what exactly the problem is.
When I do mvn clean install and right after I deploy the applications in my server (Weblogic 10.3.3), everything is correct, and all the applications work fine. But, every morning, when I try to redeploy the same applications, it was shown an error message like this:
Error creating bean with name 'path.to.my.bean.JDBCPropertiesFactoryBean#6015a10' defined in class path resource [spring/configuration/placeholder-jdbcproperties.xml]: Invocation of init method failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [
SELECT
A.COLUMN1 || '.' || P.COLUMN2,
COLUMN3
FROM
T_TABLE_WITH_PROPERTIES${application.version} P,
T_TABLE_WITH_PROPERTIES_2 G
WHERE G.ID = P.ID
]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
This application.version comes from maven pom.xml:
<properties>
...
<application.version>MyVersion</application.version>
...
</properties>
The bean is:
<bean id="jdbcPlaceholderConfig"
class="path.to.my.bean.DefaultPropertyPlaceholderConfigurer"> <!-- Class to extend PropertyPlaceholderConfigurer -->
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="properties">
<bean class="path.to.my.bean.JDBCPropertiesFactoryBean"> <!-- Class to extend PropertiesFactoryBean -->
<property name="query">
<value>
SELECT
A.COLUMN1 || '.' || P.COLUMN2,
COLUMN3
FROM
T_TABLE_WITH_PROPERTIES${application.version} P,
T_TABLE_WITH_PROPERTIES_2 G
WHERE G.ID = P.ID
</value>
</property>
<property name="dataSource" ref="ref.to.datasource.bean"/>
</bean>
</property>
So, every morning I have to rebuild with maven, and the loop starts again.
Additional information: I try to use JRebel too, but I'm not sure where can be the problem, maybe this is relevant.
Thanks in advance.
UPDATE:
This how I generate the rebel.xml:
<build>
...
<plugins>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.5</version>
<configuration>
<relativePath>../../</relativePath>
<rootPath>PATH\TO\MY\SIS_VOB</rootPath>
<addResourcesDirToRebelXml>true</addResourcesDirToRebelXml>
<alwaysGenerate>true</alwaysGenerate>
</configuration>
<executions>
<execution>
<id>generate-rebel-xml</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I've just realized that with <executions>...<goal>generate</goal>...</executions>, when I do mvn clean install, without jrebel:generate, the rebel.xml files are always generated, so maybe I have to delete the executions tag, and generate the rebel.xml files once with jrebel:generate, and then, edit the rebel.xml and do again mvn clean install.
Would be that correct?
Thanks.
UPDATE WITH THE SOLUTION:
This is the final version of maven jrebel plugin in the pom.xml:
<build>
...
<plugins>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.5</version>
<configuration>
<relativePath>../../</relativePath>
<rootPath>PATH\TO\MY\SIS_VOB</rootPath>
<addResourcesDirToRebelXml>true</addResourcesDirToRebelXml>
<alwaysGenerate>true</alwaysGenerate>
</configuration>
<!-- executions tag out! to not regenerate files always -->
</plugin>
</plugins>
</build>
To create the rebel.xml:
mvn jrebel:generate
Then, if we want, we can modify the rebel.xml files if we want to exclude some files, like *.properties, as Henri's answer.
And that's it!
This can happen if you're using resource filtering with JRebel, as the application looks up the bean's xml in its unfiltered form from the project working directory (as per rebel.xml).
To resolve this, you'll need to update rebel.xml for that module, adding exclude for that particular XML file - see here.
Example

Can I weave aspect into same maven module it was defined in and into a 3rd party dependency?

I have a multimodule maven project with the following setup of relevant modules:
root
commons-app
backend
frontend
Module frontend is built into war and deployed on Tomcat. Module backend is a standard Java application packaged as jar. All I am trying to accomplish is to make the following aspect work (in both frontend and backend):
#Aspect
public class VirtuosoSequenceSanitizerAspect {
#Around("execution(* cz.cuni.mff.xrg.odcs.commons.app.facade.*Facade.save(..))")
public Object sanitizeSequenceOnSave(ProceedingJoinPoint pjp) throws Throwable {
// ... some code
}
#Before("execution(* org.eclipse.persistence.internal.descriptors.ObjectBuilder.assignSequenceNumber(java.lang.Object, org.eclipse.persistence.internal.sessions.AbstractSession))")
public void rememberAssignSequence(JoinPoint jp) {
// .. some code
}
}
This aspect is setup as a Spring bean in commons-app-context.xml like so:
<!-- enable aspects -->
<aop:aspectj-autoproxy />
<!-- Aspect for fixing corrupted database sequences. -->
<bean id="sequenceAspect" class="cz.cuni.mff.xrg.odcs.commons.app.dao.VirtuosoSequenceSanitizerAspect" />
With this setup the around advice is working properly, however the before advice is not triggered. From what I found I concluded I need to use aspectj-maven-plugin to weave to 3rd party libs. So I added the plugin into the pom.xml for commons-app module like so:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<complianceLevel>1.7</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<!-- Weave EclipseLink dependency -->
<weaveDependencies>
<weaveDependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
</dependencies>
</plugin>
With this plugin before advice works, but around advice stops working. I have been struggling to set this up correctly so both advices work as expected, but to no avail. When building commons-app module log says both advices are woven:
--- aspectj-maven-plugin:1.5:compile (default) # commons-app ---
Join point 'method-execution(void cz.cuni.mff.xrg.odcs.commons.app.facade.ScheduleFacade.save(cz.cuni.mff.xrg.odcs.commons.app.scheduling.Schedule))' in Type 'cz.cuni.mff.xrg.odcs.commons.app.facade.ScheduleFacade' (ScheduleFacade.java:127) advised by around advice from 'cz.cuni.mff.xrg.odcs.commons.app.dao.VirtuosoSequenceSanitizerAspect' (VirtuosoSequenceSanitizerAspect.java:90)
Join point 'method-execution(void cz.cuni.mff.xrg.odcs.commons.app.facade.DPUFacade.save(cz.cuni.mff.xrg.odcs.commons.app.dpu.DPUTemplateRecord))' in Type 'cz.cuni.mff.xrg.odcs.commons.app.facade.DPUFacade' (DPUFacade.java:123) advised by around advice from 'cz.cuni.mff.xrg.odcs.commons.app.dao.VirtuosoSequenceSanitizerAspect' (VirtuosoSequenceSanitizerAspect.java:90)
Join point 'method-execution(void cz.cuni.mff.xrg.odcs.commons.app.facade.DPUFacade.save(cz.cuni.mff.xrg.odcs.commons.app.dpu.DPUInstanceRecord))' in Type 'cz.cuni.mff.xrg.odcs.commons.app.facade.DPUFacade' (DPUFacade.java:185) advised by around advice from 'cz.cuni.mff.xrg.odcs.commons.app.dao.VirtuosoSequenceSanitizerAspect' (VirtuosoSequenceSanitizerAspect.java:90)
Join point 'method-execution(void cz.cuni.mff.xrg.odcs.commons.app.facade.PipelineFacade.save(cz.cuni.mff.xrg.odcs.commons.app.pipeline.Pipeline))' in Type 'cz.cuni.mff.xrg.odcs.commons.app.facade.PipelineFacade' (PipelineFacade.java:134) advised by around advice from 'cz.cuni.mff.xrg.odcs.commons.app.dao.VirtuosoSequenceSanitizerAspect' (VirtuosoSequenceSanitizerAspect.java:90)
...
However, when I deploy frontend to Tomcat, only the before advice is triggered. How can I configure maven to always weave both advices?
My mistake, I actually found out, that the around advice is being triggered. I did not see this because the code did not do what I expected. Also, I thought it is not triggered because a debugger breakpoint was not hit. From a brief googling I found the reason...
If around advice is inlined, the debugger can't figure out what to do
(we still have some JSR 45 related work to do in this area, and
possibly so does the Eclipse debugger). To debug around advice, you
also need to go to the project properties and turn off the "inline
around advice" AspectJ compiler option. Debugging should then
hopefully work as expected...

Jetty Maven Plugin is ignoring custom webdefault.xml

I'm trying to get around the common issue of Jetty locking static files on Windows with the technique of setting useFileMappedBuffer to false in webdefault.xml. Unfortunately, every time Jetty is not picking up my customized webdefault.xml.
I'm using Apache Maven 3.0.2. I've tried using the maven-jetty-plugin (v6.1.26) and jetty-maven-plugin (v8.0.0.M2) but with no difference. I've tried running clean and rebuilding as well before running Jetty.
I've verified each time that my webdefault.xml was taken from the same version as the plugin and has the correct settings, namely, only changing this setting from true to false:
...
<init-param>
<param-name>useFileMappedBuffer</param-name>
<param-value>false</param-value>
</init-param>
...
And here's what my pom.xml Jetty plugin section looks like:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextPath>/</contextPath>
<webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
</configuration>
</plugin>
I've also tried altering the path to my file:
<webDefaultXml>${basedir}/src/main/resources/webdefault.xml</webDefaultXml>
Everywhere I've seen this exact solution and it sounds like it is working for others (although I found one instance where someone had my issue). The startup for jetty has this in the output:
> mvn jetty:run
...
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
...
This further makes me think it isn't being applied. All the other paths are correct in the output.
My most direct issue that I'm seeing while Jetty is running is that whenever I edit a static file (JavaScript, CSS, etc.) with IntelliJ IDEA 10, I get this error message:
Cannot save file:
D:\...\... (The requested operation cannot be performed on a file with a user-mapped section open)
After I stop Jetty then it saves just fine. This happens every time.
Any ideas what I could be doing wrong? Thanks in advance.
I found an entirely different doc for the newer Jetty plugin jetty-maven-plugin (v8.0.0.M2) and it looks like the configuration name has changed:
http://wiki.eclipse.org/Jetty/Reference/webdefault.xml#Using_the_Jetty_Maven_Plugin
<project>
...
<plugins>
<plugin>
...
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<webAppConfig>
...
<defaultsDescriptor>/my/path/to/webdefault.xml</defaultsDescriptor>
</webAppConfig>
</configuration>
</plugin>
...
</plugins>
...
</project>
This now seems to work for the newer plugin. I'm still unsure why the v6 plugin does not pick up the customized config.
The only solution I found that worked with maven-jetty-plugin 6.1.24 was this:
http://false.ekta.is/2010/12/jettyrun-maven-plugin-file-locking-on-windows-a-better-way/
The Jetty documentation outlines three ways to do it (as of Jetty 9):
https://www.eclipse.org/jetty/documentation/current/troubleshooting-locked-files-on-windows.html
I successfully used the init-param method in Maven:
<!-- Running an embedded server for testing/development -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.9.v20180320</version>
<configuration>
<webApp>
<_initParams>
<org.eclipse.jetty.servlet.Default.useFileMappedBuffer>false</org.eclipse.jetty.servlet.Default.useFileMappedBuffer>
</_initParams>
</webApp>
</configuration>
</plugin>

Resources