Fórum

how to get the another service in OSGI service wrapper

thumbnail
Murali Krishna, modificado 7 Anos atrás.

how to get the another service in OSGI service wrapper

Junior Member Postagens: 47 Data de Entrada: 17/01/12 Postagens Recentes
Hi,

i am trying to call the ddmStructureLocalService in JournalArticleLocalServiceWrapper. but i am getting the null pointer exception.

please let me know, where i am doing the mistake or it's the correct way to call the service.



import com.liferay.dynamic.data.mapping.kernel.DDMStructure;
import com.liferay.dynamic.data.mapping.kernel.DDMStructureManagerUtil;
import com.liferay.dynamic.data.mapping.service.persistence.DDMStructurePersistence;
import com.liferay.journal.service.JournalArticleLocalServiceWrapper;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceWrapper;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.spring.extender.service.ServiceReference;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.osgi.service.component.annotations.Component;

@Component(
immediate = true,
property = {"service.ranking:Integer=100"},
service = ServiceWrapper.class
)
public class MyJournalArticleServiceWrapper extends JournalArticleLocalServiceWrapper {

public MyJournalArticleServiceWrapper() {
super(null);
}

@Override
public com.liferay.journal.model.JournalArticle addArticle(
long userId, long groupId, long folderId, long classNameId,
long classPK, String articleId, boolean autoArticleId,
double version, Map<Locale, String> titleMap,
Map<Locale, String> descriptionMap, String content,
String ddmStructureKey, String ddmTemplateKey, String layoutUuid,
int displayDateMonth, int displayDateDay, int displayDateYear,
int displayDateHour, int displayDateMinute, int expirationDateMonth,
int expirationDateDay, int expirationDateYear,
int expirationDateHour, int expirationDateMinute,
boolean neverExpire, int reviewDateMonth, int reviewDateDay,
int reviewDateYear, int reviewDateHour, int reviewDateMinute,
boolean neverReview, boolean indexable, boolean smallImage,
String smallImageURL, File smallImageFile,
Map<String, byte[]> images, String articleURL,
ServiceContext serviceContext)
throws PortalException {

com.liferay.dynamic.data.mapping.model.DDMStructure ddmStructure = ddmStructureLocalService.fetchDDMStructure(Long.valueOf(ddmStructureKey));

if (StringUtil.equalsIgnoreCase(
ddmStructure.getName(new Locale("en_US")), "XXXXX")) {
System.out.println("found.............");
}

System.out.println(
"ddmStructureKey-------------" + ddmStructureKey );
System.out.println(
"ddmTemplateKey-----------" + ddmTemplateKey);
return super.addArticle(
userId, groupId, folderId, classNameId,
classPK, articleId, autoArticleId,
version, titleMap,
descriptionMap, content,
ddmStructureKey, ddmTemplateKey, layoutUuid,
displayDateMonth, displayDateDay, displayDateYear,
displayDateHour, displayDateMinute, expirationDateMonth,
expirationDateDay, expirationDateYear,
expirationDateHour, expirationDateMinute,
neverExpire, reviewDateMonth, reviewDateDay,
reviewDateYear, reviewDateHour, reviewDateMinute,
neverReview, indexable, smallImage,
smallImageURL, smallImageFile,
images, articleURL,
serviceContext);
}



@ServiceReference(type = com.liferay.dynamic.data.mapping.service.DDMStructureLocalService.class)
protected com.liferay.dynamic.data.mapping.service.DDMStructureLocalService ddmStructureLocalService;
}

gradle.build:

dependencies {
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
compileOnly group: "com.liferay", name: "com.liferay.journal.api", version: "2.0.0"
compileOnly group: "com.liferay", name: "com.liferay.portal.spring.extender", version: "2.0.0"
compileOnly group: "com.liferay", name: "com.liferay.dynamic.data.mapping.api", version: "3.0.0"
compileOnly group: "com.liferay", name: "com.liferay.dynamic.data.mapping.service", version: "2.0.0"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "2.0.0"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.6.0"
compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
}
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: how to get the another service in OSGI service wrapper

Liferay Legend Postagens: 14916 Data de Entrada: 02/09/06 Postagens Recentes
Don't use the @ServiceReference annotation, try the @Reference notation instead.
thumbnail
Murali Krishna, modificado 7 Anos atrás.

RE: how to get the another service in OSGI service wrapper

Junior Member Postagens: 47 Data de Entrada: 17/01/12 Postagens Recentes
Hi David,

i tried with @Reference, but still null pointer exception, i am getting.

i tried BundleContext also, but it is also giving null pointer exception. i don't know, where i did mistake.

BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ServiceReference<DDMStructureLocalService> reference = context.getServiceReference(DDMStructureLocalService.class);
DDMStructureLocalService ddmStructureLocalService = context.getService(reference);
com.liferay.dynamic.data.mapping.model.DDMStructure ddmStructure = ddmStructureLocalService.fetchDDMStructure(Long.valueOf(ddmStructureKey));
System.out.println("data-----------------"+ddmStructure);
if (StringUtil.equalsIgnoreCase(
ddmStructure.getName(new Locale("en_US")), "Message")) {
System.out.println("found.............");
}

i have structure "Message". can you pls provide some sample project on service wrapper.

Thanks,
Murali
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: how to get the another service in OSGI service wrapper

Liferay Legend Postagens: 14916 Data de Entrada: 02/09/06 Postagens Recentes
I'm at the symposium so I won't be able to work a solution until tomorrow, but in the interim you could use the local service util class; it's actually osgi in that it has the service tracker to get the reference. That would at least be a short-term solution to get you over the hump.

It is more for legacy code that still relies on the static util classes themselves, but in a pinch it can get you by a service injection issue.
thumbnail
Eric COQUELIN, modificado 7 Anos atrás.

RE: how to get the another service in OSGI service wrapper

Expert Postagens: 254 Data de Entrada: 03/11/13 Postagens Recentes
Hi,

In case of you're still stucked with this issue, I found out why.

Indeed, you use @reference to point to a component but your service is not exposed by default... That's not because your package is exported into the bnd.bnd that the service can be instanciated from another classpath.

In order to make it work, just add the following line in your MyLocalServiceImpl.java
@Component(
    service = MyLocalService.class
)


Now, your service is an OSGI component and you can reach it from another module.

Please note that I don't know if it should be moved in parent class or not. And finally, I don't know what's the real difference with @ServiceReference (but at least I know that @ServiceReference doesn't work in that case)
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: how to get the another service in OSGI service wrapper

Liferay Legend Postagens: 14916 Data de Entrada: 02/09/06 Postagens Recentes
I'm using @Reference for service injections all over the place, I haven't needed to add @Component to my implementation class yet.

As far as how to implement a service wrapper, I'd check the blade examples: https://github.com/liferay/liferay-blade-samples/blob/master/liferay-workspace/modules/blade.service.hook.user/src/main/java/com/liferay/blade/samples/service/hook/user/UserLocalServiceHook.java.
thumbnail
Murali Krishna, modificado 7 Anos atrás.

RE: how to get the another service in OSGI service wrapper

Junior Member Postagens: 47 Data de Entrada: 17/01/12 Postagens Recentes
Hi David,

can you please share any sample code, which one service wrapper calling another service ?

Thanks,
Murali
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: how to get the another service in OSGI service wrapper

Liferay Legend Postagens: 14916 Data de Entrada: 02/09/06 Postagens Recentes
I don't have an example, but it should just be a matter of including an @Reference to the other service interface and OSGi will inject it. Internally you can then call out to the service via the injected reference.