Foren

Sending an email message from inside an OSGi module

thumbnail
Clint Wilde, geändert vor 7 Jahren.

Sending an email message from inside an OSGi module

Junior Member Beiträge: 39 Beitrittsdatum: 05.03.13 Neueste Beiträge
Hello again,

I am running into an OSGi dependency issue.

java.lang.LinkageError: loader constraint violation: when resolving method "com.liferay.mail.kernel.model.MailMessage.setTo(Ljavax/mail/internet/InternetAddress;)V" the class loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) of the current class, com/spire/services/security/util/UserManager, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, com/liferay/mail/kernel/model/MailMessage, have different Class objects for the type javax/mail/internet/InternetAddress used in the signature
at com.xxx.services.security.util.UserManager.sendMailUsingTemplate(UserManager.java:180)

All I want to do is send an email from inside my custom OSGi bundle.

My code:

private static void sendMailUsingTemplate(String to, String link) throws Exception {

String body = "Click <a href='[$link$]'here</a> to verify your account.";

body = StringUtil.replace(body, new String[] { "[$link$]"}, new String[] { link});

InternetAddress fromAddress = new InternetAddress("test123@gmail.com");
InternetAddress toAddress = new InternetAddress(to);
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(new InternetAddress(to));
mailMessage.setFrom(new InternetAddress("test456@gmail.com"));
mailMessage.setSubject("Please verify your email");
mailMessage.setBody(body);
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage);
}

The various combinations of attempts I've made:

1. Since Tomcat\lib\ext has mail.jar, I thought I could make it available as an OSGI module, so I added mail.jar to the end of module.framework.system.packages.extra, inside my portal-ext.properties, but that did work:

module.framework.system.packages.extra=\
com.ibm.crypto.provider,\
com.ibm.db2.jcc,\
com.microsoft.sqlserver.jdbc,\
com.mysql.jdbc,\
com.p6spy.engine.spy,\
com.sun.security.auth.module,\
com.sybase.jdbc4.jdbc,\
oracle.jdbc,\
org.postgresql,\
org.apache.naming.java,\
org.hsqldb.jdbc,\
org.mariadb.jdbc,\
sun.misc,\
sun.net.util,\
sun.security.provider,\
javax.mail.internet

2. I changed the build.gradle file several ways:

method 1- added it to the compileOnly section:

compileOnly 'com.liferay.portal:com.liferay.portal.kernel:2.0.0',
'com.liferay:com.liferay.portal.configuration.metatype:2.0.0',
'javax.ws.rs:javax.ws.rs-api:2.0.1',
'javax.portlet:portlet-api:2.0',
'javax.servlet:servlet-api:2.5',
'biz.aQute.bnd:biz.aQute.bndlib:3.1.0',
'org.osgi:org.osgi.compendium:5.0.0',
'com.spire:service-providers:'+version

method 2: added it the compile section:
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:'+jackson_version,
'org.apache.httpcomponents:httpclient:4.5.2',
'org.apache.commons:commons-lang3:3.4',
'org.mapstruct:mapstruct-jdk8:1.0.0.Final',
'org.apache.tomcat:tomcat-dbcp:9.0.0.M15',
'javax.mail:mail:1.5.0-b01'

I also explicitly added excluded it by adding ot to my Import-Package statement:

'Import-Package' : '!javax.mail.internet,......',

So do you have any input on what I'm missing so I can send an email from inside a custom OSGi module?

Thanks in advance.
thumbnail
Milen Dyankov, geändert vor 7 Jahren.

RE: Sending an email message from inside an OSGi module

Expert Beiträge: 310 Beitrittsdatum: 30.10.12 Neueste Beiträge
The problem is not that your module does not see the package! Thus your attempts to add it are useless.
The problem is, the package is provided by more than one module and 2 parts of your code use different version of it.

Revert to the first state you described and open Gogo shel (telnet localhost 11311). Then type p javax.mail.internet which will tell you who provides this package and who uses it! Normally it looks like this:
g! p javax.mail.internet
osgi.wiring.package; bundle-symbolic-name:List<string>="org.eclipse.osgi,system.bundle"; bundle-version:Version="3.10.200.v20150831-0856"; version:Version="1.4.0"; osgi.wiring.package="javax.mail.internet"; uses:="javax.mail"<org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
  com.liferay.portal.workflow.kaleo.runtime.impl_2.0.3 [47] imports
  com.liferay.message.boards.web_1.0.5 [166] imports
  com.liferay.portal.pop.notifications_2.0.0 [93] imports
  com.liferay.invitation.web_1.0.3 [389] imports
  com.liferay.social.networking.service_2.0.1 [173] imports
  com.liferay.portal.workflow.kaleo.runtime.api_2.0.0 [88] imports
  com.liferay.directory.web_1.0.1 [402] imports
  user-horizontal-theme_7.0.0 [489] imports
  com.liferay.invitation.invite.members.service_2.0.2 [314] imports
  user-vertical-theme_7.0.0 [488] imports
  com.liferay.portal.axis.extender_2.0.0 [110] imports
  com.liferay.social.privatemessaging.service_1.0.5 [372] imports
  com.liferay.dynamic.data.lists.form.web_1.0.4 [199] imports
