You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2019/12/05 15:30:19 UTC

[sling-org-apache-sling-installer-provider-jcr] 01/01: SLING-8877 add url handler for jcrinstall scheme

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

kwin pushed a commit to branch feature/urlhandler
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-provider-jcr.git

commit 6a6b4c75b506b36465e528d46e02ce4b87b319e4
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Dec 5 16:29:57 2019 +0100

    SLING-8877 add url handler for jcrinstall scheme
---
 pom.xml                                            |   6 ++
 .../provider/jcr/impl/JcrInstallUrlHandler.java    | 106 +++++++++++++++++++++
 2 files changed, 112 insertions(+)

diff --git a/pom.xml b/pom.xml
index 1ecbead..00706d2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -116,6 +116,12 @@
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
+         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.5.0</version>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.jcr.api</artifactId>
diff --git a/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallUrlHandler.java b/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallUrlHandler.java
new file mode 100644
index 0000000..5aa03c0
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallUrlHandler.java
@@ -0,0 +1,106 @@
+/*
+ * 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.installer.provider.jcr.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.installer.api.info.InfoProvider;
+import org.apache.sling.installer.api.info.ResourceGroup;
+import org.apache.sling.installer.api.tasks.ResourceState;
+import org.osgi.service.url.AbstractURLStreamHandlerService;
+import org.osgi.service.url.URLConstants;
+import org.osgi.service.url.URLStreamHandlerService;
+
+/**
+ * URL Handler for scheme used by the JCR Installer (used e.g. in bundle location URLs).
+ * Returns the original file contained in the underlying repository.
+ * 
+ * @see <a href="https://osgi.org/specification/osgi.core/7.0.0/service.url.html#d0e42987">OSGi URL Handlers</a>
+ */
+@Component
+@Service
+@Property(name = URLConstants.URL_HANDLER_PROTOCOL, value = JcrInstaller.URL_SCHEME)
+public class JcrInstallUrlHandler extends AbstractURLStreamHandlerService implements URLStreamHandlerService {
+
+    @Reference
+    private ResourceResolverFactory resolverFactory;
+
+    @Reference
+    private InfoProvider installerInfo;
+
+    private ResourceResolver resolver;
+
+    @Activate
+    public void activate() throws LoginException {
+        resolver = resolverFactory.getServiceResourceResolver(null);
+    }
+    
+    @Deactivate
+    public void deactivate() {
+        resolver.close();
+    }
+
+    private InputStream getInputStreamFromInstallerResourceUrl(URL url) throws IOException {
+        for (ResourceGroup resourceGroup : installerInfo.getInstallationState().getInstalledResources()) {
+            for (org.apache.sling.installer.api.info.Resource resource : resourceGroup.getResources()) {
+                if (resource.getURL().equals(url.toString()) && resource.getState().equals(ResourceState.INSTALLED)) {
+                    return resource.getInputStream();
+                }
+            }
+        }
+        throw new IOException("Could not find OSGi installer resource with url " + url);
+    }
+
+    @Override
+    public URLConnection openConnection(URL url) throws IOException {
+        return new InputStreamConnection(url, getInputStreamFromInstallerResourceUrl(url));
+    }
+
+    private static final class InputStreamConnection extends URLConnection {
+
+        private final InputStream input;
+        
+        protected InputStreamConnection(URL url, InputStream input) {
+            super(url);
+            this.input = input;
+        }
+
+        @Override
+        public void connect() throws IOException {
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return input;
+        }
+    }
+
+}