Service builder call custom java classService builder call custom java classhttps://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=747926212024-03-28T12:00:31Z2024-03-28T12:00:31ZRE: Service builder call custom java classS Hhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=748378902016-06-02T07:08:57Z2016-06-02T07:08:57ZThanks for reply.<br /><br />I tried to put my class in /WEB-INF/service , that works.<br /><br />I also checked the article https://web.liferay.com/web/pmesotten/blog/-/blogs/inject-any-custom-class-or-service-into-web-content-templates#brit_message_74831270<br />I have some issue deploy the code to my liferay 6.2, I got below errors.<br /><br />06:59:51,669 ERROR [localhost-startStop-67][HotDeployImpl:233] com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering servlet context listeners for content-hookcontent-hook<br />com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering servlet context listeners for content-hookcontent-hook<br /> at com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener.throwHotDeployException(BaseHotDeployListener.java:46)<br /> at com.liferay.portal.deploy.hot.ServletContextListenerHotDeployListener.invokeDeploy(ServletContextListenerHotDeployListener.java:40)<br /> at com.liferay.portal.deploy.hot.HotDeployImpl.doFireDeployEvent(HotDeployImpl.java:230)<br /> at com.liferay.portal.deploy.hot.HotDeployImpl.fireDeployEvent(HotDeployImpl.java:96)<br /> at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil.java:28)<br /> at com.liferay.portal.kernel.servlet.PluginContextListener.fireDeployEvent(PluginContextListener.java:164)<br /> at com.liferay.portal.kernel.servlet.PluginContextListener.doPortalInit(PluginContextListener.java:154)<br /> at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycle.java:44)<br /> at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:74)<br /> at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:58)<br /> at com.liferay.portal.kernel.util.BasePortalLifecycle.registerPortalLifecycle(BasePortalLifecycle.java:54)<br /> at com.liferay.portal.kernel.servlet.PluginContextListener.contextInitialized(PluginContextListener.java:116)<br /> at com.liferay.portal.kernel.servlet.SecurePluginContextListener.contextInitialized(SecurePluginContextListener.java:151)<br /> at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)<br /> at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)<br /> at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)<br /> at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)<br /> at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)<br /> at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)<br /> at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1263)<br /> at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1948)<br /> at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)<br /> at java.util.concurrent.FutureTask.run(FutureTask.java:262)<br /> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)<br /> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)<br /> at java.lang.Thread.run(Thread.java:745)<br />Caused by: java.lang.reflect.InvocationTargetException<br /> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br /> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)<br /> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)<br /> at java.lang.reflect.Method.invoke(Method.java:606)<br /> at com.liferay.portal.deploy.hot.ServletContextListenerHotDeployListener.doInvokeDeploy(ServletContextListenerHotDeployListener.java:77)<br /> at com.liferay.portal.deploy.hot.ServletContextListenerHotDeployListener.invokeDeploy(ServletContextListenerHotDeployListener.java:37)<br /> ... 24 more<br />Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/support/XmlWebApplicationContext<br /> at be.aca.literay.spring.VelocityBeanLocatorContextListener.<init>(VelocityBeanLocatorContextListener.java:37)<br /> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)<br /> at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)<br /> at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)<br /> at java.lang.reflect.Constructor.newInstance(Constructor.java:526)<br /> at java.lang.Class.newInstance(Class.java:379)<br /> at com.liferay.portal.kernel.util.InstanceFactory.newInstance(InstanceFactory.java:63)<br /> at com.liferay.portal.kernel.util.InstanceFactory.newInstance(InstanceFactory.java:27)<br /> at com.liferay.portal.kernel.servlet.SecurePluginContextListener.instantiatingListener(SecurePluginContextListener.java:304)<br /> at com.liferay.portal.kernel.servlet.SecurePluginContextListener.instantiatingListeners(SecurePluginContextListener.java:163)<br /> ... 30 more<br />Caused by: java.lang.ClassNotFoundException: org.springframework.web.context.support.XmlWebApplicationContext<br /> at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)<br /> at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)<br /> ... 40 more<br /><br />In addition, I also checked one article here https://dev.liferay.com/discover/deployment/-/knowledge_base/6-2/custom-java-tools-in-the-script-engine , it also provide the hook way, this is also working. But I found it only support xml way to configure spring, if I try using annotation, it won't work.S H2016-06-02T07:08:57ZRE: Service builder call custom java classDavid H Nebingerhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=748115122016-06-01T12:45:26Z2016-06-01T12:45:26Z<html><head></head><body>Hey, S H.<br><br>The problem is not that the FooLocalServiceImpl has access to the class, it certainly does. But when you declare "public ValidationUtil getUtil()" member, you're trying to expose the method within the service jar. The service jar will not have access to implementation classes like this and you cannot just pass instance pointers around since you may be crossing the class loader boundary.<br><br>What you might consider is adding an entity to service.xml such as:<br><br><pre><code><entity name="Validation" local-service="true" remote-service="false" /></code></pre><br><br>This will create a utility class of it's own. You'll get a ValidationLocalServiceImpl where you can add methods to expose validation on your entities or on primitives (or their object counterparts). In this way you can expose business logic as a service that does not have a corresponding db entity tied to the service class.<br><br>I usually don't expose validation logic like this myself. The service builder layer is (IMHO) the facade over persisting the entities. Sure saving an entity will require validation, but it's not the type of validation that you would want to employ in a user interface, for example, where you might want to highlight validation failures and suggest edits and in general provide a user-friendly validation scheme. In the SB layer, you are employing more of a pass/fail sort of logic where the entity is either valid (and it will persist) or it is not (and you throw a generic exception).<br><br>As far as exposing to FreeMarker, well you're trying to back-door a solution here that is not supported. First putting any classes in the service jar is risky because if someone comes along and does a clean build the classes might get deleted since the service jar is generated out of SB.<br><br>Honestly there are better ways to get access to custom classes correctly within FM. Check out <a href="https://web.liferay.com/web/pmesotten/blog/-/blogs/inject-any-custom-class-or-service-into-web-content-templates">Peter's blog</a> for one example.</body></html>David H Nebinger2016-06-01T12:45:26ZRE: Service builder call custom java classSamuel Konghttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=748044412016-06-01T08:50:25Z2016-06-01T08:50:25ZFooLocalService is in your portlet's xxx-service.jar. You need to make sure ValidationUtil is also in xxx-service.jar. So you need to move your class to /WEB-INF/service. However, you should probably take a hard look at your code and check if it really make sense to have ValidationUtil in service jar.Samuel Kong2016-06-01T08:50:25ZRE: Service builder call custom java classS Hhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=747929932016-06-01T05:32:43Z2016-06-01T05:32:43ZThanks for the reply.<br /><br />The ValidationUtil I am using here is just an example. Eventually I want to achieve is I could use my own java object in freemarker ADT.<br /><br />I already know I can use below syntax in freemarker ADT to get the reference to the service.<br /><#assign myservice =serviceLocator.findService("my-service-portlet","com.liferay.sample.service.FooLocalService") /><br /><br />Next step I am trying to do is using this service to return my own object to the ADT,<br />so something like below<br /><#assign myObject = myservice.getMyObject()/><br /><br />But now the issue is if I define myObject as a member variable , it will throw the error when using "build service"<br /><br />Another reason I want to use service builder is that it can be used as web service to third party.<br /><br />If this approach is not working, then is there any other approach I can use to achieve my goal?<br /><br /><br /><div class="quote-title">Gautam Sharma:</div><blockquote>Hi <br /><br />As far as i know we can't use custom classes within service builder generated code.<br /><br />Alternatively you can validate your data before passing to <Entity>LocalServiceImpl classes.<br /><br />Thanks<br />Gautam</blockquote>S H2016-06-01T05:32:43ZRE: Service builder call custom java classGautam Sharmahttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=747971472016-06-01T05:11:49Z2016-06-01T05:11:49ZHi <br /><br />As far as i know we can't use custom classes within service builder generated code.<br /><br />Alternatively you can validate your data before passing to <Entity>LocalServiceImpl classes.<br /><br />Thanks<br />GautamGautam Sharma2016-06-01T05:11:49ZService builder call custom java classS Hhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=747926202016-06-01T02:43:11Z2016-06-01T02:43:11ZHi All,<br /><br />I am using liferay 6.2 and trying to create a service builder to write some own logic. I started with the eclipse sample code FooLocalService.<br /><br />I created a new package and class below , it is simple and just for testing purpose.<br /><br />package com.liferay.sample.utils;<br /><br />public class ValidationUtil {<br /><br /> public static Boolean validate(){<br /> return false;<br /> }<br />}<br /><br />And then in my FooLocalServiceImpl . I put below code and run the "build service", there is no issue.<br />public class FooLocalServiceImpl extends FooLocalServiceBaseImpl {<br /> public Boolean validate(){<br /> return ValidationUtil.validate();<br /> } <br />} <br /><br /><br />However, when I add ValidationUtil as member variable in the FooLocalServiceImpl , then issue happen. The code is like below<br /><br />public class FooLocalServiceImpl extends FooLocalServiceBaseImpl {<br /> public Boolean validate(){<br /> return ValidationUtil.validate();<br /> }<br /> <br /> private ValidationUtil util;<br /><br /> public ValidationUtil getUtil() {<br /> return util;<br /> }<br /> <br />}<br /><br />And when I run "build service", below error in the log. I am not sure why the error happen when add ValidationUtil as member variable into FooLocalServiceImpl. Is the way I am using wrong?<br /><br /> Compiling 4 source files to C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service-classes<br /> [javac] ----------<br /> [javac] 1. ERROR in C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service\com\liferay\sample\service\FooLocalService.java (at line 254)<br /> [javac] public com.liferay.sample.utils.ValidationUtil getUtil();<br /> [javac] ^^^^^^^^^^^^^^^^^^^^^^^^<br /> [javac] com.liferay.sample.utils cannot be resolved to a type<br /> [javac] ----------<br /> [javac] ----------<br /> [javac] 2. ERROR in C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service\com\liferay\sample\service\FooLocalServiceClp.java (at line 688)<br /> [javac] public com.liferay.sample.utils.ValidationUtil getUtil() {<br /> [javac] ^^^^^^^^^^^^^^^^^^^^^^^^<br /> [javac] com.liferay.sample.utils cannot be resolved to a type<br /> [javac] ----------<br /> [javac] 3. ERROR in C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service\com\liferay\sample\service\FooLocalServiceClp.java (at line 707)<br /> [javac] return (com.liferay.sample.utils.ValidationUtil)ClpSerializer.translateOutput(returnObj);<br /> [javac] ^^^^^^^^^^^^^^^^^^^^^^^^<br /> [javac] com.liferay.sample.utils cannot be resolved to a type<br /> [javac] ----------<br /> [javac] ----------<br /> [javac] 4. ERROR in C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service\com\liferay\sample\service\FooLocalServiceUtil.java (at line 278)<br /> [javac] public static com.liferay.sample.utils.ValidationUtil getUtil() {<br /> [javac] ^^^^^^^^^^^^^^^^^^^^^^^^<br /> [javac] com.liferay.sample.utils cannot be resolved to a type<br /> [javac] ----------<br /> [javac] 5. ERROR in C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service\com\liferay\sample\service\FooLocalServiceUtil.java (at line 279)<br /> [javac] return getService().getUtil();<br /> [javac] ^^^^^^^<br /> [javac] The method getUtil() from the type FooLocalService refers to the missing type ValidationUtil<br /> [javac] ----------<br /> [javac] ----------<br /> [javac] 6. ERROR in C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service\com\liferay\sample\service\FooLocalServiceWrapper.java (at line 287)<br /> [javac] public com.liferay.sample.utils.ValidationUtil getUtil() {<br /> [javac] ^^^^^^^^^^^^^^^^^^^^^^^^<br /> [javac] com.liferay.sample.utils cannot be resolved to a type<br /> [javac] ----------<br /> [javac] 7. ERROR in C:\Users\344773\Downloads\liferay\liferay-plugins-sdk-6.2\portlets\www-service-portlet\docroot\WEB-INF\service\com\liferay\sample\service\F