</org.eclipse.osgi_3.10.200.v20150831-0856></string>

which indicates the package is exported by org.eclipse.osgi,system.bundle which is Liferay's core in this case. I bet in your case there is another version of the package exported by somo other bundle. Or you have that package embedded (perhaps embedded jar) inside your bundle!

I can't know what exactly is happening based on the information provided. You need to figure out how to make sure your module uses the same package the core is using.
Meven Kelig LIARD, geändert vor 5 Jahren.

RE: Sending an email message from inside an OSGi module

New Member Beiträge: 3 Beitrittsdatum: 22.11.17 Neueste Beiträge

Hi everone, 

 

I face the same issue here. 

Formerly, I had this line in my build.gradle file (now in comment) : 

dependencies {
...
//    compileOnly group: 'javax.mail', name: 'mail', version: '1.4'
...

}

In my service plugin, and same in API. 

I checked in the lib/ext from my Tomcat 9.0.6 and found my global mail.jar

 

Refeshing my gradle made the lib disappear from the Project and External Dependencies.

So I re-build and re-deploy both plugins, but it didn't solve anything. 

 

Following your advice, I ran the Gogoshell command "p javax.mail.internet" : 

Output
osgi.wiring.package; bundle-symbolic-name:List="org.eclipse.osgi,system.bundle";
bundle-version:Version="3.10.200.v20150831-0856";
version:Version="1.4.0";
osgi.wiring.package="javax.mail.internet";
uses:="javax.mail"
  com.liferay.portal.search.elasticsearch6.impl_2.0.5 [240] imports
  com.liferay.subscription.web_1.0.2 [215] imports
  portlet.oberexportabsences_1.0.0 [954] imports
  com.liferay.dynamic.data.mapping.form.web_1.0.12 [267] imports
  com.liferay.portal.workflow.kaleo.runtime.impl_3.0.2 [309] imports
  com.liferay.portal.security.sso.openid.connect.api_1.0.3 [660] imports
  com.liferay.portal.security.sso.openid.connect.impl_1.0.3 [668] imports
  theme-degermad_1.0.0 [945] imports
  com.liferay.portal.workflow.kaleo.runtime.api_3.0.2 [305] imports
  com.liferay.invitation.invite.members.service_4.0.2 [143] imports
  com.liferay.portal.pop.notifications_3.0.0 [484] imports
  admin-theme_2.0.6 [941] imports
  com.liferay.calendar.service_3.0.4 [244] imports
  classic-theme_2.0.7 [942] imports
  portlet-testsdk-portlet_7.1.0.1 [944] imports
  com.liferay.message.boards.service_2.0.5 [178] imports
  com.liferay.document.library.repository.cmis.impl_3.0.2 [126] imports
  com.liferay.portal.remote.axis.extender_2.0.0 [543] imports

 

... And I still get my "loader constraint violation' issue.

 

I can't find were this dependencie is still in dupe...

Did anyone have a hint of what's going on ?

 

Thanks, 

 

thumbnail
Milen Dyankov, geändert vor 5 Jahren.

RE: Sending an email message from inside an OSGi module

Expert Beiträge: 310 Beitrittsdatum: 30.10.12 Neueste Beiträge
Can you post the actuall error message?
Meven Kelig LIARD, geändert vor 5 Jahren.

RE: Sending an email message from inside an OSGi module

New Member Beiträge: 3 Beitrittsdatum: 22.11.17 Neueste Beiträge

Yes ! 

Thanks for your answer,

 

Here :

2018-11-13 10:27:55.715 ERROR [http-nio-8080-exec-4][JSONWebServiceServiceAction:117] loader constraint violation: when resolving method "com.liferay.mail.kernel.model.MailMessage.<init>(Ljavax/mail/internet/InternetAddress;Ljavax/mail/internet/InternetAddress;Ljava/lang/String;Ljava/lang/String;Z)V" the class loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) of the current class, bzh/asten/ober/util/Email, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, com/liferay/mail/kernel/model/MailMessage, have different Class objects for the type javax/mail/internet/InternetAddress used in the signature

 

I'm nearly sur too that I'm facing a case of dupe dependancy, but can't find it either than in my Tomcat lib/ext directory. 

I only found a com.liferay.petra.mail.jar in my webapps/WEB-INF/lib directory... 

thumbnail
Milen Dyankov, geändert vor 5 Jahren.

RE: Sending an email message from inside an OSGi module

Expert Beiträge: 310 Beitrittsdatum: 30.10.12 Neueste Beiträge
Check the content of the JAR file of your bundle(s). Perhaps the classes from `javax.mail.internet` were embeded into it. Or the whole JAR file. 
Meven Kelig LIARD, geändert vor 5 Jahren.

RE: Sending an email message from inside an OSGi module

New Member Beiträge: 3 Beitrittsdatum: 22.11.17 Neueste Beiträge

Nice, thank you ! 

I should have thought of it sooner, but I was lost in my search of the exact library name ... 

 

I found it, embeded in another external jar. And it took me some times after to find in wich import from gradle ... 

 

Many thanks !!