You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VaadinServletContextFactory.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.osgi.resources.impl;
  17. import java.net.URL;
  18. import org.osgi.framework.Bundle;
  19. import org.osgi.framework.ServiceFactory;
  20. import org.osgi.framework.ServiceRegistration;
  21. import org.osgi.service.http.context.ServletContextHelper;
  22. public class VaadinServletContextFactory
  23. implements ServiceFactory<ServletContextHelper> {
  24. @Override
  25. public ServletContextHelper getService(final Bundle bundle,
  26. final ServiceRegistration<ServletContextHelper> registration) {
  27. return new VaadinServletContext(bundle);
  28. }
  29. @Override
  30. public void ungetService(final Bundle bundle,
  31. final ServiceRegistration<ServletContextHelper> registration,
  32. final ServletContextHelper service) {
  33. // nothing to do
  34. }
  35. private static class VaadinServletContext extends ServletContextHelper {
  36. private final Bundle bundle;
  37. public VaadinServletContext(Bundle bundle) {
  38. super(bundle);
  39. this.bundle = bundle;
  40. }
  41. // we want to load the resources from the classpath
  42. @Override
  43. public URL getResource(String name) {
  44. if ((name != null) && (bundle != null)) {
  45. if (name.startsWith("/")) {
  46. name = name.substring(1);
  47. }
  48. return this.bundle.getResource(name);
  49. }
  50. return null;
  51. }
  52. }
  53. }