Skip to content

Commit

Permalink
config
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Hoffmann <m.hoffmann@data-in-motion.biz>
  • Loading branch information
maho7791 committed May 18, 2023
1 parent 1e5d6c7 commit 423c1cb
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 0 deletions.
3 changes: 3 additions & 0 deletions org.gecko.playground.ds.config/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-buildpath: \
org.osgi.service.cm,\
org.osgi.util.promise
16 changes: 16 additions & 0 deletions org.gecko.playground.ds.config/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
":configurator:resource-version": 1,

"MyAlbumConfig~test1":
{
"name": "Funky",
"year": 1974,
"album": "Funky, Funky"
},
"MyAlbumConfig~test2":
{
"name": "Happy",
"year": 2010,
"album": "I wanna be shappy"
}
}
17 changes: 17 additions & 0 deletions org.gecko.playground.ds.config/launch-config.bndrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-runfw: org.apache.felix.framework;version='[7.0.5,7.0.5]'
-runee: JavaSE-17
-runrequires: \
bnd.identity;id='org.apache.felix.gogo.command',\
bnd.identity;id='org.apache.felix.gogo.shell',\
bnd.identity;id='org.gecko.playground.ds.config',\
bnd.identity;id='org.apache.felix.configadmin'
-runbundles: \
org.apache.felix.configadmin;version='[1.9.26,1.9.27)',\
org.apache.felix.gogo.command;version='[1.1.2,1.1.3)',\
org.apache.felix.gogo.runtime;version='[1.1.6,1.1.7)',\
org.apache.felix.gogo.shell;version='[1.1.4,1.1.5)',\
org.apache.felix.scr;version='[2.2.6,2.2.7)',\
org.gecko.playground.ds.config;version=snapshot,\
org.osgi.service.component;version='[1.5.1,1.5.2)',\
org.osgi.util.function;version='[1.2.0,1.2.1)',\
org.osgi.util.promise;version='[1.3.0,1.3.1)'
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2012 - 2022 Data In Motion and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Data In Motion - initial API and implementation
*/
package org.gecko.playground.ds.config;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;

@Component(name = "MyAlbumConfig", configurationPolicy = ConfigurationPolicy.REQUIRE)
public class AlbumComponent {

@Activate
public void activate(MyFancyConfig c) {
System.out.println("Activate " + c.name() + " from " + c.year() + "(" + c.album() + ") from instance " + this);
}

@Modified
public void modified(MyFancyConfig c) {
System.out.println("Modified " + c.name() + " from " + c.year() + "(" + c.album() + ") from instance " + this);

}

@Deactivate
public void deactivate(MyFancyConfig c) {
System.out.println("De-Activate " + c.name() + " from " + c.year() + "(" + c.album() + ") from instance " + this);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.gecko.playground.ds.config;

import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executors;

import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.util.promise.PromiseFactory;

@Component
public class AlbumCreator {

@Reference
private ConfigurationAdmin configAdmin;
private List<Configuration> configs = new LinkedList<>();

@Activate
public void activate() {
PromiseFactory pf = new PromiseFactory(Executors.newSingleThreadExecutor());
pf.submit(this::createConfigs);
}

private Void createConfigs() {
for (int i = 0; i < 10; i++) {
createConfiguration(i, 0);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
}
}
try {
Thread.sleep(2000l);
} catch (InterruptedException e) {
}
for (int i = 0; i < 10; i++) {
createConfiguration(i, 5);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
}
}
try {
Thread.sleep(2000l);
} catch (InterruptedException e) {
}
configs.forEach(c->{
try {
c.delete();
} catch (Exception e) {
// TODO: handle exception
}
});
configs.clear();
return null;
}

private void createConfiguration(int increment, int offset) {
Configuration configuration;
try {
configuration = configAdmin.getFactoryConfiguration("MyAlbumConfig", "album-" + increment, "?");
Dictionary<String, Object> props = new Hashtable<>();
props.put("name", "name-" + increment);
props.put("album", "album-" + increment + offset);
props.put("year", 2000 + increment + offset);
configuration.updateIfDifferent(props);
configs.add(configuration);
} catch (IOException e) {
System.out.println("Error creating configuration: " + e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2012 - 2022 Data In Motion and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Data In Motion - initial API and implementation
*/
package org.gecko.playground.ds.config;

public @interface MyFancyConfig {

String name();
int year() default 42;
String album() default "nothing to say";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.gecko.playground.ds.config;

0 comments on commit 423c1cb

Please sign in to comment.