Repository: karaf
Updated Branches:
  refs/heads/master 8b7f604c5 -> 989dac12c


[KARAF-3650] Make sun.misc optional and load DumpHandler dynamically


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/989dac12
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/989dac12
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/989dac12

Branch: refs/heads/master
Commit: 989dac12c2e441f571470fa2e83ab8b1e3ae1609
Parents: a265e2b
Author: Christian Schneider <[email protected]>
Authored: Wed Apr 8 14:55:29 2015 +0200
Committer: Christian Schneider <[email protected]>
Committed: Wed Apr 8 14:56:36 2015 +0200

----------------------------------------------------------------------
 diagnostic/boot/pom.xml                         |  4 ++
 .../diagnostic/core/internal/Activator.java     | 44 +++++-----------
 .../diagnostic/core/internal/DumpHandler.java   | 55 ++++++++++++++++++++
 3 files changed, 72 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/989dac12/diagnostic/boot/pom.xml
----------------------------------------------------------------------
diff --git a/diagnostic/boot/pom.xml b/diagnostic/boot/pom.xml
index 9e79fad..fcd751b 100644
--- a/diagnostic/boot/pom.xml
+++ b/diagnostic/boot/pom.xml
@@ -62,6 +62,10 @@
                             org.apache.karaf.diagnostic.core.internal,
                             org.apache.karaf.diagnostic.core.providers
                         </Private-Package>
+                        <Import-Package>
+                            sun.misc;resolution:=optional,
+                            *
+                        </Import-Package>
                         <Karaf-Activator>
                             org.apache.karaf.diagnostic.core.internal.Activator
                         </Karaf-Activator>

http://git-wip-us.apache.org/repos/asf/karaf/blob/989dac12/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/Activator.java
----------------------------------------------------------------------
diff --git 
a/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/Activator.java
 
b/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/Activator.java
index 3b9ee88..a7e7676 100644
--- 
a/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/Activator.java
+++ 
b/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/Activator.java
@@ -16,52 +16,34 @@
  */
 package org.apache.karaf.diagnostic.core.internal;
 
-import java.io.File;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.io.Closeable;
 
-import org.apache.karaf.diagnostic.core.Dump;
-import org.apache.karaf.diagnostic.core.DumpDestination;
-import org.apache.karaf.diagnostic.core.common.ZipDumpDestination;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
-import sun.misc.Signal;
-import sun.misc.SignalHandler;
 
-public class Activator implements BundleActivator, SignalHandler {
-
-    private static final String SIGNAL = "HUP";
-
-    private BundleContext bundleContext;
-    private SignalHandler previous;
+public class Activator implements BundleActivator {
+    Closeable dumpHandler;
 
     public void start(BundleContext context) throws Exception {
-        bundleContext = context;
         if (!isWindows()) {
-            previous = sun.misc.Signal.handle(new Signal(SIGNAL), this);
+            ClassLoader cl = this.getClass().getClassLoader();
+            try {
+                Class<?> dumpHandlerClazz = 
cl.loadClass("org.apache.karaf.diagnostic.core.internal.DumpHandler");
+                dumpHandler = 
(Closeable)dumpHandlerClazz.getConstructor(BundleContext.class).newInstance(context);
+            } catch (Exception e) {
+                // Will happen if sun.misc.SignalHandler is not available
+            }
         }
     }
 
     public void stop(BundleContext context) throws Exception {
-        if (!isWindows()) {
-            sun.misc.Signal.handle(new Signal(SIGNAL), previous);
+        if (dumpHandler != null && !isWindows()) {
+            dumpHandler.close();
         }
     }
 
-    public void handle(Signal signal) {
-        SimpleDateFormat dumpFormat = new 
SimpleDateFormat("yyyy-MM-dd_HHmmss-SSS");
-        String fileName = "dump-" + dumpFormat.format(new Date()) + ".zip";
-        DumpDestination destination = new ZipDumpDestination(new 
File(fileName));
-        Dump.dump(bundleContext, destination);
-    }
-
     private boolean isWindows() {
-        String os = System.getProperty("os.name", "Unknown");
-        if (os.startsWith("Win")) {
-            return true;
-        } else {
-            return false;
-        }
+        return System.getProperty("os.name", "Unknown").startsWith("Win");
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/989dac12/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/DumpHandler.java
----------------------------------------------------------------------
diff --git 
a/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/DumpHandler.java
 
b/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/DumpHandler.java
new file mode 100644
index 0000000..7ec1f00
--- /dev/null
+++ 
b/diagnostic/boot/src/main/java/org/apache/karaf/diagnostic/core/internal/DumpHandler.java
@@ -0,0 +1,55 @@
+/*
+ * 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.diagnostic.core.internal;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.apache.karaf.diagnostic.core.Dump;
+import org.apache.karaf.diagnostic.core.DumpDestination;
+import org.apache.karaf.diagnostic.core.common.ZipDumpDestination;
+import org.osgi.framework.BundleContext;
+
+import sun.misc.Signal;
+import sun.misc.SignalHandler;
+
+public class DumpHandler implements SignalHandler, Closeable {
+    private static final String SIGNAL = "HUP";
+    private BundleContext context;
+    private SignalHandler previous;
+
+    public DumpHandler(BundleContext context) {
+        this.context = context;
+        previous = sun.misc.Signal.handle(new Signal(SIGNAL), this);
+    }
+    
+    public void handle(Signal signal) {
+        SimpleDateFormat dumpFormat = new 
SimpleDateFormat("yyyy-MM-dd_HHmmss-SSS");
+        String fileName = "dump-" + dumpFormat.format(new Date()) + ".zip";
+        DumpDestination destination = new ZipDumpDestination(new 
File(fileName));
+        Dump.dump(context, destination);
+    }
+
+    @Override
+    public void close() throws IOException {
+        sun.misc.Signal.handle(new Signal(SIGNAL), previous);
+    }
+
+}

Reply via email to