Vista combinada Visión Plana Vista de árbol
Discusiones [ Anterior | Siguiente ]
toggle
Pepe Perez
Using classes form portal-impl.jar
19 de diciembre de 2012 9:50
Respuesta

Pepe Perez

Ranking: New Member

Mensajes: 10

Fecha de incorporación: 18 de septiembre de 2012

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.
David H Nebinger
RE: Using classes form portal-impl.jar
19 de diciembre de 2012 10:03
Respuesta

David H Nebinger

Ranking: Liferay Legend

Mensajes: 6455

Fecha de incorporación: 1 de septiembre de 2006

Mensajes recientes

You shouldn't be using it at all.

Instead, use the LogFactoryUtil class to get a logger:

1
2import com.liferay.portal.kernel.log.Log;
3import com.liferay.portal.kernel.log.LogFactoryUtil;
4
5...
6
7private static final Log logger = LogFactoryUtil.getLog(MyClass.class);
Pepe Perez
RE: Using classes form portal-impl.jar
20 de diciembre de 2012 1:24
Respuesta

Pepe Perez

Ranking: New Member

Mensajes: 10

Fecha de incorporación: 18 de septiembre de 2012

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.
Sushil Saini
RE: Using classes form portal-impl.jar
20 de diciembre de 2012 4:35
Respuesta

Sushil Saini

Ranking: Regular Member

Mensajes: 102

Fecha de incorporación: 27 de julio de 2011

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
1
2<Context>
3    <Loader
4        loaderClass="com.liferay.support.tomcat.loader.PortalClassLoader"
5    />
6</Context>


Cheers
Sushil Saini
Sushil Saini
RE: Using classes form portal-impl.jar
20 de diciembre de 2012 4:37
Respuesta

Sushil Saini

Ranking: Regular Member

Mensajes: 102

Fecha de incorporación: 27 de julio de 2011

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
RE: Using classes form portal-impl.jar
20 de diciembre de 2012 7:16
Respuesta

Pepe Perez

Ranking: New Member

Mensajes: 10

Fecha de incorporación: 18 de septiembre de 2012

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.
David H Nebinger
RE: Using classes form portal-impl.jar
20 de diciembre de 2012 7:55
Respuesta

David H Nebinger

Ranking: Liferay Legend

Mensajes: 6455

Fecha de incorporación: 1 de septiembre de 2006

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:

1
2Field logFactory = LogFactoryUtil.class.getDeclaredField("_logFactory");
3
4logFactory.setAccessible(true);
5
6LogFactory 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
RE: Using classes form portal-impl.jar
20 de diciembre de 2012 8:34
Respuesta

Pepe Perez

Ranking: New Member

Mensajes: 10

Fecha de incorporación: 18 de septiembre de 2012

Mensajes recientes

Thanks David. You're very helpful.
Sushil Saini
RE: Using classes form portal-impl.jar
20 de diciembre de 2012 15:24
Respuesta

Sushil Saini

Ranking: Regular Member

Mensajes: 102

Fecha de incorporación: 27 de julio de 2011

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