Foros de discusión

Using classes form portal-impl.jar

Pepe Perez, modificado hace 11 años.

Using classes form portal-impl.jar

New Member Mensajes: 14 Fecha de incorporación: 18/09/12 Mensajes recientes
Hi,

I'm creating a new portlet using Liferay 6.1.1 CE GA2 version, and I'm compiling it with Maven 3.0.4. I need to create a new instance of the "com.liferay.portal.log.Log4jLogFactoryImpl" class from my portlet, and this class just belongs to "portal-impl" library.

As Mika Koivisto says in his blog, "portal-impl" should never be added as dependency to a Liferay plugin:

http://www.liferay.com/web/mika.koivisto/blog/-/blogs/4375832

So, how could I reference this class without using this dependency?

Thanks.
thumbnail
David H Nebinger, modificado hace 11 años.

RE: Using classes form portal-impl.jar

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
You shouldn't be using it at all.

Instead, use the LogFactoryUtil class to get a logger:


import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

...

private static final Log logger = LogFactoryUtil.getLog(MyClass.class);
Pepe Perez, modificado hace 11 años.

RE: Using classes form portal-impl.jar

New Member Mensajes: 14 Fecha de incorporación: 18/09/12 Mensajes recientes
Hi,

Sorry, I should explain what I'm trying to do. I'm making a portlet which is able to add, dynamically, new loggers to Log4J system. These loggers can be configured, specifing the file where it writes, the layout, the maximum size of the file...

This is the example code:


DOMConfigurator domConfigurator = new DOMConfigurator();

domConfigurator.doConfigure(reader, LogManager.getLoggerRepository());

Log4JUtil.setLevel(loggerName, loggerLevel, false);

LogFactoryUtil.setLogFactory(new com.liferay.portal.log.Log4jLogFactoryImpl());


Variables:

- reader: where xml configuration logger is stored.

- loggerName, loggerLevel: no need to explain.


So, that's why I need to have access to the "Log4jLogFactoryImpl" class.

Thanks.
thumbnail
Sushil Kumar Saini, modificado hace 11 años.

RE: Using classes form portal-impl.jar

Regular Member Mensajes: 104 Fecha de incorporación: 27/07/11 Mensajes recientes
Hi Pepe,

For the functionality which you want to achieve, I think, you will have to configure your portlet to use the Portal Webapp Class Loader. In the tomcat you can do this by doing the following configuration in the context.xml

<context>
	<loader loaderClass="com.liferay.support.tomcat.loader.PortalClassLoader" />
</context>


Cheers
Sushil Saini
thumbnail
Sushil Kumar Saini, modificado hace 11 años.

RE: Using classes form portal-impl.jar

Regular Member Mensajes: 104 Fecha de incorporación: 27/07/11 Mensajes recientes
Go to the following link to see the complete detail.

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+access+the+full+Liferay+API+from+a+portlet+application
Pepe Perez, modificado hace 11 años.

RE: Using classes form portal-impl.jar

New Member Mensajes: 14 Fecha de incorporación: 18/09/12 Mensajes recientes
Hi,

Thanks for your answers, but I'm not using Tomcat. My server is JBoss AS 7.1.2. Do you know how could I make it with this server?

Thanks again.
thumbnail
David H Nebinger, modificado hace 11 años.

RE: Using classes form portal-impl.jar

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
I think the crux of the problem here is that you're trying to use the logging framework for something other than logging...

Logging is meant to push information relevant to a developer to a persistent store in order to help them diagnose issues.

A developer has no need for an ability to dynamically add loggers to the logging system, they are always statically defined and rarely (if ever) changed.

But if you're trying to use the logging framework for some other purpose (i.e. some sort of crazy auditing scheme), then I think what you're doing is trying to pound a square peg into a round hole...

However, since you seem totally set on this, the answer for your problem is to use reflection on the LogFactoryUtil class:


Field logFactory = LogFactoryUtil.class.getDeclaredField("_logFactory");

logFactory.setAccessible(true);

LogFactory liferayLogFactory = (LogFactory) logFactory.get(null);


From here you can test to see if it is the Log4jLogFactoryImpl that you're looking for.

Since the liferayLogFactory class is actually in another class loader, you may not be able to cast and use it directly. You may need to use reflection exclusively to invoke methods on the returned value of logFactory.get(null) instead of trying to cast it to some type in your own class loader.
Pepe Perez, modificado hace 11 años.

RE: Using classes form portal-impl.jar

New Member Mensajes: 14 Fecha de incorporación: 18/09/12 Mensajes recientes
Thanks David. You're very helpful.
thumbnail
Sushil Kumar Saini, modificado hace 11 años.

RE: Using classes form portal-impl.jar

Regular Member Mensajes: 104 Fecha de incorporación: 27/07/11 Mensajes recientes
Hi Pepe,.... Its good if your problem is solved ....

But if you want to user single class loader mechanism .. you can go to the link which i have provided... It tells you how to setup single class loader in jboss server.

http://www.liferay.com/community/wiki/-/wiki/Main/Access+Liferay+API+from+Portlet#section-Access+Liferay+API+from+Portlet-JBoss-Tomcat