diff options
author | rescobar | 2011-09-21 02:37:50 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2011-09-21 02:37:50 +0000 |
commit | f3811cbac50e33d2e55d75ab0760e6312d843371 (patch) | |
tree | 1851a599e475eae27615bb0fd68c80f48e07350e /plugins | |
parent | b7c7b73df18c5965651052d543cfb641cc29d93e (diff) | |
download | org.eclipse.osee-f3811cbac50e33d2e55d75ab0760e6312d843371.tar.gz org.eclipse.osee-f3811cbac50e33d2e55d75ab0760e6312d843371.tar.xz org.eclipse.osee-f3811cbac50e33d2e55d75ab0760e6312d843371.zip |
feature[ats_Q9NLC]: Create Osee Vaadin bundle
Diffstat (limited to 'plugins')
14 files changed, 673 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.vaadin/.classpath b/plugins/org.eclipse.osee.vaadin/.classpath new file mode 100644 index 00000000000..ad32c83a788 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/plugins/org.eclipse.osee.vaadin/.project b/plugins/org.eclipse.osee.vaadin/.project new file mode 100644 index 00000000000..238676708f4 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/.project @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.osee.vaadin</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ds.core.builder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/plugins/org.eclipse.osee.vaadin/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.vaadin/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..a47b2942b2f --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/META-INF/MANIFEST.MF @@ -0,0 +1,16 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: OSEE Vaadin +Bundle-SymbolicName: org.eclipse.osee.vaadin +Bundle-Version: 0.9.9.qualifier +Bundle-Vendor: Eclipse Open System Engineering Environment +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Service-Component: OSGI-INF/*.xml +Import-Package: javax.servlet;version="2.5.0", + javax.servlet.http, + org.eclipse.osee.event, + org.eclipse.osee.logger, + org.osgi.framework;version="1.6.0", + org.osgi.service.http;version="1.2.1" +Require-Bundle: com.vaadin;bundle-version="6.6.6";visibility:=reexport +Export-Package: org.eclipse.osee.vaadin diff --git a/plugins/org.eclipse.osee.vaadin/OSGI-INF/vaadin.application.manager.xml b/plugins/org.eclipse.osee.vaadin/OSGI-INF/vaadin.application.manager.xml new file mode 100644 index 00000000000..358f1c380ec --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/OSGI-INF/vaadin.application.manager.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" deactivate="stop" enabled="true" immediate="true" name="org.eclipse.osee.vaadin.internal.ApplicationServletManager"> + <implementation class="org.eclipse.osee.vaadin.internal.ApplicationServletManager"/> + + <reference interface="org.osgi.service.http.HttpService" name="HttpService" + cardinality="1..1" + policy="static" + bind="setHttpService" /> + + <reference interface="org.eclipse.osee.logger.Log" name="Log" + cardinality="1..1" + policy="static" + bind="setLogger" /> + + <reference interface="org.eclipse.osee.event.EventService" name="EventService" + cardinality="1..1" + policy="static" + bind="setEventService" /> + + <reference interface="org.eclipse.osee.vaadin.ApplicationFactory" name="ApplicationFactory" + cardinality="1..n" + policy="dynamic" + bind="addFactory" + unbind="removeFactory" /> + +</scr:component> diff --git a/plugins/org.eclipse.osee.vaadin/OSGI-INF/vaadin.resource.http.context.xml b/plugins/org.eclipse.osee.vaadin/OSGI-INF/vaadin.resource.http.context.xml new file mode 100644 index 00000000000..d040938020f --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/OSGI-INF/vaadin.resource.http.context.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" deactivate="stop" enabled="true" immediate="true" name="org.eclipse.osee.vaadin.internal.ResourceHttpContext"> + <implementation class="org.eclipse.osee.vaadin.internal.ResourceHttpContext"/> + <property name="http.context" type="String" value="VAADIN"/> + <reference bind="setHttpService" cardinality="1..1" interface="org.osgi.service.http.HttpService" name="HttpService" policy="static"/> +</scr:component> diff --git a/plugins/org.eclipse.osee.vaadin/build.properties b/plugins/org.eclipse.osee.vaadin/build.properties new file mode 100644 index 00000000000..19095648f52 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/build.properties @@ -0,0 +1,9 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/ +additional.bundles = org.eclipse.osee.event,\ + org.eclipse.osgi.services,\ + org.eclipse.osee.logger,\ + org.eclipse.osgi diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/ApplicationConstants.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/ApplicationConstants.java new file mode 100644 index 00000000000..81ffeac0d02 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/ApplicationConstants.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin; + +/** + * @author Roberto E. Escobar + */ +public final class ApplicationConstants { + + private ApplicationConstants() { + // Constants Class + } + + public static final String APP_REGISTRATION_EVENT = "org/eclipse/osee/vaadin/event/APP_REGISTRATION"; + + public static final String APP_DEREGISTRATION_EVENT = "org/eclipse/osee/vaadin/event/APP_DEREGISTRATION"; + + public static final String APP_CONTEXT_NAME = "context.name"; + + public static final String APP_COMPONENT_NAME = "component.name"; +} diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/ApplicationFactory.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/ApplicationFactory.java new file mode 100644 index 00000000000..fef220f9407 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/ApplicationFactory.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin; + +import com.vaadin.Application; + +/** + * @author Roberto E. Escobar + */ +public interface ApplicationFactory { + + Application createInstance(); + + Class<? extends Application> getApplicationClass(); + +} diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationServlet.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationServlet.java new file mode 100644 index 00000000000..75f7bbdc377 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationServlet.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin.internal; + +import java.security.Principal; +import java.util.Set; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; +import org.eclipse.osee.vaadin.ApplicationFactory; +import com.vaadin.Application; +import com.vaadin.terminal.gwt.server.AbstractApplicationServlet; + +/** + * @author Roberto E. Escobar + */ +@SuppressWarnings("serial") +public class ApplicationServlet extends AbstractApplicationServlet { + + private final Set<ApplicationSession> sessions; + private final ApplicationFactory factory; + + public ApplicationServlet(Set<ApplicationSession> sessions, ApplicationFactory factory) { + this.sessions = sessions; + this.factory = factory; + } + + @Override + protected Application getNewApplication(HttpServletRequest request) throws ServletException { + Application application = factory.createInstance(); + if (application == null) { + throw new ServletException(String.format("Error creating vaading application using [%s]", + factory.getClass().getName())); + } + setApplicationMetaData(request, application); + HttpSession httpSession = request.getSession(); + final ApplicationSession session = new ApplicationSession(application, httpSession); + sessions.add(session); + httpSession.setAttribute(ApplicationSession.class.getName(), new HttpSessionListener() { + + @Override + public void sessionDestroyed(HttpSessionEvent arg0) { + session.dispose(); + sessions.remove(session); + } + + @Override + public void sessionCreated(HttpSessionEvent arg0) { + // Do Nothing + } + }); + return application; + } + + @SuppressWarnings("unused") + private void setApplicationMetaData(HttpServletRequest request, Application application) throws ServletException { + + // TODO: Hook into user admin - or have this done through a filter + // application.setUser(user); + // application.setLogoutURL(logoutURL); + // application.setMainWindow(mainWindow); + + Principal principal = request.getUserPrincipal(); + if (principal == null) { + principal = new Principal() { + + @Override + public String getName() { + return "Guest"; + } + + @Override + public String toString() { + return getName(); + } + }; + } + // if (request.isUserInRole("Some Role")) { + // application.setUserRole("myRole"); + // } else { + // throw new ServletException("Access Denied"); + // } + application.setUser(principal); + // application.setLogoutURL(request.getContextPath() + "logout.jsp"); + } + + @Override + protected Class<? extends Application> getApplicationClass() { + return factory.getApplicationClass(); + } + + @Override + public void destroy() { + super.destroy(); + for (ApplicationSession info : sessions) { + info.dispose(); + } + sessions.clear(); + } +} diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationServletManager.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationServletManager.java new file mode 100644 index 00000000000..da9f63007c9 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationServletManager.java @@ -0,0 +1,162 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin.internal; + +import java.util.Dictionary; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.CopyOnWriteArrayList; +import javax.servlet.http.HttpServlet; +import org.eclipse.osee.event.EventService; +import org.eclipse.osee.logger.Log; +import org.eclipse.osee.vaadin.ApplicationConstants; +import org.eclipse.osee.vaadin.ApplicationFactory; +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceReference; +import org.osgi.service.http.HttpContext; +import org.osgi.service.http.HttpService; + +/** + * @author Roberto E. Escobar + */ +public class ApplicationServletManager { + + private final Map<String, HttpServlet> registered = new ConcurrentHashMap<String, HttpServlet>(); + + private final List<ServiceReference<ApplicationFactory>> pending = + new CopyOnWriteArrayList<ServiceReference<ApplicationFactory>>(); + + private HttpService httpService; + private EventService eventService; + private Log logger; + private Thread thread; + + private Dictionary<String, String> initParams; + + public void setHttpService(HttpService httpService) { + this.httpService = httpService; + } + + public void setEventService(EventService eventService) { + this.eventService = eventService; + } + + public void setLogger(Log logger) { + this.logger = logger; + } + + private HttpService getHttpService() { + return httpService; + } + + private EventService getEventService() { + return eventService; + } + + private Log getLogger() { + return logger; + } + + public void start(Map<String, Object> properties) { + initParams = ApplicationUtils.getConfigParams(properties); + thread = new Thread("Register Pending Vaadin Applications") { + @Override + public void run() { + for (ServiceReference<ApplicationFactory> reference : pending) { + register(reference); + } + pending.clear(); + } + }; + thread.start(); + } + + public void stop() { + if (thread != null && thread.isAlive()) { + thread.interrupt(); + } + initParams = null; + } + + private boolean isReady() { + return getHttpService() != null && getLogger() != null && getEventService() != null && initParams != null; + } + + public void addFactory(ServiceReference<ApplicationFactory> reference) { + if (isReady()) { + register(reference); + } else { + pending.add(reference); + } + } + + public void removeFactory(ServiceReference<ApplicationFactory> reference) { + if (isReady()) { + unregister(reference); + } else { + pending.remove(reference); + } + } + + private void register(ServiceReference<ApplicationFactory> reference) { + String componentName = ApplicationUtils.getComponentName(reference); + String contextName = ApplicationUtils.getContextName(reference); + + try { + Bundle bundle = reference.getBundle(); + ApplicationFactory applicationFactory = bundle.getBundleContext().getService(reference); + + ApplicationUtils.checkValid(applicationFactory); + + HttpServlet servlet = createContainer(applicationFactory); + HttpContext httpContext = new BundleHttpContext(bundle); + + httpService.registerServlet(contextName, servlet, initParams, httpContext); + registered.put(componentName, servlet); + notifyRegistration(reference); + getLogger().debug("Registered servlet for '%s' alias '%s'", componentName, contextName); + } catch (Exception ex) { + getLogger().error(ex, "Error registering servelt for '%s' alias '%s'", componentName, contextName); + } + } + + private HttpServlet createContainer(ApplicationFactory applicationFactory) throws Exception { + ApplicationUtils.checkValid(applicationFactory); + Set<ApplicationSession> sessions = new ConcurrentSkipListSet<ApplicationSession>(); + return new ApplicationServlet(sessions, applicationFactory); + } + + private void unregister(ServiceReference<ApplicationFactory> reference) { + String componentName = ApplicationUtils.getComponentName(reference); + String contextName = ApplicationUtils.getContextName(reference); + + getLogger().debug("De-registering servlet for '%s' alias '%s'", componentName, contextName); + HttpServlet servlet = registered.remove(componentName); + if (servlet != null) { + httpService.unregister(contextName); + servlet.destroy(); + } + notifyDeRegistration(reference); + } + + private void notifyRegistration(ServiceReference<ApplicationFactory> reference) { + Map<String, String> data = ApplicationUtils.toMap(reference); + eventService.postEvent(ApplicationConstants.APP_REGISTRATION_EVENT, data); + } + + private void notifyDeRegistration(ServiceReference<ApplicationFactory> reference) { + Map<String, String> data = ApplicationUtils.toMap(reference); + eventService.postEvent(ApplicationConstants.APP_DEREGISTRATION_EVENT, data); + } +} diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationSession.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationSession.java new file mode 100644 index 00000000000..3afbcbb14d2 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationSession.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin.internal; + +import javax.servlet.http.HttpSession; +import com.vaadin.Application; +import com.vaadin.terminal.gwt.server.WebApplicationContext; + +/** + * @author Roberto E. Escobar + */ +public final class ApplicationSession implements Comparable<ApplicationSession> { + + private final Application application; + private final HttpSession httpSession; + + public ApplicationSession(Application application, HttpSession session) { + this.application = application; + this.httpSession = session; + } + + public Application getApplication() { + return application; + } + + public HttpSession getHttpSession() { + return httpSession; + } + + public void dispose() { + Application application = getApplication(); + if (application != null) { + application.close(); + } + httpSession.removeAttribute(ApplicationServlet.class.getName()); + httpSession.removeAttribute(WebApplicationContext.class.getName()); + } + + @Override + public int compareTo(ApplicationSession o) { + return o.getHttpSession().getId().compareTo(this.getHttpSession().getId()); + } + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationUtils.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationUtils.java new file mode 100644 index 00000000000..c92ee4f4433 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ApplicationUtils.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin.internal; + +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; +import org.eclipse.osee.vaadin.ApplicationConstants; +import org.eclipse.osee.vaadin.ApplicationFactory; +import org.osgi.framework.ServiceReference; +import com.vaadin.terminal.gwt.server.Constants; + +/** + * @author Roberto E. Escobar + */ +public final class ApplicationUtils { + + private ApplicationUtils() { + // Utility class + } + + public static String getContextName(ServiceReference<?> reference) { + String contextName = (String) reference.getProperty(ApplicationConstants.APP_CONTEXT_NAME); + if (!isValid(contextName)) { + contextName = getComponentName(reference); + } + return normalize(contextName); + } + + public static String getComponentName(ServiceReference<?> reference) { + return (String) reference.getProperty(ApplicationConstants.APP_COMPONENT_NAME); + } + + public static void checkValid(ApplicationFactory application) throws Exception { + if (application == null) { + throw new IllegalStateException("ApplicationFactory service was null"); + } + } + + public static String normalize(String contextName) { + return contextName != null && !contextName.startsWith("/") ? "/" + contextName : contextName; + } + + private static boolean isValid(String value) { + return value != null && value.length() > 0; + } + + public static Dictionary<String, String> getConfigParams(Map<String, Object> properties) { + String productionMode = (String) properties.get(Constants.SERVLET_PARAMETER_PRODUCTION_MODE); + Dictionary<String, String> initParams = new Hashtable<String, String>(); + if (productionMode != null) { + initParams.put(Constants.SERVLET_PARAMETER_PRODUCTION_MODE, productionMode); + } + return initParams; + } + + public static Map<String, String> toMap(ServiceReference<ApplicationFactory> reference) { + Map<String, String> data = new HashMap<String, String>(); + for (String key : reference.getPropertyKeys()) { + Object object = reference.getProperty(key); + data.put(key, String.valueOf(object)); + } + return data; + } +} diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/BundleHttpContext.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/BundleHttpContext.java new file mode 100644 index 00000000000..ef2b956f3e6 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/BundleHttpContext.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin.internal; + +import java.net.URL; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.osgi.framework.Bundle; +import org.osgi.service.http.HttpContext; + +/** + * @author Roberto E. Escobar + */ +public class BundleHttpContext implements HttpContext { + + private final Bundle bundle; + + public BundleHttpContext(Bundle bundle) { + this.bundle = bundle; + } + + @Override + public URL getResource(String name) { + return bundle.getEntry(name); + } + + @Override + public String getMimeType(String name) { + return null; + } + + @Override + public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) { + // Assume the container has already performed authentication + return true; + } + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ResourceHttpContext.java b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ResourceHttpContext.java new file mode 100644 index 00000000000..9c62dd41ae0 --- /dev/null +++ b/plugins/org.eclipse.osee.vaadin/src/org/eclipse/osee/vaadin/internal/ResourceHttpContext.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.vaadin.internal; + +import java.net.URL; +import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.service.http.HttpContext; +import org.osgi.service.http.HttpService; + +/** + * @author Roberto E. Escobar + */ +public class ResourceHttpContext implements HttpContext { + + private static final String VAADIN_BUNDLE = "com.vaadin"; + private static final String HTTP_CONTEXT = "http.context"; + + private HttpService httpService; + private String contextName; + private Bundle resourceBundle; + + public void setHttpService(HttpService httpService) { + this.httpService = httpService; + } + + public void start(BundleContext bundleContext, Map<String, String> properties) throws Exception { + resourceBundle = getVaadinBundle(bundleContext); + contextName = properties.get(HTTP_CONTEXT); + contextName = ApplicationUtils.normalize(contextName); + + httpService.registerResources(contextName, contextName, this); + } + + private Bundle getVaadinBundle(BundleContext bundleContext) { + Bundle vaadinBundle = null; + for (Bundle bundle : bundleContext.getBundles()) { + if (VAADIN_BUNDLE.equals(bundle.getSymbolicName())) { + vaadinBundle = bundle; + break; + } + } + return vaadinBundle; + } + + public void stop() { + httpService.unregister(contextName); + contextName = null; + httpService = null; + resourceBundle = null; + } + + @Override + public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) { + // Assume the container has already performed authentication + return true; + } + + @Override + public URL getResource(String name) { + return resourceBundle.getResource(name); + } + + @Override + public String getMimeType(String name) { + return null; + } +} |