Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Access objects from multiple parts of an Eclipse RCP Application(How to get an object (e.g. a logger) in one Part accessable by another Part (e.g. a server))
Access objects from multiple parts of an Eclipse RCP Application [message #1695709] Mon, 18 May 2015 13:53 Go to next message
Bastian Hesselbarth is currently offline Bastian HesselbarthFriend
Messages: 2
Registered: May 2015
Junior Member
Hello together,

I'm quite new with creating RCP-Applications and using "Dependency Injection".

What I plan to do / have done so far:
I have set up an Application.e4mi by using the Eclipse 4 Model Editor. In a Trimmed Window I have organized several Parts.

One of these Parts is meant to be a console for logging, another is a server, which shall use the logger. Now I want to bind the logger to the Application context, to make it accessable for the server, therefore I use
application.getContext().set("MainLogger", logger);

The whole logger-part looks like this.
public class ConsolePart {
	
	private Logger logger = Logger.getLogger("MainLogger");
	private Text consoleText;
	
	@Inject
	public ConsolePart(MApplication application) {
		application.getContext().set("MainLogger", logger);
	}
	
	@PostConstruct
	public void postConstruct(Composite parent) {
		consoleText = new Text(parent, SWT.NORMAL);
		logger.addHandler(new Handler()  {

			@Override
			public void close() throws SecurityException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void flush() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void publish(LogRecord arg0) {
				// TODO Auto-generated method stub
				consoleText.append(arg0.getMessage());
			}
			
		});
		logger.info("Logger ready");
	}
}


In the Server-Part I try to inject the logger from the context and use it there
	
@Inject
private Logger logger;

But I get an Exeption / the logger object equals null.
Here is the whole server class
public class ServerPart {
	
	// User Interface Attributes
	private Label serverInformationLabel;
	private String serverInformation;
	private Server server;
	
	@Inject
	private Logger logger;
	
	@Inject
	public ServerPart(MApplication application) {
		// Functional things
		server = new Server(logger);
		application.getContext().set("server", server);
	}
	
	@PostConstruct
	public void postConstruct(Composite parent) {
		
		serverInformationLabel = new Label(parent, SWT.NO_FOCUS);
		serverInformationLabel.setAlignment(SWT.CENTER);
		serverInformation = server.getServerInformation();
		serverInformationLabel.setText(serverInformation);
		logger.info("test");
	}
		
}

My target is to share objects under several parts (the ServerPart won't be the only one to use the logger, and there will be other parts, that have to use the server object, provided by the ServerPart)
How is the normal way to do this? What am I doing wrong?

Best regards,
Bastian
Re: Access objects from multiple parts of an Eclipse RCP Application [message #1695866 is a reply to message #1695709] Tue, 19 May 2015 20:46 Go to previous message
Guenther Mahr is currently offline Guenther MahrFriend
Messages: 25
Registered: September 2011
Junior Member
The problem is that you set the logger into the context using the name "MyLogger" but you expect to inject it just as "Logger".

If you want to use a name then you have to inject it in the following way:
@ Inject
@ Named ("MyLogger")
Logger logger;

but it may be more convenient just to put it into the context using the class object like

context.set(Logger.class, logger);

and then inject it like this:

@Inject
Logger logger;

For a first understanding of E4 Dependency Injection see the excellent tutorial by Maximilan Koegel and Jonas Helming:
http://eclipsesource.com/blogs/tutorials/eclipse-4-e4-tutorial-part-4-dependency-injection-basics/

That said, there are other easier ways to make a standard logger available in all parts of an application like e.g. with Logback using a system variable that points to the configuration file logback.xml.

Previous Topic:Re-injection of a Java collection
Next Topic:Share Objects with MApplication.getContext()
Goto Forum:
  


Current Time: Mon Dec 18 03:24:05 GMT 2017

Powered by FUDForum. Page generated in 0.01940 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software