Author: [email protected]
Date: Thu Oct 27 11:29:44 2011
New Revision: 1666

Log:
AMDATU-435 some additional fixes to prevent potential deadlocks, removed a 
dependendency on the DispatcherService because all this bundle does is register 
servlets whiteboard style so it does not need to depend on anything

Modified:
   
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/osgi/Activator.java
   
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/service/WinkRegistrationServiceImpl.java

Modified: 
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/osgi/Activator.java
       (original)
+++ 
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/osgi/Activator.java
       Thu Oct 27 11:29:44 2011
@@ -13,8 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.web.rest.wink.osgi;
-
+package org.amdatu.web.rest.wink.osgi;
+
 import org.amdatu.web.dispatcher.DispatcherService;
 import org.amdatu.web.rest.wink.service.WinkRegistrationServiceImpl;
 import org.apache.felix.dm.Component;
@@ -24,34 +24,31 @@
 import org.osgi.framework.Constants;
 import org.osgi.service.log.LogService;
 
-
-/**
- * This is the OSGi activator for the Amdatu REST framework based on Apache 
Wink.
- */
-public class Activator extends DependencyActivatorBase {
-
-    public final static String CONTEXTID = "amdatu-rest";
-    public final static String ALIAS = "/rest";
-
-    @Override
-    public void init(BundleContext context, DependencyManager manager) throws 
Exception {
+
+/**
+ * This is the OSGi activator for the Amdatu REST framework based on Apache 
Wink.
+ */
+public class Activator extends DependencyActivatorBase {
+
+    public final static String CONTEXTID = "amdatu-rest";
+    public final static String ALIAS = "/rest";
+
+    @Override
+    public void init(BundleContext context, DependencyManager manager) throws 
Exception {
         manager.add(createComponent()
-            .setAutoConfig(Component.class, Boolean.FALSE)
-            .setImplementation(WinkRegistrationServiceImpl.class)
-            .add(createServiceDependency()
-                       .setService(DispatcherService.class)
-                       .setRequired(true))
+            .setAutoConfig(Component.class, Boolean.FALSE)
+            .setImplementation(WinkRegistrationServiceImpl.class)
             .add(createServiceDependency()
                        .setService("(" + Constants.OBJECTCLASS + "=*)")
                        .setCallbacks("onAdded", "onRemoved")
-                       .setRequired(false))
+                       .setRequired(false))
             .add(createServiceDependency()
                        .setService(LogService.class)
                        .setRequired(false))
-               );
-    }
-
-    @Override
-    public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
-    }
-}
+               );
+    }
+
+    @Override
+    public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
+    }
+}

Modified: 
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/service/WinkRegistrationServiceImpl.java
==============================================================================
--- 
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/service/WinkRegistrationServiceImpl.java
  (original)
+++ 
trunk/amdatu-web/rest-wink/src/main/java/org/amdatu/web/rest/wink/service/WinkRegistrationServiceImpl.java
  Thu Oct 27 11:29:44 2011
@@ -13,8 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.web.rest.wink.service;
-
+package org.amdatu.web.rest.wink.service;
+
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.concurrent.ConcurrentHashMap;
@@ -37,160 +37,150 @@
 import org.apache.wink.common.internal.runtime.RuntimeDelegateImpl;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.log.LogService;
