Felix Meschberger wrote:
Hi Alexander,
Alexander Shutyaev schrieb:
Thanks for your help! I checked on your example. It looks like this is
the thing.
Glad to hear ;-)
But I'm a little bit of newbie in OSGi, so I'm not confident
in two things:
1) How should I declare that component in a xml-file in that case.
2) How should I construct new Configuration objects in that case.
If you could you give me the svn-links that clarify these two questions
for Apache's 'RequestLoggerService' it would be very great.
In Apache Felix we have a Maven 2 plugin, which constructs the
Declarative Services declaration from the @scr.* tags in the Java source
file. You might want to check that out. Otherwise you might want to
check out the Sling Engine bundle from [1], which encloses an
OSGI-INF/serviceComponents.xml file, which contains the declaration for
the RequestLoggerService.
As for the generation of configuration objects: You can manually create
those, by getting the Configuration Admin Service and call
createFactoryConfiguration on it:
ConfigurationAdmin ca = getConfigurationAdmin();
Configuration config =
ca.createFactoryConfiguration("the.factory.pid");
Properties props = new Properties();
// prepare configuration properties
config.update(props);
Hope this helps.
Regards
Felix
[1]
http://people.apache.org/repo/m2-incubating-repository/org/apache/sling/org.apache.sling.engine/2.0.2-incubator/org.apache.sling.engine-2.0.2-incubator.jar
_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev
Hi Felix,
I did everything like in the example but there are still some problems.
Maybe I do something wrong. I extracted the part concerning this matter
from my project and added them as attachments, so you could see the
source code. I have two bundles:
1) ru.cma.pie4j that has Core.java, Module.java and pie4j_config.xml
2) ru.cma.pie4j.emailfetcher that has EMailFetcher.java and
pie4j_module.xml
Module.java is an abstract class that provides common functionality. And
EMailFetcher.java extends this class.
Everything goes fine at first. The configurations are posted by config
admin:
75 [Component Resolve Thread (Bundle 8)] INFO Core - instance 1
configured
117 [Component Resolve Thread (Bundle 8)] INFO Core - instance 2
configured
but the EMailFetcher outputs nothing and when I run the 'ls -c' command
it shows that there are no instances of EMailFetcher (neither it sees
the configurations)
2 Component[
name = ru.cma.pie4j.emailfetcher.EMailFetcher
autoenable = true
factory = ru.cma.pie4j.factory.EMailFetcher
immediate = false
implementation = ru.cma.pie4j.emailfetcher.EMailFetcher
properties = null
serviceFactory = false
serviceInterface = [ru.cma.pie4j.Module]
references = null
located in bundle = ru.cma.pie4j.emailfetcher_1.0.0.qualifier [5]
]
Dynamic information :
The component is resolved
All component references are resolved
Component configurations :
Configuration properties:
component.name = ru.cma.pie4j.emailfetcher.EMailFetcher
component.id = 1
objectClass = Object[ru.cma.pie4j.Module]
Instances:
Thanks in advance for your help.
package ru.cma.pie4j;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
public class Core
{
private static Logger logger = Logger.getLogger("Core");
private ConfigurationAdmin ca = null;
public Core()
{
DOMConfigurator.configure("log4j_config.xml");
logger.info("Core started.");
}
protected void bindCA(ConfigurationAdmin ca)
{
this.ca = ca;
}
protected void unbindCA(ConfigurationAdmin ca)
{
this.ca = null;
}
protected void addModule(Module m)
{
logger.info("Adding module");
}
protected void removeModule(Module m)
{
logger.info("Removing module");
}
protected void activate(ComponentContext context)
{
Configuration config;
try
{
config =
ca.createFactoryConfiguration("ru.cma.pie4j.emailfetcher.EMailFetcher.Factory");
Properties props = new Properties();
props.put("myproperty", "hello, instance 1");
config.update(props);
logger.info("instance 1 configured");
config =
ca.createFactoryConfiguration("ru.cma.pie4j.emailfetcher.EMailFetcher.Factory");
props = new Properties();
props.put("myproperty", "hello, instance 2");
config.update(props);
logger.info("instance 2 configured");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package ru.cma.pie4j;
import java.util.Dictionary;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
public abstract class Module
{
BundleContext bundleContext = null;
@SuppressWarnings("unchecked")
protected abstract boolean setProperties(Dictionary properties);
protected void activate(ComponentContext componentContext)
{
this.bundleContext = componentContext.getBundleContext();
this.setProperties(componentContext.getProperties());
}
protected void deactivate(ComponentContext componentContext)
{
this.bundleContext = null;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<component name="pie4j.Core">
<implementation class="ru.cma.pie4j.Core"/>
<reference name="pie4j.Module" interface="ru.cma.pie4j.Module" cardinality="0..n" bind="addModule" unbind="removeModule"/>
<reference name="CA" interface="org.osgi.service.cm.ConfigurationAdmin" bind="bindCA" unbind="unbindCA"/>
</component>
package ru.cma.pie4j.emailfetcher;
import java.util.Dictionary;
import ru.cma.pie4j.Module;
public class EMailFetcher extends Module
{
@SuppressWarnings("unchecked")
protected boolean setProperties(Dictionary properties)
{
System.out.println("myproperty = " +
properties.get("myproperty"));
return true;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<component name="ru.cma.pie4j.emailfetcher.EMailFetcher" factory="ru.cma.pie4j.factory.EMailFetcher">
<implementation class="ru.cma.pie4j.emailfetcher.EMailFetcher"/>
<service>
<provide interface="ru.cma.pie4j.Module"/>
</service>
</component>
_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev