You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by pa...@apache.org on 2018/12/05 13:48:22 UTC

[sling-org-apache-sling-launchpad-startupmanager] 02/08: Implement missing launchpad services.

This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-launchpad-startupmanager.git

commit fdf9e8d0a9be3c567d95284e0b2506cd0c0a77e6
Author: Karl Pauls <kp...@adobe.com>
AuthorDate: Thu Nov 22 22:34:42 2018 +0100

    Implement missing launchpad services.
---
 .../sling/launchpad/startupmanager/Activator.java  |  36 ++++---
 .../LaunchpadContentProviderImpl.java              | 111 +++++++++++++++++++++
 .../startupmanager/StartUpHandlerImpl.java         |  52 ++++++++++
 3 files changed, 183 insertions(+), 16 deletions(-)

diff --git a/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/Activator.java b/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/Activator.java
index bddfc1a..5dae9ca 100644
--- a/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/Activator.java
+++ b/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/Activator.java
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.launchpad.startupmanager;
 
+import org.apache.sling.launchpad.api.LaunchpadContentProvider;
+import org.apache.sling.launchpad.api.StartupHandler;
 import org.apache.sling.launchpad.api.StartupListener;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
@@ -37,40 +39,30 @@ public class Activator implements BundleActivator {
 
     private static final Logger log = LoggerFactory.getLogger(Activator.class);
 
-    private ServiceRegistration<?> mbeanServerReg;
-
-    private ServiceRegistration<StartupListener> mbeanStartupListenerReg;
-
     private StartupListenerTracker startupListenerTracker;
 
     @Override
     public void start(final BundleContext bundleContext) {
         startupListenerTracker = new StartupListenerTracker(bundleContext);
-        mbeanServerReg = registerMBeanServer(bundleContext);
+        registerMBeanServer(bundleContext);
         try {
-            mbeanStartupListenerReg = bundleContext.registerService(StartupListener.class, new MBeanStartupListener(), new Hashtable<String, Object>());
+            registerMBeanStartupListener(bundleContext);
         } catch (MalformedObjectNameException e) {
             log.error("Can't instantiate MBeanStartupListener");
         }
+        registerStartupHandler(bundleContext);
+        registerLaunchpadContentProvider(bundleContext);
     }
 
     @Override
     public void stop(final BundleContext context) {
-        if (mbeanStartupListenerReg != null) {
-            mbeanStartupListenerReg.unregister();
-            mbeanStartupListenerReg = null;
-        }
-        if (mbeanServerReg != null) {
-            mbeanServerReg.unregister();
-            mbeanServerReg = null;
-        }
         if (startupListenerTracker != null) {
             startupListenerTracker.close();
             startupListenerTracker = null;
         }
     }
 
-    private ServiceRegistration<?> registerMBeanServer(final BundleContext bundleContext) {
+    private ServiceRegistration<MBeanServer> registerMBeanServer(final BundleContext bundleContext) {
         // register the platform MBeanServer
         MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
         Hashtable<String, Object> mbeanProps = new Hashtable<String, Object>();
@@ -90,6 +82,18 @@ public class Activator implements BundleActivator {
         } catch (Exception je) {
             log.error("start: Cannot set service properties of Platform MBeanServer service, registering without", je);
         }
-        return bundleContext.registerService(MBeanServer.class.getName(), platformMBeanServer, mbeanProps);
+        return bundleContext.registerService(MBeanServer.class, platformMBeanServer, mbeanProps);
+    }
+
+    private ServiceRegistration<StartupHandler> registerStartupHandler(final BundleContext bundleContext) {
+        return bundleContext.registerService(StartupHandler.class, new StartUpHandlerImpl(bundleContext.getBundle(0)), null);
+    }
+
+    private ServiceRegistration<LaunchpadContentProvider> registerLaunchpadContentProvider(final BundleContext bundleContext) {
+        return bundleContext.registerService(LaunchpadContentProvider.class, new LaunchpadContentProviderImpl(bundleContext.getBundle(0)), null);
+    }
+
+    private ServiceRegistration<StartupListener> registerMBeanStartupListener(final BundleContext bundleContext) throws MalformedObjectNameException {
+        return bundleContext.registerService(StartupListener.class, new MBeanStartupListener(), null);
     }
 }
\ No newline at end of file
diff --git a/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/LaunchpadContentProviderImpl.java b/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/LaunchpadContentProviderImpl.java
new file mode 100644
index 0000000..c76ae7f
--- /dev/null
+++ b/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/LaunchpadContentProviderImpl.java
@@ -0,0 +1,111 @@
+/*
+ * 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.sling.launchpad.startupmanager;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Pattern;
+
+import org.apache.sling.launchpad.api.LaunchpadContentProvider;
+import org.osgi.framework.Bundle;
+
+public class LaunchpadContentProviderImpl implements LaunchpadContentProvider
+{
+    private final Bundle framework;
+
+    public LaunchpadContentProviderImpl(Bundle framework) {
+        this.framework = framework;
+    }
+
+    @Override
+    public Iterator<String> getChildren(String path) {
+        List<String> children;
+
+        // Guard against extra trailing slashes
+        if(path.endsWith("/") && path.length() > 1) {
+            path = path.substring(0, path.length()-1);
+        }
+
+        URL url = framework.getResource(path);
+        if (url != null) {
+            Pattern pathPattern = Pattern.compile("^" + path + "/[^/]+/?$");
+
+            children = new ArrayList<String>();
+            try {
+                URLConnection conn = url.openConnection();
+                if (conn instanceof JarURLConnection) {
+                    JarFile jar = ((JarURLConnection) conn).getJarFile();
+                    Enumeration<JarEntry> entries = jar.entries();
+                    while (entries.hasMoreElements()) {
+                        String entry = entries.nextElement().getName();
+                        if (pathPattern.matcher(entry).matches()) {
+                            children.add(entry);
+                        }
+                    }
+                }
+            } catch (IOException ioe) {
+                // ignore for now
+            }
+        } else {
+            children = Collections.emptyList();
+        }
+
+        return children.iterator();
+    }
+
+    @Override
+    public URL getResource(String path) {
+        // ensure path
+        if (path == null || path.length() == 0) {
+            return null;
+        }
+
+        // remove leading slash
+        if (path.charAt(0) == '/') {
+            path = path.substring(1);
+        }
+
+        return framework.getResource(path);
+    }
+
+    @Override
+    public InputStream getResourceAsStream(String path) {
+        URL res = this.getResource(path);
+        if (res != null) {
+            try {
+                return res.openStream();
+            } catch (IOException ioe) {
+                // ignore this one
+            }
+        }
+
+        // no resource
+        return null;
+    }
+}
diff --git a/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/StartUpHandlerImpl.java b/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/StartUpHandlerImpl.java
new file mode 100644
index 0000000..8b75a52
--- /dev/null
+++ b/startupmanager/src/main/java/org/apache/sling/launchpad/startupmanager/StartUpHandlerImpl.java
@@ -0,0 +1,52 @@
+/*
+ * 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.sling.launchpad.startupmanager;
+
+import org.apache.sling.launchpad.api.StartupHandler;
+import org.apache.sling.launchpad.api.StartupMode;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceFactory;
+
+public class StartUpHandlerImpl implements StartupHandler
+{
+    private final Bundle framework;
+
+    public StartUpHandlerImpl(Bundle bundle)
+    {
+        framework = bundle;
+    }
+
+    @Override
+    public StartupMode getMode()
+    {
+        return StartupMode.RESTART;
+    }
+
+    @Override
+    public boolean isFinished()
+    {
+        return framework.getState() == Bundle.ACTIVE;
+    }
+
+    @Override
+    public void waitWithStartup(boolean b)
+    {
+        // Ignore
+    }
+}