Added demo for events producer/consumer to let preview topics in webconsole

Signed-off-by: Lukasz Dywicki <luke@code-house.org>

git-svn-id: https://svn.apache.org/repos/asf/karaf/webconsole/trunk@1214515 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/examples/events/consumer/pom.xml b/examples/events/consumer/pom.xml
new file mode 100644
index 0000000..6c894b0
--- /dev/null
+++ b/examples/events/consumer/pom.xml
@@ -0,0 +1,58 @@
+<?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">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.karaf.webconsole.examples</groupId>
+        <artifactId>events</artifactId>
+        <version>0.3.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>org.apache.karaf.webconsole.examples.events</groupId>
+    <artifactId>consumer</artifactId>
+    <name>Apache Karaf :: WebConsole :: Examples :: Events :: Consumer</name>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <version>2.3.5</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+                        <Import-Package>
+                            *
+                        </Import-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/examples/events/consumer/src/main/java/org/apache/karaf/webconsole/examples/events/consumer/DemoConsumer.java b/examples/events/consumer/src/main/java/org/apache/karaf/webconsole/examples/events/consumer/DemoConsumer.java
new file mode 100644
index 0000000..b89443a
--- /dev/null
+++ b/examples/events/consumer/src/main/java/org/apache/karaf/webconsole/examples/events/consumer/DemoConsumer.java
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+package org.apache.karaf.webconsole.examples.events.consumer;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+
+/**
+ * Example brand provider which modify console look and feel.
+ */
+public class DemoConsumer implements EventHandler {
+
+    private Logger logger = Logger.getLogger(DemoConsumer.class.getName());
+
+    public void handleEvent(Event event) {
+        logger.log(Level.FINE, "Received event " + event);
+    }
+
+}
\ No newline at end of file
diff --git a/examples/events/consumer/src/main/resources/OSGI-INF/blueprint/event-consumer.xml b/examples/events/consumer/src/main/resources/OSGI-INF/blueprint/event-consumer.xml
new file mode 100644
index 0000000..f440e1d
--- /dev/null
+++ b/examples/events/consumer/src/main/resources/OSGI-INF/blueprint/event-consumer.xml
@@ -0,0 +1,39 @@
+<?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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <service ref="eventHandler" interface="org.osgi.service.event.EventHandler">
+        <service-properties>
+            <entry key="event.topics" value="org/apache/karaf/webconsole/examples" />
+        </service-properties>
+    </service>
+
+    <service ref="eventHandler" interface="org.osgi.service.event.EventHandler">
+        <service-properties>
+            <entry key="event.topics">
+                <array value-type="java.lang.String">
+                    <value>org/apache/karaf/webconsole/examples/first</value>
+                    <value>org/apache/karaf/webconsole/examples/second</value>
+                </array>
+            </entry>
+        </service-properties>
+    </service>
+
+    <bean id="eventHandler" class="org.apache.karaf.webconsole.examples.events.consumer.DemoConsumer" />
+
+</blueprint>
diff --git a/examples/events/pom.xml b/examples/events/pom.xml
new file mode 100644
index 0000000..4251ebe
--- /dev/null
+++ b/examples/events/pom.xml
@@ -0,0 +1,38 @@
+<?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">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.karaf.webconsole</groupId>
+        <artifactId>examples</artifactId>
+        <version>0.3.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>org.apache.karaf.webconsole.examples</groupId>
+    <artifactId>events</artifactId>
+    <name>Apache Karaf :: WebConsole :: Examples :: Events</name>
+    <packaging>pom</packaging>
+
+    <modules>
+        <module>consumer</module>
+        <module>provider</module>
+    </modules>
+
+</project>
diff --git a/examples/events/provider/pom.xml b/examples/events/provider/pom.xml
new file mode 100644
index 0000000..c1460f8
--- /dev/null
+++ b/examples/events/provider/pom.xml
@@ -0,0 +1,59 @@
+<?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">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.karaf.webconsole.examples</groupId>
+        <artifactId>events</artifactId>
+        <version>0.3.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>org.apache.karaf.webconsole.examples.events</groupId>
+    <artifactId>provider</artifactId>
+    <name>Apache Karaf :: WebConsole :: Examples :: Events :: Provider</name>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <version>2.3.5</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+                        <Import-Package>
+                            *
+                        </Import-Package>
+                        <Private-Package>org.apache.karaf.webconsole.examples.events.provider</Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/examples/events/provider/src/main/java/org/apache/karaf/webconsole/examples/events/provider/DemoProducer.java b/examples/events/provider/src/main/java/org/apache/karaf/webconsole/examples/events/provider/DemoProducer.java
new file mode 100644
index 0000000..11146e5
--- /dev/null
+++ b/examples/events/provider/src/main/java/org/apache/karaf/webconsole/examples/events/provider/DemoProducer.java
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+package org.apache.karaf.webconsole.examples.events.provider;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+
+/**
+ * Example brand provider which modify console look and feel.
+ */
+public class DemoProducer {
+
+    private EventAdmin eventAdmin;
+    private List<String> topics;
+    private AtomicBoolean run = new AtomicBoolean(true);
+
+    public DemoProducer(EventAdmin eventAdmin) {
+        this.eventAdmin = eventAdmin;
+    }
+
+    public void setTopics(List<String> topics) {
+        this.topics = topics;
+    }
+
+    public void start() {
+        new Thread(new Runnable() {
+            public void run() {
+                int counter = 0;
+                int topicCount = 0;
+
+                while (run.get()) {
+                    if (++topicCount >= topics.size()) {
+                        topicCount = 0;
+                    }
+                    Map properties = new HashMap();
+                    properties.put("topicCounter", topicCount);
+                    properties.put("counter", ++counter);
+                    eventAdmin.postEvent(new Event(topics.get(topicCount), properties));
+
+                    try {
+                        Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+                    } catch (InterruptedException e) {
+                        // TODO Auto-generated catch block
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }).start();
+    }
+
+    public void stop() {
+        run.set(false);
+    }
+
+}
\ No newline at end of file
diff --git a/examples/events/provider/src/main/resources/OSGI-INF/blueprint/event-provider.xml b/examples/events/provider/src/main/resources/OSGI-INF/blueprint/event-provider.xml
new file mode 100644
index 0000000..69678e2
--- /dev/null
+++ b/examples/events/provider/src/main/resources/OSGI-INF/blueprint/event-provider.xml
@@ -0,0 +1,34 @@
+<?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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <reference id="eventAdmin" interface="org.osgi.service.event.EventAdmin" />
+
+    <bean class="org.apache.karaf.webconsole.examples.events.provider.DemoProducer"
+        init-method="start" destroy-method="stop">
+        <argument ref="eventAdmin" />
+        <property name="topics">
+            <list>
+                <value>org/apache/karaf/webconsole/examples</value>
+                <value>org/apache/karaf/webconsole/examples/first</value>
+                <value>org/apache/karaf/webconsole/examples/second</value>
+            </list>
+        </property>
+    </bean>
+
+</blueprint>
diff --git a/examples/pom.xml b/examples/pom.xml
index c710396..bb7dff8 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -32,6 +32,7 @@
 
     <modules>
         <module>branding</module>
+        <module>events</module>
     </modules>
 
 </project>