You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by classical_89 <lu...@gmail.com> on 2013/12/27 09:26:45 UTC

[ipojo] create instance with configuration using xml

I'm new to ipojo and OSGi platform i not sure that my question is reasonable
,so tell me if i wrong
I know that i can create an instance service using Configuration by :

ConfigurationAdmin pm;
Configuration conf = pm.creatFactorConfiguration(type);
conf.update(props)

so it will create an instance and  instance's configuration will be store in
Configuration
(I can list Configuration to see it)
Now i want to creat another instance using XML metadata like:
	<instance component="HelloComponent" name="HelloComponentInstance" >
          <properties>
            <property field="aFieldInHelloClass" value ="HELLO IPOJO"/>
          </properties>
       </instance>

So ,i want when this instance is created ,it also has a configuration
information (may be aFieldInHelloClass ) store in Configuration 
what can i do only with XML ?



--
View this message in context: http://apache-felix.18485.x6.nabble.com/ipojo-create-instance-with-configuration-using-xml-tp5006611.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: [ipojo] create instance with configuration using xml

Posted by classical_89 <lu...@gmail.com>.
Many thanks ,i will continue with iPOJO and waiting for 2.0 version  



--
View this message in context: http://apache-felix.18485.x6.nabble.com/ipojo-create-instance-with-configuration-using-xml-tp5006611p5006634.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: [ipojo] create instance with configuration using xml

Posted by classical_89 <lu...@gmail.com>.
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: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: [ipojo] create instance with configuration using xml

Posted by Clement Escoffier <cl...@gmail.com>.
Hi,

Sent from my iPad

> On 27 déc. 2013, at 09:26, classical_89 <lu...@gmail.com> wrote:
> 
> I'm new to ipojo and OSGi platform i not sure that my question is reasonable
> ,so tell me if i wrong
> I know that i can create an instance service using Configuration by :
> 
> ConfigurationAdmin pm;
> Configuration conf = pm.creatFactorConfiguration(type);
> conf.update(props)
> 
> so it will create an instance and  instance's configuration will be store in
> Configuration
> (I can list Configuration to see it)
> Now i want to creat another instance using XML metadata like:
>    <instance component="HelloComponent" name="HelloComponentInstance" >
>          <properties>
>            <property field="aFieldInHelloClass" value ="HELLO IPOJO"/>
>          </properties>
>       </instance>
> 
> So ,i want when this instance is created ,it also has a configuration
> information (may be aFieldInHelloClass ) store in Configuration 
> what can i do only with XML ?
> 

You are creating an instance using an factory configuration. By using this way you are creating the instance and configuring it. Updating the configuration reconfigures the instance. 

For what you want you should use a regular configuration (not factory) and let your instance (declared in XML) be bound to this configuration. To do that follow  the instructions from http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/configuration-handler.html#exposing-a-managed-service

Best regards,

Clement

> 
> 
> --
> View this message in context: http://apache-felix.18485.x6.nabble.com/ipojo-create-instance-with-configuration-using-xml-tp5006611.html
> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org