Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactror: Move osgi from flow #2

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/target/
**/target/
1 change: 0 additions & 1 deletion flow-osgi/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ Bundle-Name: Vaadin Flow OSGi Support
Bundle-Version: ${osgi.bundle.version}
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0
Import-Package: *
Export-Package: com.vaadin.flow.osgi.support*

3 changes: 2 additions & 1 deletion flow-osgi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<artifactId>osgi.core</artifactId>
<version>${osgi.core.version}</version>
<scope>provided</scope>
</dependency>
Expand All @@ -51,6 +51,7 @@
<version>${osgi.compendium.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.vaadin.flow.server.VaadinServiceInitListener;

/**
* OSGi capable implementation of instantiator factory.
*
* @author Vaadin Ltd
* @since
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright (C) 2020 Vaadin Ltd
*
* This program is available under Commercial Vaadin Developer License
* 4.0 (CVDLv4).
*
*
* For the full License, see <https://vaadin.com/license/cvdl-4.0>.
*/
package com.vaadin.flow.osgi.support;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;

import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.annotations.Component;

import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.server.VaadinServletService;

/**
* OSGi capable implementation of {@link ResourceProvider}.
* ServiceComponentRuntime activates this Service because of @Component
*
* @author Vaadin Ltd
* @since
*
*/
@Component
public class OSGiResourceProvider implements ResourceProvider {

@Override
public URL getApplicationResource(Class<?> clazz, String path) {
return FrameworkUtil.getBundle(Objects.requireNonNull(clazz))
.getResource(path);
}

@Override
public URL getApplicationResource(Object context, String path) {
Class<?> appClass = getApplicationClass(context);
if (appClass == null) {
return null;
}
return getApplicationResource(appClass, path);
}

@Override
public List<URL> getApplicationResources(Object context, String path)
throws IOException {
Class<?> appClass = getApplicationClass(context);
if (appClass == null) {
return Collections.emptyList();
}
return getApplicationResources(appClass, path);

}

@Override
public List<URL> getApplicationResources(Class<?> clazz, String path)
throws IOException {
Bundle bundle = FrameworkUtil.getBundle(Objects.requireNonNull(clazz));
Enumeration<URL> resources = bundle.getResources(path);
if (resources == null) {
return Collections.emptyList();
}
return Collections.list(resources);
}

@Override
public URL getClientResource(String path) {
Bundle[] bundles = FrameworkUtil.getBundle(OSGiResourceProvider.class)
.getBundleContext().getBundles();
for (Bundle bundle : bundles) {
if ("com.vaadin.flow.client".equals(bundle.getSymbolicName())) {
return bundle.getResource(path);
}
}
return null;
}

@Override
public InputStream getClientResourceAsStream(String path)
throws IOException {
// No any caching !: flow-client may be reinstalled at any moment
return getClientResource(path).openStream();
}

private Class<?> getApplicationClass(Object context) {
Class<?> clazz = Objects.requireNonNull(context).getClass();
if (context instanceof VaadinServletService) {
clazz = ((VaadinServletService) context).getServlet().getClass();
}
if (!"com.vaadin.flow.server"
.equals(FrameworkUtil.getBundle(clazz).getSymbolicName())) {
return clazz;
}
return null;
}
}
Loading