Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Corona management simple object"

 
Line 3: Line 3:
 
<code><pre>
 
<code><pre>
 
package org.eclipse.corona.wsdm.example;
 
package org.eclipse.corona.wsdm.example;
 +
 +
 +
import org.osgi.framework.ServiceReference;
 +
import org.osgi.service.component.ComponentContext;
 +
import org.eclipse.corona.management.common.ContributionManager;
  
 
public class SimpleJavaObject {
 
public class SimpleJavaObject {
Line 15: Line 20:
 
return "Property";
 
return "Property";
 
}
 
}
 +
 +
ContributionManager manager;
 +
 +
//Declarative Services methods
 +
protected void activate(ComponentContext ctx)
 +
{
 +
ServiceReference ref = ctx.getBundleContext().getServiceReference(ContributionManager.class.getName());
 +
manager = (ContributionManager)ctx.getBundleContext().getService(ref);
 +
 +
manager.manage(this);
 +
}
 +
 +
protected void deactivate(ComponentContext ctxt)
 +
{
 +
manager.remove(this);
 +
}
 +
  
 
//Other non-management oprations and properties
 
//Other non-management oprations and properties
Line 25: Line 47:
 
<code><pre>
 
<code><pre>
 
package org.eclipse.corona.wsdm.example;
 
package org.eclipse.corona.wsdm.example;
 +
 +
 +
import org.osgi.framework.ServiceReference;
 +
import org.osgi.service.component.ComponentContext;
 +
import org.eclipse.corona.management.common.ContributionManager;
  
 
import org.eclipse.corona.management.annotations.ManagedOperation;
 
import org.eclipse.corona.management.annotations.ManagedOperation;
 
import org.eclipse.corona.management.annotations.ManagedProperty;
 
import org.eclipse.corona.management.annotations.ManagedProperty;
 
import org.eclipse.corona.management.annotations.ManagedResource;
 
import org.eclipse.corona.management.annotations.ManagedResource;
 +
  
 
@ManagedResource(namespace="http://org.eclipse.corona.wsdm.example")
 
@ManagedResource(namespace="http://org.eclipse.corona.wsdm.example")
Line 44: Line 72:
 
return "Property";
 
return "Property";
 
}
 
}
 +
 +
ContributionManager manager;
 +
 +
//Declarative Services methods
 +
protected void activate(ComponentContext ctx)
 +
{
 +
ServiceReference ref = ctx.getBundleContext().getServiceReference(ContributionManager.class.getName());
 +
manager = (ContributionManager)ctx.getBundleContext().getService(ref);
 +
 +
manager.manage(this);
 +
}
 +
 +
protected void deactivate(ComponentContext ctxt)
 +
{
 +
manager.remove(this);
 +
}
 +
  
 
//Other non-management oprations and properties
 
//Other non-management oprations and properties
Line 57: Line 102:
 
manager.manage(objInstance );
 
manager.manage(objInstance );
 
</pre></code>
 
</pre></code>
 +
 +
 +
[[Corona_HowTo_Management_Annotations|Return to Tutorial]]

Latest revision as of 10:21, 30 April 2007

Enabling a Java class for management is a relatively easy process. Consider the following class, which contains a property prop and an operation doOperation(), which comprise the management interface for the class SimpleJavaObject.

package org.eclipse.corona.wsdm.example;


import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.eclipse.corona.management.common.ContributionManager;

public class SimpleJavaObject {

	private String prop;

	public void doOperation() {
		System.out.println("invoked operation");
	}

	public String getProp() {
		return "Property";
	}
	
	ContributionManager manager;
	
	//Declarative Services methods
	protected void activate(ComponentContext ctx)
	{
		ServiceReference ref = ctx.getBundleContext().getServiceReference(ContributionManager.class.getName());
		manager = (ContributionManager)ctx.getBundleContext().getService(ref);

		manager.manage(this);
	}
	
	protected void deactivate(ComponentContext ctxt)
	{
		manager.remove(this);
	}
	

	//Other non-management oprations and properties

}

Instances of the class can be made manageable by adding the following annotations:

package org.eclipse.corona.wsdm.example;


import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.eclipse.corona.management.common.ContributionManager;

import org.eclipse.corona.management.annotations.ManagedOperation;
import org.eclipse.corona.management.annotations.ManagedProperty;
import org.eclipse.corona.management.annotations.ManagedResource;


@ManagedResource(namespace="http://org.eclipse.corona.wsdm.example")
public class SimpleJavaObject {

	@ManagedProperty
	private String prop;

	@ManagedOperation
	public void doOperation() {
		System.out.println("invoked operation");
	}

	public String getProp() {
		return "Property";
	}
	
	ContributionManager manager;
	
	//Declarative Services methods
	protected void activate(ComponentContext ctx)
	{
		ServiceReference ref = ctx.getBundleContext().getServiceReference(ContributionManager.class.getName());
		manager = (ContributionManager)ctx.getBundleContext().getService(ref);

		manager.manage(this);
	}
	
	protected void deactivate(ComponentContext ctxt)
	{
		manager.remove(this);
	}
	

	//Other non-management oprations and properties

}

Instances of the class are then registered with the Management Runtime using the ContributionManager<Link to Obtaining the Management Enabling environment> as follows:


	SimpleJavaObject objInstance = new SimpleJavaObject();
	manager.manage(objInstance );


Return to Tutorial

Back to the top