[Osgi-messages] SF.net SVN: osgi:[250] papoose-cmpn/trunk/http/src/main/java/org/papoose/ http
Status: Beta
Brought to you by:
maguro
|
From: <osg...@li...> - 2010-02-24 03:57:19
|
Revision: 250
http://osgi.svn.sourceforge.net/osgi/?rev=250&view=rev
Author: maguro
Date: 2010-02-24 03:57:10 +0000 (Wed, 24 Feb 2010)
Log Message:
-----------
More work
Modified Paths:
--------------
papoose-cmpn/trunk/http/src/main/java/org/papoose/http/HttpServiceImpl.java
papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletConfigImpl.java
papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletContextImpl.java
papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletRegistration.java
Added Paths:
-----------
papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletWrapper.java
Modified: papoose-cmpn/trunk/http/src/main/java/org/papoose/http/HttpServiceImpl.java
===================================================================
--- papoose-cmpn/trunk/http/src/main/java/org/papoose/http/HttpServiceImpl.java 2010-02-23 19:45:40 UTC (rev 249)
+++ papoose-cmpn/trunk/http/src/main/java/org/papoose/http/HttpServiceImpl.java 2010-02-24 03:57:10 UTC (rev 250)
@@ -18,11 +18,18 @@
import javax.servlet.Servlet;
import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URL;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
+import java.util.logging.Level;
import java.util.logging.Logger;
+import org.osgi.framework.BundleContext;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
@@ -35,47 +42,135 @@
{
private final static String CLASS_NAME = HttpServiceImpl.class.getName();
private final static Logger LOGGER = Logger.getLogger(CLASS_NAME);
+ private final static Properties EMPTY_PARAMS = new Properties();
+ private final Object lock = new Object();
private final Map<HttpContext, ServletContextImpl> contexts = new HashMap<HttpContext, ServletContextImpl>();
private final Map<String, ServletRegistration> registrations = new HashMap<String, ServletRegistration>();
+ private final BundleContext context;
+ private final ServletDispatcher dispatcher;
- public void registerServlet(String alias, Servlet servlet, Dictionary initparams, HttpContext httpContext) throws ServletException, NamespaceException
+ public HttpServiceImpl(BundleContext context, ServletDispatcher dispatcher)
{
- if (registrations.containsKey(alias)) throw new NamespaceException("Alias " + alias + " already registered");
- if (httpContext == null) httpContext = createDefaultHttpContext();
- ServletRegistration registration = new ServletRegistration(alias, servlet, initparams, httpContext);
- registrations.put(alias, registration);
+ if (context == null) throw new IllegalArgumentException("Bundle context is null");
+ if (dispatcher == null) throw new IllegalArgumentException("Servlet dispatcher is null");
- ServletContextImpl servletContext = contexts.get(httpContext);
- if (servletContext == null) contexts.put(httpContext, servletContext = new ServletContextImpl(httpContext));
- servletContext.incrementReferenceCount();
+ this.context = context;
+ this.dispatcher = dispatcher;
+ }
- servlet.init(new ServletConfigImpl(alias, servletContext, initparams));
+ /**
+ * {@inheritDoc}
+ */
+ public void registerServlet(String alias, Servlet servlet, Dictionary initParams, HttpContext httpContext) throws ServletException, NamespaceException
+ {
+ ServletRegistration registration;
+ ServletContextImpl servletContext;
+ synchronized (lock)
+ {
+ if (registrations.containsKey(alias)) throw new NamespaceException("Alias " + alias + " already registered");
+ if (httpContext == null) httpContext = createDefaultHttpContext();
- //Todo change body of implemented methods use File | Settings | File Templates.
+ registration = new ServletRegistration(alias, servlet, httpContext);
+ registrations.put(alias, registration);
+
+ servletContext = contexts.get(httpContext);
+ if (servletContext == null) contexts.put(httpContext, servletContext = new ServletContextImpl(httpContext));
+
+ servletContext.incrementReferenceCount();
+ }
+
+ try
+ {
+ servlet.init(new ServletConfigImpl(alias, servletContext, initParams));
+
+ dispatcher.register(registration);
+ }
+ catch (Throwable t)
+ {
+ LOGGER.log(Level.WARNING, "Error initializing servlet", t);
+
+ synchronized (lock)
+ {
+ servletContext = contexts.get(httpContext);
+ servletContext.decrementReferenceCount();
+ if (servletContext.getReferenceCount() == 0) contexts.remove(registration.getContext());
+ }
+
+ if (t instanceof ServletException) throw (ServletException) t;
+ throw (RuntimeException) t;
+ }
}
- public void registerResources(String alias, String name, HttpContext context) throws NamespaceException
+
+ /**
+ * {@inheritDoc}
+ */
+ public void registerResources(String alias, String name, HttpContext httpContext) throws NamespaceException
{
- //Todo change body of implemented methods use File | Settings | File Templates.
+ try
+ {
+ registerServlet(alias, new ServletWrapper(alias, name, httpContext), EMPTY_PARAMS, httpContext);
+ }
+ catch (ServletException se)
+ {
+ LOGGER.log(Level.SEVERE, "Error registering resource wrapper servlet", se);
+ }
}
+ /**
+ * {@inheritDoc}
+ */
public void unregister(String alias)
{
- ServletRegistration registration = registrations.get(alias);
+ ServletRegistration registration;
+ synchronized (lock)
+ {
+ registration = registrations.remove(alias);
- registration.getServlet().destroy();
- ServletContextImpl servletContext = contexts.get(registration.getContext());
+ if (registration == null) return;
- servletContext.decrementReferenceCount();
- if (servletContext.getReferenceCount() == 0) contexts.remove(registration.getContext());
+ ServletContextImpl servletContext = contexts.get(registration.getContext());
+ servletContext.decrementReferenceCount();
+ if (servletContext.getReferenceCount() == 0) contexts.remove(registration.getContext());
+ }
- //Todo change body of implemented methods use File | Settings | File Templates.
+ try
+ {
+ dispatcher.unregister(registration);
+
+ registration.getServlet().destroy();
+ }
+ catch (Throwable t)
+ {
+ LOGGER.log(Level.WARNING, "Error destroying servlet", t);
+ }
}
+ /**
+ * {@inheritDoc}
+ */
public HttpContext createDefaultHttpContext()
{
- return null; //Todo change body of implemented methods use File | Settings | File Templates.
+ return new HttpContext()
+ {
+ public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException
+ {
+ return true;
+ }
+
+ public URL getResource(String name)
+ {
+ if (name.startsWith("/")) name = name.substring(1);
+
+ return context.getBundle().getResource(name);
+ }
+
+ public String getMimeType(String name)
+ {
+ return null;
+ }
+ };
}
}
Modified: papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletConfigImpl.java
===================================================================
--- papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletConfigImpl.java 2010-02-23 19:45:40 UTC (rev 249)
+++ papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletConfigImpl.java 2010-02-24 03:57:10 UTC (rev 250)
@@ -20,42 +20,50 @@
import javax.servlet.ServletContext;
import java.util.Dictionary;
import java.util.Enumeration;
-import java.util.logging.Logger;
+import java.util.Properties;
-import org.osgi.service.http.HttpContext;
-
/**
* @version $Revision: $ $Date: $
*/
-public class ServletConfigImpl implements ServletConfig
+class ServletConfigImpl implements ServletConfig
{
- private final static String CLASS_NAME = ServletConfigImpl.class.getName();
- private final static Logger LOGGER = Logger.getLogger(CLASS_NAME);
+ private final static Properties EMPTY = new Properties();
+ private final String alias;
+ private final ServletContextImpl servletContext;
+ private final Properties initParams;
- public ServletConfigImpl(String alias, ServletContextImpl servletContext, Dictionary httpContext)
+ ServletConfigImpl(String alias, ServletContextImpl servletContext, Dictionary initParams)
{
+ this.alias = alias;
+ this.servletContext = servletContext;
-
+ if (initParams == null) this.initParams = EMPTY;
+ else
+ {
+ Enumeration enumeration = initParams.keys();
+ while (enumeration.)
+ }
+ this.initParams = initParams;
}
public String getServletName()
{
- return null; //Todo change body of implemented methods use File | Settings | File Templates.
+ return alias;
}
public ServletContext getServletContext()
{
- return null; //Todo change body of implemented methods use File | Settings | File Templates.
+ return servletContext;
}
public String getInitParameter(String name)
{
- return null; //Todo change body of implemented methods use File | Settings | File Templates.
+ return (String) initParams.get(name);
}
public Enumeration getInitParameterNames()
{
- return null; //Todo change body of implemented methods use File | Settings | File Templates.
+ return initParams.keys();
}
}
Modified: papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletContextImpl.java
===================================================================
--- papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletContextImpl.java 2010-02-23 19:45:40 UTC (rev 249)
+++ papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletContextImpl.java 2010-02-24 03:57:10 UTC (rev 250)
@@ -33,14 +33,14 @@
/**
* @version $Revision: $ $Date: $
*/
-public class ServletContextImpl implements ServletContext
+ class ServletContextImpl implements ServletContext
{
private final static String CLASS_NAME = ServletContextImpl.class.getName();
private final static Logger LOGGER = Logger.getLogger(CLASS_NAME);
private final HttpContext httpContext;
private int referenceCount;
- public ServletContextImpl(HttpContext httpContext)
+ ServletContextImpl(HttpContext httpContext)
{
this.httpContext = httpContext;
}
Modified: papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletRegistration.java
===================================================================
--- papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletRegistration.java 2010-02-23 19:45:40 UTC (rev 249)
+++ papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletRegistration.java 2010-02-24 03:57:10 UTC (rev 250)
@@ -26,20 +26,18 @@
/**
* @version $Revision: $ $Date: $
*/
-public class ServletRegistration
+class ServletRegistration
{
private final static String CLASS_NAME = ServletRegistration.class.getName();
private final static Logger LOGGER = Logger.getLogger(CLASS_NAME);
private final String alias;
private final Servlet servlet;
- private final Dictionary initparams;
private final HttpContext context;
- public ServletRegistration(String alias, Servlet servlet, Dictionary initparams, HttpContext context)
+ ServletRegistration(String alias, Servlet servlet, HttpContext context)
{
this.alias = alias;
this.servlet = servlet;
- this.initparams = initparams;
this.context = context;
}
@@ -57,9 +55,4 @@
{
return servlet;
}
-
- public Dictionary getInitparams()
- {
- return initparams;
- }
}
Added: papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletWrapper.java
===================================================================
--- papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletWrapper.java (rev 0)
+++ papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletWrapper.java 2010-02-24 03:57:10 UTC (rev 250)
@@ -0,0 +1,52 @@
+/**
+ *
+ * Copyright 2010 (C) The original author or authors
+ *
+ * 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 org.papoose.http;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.logging.Logger;
+
+import org.osgi.service.http.HttpContext;
+
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+ class ServletWrapper extends HttpServlet
+{
+ private final static String CLASS_NAME = ServletWrapper.class.getName();
+ private final static Logger LOGGER = Logger.getLogger(CLASS_NAME);
+ private final String alias;
+ private final String name;
+ private final HttpContext httpContext;
+
+ ServletWrapper(String alias, String name, HttpContext httpContext)
+ {
+ this.alias = alias;
+ this.name = name;
+ this.httpContext = httpContext;
+ }
+
+ @Override
+ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ super.service(req, resp); //Todo change body of overridden methods use File | Settings | File Templates.
+ }
+}
Property changes on: papoose-cmpn/trunk/http/src/main/java/org/papoose/http/ServletWrapper.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|