diff options
author | Mirjan Merruko <mirjan@vaadin.com> | 2017-03-24 13:33:37 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-04-25 15:01:06 +0300 |
commit | 9a6ebeb5a80bdb1d97d47422c62a973b3f2d2cf9 (patch) | |
tree | 20b1c09d6c2e6319cba036f9ecf409893c4b6c56 /shared | |
parent | d0a8608c7e074d4baeef0282146b2da5c47603a5 (diff) | |
download | vaadin-framework-9a6ebeb5a80bdb1d97d47422c62a973b3f2d2cf9.tar.gz vaadin-framework-9a6ebeb5a80bdb1d97d47422c62a973b3f2d2cf9.zip |
Add OSGi support #8830 #8827 #8828 #8829
Use bnd-maven-plugin instead of maven-bundle-plugin and helper class, and
add support for publishing static resources such as themes and widgetsets.
Diffstat (limited to 'shared')
5 files changed, 299 insertions, 23 deletions
diff --git a/shared/bnd.bnd b/shared/bnd.bnd new file mode 100644 index 0000000000..1894e06bd1 --- /dev/null +++ b/shared/bnd.bnd @@ -0,0 +1,8 @@ +Bundle-SymbolicName: ${project.groupId}.shared +Bundle-Activator: com.vaadin.osgi.resources.OSGiVaadinResources +Bundle-Name: Vaadin Shared +Import-Package: com.vaadin*;version="[${osgi.bundle.version},${osgi.bundle.version}]",\ + org.osgi* +Export-Package: com.vaadin.osgi.resources;-noimport:=true,\ + com.vaadin.shared*;-noimport:=true,\ + elemental*;-noimport:=true diff --git a/shared/pom.xml b/shared/pom.xml index 1cb9ed07dc..d182030978 100644 --- a/shared/pom.xml +++ b/shared/pom.xml @@ -24,6 +24,15 @@ <artifactId>gwt-elemental</artifactId> <scope>provided</scope> </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi.cmpn</artifactId> + </dependency> </dependencies> <build> @@ -99,31 +108,10 @@ </execution> </executions> </plugin> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <extensions>true</extensions> - <configuration> - <instructions> - <Bundle-RequiredExecutionEnvironment>${osgi.execution.environment}</Bundle-RequiredExecutionEnvironment> - <Bundle-Version>${osgi.bundle.version}</Bundle-Version> - <Export-Package>com.vaadin.*;version="${osgi.bundle.version}",elemental.*;version="${osgi.bundle.version}"</Export-Package> - <Import-Package></Import-Package> - <Require-Bundle></Require-Bundle> - </instructions> - </configuration> - <executions> - <execution> - <id>bundle-manifest</id> - <phase>prepare-package</phase> - <goals> - <goal>manifest</goal> - </goals> - </execution> - </executions> + <groupId>biz.aQute.bnd</groupId> + <artifactId>bnd-maven-plugin</artifactId> </plugin> - <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> diff --git a/shared/src/main/java/com/vaadin/osgi/resources/OSGiVaadinResources.java b/shared/src/main/java/com/vaadin/osgi/resources/OSGiVaadinResources.java new file mode 100644 index 0000000000..96f6f997ca --- /dev/null +++ b/shared/src/main/java/com/vaadin/osgi/resources/OSGiVaadinResources.java @@ -0,0 +1,86 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed 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 com.vaadin.osgi.resources; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Version; + +import com.vaadin.osgi.resources.impl.VaadinResourceServiceImpl; + +/** + * {@link BundleActivator} used to provide access to the + * {@link VaadinResourceService} singleton for publishing themes, widgetsets and + * other necessary resources. + * + * @author Vaadin Ltd. + * + * @since 8.1 + */ +public class OSGiVaadinResources implements BundleActivator { + + /** + * Thrown if a method is called when the Resource bundle is not active. + * + * @author Vaadin Ltd. + * + * @since 8.1 + */ + @SuppressWarnings("serial") + public static class ResourceBundleInactiveException extends Exception { + public ResourceBundleInactiveException(String message) { + super(message); + } + } + + private static OSGiVaadinResources instance; + + private VaadinResourceServiceImpl service; + private Version version; + + /** + * Returns the {@link VaadinResourceService} instance. Always returns + * non-null. + * + * @return the {@link VaadinResourceService resource service} to use for + * publishing themes, widgetsets and other necessary resources + * @throws ResourceBundleInactiveException + * if the bundle is not active + */ + public static VaadinResourceService getService() + throws ResourceBundleInactiveException { + if (instance == null) { + throw new ResourceBundleInactiveException( + "Vaadin Shared is not active!"); + } + return instance.service; + } + + @Override + public void start(BundleContext context) throws Exception { + version = context.getBundle().getVersion(); + service = new VaadinResourceServiceImpl(); + service.setBundleVersion(version.toString()); + instance = this; + } + + @Override + public void stop(BundleContext context) throws Exception { + instance = null; + service = null; + version = null; + } +} diff --git a/shared/src/main/java/com/vaadin/osgi/resources/VaadinResourceService.java b/shared/src/main/java/com/vaadin/osgi/resources/VaadinResourceService.java new file mode 100644 index 0000000000..819d3ac143 --- /dev/null +++ b/shared/src/main/java/com/vaadin/osgi/resources/VaadinResourceService.java @@ -0,0 +1,97 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed 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 com.vaadin.osgi.resources; + +import org.osgi.service.http.HttpService; +import org.osgi.service.http.NamespaceException; + +/** + * Service used to publish themes, widgetsets and static resources at the root + * of a versioned namespaced /VAADIN/ folder. + * + * @author Vaadin Ltd. + * + * @since 8.1 + */ +public interface VaadinResourceService { + + /** + * Register the theme with the given name under the + * {@link VaadinResourceService} versioned namespace. The theme folder is + * expected to be compiled and under "/VAADIN/themes/" in the calling + * bundle. + * + * The theme will become accessible under the url + * "/vaadin-x.x.x/VAADIN/themes/themeName" where x.x.x is the version of the + * Vaadin Shared bundle + * + * @param themeName + * the name of the theme + * @param httpService + * the {@link HttpService} instance for the calling bundle + * @throws NamespaceException + * if there is a clash during the theme registration + */ + void publishTheme(String themeName, HttpService httpService) + throws NamespaceException; + + /** + * Register the resource with the given name under the + * {@link VaadinResourceService} versioned namespace. The resource is + * expected to be under "/VAADIN/" in the calling bundle. + * + * The resource will become accessible under the url "/vaadin-x.x.x/VAADIN/" + * where x.x.x is the version of the Vaadin Shared bundle + * + * @param resourceName + * the name of the resource + * @param httpService + * the {@link HttpService} instance for the calling bundle + * @throws NamespaceException + * if there is a clash during the theme registration + */ + void publishResource(String resourceName, HttpService httpService) + throws NamespaceException; + + /** + * Register the widgetset with the given name under the + * {@link VaadinResourceService} versioned namespace. The resource is + * expected to be under "/VAADIN/widgetsets" in the calling bundle. + * + * The resource will become accessible under the url + * "/vaadin-x.x.x/VAADIN/widgetsets" where x.x.x is the version of the + * Vaadin Shared bundle + * + * @param widgetsetName + * the name of the resource + * @param httpService + * the {@link HttpService} instance for the calling bundle + * @throws NamespaceException + * if there is a clash during the theme registration + */ + void publishWidgetset(String widgetsetName, HttpService httpService) + throws NamespaceException; + + /** + * Returns the prefix of the versioned namespace for the resources. The + * result can't be null and is of the format "vaadin-x.x.x" where x.x.x the + * version of the Vaadin Shared bundle. + * + * @return the prefix of the resources folder managed by this service + */ + String getResourcePathPrefix(); + +} diff --git a/shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceServiceImpl.java b/shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceServiceImpl.java new file mode 100644 index 0000000000..d33a56abfa --- /dev/null +++ b/shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceServiceImpl.java @@ -0,0 +1,97 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed 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 com.vaadin.osgi.resources.impl; + +import org.osgi.service.http.HttpService; +import org.osgi.service.http.NamespaceException; + +import com.vaadin.osgi.resources.VaadinResourceService; + +/** + * Implementation of {@link VaadinResourceService}. Uses bundle version as a + * prefix for the /VAADIN/ folder. + * + * @author Vaadin Ltd. + * + * @since 8.1 + */ +public class VaadinResourceServiceImpl implements VaadinResourceService { + private static final String NAMESPACE_PREFIX = "vaadin-%s"; + + private static final String VAADIN_ROOT_ALIAS_FORMAT = "/%s/VAADIN/%s"; + private static final String VAADIN_ROOT_FORMAT = "/VAADIN/%s"; + + private static final String VAADIN_THEME_ALIAS_FORMAT = "/%s/VAADIN/themes/%s"; + private static final String VAADIN_WIDGETSET_ALIAS_FORMAT = "/%s/VAADIN/widgetsets/%s"; + + private static final String VAADIN_THEME_PATH_FORMAT = "/VAADIN/themes/%s"; + private static final String VAADIN_WIDGETSET_PATH_FORMAT = "/VAADIN/widgetsets/%s"; + + private String bundleVersion; + + /** + * Sets the version of the bundle managing this service. + * + * <p> + * This needs to be called before any other method after the service is + * created. + * + * @param bundleVersion + * the version of the bundle managing this service + */ + public void setBundleVersion(String bundleVersion) { + this.bundleVersion = bundleVersion; + } + + @Override + public void publishTheme(String themeName, HttpService httpService) + throws NamespaceException { + doPublish(themeName, VAADIN_THEME_ALIAS_FORMAT, + VAADIN_THEME_PATH_FORMAT, httpService); + } + + private void doPublish(String resourceName, String aliasFormat, + String pathFormat, HttpService httpService) + throws NamespaceException { + String bundleVersionPrefix = String.format(NAMESPACE_PREFIX, + bundleVersion); + + String resourcePath = String.format(pathFormat, resourceName); + String resourceAlias = String.format(aliasFormat, bundleVersionPrefix, + resourceName); + + httpService.registerResources(resourceAlias, resourcePath, null); + } + + @Override + public void publishResource(String resource, HttpService httpService) + throws NamespaceException { + doPublish(resource, VAADIN_ROOT_ALIAS_FORMAT, VAADIN_ROOT_FORMAT, + httpService); + } + + @Override + public void publishWidgetset(String widgetset, HttpService httpService) + throws NamespaceException { + doPublish(widgetset, VAADIN_WIDGETSET_ALIAS_FORMAT, + VAADIN_WIDGETSET_PATH_FORMAT, httpService); + } + + @Override + public String getResourcePathPrefix() { + return String.format(NAMESPACE_PREFIX, bundleVersion); + } +} |