Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Pepe Perez
Using classes form portal-impl.jar
December 19, 2012 9:50 AM
Answer

Pepe Perez

Rank: New Member

Posts: 14

Join Date: September 18, 2012

Recent Posts

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
December 19, 2012 10:03 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11770

Join Date: September 1, 2006

Recent Posts

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
December 20, 2012 1:24 AM
Answer

Pepe Perez

Rank: New Member

Posts: 14

Join Date: September 18, 2012

Recent Posts

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
December 20, 2012 4:35 AM
Answer

Sushil Saini

Rank: Regular Member

Posts: 104

Join Date: July 27, 2011

Recent Posts

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
December 20, 2012 4:37 AM
Answer

Sushil Saini

Rank: Regular Member

Posts: 104

Join Date: July 27, 2011

Recent Posts

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
December 20, 2012 7:16 AM
Answer

Pepe Perez

Rank: New Member

Posts: 14

Join Date: September 18, 2012

Recent Posts

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
December 20, 2012 7:55 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11770

Join Date: September 1, 2006

Recent Posts

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
December 20, 2012 8:34 AM
Answer

Pepe Perez

Rank: New Member

Posts: 14

Join Date: September 18, 2012

Recent Posts

Thanks David. You're very helpful.
Sushil Saini
RE: Using classes form portal-impl.jar
December 20, 2012 3:24 PM
Answer

Sushil Saini

Rank: Regular Member

Posts: 104

Join Date: July 27, 2011

Recent Posts

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