-
-/**
- * This class is responsible for booting Wink as well as publication of new 
REST
- * services. Any service that declares a valid <code>Path</code> annotation on
- * its interfaces will be published as a REST servlet. The servlet alias is
- * determined by adding the resource Path value to the REST base path.
- */
-public final class WinkRegistrationServiceImpl {
-
-    // Location of application properties in this bundle
-    private final static String APP_RPOPS = "/conf/application.properties";
-
-    // Map of registered REST service components
-    private final ConcurrentHashMap<ServiceReference, Component> 
m_servletComponents =
-        new ConcurrentHashMap<ServiceReference, Component>();
-
-    // Services injected by dependency manager
-    private volatile DependencyManager m_dependencyManager;
-    private volatile LogService m_logService;
-
-    // Autoconf disabled in activator
+
+/**
+ * This class is responsible for booting Wink as well as publication of new 
REST
+ * services. Any service that declares a valid <code>Path</code> annotation on
+ * its interfaces will be published as a REST servlet. The servlet alias is
+ * determined by adding the resource Path value to the REST base path.
+ */
+public class WinkRegistrationServiceImpl {
+    // Location of application properties in this bundle
+    private final static String APP_PROPS = "/conf/application.properties";
+
+    // Services injected by dependency manager
+    private volatile DependencyManager m_dependencyManager;
+    private volatile LogService m_logService;
+
+    // Map of registered REST service components
+    private final ConcurrentHashMap<ServiceReference, Component> 
m_servletComponents = new ConcurrentHashMap<ServiceReference, Component>();
+
     private Component m_spiComponent;
-
-    public synchronized void init() {
-        setRuntimeDelegate();
-        registerSpiService();
-        m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
initialized");
-    }
-
-    public synchronized void destroy() {
-        removeSpiService();
-        for (Component servletComponent : m_servletComponents.values()) {
-            m_dependencyManager.remove(servletComponent);
-        }
-        m_servletComponents.clear();
-        m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
destroyed");
-    }
-
-    public void onAdded(ServiceReference serviceReference, Object service) {
-        if (service.getClass().getAnnotation(Path.class) != null) {
-            addRestServlet(serviceReference, service);
-        }
-    }
-
-    public void onRemoved(ServiceReference serviceReference, Object service) {
-        if (service.getClass().getAnnotation(Path.class) != null) {
-            removeRestServlet(serviceReference, service);
-        }
-    }
-
-    private void addRestServlet(ServiceReference serviceReference, Object 
service) {
-
-        Dictionary<String, String> initParams = new Hashtable<String, 
String>();
-        initParams.put("init.applicationConfigLocation", APP_RPOPS);
-
-        String restPath = service.getClass().getAnnotation(Path.class).value();
-        if (restPath.startsWith("/")) {
-            restPath = restPath.substring(1);
-        }
-        String servletAlias = Activator.ALIAS + "/" + restPath;
-        initParams.put(DispatcherService.ALIAS_KEY, servletAlias);
-
-        String contextId = getStringProperty(serviceReference, 
HttpContextManagerService.CONTEXT_ID_KEY);
-        if (!contextId.equals("")) {
-            initParams.put(HttpContextManagerService.CONTEXT_ID_KEY, 
contextId);
-        }
-
-        String tenantId = getStringProperty(serviceReference, 
Tenant.TENANT_ID_SERVICEPROPERTY);
-        if (!tenantId.equals("")) {
-            initParams.put(Tenant.TENANT_ID_SERVICEPROPERTY, tenantId);
-        }
-
-        WinkRestServlet restServlet = new WinkRestServlet(restPath, service, 
tenantId);
-
-        Component servletComponent = m_dependencyManager.createComponent()
-            .setInterface(Servlet.class.getName(), initParams)
-            .setImplementation(restServlet)
-            .setCallbacks("_init", "start", "stop", "_destroy");
-        
-        // threadsafe
-        if (m_servletComponents.putIfAbsent(serviceReference, 
servletComponent) == null) {
-            m_dependencyManager.add(servletComponent);
-            m_logService.log(LogService.LOG_DEBUG, "Wink application 
registered REST servlet '" + servletAlias + "'");
-        }
-        else {
-            m_logService.log(LogService.LOG_ERROR, "Duplicate ServiceReference 
in callback: " + serviceReference);
-        }
-    }
-
-    private void removeRestServlet(ServiceReference serviceReference, Object 
service) {
-        // threadsafe
-        Component servletComponent = 
m_servletComponents.remove(serviceReference);
-        if (servletComponent != null) {
-            m_dependencyManager.remove(servletComponent);
-            m_logService.log(LogService.LOG_DEBUG, "Wink application 
unregistered REST servlet");
-        }
-        else {
-            m_logService.log(LogService.LOG_ERROR, "Unknown ServiceReference 
in callback: " + serviceReference);
-        }
-    }
-
-    private void registerSpiService() {
-        Component comp = m_dependencyManager.createComponent()
-            .setInterface(JaxRsSpi.class.getName(), null)
-            .setImplementation(new JaxRsSpi() {
-            });
-        m_dependencyManager.add(comp);
+
+    public void init() {
+        setRuntimeDelegate();
+        registerSpiService();
+    }
+
+    public void destroy() {
+        removeSpiService();
+        for (Component servletComponent : m_servletComponents.values()) {
+            m_dependencyManager.remove(servletComponent);
+        }
+        m_servletComponents.clear();
+    }
+
+    public void onAdded(ServiceReference serviceReference, Object service) {
+        if (service.getClass().getAnnotation(Path.class) != null) {
+            addRestServlet(serviceReference, service);
+        }
+    }
+
+    public void onRemoved(ServiceReference serviceReference, Object service) {
+        if (service.getClass().getAnnotation(Path.class) != null) {
+            removeRestServlet(serviceReference, service);
+        }
+    }
+
+    private void addRestServlet(ServiceReference serviceReference, Object 
service) {
+        Dictionary<String, String> initParams = new Hashtable<String, 
String>();
+        initParams.put("init.applicationConfigLocation", APP_PROPS);
+
+        String restPath = service.getClass().getAnnotation(Path.class).value();
+        String servletAlias = Activator.ALIAS + (restPath.startsWith("/") ? 
restPath : "/" + restPath);
+        initParams.put(DispatcherService.ALIAS_KEY, servletAlias);
+
+        String contextId = getStringProperty(serviceReference, 
HttpContextManagerService.CONTEXT_ID_KEY);
+        if (!contextId.equals("")) {
+            initParams.put(HttpContextManagerService.CONTEXT_ID_KEY, 
contextId);
+        }
+
+        String tenantId = getStringProperty(serviceReference, 
Tenant.TENANT_ID_SERVICEPROPERTY);
+        if (!tenantId.equals("")) {
+            initParams.put(Tenant.TENANT_ID_SERVICEPROPERTY, tenantId);
+        }
+
+        WinkRestServlet restServlet = new WinkRestServlet(restPath, service, 
tenantId);
+
+        Component servletComponent = m_dependencyManager.createComponent()
+            .setInterface(Servlet.class.getName(), initParams)
+            .setImplementation(restServlet)
+            .setCallbacks("_init", "start", "stop", "_destroy");
+        
+        // threadsafe
+        if (m_servletComponents.putIfAbsent(serviceReference, 
servletComponent) == null) {
+            m_dependencyManager.add(servletComponent);
+            m_logService.log(LogService.LOG_DEBUG, "Wink application 
registered REST servlet '" + servletAlias + "'");
+        }
+        else {
+            m_logService.log(LogService.LOG_ERROR, "Duplicate ServiceReference 
in callback: " + serviceReference);
+        }
+    }
+
+    private void removeRestServlet(ServiceReference serviceReference, Object 
service) {
+        // threadsafe
+        Component servletComponent = 
m_servletComponents.remove(serviceReference);
+        if (servletComponent != null) {
+            m_dependencyManager.remove(servletComponent);
+            m_logService.log(LogService.LOG_DEBUG, "Wink application 
unregistered REST servlet");
+        }
+        else {
+            m_logService.log(LogService.LOG_ERROR, "Unknown ServiceReference 
in callback: " + serviceReference);
+        }
+    }
+
+    private void registerSpiService() {
+        Component comp = m_dependencyManager.createComponent()
+            .setInterface(JaxRsSpi.class.getName(), null)
+            .setImplementation(new JaxRsSpi() {});
+        m_dependencyManager.add(comp);
         m_spiComponent = comp;
-    }
-
-    private void removeSpiService() {
+    }
+
+    private void removeSpiService() {
         m_dependencyManager.remove(m_spiComponent);
-    }
-
-    /**
-     * Sets the runtime delegate, used to create instances of desired endpoint 
classes.
-     */
-    private void setRuntimeDelegate() {
-        // FIXME: OK, this is nasty, but necessary. Without this piece of code 
a NoClassDefFoundError
-        // is thrown during servlet initialization on the class 
com.sun.ws.rs.ext.RuntimeDelegateImpl.
-        // The reason for this is that JAX-RS by default delegates to this 
class if no other RuntimeDelegate
-        // implementation could be found. Apache Wink does come with its own 
RuntimeDelegate, but during
-        // initialization of this instance, RuntimeDelegate.getInstance() is 
invoked which causes a method
-        // call to com.sun.ws.rs.ext.RuntimeDelegateImpl (see 
EntityTagMatchHeaderDelegate).
-        // Other frameworks face similar issues using JAX-RS, see for example 
this URL for the exact same
-        // problem in the Restlet framework: 
http://www.mail-archive.com/[email protected]/msg07539.html
-        // The nasty fix is to set some dummy RuntimeDelegate first, then set 
the Wink RuntimeDelegateImpl
-        RuntimeDelegate.setInstance(new RuntimeDelegate() {
-            public <T> T createEndpoint(Application arg0, Class<T> arg1) 
throws IllegalArgumentException,
-                UnsupportedOperationException {
-                return null;
-            }
-
-            public <T> HeaderDelegate<T> createHeaderDelegate(Class<T> arg0) {
-                return null;
-            }
-
-            public ResponseBuilder createResponseBuilder() {
-                return null;
-            }
-
-            public UriBuilder createUriBuilder() {
-                return null;
-            }
-
-            public VariantListBuilder createVariantListBuilder() {
-                return null;
-            }
-        });
-        RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
-    }
-
-    private String getStringProperty(ServiceReference ref, String key) {
-        Object value = ref.getProperty(key);
-        return (value instanceof String) ? (String) value : "";
-    }
-}
+    }
+
+    /**
+     * Sets the runtime delegate, used to create instances of desired endpoint 
classes.
+     */
+    private void setRuntimeDelegate() {
+        // FIXME: OK, this is nasty, but necessary. Without this piece of code 
a NoClassDefFoundError
+        // is thrown during servlet initialization on the class 
com.sun.ws.rs.ext.RuntimeDelegateImpl.
+        // The reason for this is that JAX-RS by default delegates to this 
class if no other RuntimeDelegate
+        // implementation could be found. Apache Wink does come with its own 
RuntimeDelegate, but during
+        // initialization of this instance, RuntimeDelegate.getInstance() is 
invoked which causes a method
+        // call to com.sun.ws.rs.ext.RuntimeDelegateImpl (see 
EntityTagMatchHeaderDelegate).
+        // Other frameworks face similar issues using JAX-RS, see for example 
this URL for the exact same
+        // problem in the Restlet framework: 
http://www.mail-archive.com/[email protected]/msg07539.html
+        // The nasty fix is to set some dummy RuntimeDelegate first, then set 
the Wink RuntimeDelegateImpl
+        RuntimeDelegate.setInstance(new RuntimeDelegate() {
+            public <T> T createEndpoint(Application arg0, Class<T> arg1) 
throws IllegalArgumentException,
+                UnsupportedOperationException {
+                return null;
+            }
+
+            public <T> HeaderDelegate<T> createHeaderDelegate(Class<T> arg0) {
+                return null;
+            }
+
+            public ResponseBuilder createResponseBuilder() {
+                return null;
+            }
+
+            public UriBuilder createUriBuilder() {
+                return null;
+            }
+
+            public VariantListBuilder createVariantListBuilder() {
+                return null;
+            }
+        });
+        RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
+    }
+
+    private String getStringProperty(ServiceReference ref, String key) {
+        Object value = ref.getProperty(key);
+        return (value instanceof String) ? (String) value : "";
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to