You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by do...@apache.org on 2008/11/13 15:13:54 UTC

svn commit: r713730 - in /felix/sandbox/donsez/monitor.sample.memorymonitorjob: pom.xml src/main/java/org/apache/felix/sandbox/monitor/sample/MemoryMonitorJob.java

Author: donsez
Date: Thu Nov 13 06:13:54 2008
New Revision: 713730

URL: http://svn.apache.org/viewvc?rev=713730&view=rev
Log:
change dependencies versions in pom.xml and add a missing class

Added:
    felix/sandbox/donsez/monitor.sample.memorymonitorjob/src/main/java/org/apache/felix/sandbox/monitor/sample/MemoryMonitorJob.java   (with props)
Modified:
    felix/sandbox/donsez/monitor.sample.memorymonitorjob/pom.xml

Modified: felix/sandbox/donsez/monitor.sample.memorymonitorjob/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/monitor.sample.memorymonitorjob/pom.xml?rev=713730&r1=713729&r2=713730&view=diff
==============================================================================
--- felix/sandbox/donsez/monitor.sample.memorymonitorjob/pom.xml (original)
+++ felix/sandbox/donsez/monitor.sample.memorymonitorjob/pom.xml Thu Nov 13 06:13:54 2008
@@ -23,35 +23,42 @@
 		<description>provides a simple monitoring job.</description>
 	</properties>  
 
+<!--
   <parent>
-    <groupId>org.apache.felix</groupId>
-    <artifactId>felix</artifactId>
-    <version>0.9.0-incubator-SNAPSHOT</version>
+    <groupId>org.apache.felix.sandbox</groupId>
+    <artifactId>sandbox</artifactId>
+    <version>0.1.0-SNAPSHOT</version>
   </parent>
-  
+-->
+
   <modelVersion>4.0.0</modelVersion>
   <packaging>bundle</packaging>
   <name>Apache Felix Monitor Admin Sample : Memory Monitor Job</name>
+  
   <artifactId>org.apache.felix.sandbox.monitor.sample.memorymonitorjob</artifactId>
+  <groupId>org.apache.felix.sandbox</groupId>
+  <version>0.1.0-SNAPSHOT</version>
+
   <description>${description}</description>
 
   <dependencies>
     <dependency>
-      <groupId>${pom.groupId}</groupId>
+      <groupId>org.apache.felix</groupId>
       <artifactId>org.osgi.core</artifactId>
-      <version>${pom.version}</version>
+      <version>1.0.0</version>
     </dependency>
     <dependency>
-      <groupId>${pom.groupId}</groupId>
+      <groupId>org.apache.felix</groupId>
       <artifactId>org.osgi.compendium</artifactId>
-      <version>${pom.version}</version>
+      <version>1.0.0</version>
     </dependency>
     <dependency>
-      <groupId>${pom.groupId}</groupId>
       <artifactId>org.apache.felix.sandbox.monitor</artifactId>
-      <version>${pom.version}</version>
+      <groupId>org.apache.felix.sandbox</groupId>
+      <version>0.1.0-SNAPSHOT</version>
     </dependency>
   </dependencies>
+
   <build>
     <plugins>
       <plugin>
@@ -72,37 +79,9 @@
             
             <Bundle-Activator>${pom.artifactId}.MemoryMonitorJob</Bundle-Activator>
 
-            <!--
-            <Bundle-DocURL>${repositoryLocation}${pom.artifactId}/index.html</Bundle-DocURL>
-            <Bundle-Url>${repositoryLocation}${pom.artifactId}/${pom.artifactId}-${pom.version}.jar</Bundle-Url>
-            <Bundle-Source>${repositoryLocation}${pom.artifactId}/${pom.artifactId}-${pom.version}-src.jar</Bundle-Source>
-			-->
-			
-            <!--
-            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
-            <Bundle-Description>${description}</Bundle-Description>
-            <Bundle-Vendor>Apache Software Foundation</Bundle-Vendor>
-			-->
-
           </instructions>
         </configuration>
       </plugin>
     </plugins>
   </build>
