Author: [email protected]
Date: Wed Aug 10 10:31:17 2011
New Revision: 1304

Log:
[AMDATUOPENSOCIAL-38] Resolved potential nullpointer exceptions, added 
try/finally clause for PROPERTIES reset

Modified:
   
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/TenantHostnameDispatchExtenderFilter.java
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java

Modified: 
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/TenantHostnameDispatchExtenderFilter.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/TenantHostnameDispatchExtenderFilter.java
    (original)
+++ 
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/TenantHostnameDispatchExtenderFilter.java
    Wed Aug 10 10:31:17 2011
@@ -13,86 +13,98 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.opensocial.profile.service;
-
-import java.io.IOException;
-
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-
-import org.amdatu.core.tenant.Tenant;
-import org.amdatu.web.dispatcher.DispatchExtenderFilter;
-import 
org.amdatu.web.tenantresolver.hostname.service.HostnameTenantResolverExtenderFilter;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.useradmin.UserAdmin;
-
-/**
- * This tenant dispatch extender filter is registered on .* and captures all 
HTTP requests to resolve the
- * tenant from the hostname of the request. It stores the tenant for the 
lifetime of that request on a
- * ThreadLocal so that the PersonServiceImpl can retrieve the tenant from this 
ThreadLocal. Note that
- * the PersonServiceImpl is not tenant aware, nor does it have access to the 
HTTP request.
- * 
- * @author ivol
- */
-public class TenantHostnameDispatchExtenderFilter implements 
DispatchExtenderFilter {
-    // Service dependencies, injected by the framework
-    private static volatile BundleContext BUNDLE_CONTEXT;
-
-    // Tenant context information is bound to a ThreadLocal variable.
-    private static ThreadLocal<Tenant> PROPERTIES = null;
-
-    public void init(final FilterConfig filterConfig) throws ServletException {
-        // Create the thread local
-        PROPERTIES = new ThreadLocal<Tenant>();
-    }
-
-    public void doFilter(final ServletRequest request, final ServletResponse 
response, final FilterChain chain)
-        throws IOException, ServletException {
-        Object tenant = 
request.getAttribute(HostnameTenantResolverExtenderFilter.TENANT_REQUESTCONTEXT_KEY);
-        if (tenant != null && tenant instanceof Tenant) {
-            PROPERTIES.set((Tenant) tenant);
-        }
-
-        chain.doFilter(request, response);
-
-        // Set the tenant to null
-        PROPERTIES.set(null);
-    }
-
-    public void destroy() {
-        // Destroy the thread local
-        PROPERTIES = null;
-    }
-
-    /**
-     * Returns the tenant on the ThreadLocal.
-     * 
-     * @return the current tenant
-     */
-    public static Tenant getTenant() {
-        return PROPERTIES.get();
-    }
-
-    /**
-     * Returns the UserAdmin service that is associated with the current 
tenant.
-     * 
-     * @return the UserAdmin service that is associated with the current 
tenant.
-     * @throws InvalidSyntaxException
-     *         In case the UserAdmin service references could not be retrieved.
-     */
-    public static UserAdmin getUserAdmin() throws InvalidSyntaxException {
-        Tenant tenant = getTenant();
-        ServiceReference[] refs = 
BUNDLE_CONTEXT.getAllServiceReferences(UserAdmin.class.getName(), null);
-        for (ServiceReference ref : refs) {
-            if 
(tenant.getId().equals(ref.getProperty(Tenant.TENANT_ID_SERVICEPROPERTY))) {
-                return (UserAdmin) BUNDLE_CONTEXT.getService(ref);
-            }
-        }
-        return null;
-    }
-}
+package org.amdatu.opensocial.profile.service;
+
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.amdatu.core.tenant.Tenant;
+import org.amdatu.web.dispatcher.DispatchExtenderFilter;
+import 
org.amdatu.web.tenantresolver.hostname.service.HostnameTenantResolverExtenderFilter;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.useradmin.UserAdmin;
+
+/**
+ * This tenant dispatch extender filter is registered on .* and captures all 
HTTP requests to resolve the
+ * tenant from the hostname of the request. It stores the tenant for the 
lifetime of that request on a
+ * ThreadLocal so that the PersonServiceImpl can retrieve the tenant from this 
ThreadLocal. Note that
+ * the PersonServiceImpl is not tenant aware, nor does it have access to the 
HTTP request.
+ * 
+ * @author ivol
+ */
+public class TenantHostnameDispatchExtenderFilter implements 
DispatchExtenderFilter {
+    // Service dependencies, injected by the framework
+    private static volatile BundleContext BUNDLE_CONTEXT;
+
+    // Tenant context information is bound to a ThreadLocal variable.
+    private static ThreadLocal<Tenant> PROPERTIES = null;
+
+    public void init(final FilterConfig filterConfig) throws ServletException {
+        // Create the thread local
+        PROPERTIES = new ThreadLocal<Tenant>();
+    }
+
+    public void doFilter(final ServletRequest request, final ServletResponse 
response, final FilterChain chain)
+        throws IOException, ServletException {
+        try {
+            Object tenant = 
request.getAttribute(HostnameTenantResolverExtenderFilter.TENANT_REQUESTCONTEXT_KEY);
+            if (tenant != null && tenant instanceof Tenant) {
+                PROPERTIES.set((Tenant) tenant);
+            }
+
+            chain.doFilter(request, response);
+        }
+        finally {
+            // Set the tenant to null
+            PROPERTIES.set(null);
+        }
+    }
+
+    public void destroy() {
+        // Destroy the thread local
+        PROPERTIES = null;
+        BUNDLE_CONTEXT = null;
+    }
+
+    /**
+     * Returns the tenant on the ThreadLocal.
+     * 
+     * @return the current tenant
+     */
+    public static Tenant getTenant() {
+        if (PROPERTIES == null || BUNDLE_CONTEXT == null) {
+            return null;
+        }
+        return PROPERTIES.get();
+    }
+
+    /**
+     * Returns the UserAdmin service that is associated with the current 
tenant.
+     * 
+     * @return the UserAdmin service that is associated with the current 
tenant.
+     * @throws InvalidSyntaxException
+     *         In case the UserAdmin service references could not be retrieved.
+     */
+    public static UserAdmin getUserAdmin() throws InvalidSyntaxException {
+        if (PROPERTIES == null || BUNDLE_CONTEXT == null) {
+            return null;
+        }
+        Tenant tenant = getTenant();
+        ServiceReference[] refs = 
BUNDLE_CONTEXT.getAllServiceReferences(UserAdmin.class.getName(), null);
+        if (refs != null) {
+            for (ServiceReference ref : refs) {
+                if (tenant != null && 
tenant.getId().equals(ref.getProperty(Tenant.TENANT_ID_SERVICEPROPERTY))) {
+                    return (UserAdmin) BUNDLE_CONTEXT.getService(ref);
+                }
+            }
+        }
+        return null;
+    }
+}

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java
    (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java
    Wed Aug 10 10:31:17 2011
@@ -13,86 +13,98 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.opensocial.shindig.service;
-
-import java.io.IOException;
-
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-
-import org.amdatu.core.tenant.Tenant;
-import org.amdatu.web.dispatcher.DispatchExtenderFilter;
-import 
org.amdatu.web.tenantresolver.hostname.service.HostnameTenantResolverExtenderFilter;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.useradmin.UserAdmin;
-
-/**
- * This tenant dispatch extender filter is registered on .* and captures all 
HTTP requests to resolve the
- * tenant from the hostname of the request. It stores the tenant for the 
lifetime of that request on a
- * ThreadLocal so that the PersonServiceImpl can retrieve the tenant from this 
ThreadLocal. Note that
- * the CassandraAppDataServiceStore is not tenant aware, nor does it have 
access to the HTTP request.
- * 
- * @author ivol
- */
-public class TenantHostnameDispatchExtenderFilter implements 
DispatchExtenderFilter {
-    // Service dependencies, injected by the framework
-    private static volatile BundleContext BUNDLE_CONTEXT;
-
-    // Tenant context information is bound to a ThreadLocal variable.
-    private static ThreadLocal<Tenant> PROPERTIES = null;
-
-    public void init(final FilterConfig filterConfig) throws ServletException {
-        // Create the thread local
-        PROPERTIES = new ThreadLocal<Tenant>();
-    }
-
-    public void doFilter(final ServletRequest request, final ServletResponse 
response, final FilterChain chain)
-        throws IOException, ServletException {
-        Object tenant = 
request.getAttribute(HostnameTenantResolverExtenderFilter.TENANT_REQUESTCONTEXT_KEY);
-        if (tenant != null && tenant instanceof Tenant) {
-            PROPERTIES.set((Tenant) tenant);
-        }
-
-        chain.doFilter(request, response);
-
-        // Set the tenant to null
-        PROPERTIES.set(null);
-    }
-
-    public void destroy() {
-        // Destroy the thread local
-        PROPERTIES = null;
-    }
-
-    /**
-     * Returns the tenant on the ThreadLocal.
-     * 
-     * @return the current tenant
-     */
-    public static Tenant getTenant() {
-        return PROPERTIES.get();
-    }
-
-    /**
-     * Returns the UserAdmin service that is associated with the current 
tenant.
-     * 
-     * @return the UserAdmin service that is associated with the current 
tenant.
-     * @throws InvalidSyntaxException
-     *         In case the UserAdmin service references could not be retrieved.
-     */
-    public static UserAdmin getUserAdmin() throws InvalidSyntaxException {
-        Tenant tenant = getTenant();
-        ServiceReference[] refs = 
BUNDLE_CONTEXT.getAllServiceReferences(UserAdmin.class.getName(), null);
-        for (ServiceReference ref : refs) {
-            if (tenant != null && 
tenant.getId().equals(ref.getProperty(Tenant.TENANT_ID_SERVICEPROPERTY))) {
-                return (UserAdmin) BUNDLE_CONTEXT.getService(ref);
-            }
-        }
-        return null;
-    }
-}
+package org.amdatu.opensocial.shindig.service;
+
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.amdatu.core.tenant.Tenant;
+import org.amdatu.web.dispatcher.DispatchExtenderFilter;
+import 
org.amdatu.web.tenantresolver.hostname.service.HostnameTenantResolverExtenderFilter;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.useradmin.UserAdmin;
+
+/**
+ * This tenant dispatch extender filter is registered on .* and captures all 
HTTP requests to resolve the
+ * tenant from the hostname of the request. It stores the tenant for the 
lifetime of that request on a
+ * ThreadLocal so that the PersonServiceImpl can retrieve the tenant from this 
ThreadLocal. Note that
+ * the CassandraAppDataServiceStore is not tenant aware, nor does it have 
access to the HTTP request.
+ * 
+ * @author ivol
+ */
+public class TenantHostnameDispatchExtenderFilter implements 
DispatchExtenderFilter {
+    // Service dependencies, injected by the framework
+    private static volatile BundleContext BUNDLE_CONTEXT;
+
+    // Tenant context information is bound to a ThreadLocal variable.
+    private static ThreadLocal<Tenant> PROPERTIES = null;
+
+    public void init(final FilterConfig filterConfig) throws ServletException {
+        // Create the thread local
+        PROPERTIES = new ThreadLocal<Tenant>();
+    }
+
+    public void doFilter(final ServletRequest request, final ServletResponse 
response, final FilterChain chain)
+        throws IOException, ServletException {
+        try {
+            Object tenant = 
request.getAttribute(HostnameTenantResolverExtenderFilter.TENANT_REQUESTCONTEXT_KEY);
+            if (tenant != null && tenant instanceof Tenant) {
+                PROPERTIES.set((Tenant) tenant);
+            }
+
+            chain.doFilter(request, response);
+        }
+        finally {
+            // Set the tenant to null
+            PROPERTIES.set(null);
+        }
+    }
+
+    public void destroy() {
+        // Destroy the thread local
+        PROPERTIES = null;
+        BUNDLE_CONTEXT = null;
+    }
+
+    /**
+     * Returns the tenant on the ThreadLocal.
+     * 
+     * @return the current tenant
+     */
+    public static Tenant getTenant() {
+        if (PROPERTIES == null || BUNDLE_CONTEXT == null) {
+            return null;
+        }
+        return PROPERTIES.get();
+    }
+
+    /**
+     * Returns the UserAdmin service that is associated with the current 
tenant.
+     * 
+     * @return the UserAdmin service that is associated with the current 
tenant.
+     * @throws InvalidSyntaxException
+     *         In case the UserAdmin service references could not be retrieved.
+     */
+    public static UserAdmin getUserAdmin() throws InvalidSyntaxException {
+        if (PROPERTIES == null || BUNDLE_CONTEXT == null) {
+            return null;
+        }
+        Tenant tenant = getTenant();
+        ServiceReference[] refs = 
BUNDLE_CONTEXT.getAllServiceReferences(UserAdmin.class.getName(), null);
+        if (refs != null) {
+            for (ServiceReference ref : refs) {
+                if (tenant != null && 
tenant.getId().equals(ref.getProperty(Tenant.TENANT_ID_SERVICEPROPERTY))) {
+                    return (UserAdmin) BUNDLE_CONTEXT.getService(ref);
+                }
+            }
+        }
+        return null;
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to