Thanks , so i need to implement ManageService to configure my instance :)
Anyway , please help me correct my code ,
<pre>
package com.insight_tec.ipojo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
public class ManageServiceImpl implements ManageService, Runnable {
ConfigurationAdmin pm;
boolean status;
boolean created = false;
Map<String, String> mapConf = null;
// Create an service instance with configuration from file using
ConfigurationAdmin
public void create() throws IOException {
Properties props = new Properties();
mapConf = this.processCfgFile(new File("C:\\ipojo.conf"));
String type = mapConf.get("service.factoryPid");
props.put("service.factoryPid", type);
mapConf.remove("service.factoryPid");
for (Map.Entry<String, String> entry : mapConf.entrySet()) {
props.put(entry.getKey(), entry.getValue());
}
Configuration conf = pm.createFactoryConfiguration(type);
conf.update(props);
created = true;
}
//Update existing service instance with configuration from file
public void update() throws InterruptedException {
if (created) {
Properties props = new Properties();
mapConf = this.processCfgFile(new
File("C:\\ipojo.conf"));
String type = mapConf.get("service.factoryPid");
for (Map.Entry<String, String> entry :
mapConf.entrySet()) {
props.put(entry.getKey(), entry.getValue());
}
try {
Configuration conf = pm.getConfiguration(type);
conf.update(props);
Configuration[] confs = null;
try {
confs = pm.listConfigurations(null);
for (int i = 0; i < confs.length; i++) {
System.out.println(confs[i].getPid() + " : "
+
confs[i].getProperties());
}
} catch (IOException e) {
System.out
.println("An error
occurs when listing configurations : "
+
e.getMessage());
} catch (InvalidSyntaxException e) {
System.out
.println("An error
occurs when listing configurations : "
+
e.getMessage());
}
} catch (IOException e) {
System.out.println(e);
}
} else {
try {
this.create();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*Process configuration file to get configuration data
configuration data file like this:
service.factoryPid=com.insight_tec.ipojo.Hello2
to=Someone-changed
*/
public Map<String, String> processCfgFile(File file) {
Map<String, String> mapConf = new HashMap<String, String>();
if (file.exists()) {
try {
BufferedReader bf = new BufferedReader(new
FileReader(file));
String line = null;
while ((line = bf.readLine()) != null) {
String[] tmp = line.split("=");
mapConf.put(tmp[0], tmp[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mapConf;
} else {
System.out.print("File " + file.getName() + " not found
!");
return null;
}
}
public void run() {
//while (status) {
try {
this.update();
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}
}
public void start() {
Thread t = new Thread(this);
t.start();
status = true;
}
public void stop() {
status = false;
}
public void registered(ServiceReference ref) {
System.out.println("Registered" + ref);
}
public void unregistered(ServiceReference ref) {
System.out.println("Unregistered" + ref);
}
}
</pre>
XML iPOJO config:
<?xml version="1.0" encoding="UTF-8"?>
<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="org.apache.felix.ipojo
http://felix.apache.org/ipojo/schemas/CURRENT/core.xsd"
xmlns="org.apache.felix.ipojo">
<component classname="com.insight_tec.ipojo.ManageServiceImpl"
name="ManageServiceImplComponent" architecture="false">
<requires field="pm" />
<callback transition="validate" method="update" />
<provides />
<callback transition="validate" method="start" />
<callback transition="invalidate" method="stop" />
</component>
<component classname="com.insight_tec.ipojo.Hello2"
immediate="true" architecture="true" name="Hello2Component">
<callback transition="invalidate" method="stop" />
<properties >
<property field="m_name" name="to" method="setName" />
</properties>
</component>
<instance component="ManageServiceImplComponent"
name="ManageServiceImplInstance" />
</ipojo>
I was using Configuration to create instance but only configuration is
create( using command list_conf in your demo about ConfigurationAdmin and
iPOJO
<http://felix.apache.org/site/combining-ipojo-and-configuration-admin.html>
) ,instance is not (i using arch command to check instance and no more
created after every time update function run)
--
View this message in context:
http://apache-felix.18485.x6.nabble.com/ipojo-create-instance-with-configuration-using-xml-tp5006611p5006635.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]