added a project that contains all samples used in the documentation: this is just a collection of code snippets, it's not meant to be deployed or run

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@893234 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/pom.xml b/dependencymanager/pom.xml
index 71b350f..1fc5f9f 100644
--- a/dependencymanager/pom.xml
+++ b/dependencymanager/pom.xml
@@ -36,6 +36,7 @@
     <module>core</module>
     <module>shell</module>
     <module>compat</module>
+    <module>samples</module>
     <module>test</module>
   </modules>
 </project>
diff --git a/dependencymanager/samples/pom.xml b/dependencymanager/samples/pom.xml
new file mode 100644
index 0000000..7b91f6c
--- /dev/null
+++ b/dependencymanager/samples/pom.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+  <!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements. See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to you under the Apache License, Version
+    2.0 (the "License"); you may not use this file except in compliance
+    with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+    applicable law or agreed to in writing, software distributed under
+    the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
+    OR CONDITIONS OF ANY KIND, either express or implied. See the
+    License for the specific language governing permissions and
+    limitations under the License.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix</artifactId>
+    <version>1.0.4</version>
+    <relativePath>../../pom/pom.xml</relativePath>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <name>Apache Felix Dependency Manager Samples</name>
+  <version>3.0.0-SNAPSHOT</version>
+  <artifactId>org.apache.felix.dependencymanager.samples</artifactId>
+  <dependencies>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>4.1.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>4.1.0</version>
+    </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.dependencymanager</artifactId>
+      <version>3.0.0-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+          <useDefaultManifestFile>false</useDefaultManifestFile>
+         <compilerArgument>-g</compilerArgument>
+          <compilerVersion>1.5</compilerVersion>
+        </configuration> 
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/dependencymanager/samples/src/main/java/samples/dependonconfiguration/Activator.java b/dependencymanager/samples/src/main/java/samples/dependonconfiguration/Activator.java
new file mode 100644
index 0000000..9364f24
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/dependonconfiguration/Activator.java
@@ -0,0 +1,18 @@
+package samples.dependonconfiguration;
+
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+
+public class Activator extends DependencyActivatorBase {
+    public void init(BundleContext context, DependencyManager manager) throws Exception {
+        manager.add(createService()
+            .setImplementation(Task.class)
+            .add(createConfigurationDependency()
+                .setPid("config.pid")
+            )
+        );
+    }
+    
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
+}
diff --git a/dependencymanager/samples/src/main/java/samples/dependonconfiguration/Task.java b/dependencymanager/samples/src/main/java/samples/dependonconfiguration/Task.java
new file mode 100644
index 0000000..4c439b4
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/dependonconfiguration/Task.java
@@ -0,0 +1,23 @@
+package samples.dependonconfiguration;
+
+import java.util.Dictionary;
+
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+
+public class Task implements ManagedService {
+    private String m_interval;
+
+    public void execute() {
+        System.out.println("Scheduling task with interval " + m_interval);
+    }
+
+    public void updated(Dictionary properties) throws ConfigurationException {
+        if (properties != null) {
+            m_interval = (String) properties.get("interval");
+            if (m_interval == null) {
+                throw new ConfigurationException("interval", "must be specified");
+            }
+        }
+    }
+}
diff --git a/dependencymanager/samples/src/main/java/samples/dependonservice/Activator.java b/dependencymanager/samples/src/main/java/samples/dependonservice/Activator.java
new file mode 100644
index 0000000..0a0ee26
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/dependonservice/Activator.java
@@ -0,0 +1,24 @@
+package samples.dependonservice;
+
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+public class Activator extends DependencyActivatorBase {
+    public void init(BundleContext context, DependencyManager manager) throws Exception {
+        manager.add(createService()
+            .setImplementation(DataGenerator.class)
+            .add(createServiceDependency()
+                .setService(Store.class)
+                .setRequired(true)
+            )
+            .add(createServiceDependency()
+                .setService(LogService.class)
+                .setRequired(false)
+            )
+        );
+    }
+    
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
+}
diff --git a/dependencymanager/samples/src/main/java/samples/dependonservice/DataGenerator.java b/dependencymanager/samples/src/main/java/samples/dependonservice/DataGenerator.java
new file mode 100644
index 0000000..f24ef43
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/dependonservice/DataGenerator.java
@@ -0,0 +1,15 @@
+package samples.dependonservice;
+
+import org.osgi.service.log.LogService;
+
+public class DataGenerator {
+    private volatile Store m_store;
+    private volatile LogService m_log;
+    
+    public void generate() {
+        for (int i = 0; i < 10; i++) {
+            m_store.put("#" + i, "value_" + i);
+        }
+        m_log.log(LogService.LOG_INFO, "Data generated.");
+    }
+}
diff --git a/dependencymanager/samples/src/main/java/samples/dependonservice/Store.java b/dependencymanager/samples/src/main/java/samples/dependonservice/Store.java
new file mode 100644
index 0000000..8d111cf
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/dependonservice/Store.java
@@ -0,0 +1,6 @@
+package samples.dependonservice;
+
+public interface Store {
+    public void put(String key, Object value);
+    public Object get(String key);
+}
diff --git a/dependencymanager/samples/src/main/java/samples/registerservice/Activator.java b/dependencymanager/samples/src/main/java/samples/registerservice/Activator.java
new file mode 100644
index 0000000..9b437fd
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/registerservice/Activator.java
@@ -0,0 +1,16 @@
+package samples.registerservice;
+
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+
+public class Activator extends DependencyActivatorBase {
+    public void init(BundleContext context, DependencyManager manager) throws Exception {
+        manager.add(createService()
+            .setInterface(Store.class.getName(), null)
+            .setImplementation(MemoryStore.class)
+        );
+    }
+    
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
+}
diff --git a/dependencymanager/samples/src/main/java/samples/registerservice/MemoryStore.java b/dependencymanager/samples/src/main/java/samples/registerservice/MemoryStore.java
new file mode 100644
index 0000000..5b44151
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/registerservice/MemoryStore.java
@@ -0,0 +1,16 @@
+package samples.registerservice;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class MemoryStore implements Store {
+    private Map m_map = new HashMap();
+
+    public Object get(String key) {
+        return m_map.get(key);
+    }
+
+    public void put(String key, Object value) {
+        m_map.put(key, value);
+    }
+}
diff --git a/dependencymanager/samples/src/main/java/samples/registerservice/Store.java b/dependencymanager/samples/src/main/java/samples/registerservice/Store.java
new file mode 100644
index 0000000..90dcf5b
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/registerservice/Store.java
@@ -0,0 +1,6 @@
+package samples.registerservice;
+
+public interface Store {
+    public void put(String key, Object value);
+    public Object get(String key);
+}
diff --git a/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/Activator.java b/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/Activator.java
new file mode 100644
index 0000000..4fb8883
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/Activator.java
@@ -0,0 +1,20 @@
+package samples.trackingserviceswithcallbacks;
+
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+
+public class Activator extends DependencyActivatorBase {
+    public void init(BundleContext context, DependencyManager manager) throws Exception {
+        manager.add(createService()
+            .setImplementation(DocumentTranslator.class)
+            .add(createServiceDependency()
+                .setService(Translator.class)
+                .setRequired(false)
+                .setCallbacks("added", "removed")
+            )
+        );
+    }
+    
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
+}
diff --git a/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/DocumentTranslator.java b/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/DocumentTranslator.java
new file mode 100644
index 0000000..41848eb
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/DocumentTranslator.java
@@ -0,0 +1,27 @@
+package samples.trackingserviceswithcallbacks;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.text.Document;
+
+public class DocumentTranslator {
+    private List<Translator> m_translators = new ArrayList<Translator>();
+    
+    public void added(Translator translator) {
+        m_translators.add(translator);
+    }
+    
+    public void removed(Translator translator) {
+        m_translators.remove(translator);
+    }
+    
+    public Document translate(Document document, String from, String to) {
+        for (Translator translator : m_translators) {
+            if (translator.canTranslate(from, to)) {
+                return translator.translate(document, from, to);
+            }
+        }
+        return null;
+    }
+}
diff --git a/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/Translator.java b/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/Translator.java
new file mode 100644
index 0000000..de7ac30
--- /dev/null
+++ b/dependencymanager/samples/src/main/java/samples/trackingserviceswithcallbacks/Translator.java
@@ -0,0 +1,8 @@
+package samples.trackingserviceswithcallbacks;
+
+import javax.swing.text.Document;
+
+public interface Translator {
+    public boolean canTranslate(String from, String to);
+    public Document translate(Document document, String from, String to);
+}