-  <!--
-  <repositories>
-    <repository>
-      <id>apache.m2.incubator</id>
-      <name>Apache M2 Incubator Repository</name>
-      <url>http://people.apache.org/repo/m2-incubating-repository/</url>
-    </repository>
-  </repositories>
-  <pluginRepositories>
-    <pluginRepository>
-      <id>apache.m2.incubator</id>
-      <name>Apache M2 Incubator Repository</name>
-      <url>http://people.apache.org/repo/m2-incubating-repository/</url>
-    </pluginRepository>
-  </pluginRepositories>
-  -->
 </project>

Added: felix/sandbox/donsez/monitor.sample.memorymonitorjob/src/main/java/org/apache/felix/sandbox/monitor/sample/MemoryMonitorJob.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/monitor.sample.memorymonitorjob/src/main/java/org/apache/felix/sandbox/monitor/sample/MemoryMonitorJob.java?rev=713730&view=auto
==============================================================================
--- felix/sandbox/donsez/monitor.sample.memorymonitorjob/src/main/java/org/apache/felix/sandbox/monitor/sample/MemoryMonitorJob.java (added)
+++ felix/sandbox/donsez/monitor.sample.memorymonitorjob/src/main/java/org/apache/felix/sandbox/monitor/sample/MemoryMonitorJob.java Thu Nov 13 06:13:54 2008
@@ -0,0 +1,100 @@
+/*
+ * 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.felix.sandbox.monitor.sample.memorymonitorjob;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+import org.osgi.service.monitor.MonitorAdmin;
+import org.osgi.service.monitor.MonitoringJob;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * This example is inspired from the 119.4.4.1 Example Monitoring Job.
+ * For example, a bundle is interested in working with periodic samples of the
+ * com.acme.foo/memory.free Status Variable. It should therefore register an
+ * Event Handler with the correct topic and a filter on its Event Handler service.
+ * It then starts a monitoring job that is stopped in the BundleActivator
+ * stop method.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ *
+ */
+public class MemoryMonitorJob implements BundleActivator, EventHandler {
+
+	private static final String SERVICE_PID = "java.lang.runtime";
+
+	private static final String MEMORY_FREE_VARIABLE_NAME = "memory.free";
+	private static final String MEMORY_TOTAL_VARIABLE_NAME = "memory.total";
+
+	private MonitoringJob job;
+	private ServiceTracker serviceTracker;
+
+	/**
+	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+	 */
+	public void start(BundleContext context) throws Exception {
+		
+		String monListenerId="org.apache.felix.sandbox.monitor.sample.memorymonitorjob#"+context.getBundle().getBundleId();
+		
+		serviceTracker=new ServiceTracker(context,MonitorAdmin.class.getName(),null);
+		serviceTracker.open();
+		
+		Dictionary properties = new Hashtable();
+		properties.put(EventConstants.EVENT_TOPIC, new String[]{"org/osgi/service/monitor"});
+		properties.put(EventConstants.EVENT_FILTER,
+				"(mon.listener.id="+monListenerId+")");
+		context.registerService(EventHandler.class.getName(), this, properties);
+		MonitorAdmin monitorAdmin=(MonitorAdmin) serviceTracker.getService();
+		if(monitorAdmin==null)
+			throw new IllegalStateException("could not start if no MonitorAdmin service is available");
+		
+		job = monitorAdmin.startScheduledJob(
+				monListenerId, // listener.id
+				new String[] {
+						SERVICE_PID + "/" + MEMORY_FREE_VARIABLE_NAME,
+						SERVICE_PID + "/" + MEMORY_TOTAL_VARIABLE_NAME
+					},
+				20, // seconds
+				0 // Forever
+				);
+	}
+
+	/**
+	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+	 */
+	 public void stop(BundleContext context) throws Exception {
+		job.stop();
+		serviceTracker.close();
+	}
+
+	/**
+	 * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
+	 */
+	public void handleEvent(Event event) {
+		String pid = (String) event.getProperty("mon.monitorable.pid");
+		String name = (String) event.getProperty("mon.statusvariable.name");
+		Object value = event.getProperty("mon.statusvariable.value");
+		System.out.println("StatusVariable "+pid+"/" + name + "=" + value);
+	}
+}

Propchange: felix/sandbox/donsez/monitor.sample.memorymonitorjob/src/main/java/org/apache/felix/sandbox/monitor/sample/MemoryMonitorJob.java
------------------------------------------------------------------------------
    svn:eol-style = native