javax.mail.Session in cloudBees - java

I am developing a webapp in cloudbees platform and trying to add functionality for sending mail.
I am following https://wiki.cloudbees.com/bin/view/RUN/SendGrid documentation and as per the document we can get the mail session using below:
Context initCtx = new InitialContext();
Session session = (Session) initCtx.lookup("java:comp/env/mail/SendGrid");
But when running thie code in cloud i am getting:
java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session.
the problem seems to be with javax.mail.jar
i have downloaded the latest jar from https://java.net/projects/javamail/pages/Home .
As i understand the version which is required in cloud bees is different from what i am deploying.Can anyone help in getting correct jar for this particular purpose.
Note: I have added send grid to my stack in cloudBees.

I think you have more than one version of the javax.mail.
Indeed, the java mail session jars should be already provided by the container.
You can refer to this, as example: https://github.com/CloudBees-community/tomcat7-sendgrid-clickstart/blob/master/pom.xml
Try to remove your mail jar and if it still does not work and you are using maven try using the dependency plugin with
mvn dependency:tree -Dverbose
to check if you have conflicts with mails dependencies.

Duplicate Problem Resolved with Spring Framework on Cloudbees
I had the same problem only from Spring and can confirm it was from including javax-mail in the Maven dependencies (which also brings in the mail jar). After taking out the dependency/jar file I was having a problem with the constructor for org.springframework.mail.javamail.JavaMailSenderImpl not liking the type of the argument from jndi, however. At least the original error seemed to be objecting to the same class rather than saying it didn't know the type/name/etc. Finally, taking out passing the session to the constructor and putting it in the property (which in theory should do the same thing, I believe) fixed the problem:
<jee:jndi-lookup id="mailSource"
jndi-name="java:comp/env/mail/SendGrid"
expected-type="javax.mail.Session" />
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="session" ref="mailSource" />
</bean>

Related

How to fire liquibase rollback script from spring application

I am using spring with liquibase to update my database. Since know I have not need to user rollback functonality, but the times come where I would like to make it work.
But I cant seems to fire it from my application.
I know that maven has plugin which helps with that, but until know I was not using it and when I add it I need to provide source and credentials to my database.
In this moment liquibase is configured in xml.
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="p6spyDataSource"/>
<property name="changeLog" value="classpath:db.changelog-master.yaml"/>
.
.
</bean>
And in maven I have only dependency to liquibase-core.
And the place where I set liquibase.shouldRun is in application.properties
DataSource is taken from TomEE configuration server.xml file
So the question is if I can maybe somehow add maven plugin without adding credentials (should be taken from dataSource). Or is there other way to run rollback script from my changelog?
There are several related questions that have been posted previously about using Liquibase rollback with Spring Boot. This one seems the most similar to your post: Perform a liquibase:rollback from the command line when properties are in Spring-boot files (application.properties) and not liquibase.properties
Here is the answer as provided by Robert Kleinschmager:
The property names within springs application.properties and liquidate.properties are not compatible. You have three options
#1 just create a separate liquibase.properties file with the content you need - see liquibase doc as you only need to fix your current setup
#2 give the database parameters via command-line arguments
mvn liquibase:rollback -Dliquibase.rollbackCount=1 -Dliquibase.url=jdbc:postgresql://localhost:5432/comptesfrance -Dliquibase.username
see rollback goal for all arguments
#3 if you need a permanent solution, then you may add the liquibase properties into your application.properties and reuse them in the same file. i.e.
liquibase.url=jdbc:postgresql://localhost:5432/comptesfrance
spring.datasource.url=${liquibase.url}

is Hibernate Elasticsearch compatible with wildfly 14

I am trying to index my entities on AWS Elasticsearch cluster, I am currently using hibernate search and local file for it. Therefore, the hibernate integration with elasticsearch is the only option I have, I've followed the hibernate search doc but it ends up with
Caused by: java.util.ServiceConfigurationError: org.hibernate.search.bridge.spi.IndexManagerTypeSpecificBridgeProvider: Provider org.hibernate.search.elasticsearch.bridge.impl.ElasticsearchBridgeProvider not a subtype
I tried to remove all the jars and clean install maven once again, it didn't change anything
I've tried to add the hibernate-search-elasticsearch as a module in wildfly but ends up with many issues as well like Lucene query parser is not found in the class loader (maybe I missed up something while adding the jar as a module )
As I understood I don't need server provisioning since I am using the version which is supported by wildfly (correct me if I am wrong).
I am using:
Wildfly server 14.0.1
Hibernate core 5.3.6.Final
Hibernate search orm 5.10.3.Final
Hibernate search elasticsearch 5.10.3.Final
any ideas what could be wrong? and the better question am I adding the correct dependencies for wildfly?
P.S I know the similar question was asked before but the answer didn't help at all.
All that needed to be done is to add the dependency for hibernate elasticsearch as compile scope and add those properties in persistence.xml.
<property name="jboss.as.jpa.providerModule" value="org.hibernate" />
<property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm" />

