Foren

Home » Liferay Portal » English » 3. Development

Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Pepe Perez
Using classes form portal-impl.jar
19. Dezember 2012 09:50
Antwort

Pepe Perez

Rang: New Member

Nachrichten: 14

Eintrittsdatum: 18. September 2012

Neue Beiträge

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. Dezember 2012 10:03
Antwort

David H Nebinger

Rang: Liferay Legend

Nachrichten: 7153

Eintrittsdatum: 1. September 2006

Neue Beiträge

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. Dezember 2012 01:24
Antwort

Pepe Perez

Rang: New Member

Nachrichten: 14

Eintrittsdatum: 18. September 2012

Neue Beiträge

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. Dezember 2012 04:35
Antwort

Sushil Saini

Rang: Regular Member

Nachrichten: 104

Eintrittsdatum: 27. Juli 2011

Neue Beiträge

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. Dezember 2012 04:37
Antwort

Sushil Saini

Rang: Regular Member

Nachrichten: 104

Eintrittsdatum: 27. Juli 2011

Neue Beiträge

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. Dezember 2012 07:16
Antwort

Pepe Perez

Rang: New Member

Nachrichten: 14

Eintrittsdatum: 18. September 2012

Neue Beiträge

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. Dezember 2012 07:55
Antwort

David H Nebinger

Rang: Liferay Legend

Nachrichten: 7153

Eintrittsdatum: 1. September 2006

Neue Beiträge

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. Dezember 2012 08:34
Antwort

Pepe Perez

Rang: New Member

Nachrichten: 14

Eintrittsdatum: 18. September 2012

Neue Beiträge

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

Sushil Saini

Rang: Regular Member

Nachrichten: 104

Eintrittsdatum: 27. Juli 2011

Neue Beiträge

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