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

refactoring: get rid of OSGiClientStaticResource #9277

Merged
merged 1 commit into from
Nov 9, 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
3 changes: 1 addition & 2 deletions flow-client/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ Bundle-Name: Flow Client Engine
Bundle-Version: ${osgi.bundle.version}
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0
Import-Package: !*, com.vaadin.flow.client, org.osgi.framework, \
org.osgi.service.http, com.vaadin.flow.osgi.support
Import-Package: !*
Export-Package: !*
6 changes: 0 additions & 6 deletions flow-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-osgi</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ public class VaadinBundleTracker extends BundleTracker<Bundle> {

private Executor executor = Executors.newSingleThreadExecutor();

private final AtomicReference<ServiceRegistration<Servlet>> servletRegistration = new AtomicReference<>();
private final AtomicReference<ServiceRegistration<Servlet>> servletPushRegistration = new AtomicReference<>();
private final AtomicReference<ServiceRegistration<Servlet>> servletClientRegistration = new AtomicReference<>();

/**
* Dedicated servlet for serving resources in Flow bundles.
*/
private static class PushResourceServlet extends HttpServlet {
private static class ResourceServlet extends HttpServlet {

private final Bundle bundle;
private final String resourceDirPath;

public PushResourceServlet(Bundle pushBundle) {
bundle = pushBundle;
public ResourceServlet(Bundle bundle, String resourceDirPath) {
this.bundle = bundle;
this.resourceDirPath = resourceDirPath;
}

@Override
Expand All @@ -84,8 +87,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
if (pathInfo == null) {
resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
}
URL resource = bundle.getResource(
"/META-INF/resources/VAADIN/static/push" + pathInfo);
URL resource = bundle.getResource(resourceDirPath + pathInfo);
if (resource == null) {
resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
}
Expand Down Expand Up @@ -119,6 +121,8 @@ public Bundle addingBundle(Bundle bundle, BundleEvent event) {
executor.execute(this::scanActiveBundles);
} else if (isPushModule(bundle)) {
registerPushResources(bundle);
} else if (isClientModule(bundle)) {
registerClientResources(bundle);
} else if ((flowServerBundle.getState() & Bundle.ACTIVE) != 0) {
// If flow-server bundle is already active then scan bundle for
// classes
Expand All @@ -128,6 +132,8 @@ public Bundle addingBundle(Bundle bundle, BundleEvent event) {
&& ((event.getType() & BundleEvent.STOPPED) > 0)) {
if (isPushModule(bundle)) {
unregisterPushResource(bundle);
} else if (isClientModule(bundle)) {
unregisterClientResource(bundle);
} else if (isVaadinExtender(bundle)) {
// Remove all bundle classes once the bundle becomes stopped
OSGiAccess.getInstance()
Expand All @@ -141,24 +147,52 @@ private void registerPushResources(Bundle pushBundle) {
Hashtable<String, Object> properties = new Hashtable<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace the synchronized class "Hashtable" by an unsynchronized one such as "HashMap". rule

properties.put("osgi.http.whiteboard.servlet.pattern",
"/VAADIN/static/push/*");
servletRegistration.compareAndSet(null,
servletPushRegistration.compareAndSet(null,
pushBundle.getBundleContext().registerService(Servlet.class,
new PushResourceServlet(pushBundle), properties));
new ResourceServlet(pushBundle,
"/META-INF/resources/VAADIN/static/push"),
properties));
}

private void unregisterPushResource(Bundle pushBundle) {
ServiceRegistration<Servlet> registration = servletRegistration.get();
ServiceRegistration<Servlet> registration = servletPushRegistration
.get();
if (registration != null && registration.getReference().getBundle()
.getBundleId() == pushBundle.getBundleId()) {
registration.unregister();
servletRegistration.compareAndSet(registration, null);
servletPushRegistration.compareAndSet(registration, null);
}
}

private void registerClientResources(Bundle clientBundle) {
Hashtable<String, Object> properties = new Hashtable<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace the synchronized class "Hashtable" by an unsynchronized one such as "HashMap". rule

properties.put("osgi.http.whiteboard.servlet.pattern",
"/VAADIN/static/client/*");
servletClientRegistration.compareAndSet(null,
clientBundle.getBundleContext().registerService(Servlet.class,
new ResourceServlet(clientBundle,
"/META-INF/resources/VAADIN/static/client"),
properties));
}

private void unregisterClientResource(Bundle clientBundle) {
ServiceRegistration<Servlet> registration = servletClientRegistration
.get();
if (registration != null && registration.getReference().getBundle()
.getBundleId() == clientBundle.getBundleId()) {
registration.unregister();
servletClientRegistration.compareAndSet(registration, null);
}
}

private boolean isPushModule(Bundle bundle) {
return "com.vaadin.flow.push".equals(bundle.getSymbolicName());
}

private boolean isClientModule(Bundle bundle) {
return "com.vaadin.flow.client".equals(bundle.getSymbolicName());
}

@SuppressWarnings("unchecked")
private void scanContextInitializers() {
Map<Long, Collection<Class<?>>> map = new HashMap<>();
Expand Down