How to create javax.el.ExpressionFactory in Google App Engine to use Hibernate Validator

I'm trying to upgrade to hibernate validator version 6 on my Google App Engine app, which needs javax.el, so I added org.glassfish:javax.el:jar:3.0.1-b10 to my dependencies.
However, this is not used:
In the local dev environment, I can create a new ExpressionFactory with ExpressionFactory.newInstance(). The ExpressionFactory is from the local Jetty installation jetty93/jetty-distribution/lib/apache-jsp/org.mortbay.jasper.apache-el-8.0.33.jar and it returns the implementation org.apache.el.ExpressionFactoryImpl from the same jar.
In the deployed GAE environment, the ExpressionFactory is from java8_runtime/runtime-shared.jar!/javax/el/ExpressionFactory.class but ExpressionFactory.newInstance() still tries to get a org.apache.el.ExpressionFactoryImpl, even though com.sun.el.ExpressionFactoryImpl is present.
In both environments, I can manually load a com.sun.el.ExpressionFactoryImpl from WEB-INF/lib/javax.el-3.0.1-b10.jar, so it is present in the classpath:
final Constructor<ExpressionFactoryImpl> constructor = com.sun.el.ExpressionFactoryImpl.class.getConstructor();
final ExpressionFactoryImpl expressionFactory = constructor.newInstance();
Why is the GAE environment so keen on loading org.apache.el.ExpressionFactoryImpl even though the reference implementation com.sun.el.ExpressionFactoryImpl is on the classpath?
How can I make GAE load the existing ExpressionFactoryImpl so I can create a hibernate ValidatorFactory? Do I have to manually supply a MessageInterpolator so I can supply the correct ExpressionFactoryImpl?
I finally solved it by adding a system property to appengine-web.xml:
<system-properties>
<property name="javax.el.ExpressionFactory" value="com.sun.el.ExpressionFactoryImpl" />
</system-properties>
An other possibility could be to add the file 'META-INF/services/javax.el.ExpressionFactory' with the contents:
com.sun.el.ExpressionFactoryImpl
See also this answer
I didn't test the second method, but it has the highest priority in the source of apache-el ExpressionFactory, while the system property is only the third try.

Creating a blueprint bean from an inner class

In Spring I can create a bean from an inner class thus:
<bean id="exampleInnerBean" class="my.package.Class$InnerClass" />
But in Blueprint this doesn't work, I get a SAXParseException:
cvc-datatype-valid.1.2.1: 'my.package.Class$InnerClass' is not a valid value for 'NCName'.
Any ideas what I'm doing wrong / what syntax I should be using / if this is even possible?
If it helps / matters, I'm installing a bundle containing the above bean into JBoss Fuse 6.0.0.
Many thanks
I was able to just use the same syntax as in Java:
<bean id="exampleInnerBean" class="my.package.Clazz.InnerClass" />
The only problem I faced was because I am using the Maven Bundle plugin... the plugin assumed that my.package.Clazz was a package and therefore added it to the list of imported packages for my bundle. This is simply fixed by adding the following to the plugin <configuration><instructions> tag in the POM:
<Import-Package>!my.package.Clazz,*</Import-Package>
This stops the plugin adding my.package.Clazz to the list of imported packages for my bundle and everything just works.

Spring app on Tomcat doesnt find class in weblogic.jar

I'm trying to deploy an app on tomcat 7 that uses spring 3.1.1 and weblogic 10.3, but I keep getting a NoClassDef exception:
java.lang.NoClassDefFoundError: Could not initialize class weblogic.jndi.Environment at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
I've double checked and and the weblogic jar is present on WEB-INF/lib and it does contain said class:
jar tf WEB-INF/lib/weblogic.jar | grep /Environment.class
weblogic/cluster/messaging/internal/Environment.class
weblogic/jndi/Environment.class
org/mozilla/javascript/tools/shell/Environment.class
this happens when a spring bean factory tries to build this bean on my root-context.xml
<beans:bean
id="jmsConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiTemplate-ref="jndiTemplate"
p:jndiName="weblogic.jms.ConnectionFactory" />
I thought it might be some classpath issue, but I have been playing around with the MANIFEST.MF of the webapp and this problematic jars(spring & weblogic) for a while now, without any luck.
Any ideas on what can be causing this?
I believe using weblogic.jar directly in the classpath does not work. You need to create wlfullclient.jar and put it in WEB-INF/lib.
Follow the instructions given here: http://middlewaremagic.com/weblogic/?p=558

Categories