Author: evan
Date: Tue Oct  7 13:43:32 2008
New Revision: 702625

URL: http://svn.apache.org/viewvc?rev=702625&view=rev
Log:
Additional files for SHINDIG-641

Added:
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/HandlerDispatcher.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcher.java
    
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcherTest.java

Added: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/HandlerDispatcher.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/HandlerDispatcher.java?rev=702625&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/HandlerDispatcher.java
 (added)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/HandlerDispatcher.java
 Tue Oct  7 13:43:32 2008
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.shindig.social.opensocial.service;
+
+import com.google.inject.ImplementedBy;
+
+/**
+ * Dispatcher for DataRequestHandler requests.  The default implementation
+ * registers the three standard Shindig handlers.
+ * <p>
+ * Add a custom binding of this interface to customize request handling.
+ */
[EMAIL PROTECTED](StandardHandlerDispatcher.class)
+public interface HandlerDispatcher {
+
+  /**
+   * @param service a service name
+   * @return the handler, or null if no handler is registered for that service
+   */
+  DataRequestHandler getHandler(String service);
+}

Added: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcher.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcher.java?rev=702625&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcher.java
 (added)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcher.java
 Tue Oct  7 13:43:32 2008
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.shindig.social.opensocial.service;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.common.collect.Maps;
+
+import java.util.Map;
+
+/**
+ * Default implementation of HandlerDispatcher.  Provides default
+ * bindings for the person, activity, and app data handlers.
+ */
+public class StandardHandlerDispatcher implements HandlerDispatcher {
+  private final Map<String, Provider<? extends DataRequestHandler>> handlers;
+
+  /**
+   * Creates a dispatcher with the standard handlers.
+   * @param personHandlerProvider provider for the person handler
+   * @param activityHandlerProvider provider for the activity handler
+   * @param appDataHandlerProvider provider for the app data handler
+   */
+  @Inject
+  public StandardHandlerDispatcher(Provider<PersonHandler> 
personHandlerProvider,
+      Provider<ActivityHandler> activityHandlerProvider,
+      Provider<AppDataHandler> appDataHandlerProvider) {
+    this(Maps.immutableMap(
+        DataServiceServlet.PEOPLE_ROUTE, personHandlerProvider,
+        DataServiceServlet.ACTIVITY_ROUTE, activityHandlerProvider,
+        DataServiceServlet.APPDATA_ROUTE, appDataHandlerProvider));
+  }
+
+  /**
+   * Creates a dispatcher with a custom list of handlers.
+   * @param handlers a map of handlers by service name
+   */
+  public StandardHandlerDispatcher(Map<String,Provider<? extends 
DataRequestHandler>> handlers) {
+    this.handlers = Maps.newHashMap(handlers);
+  }
+
+  /**
+   * Gets a handler by service name.
+   */
+  public DataRequestHandler getHandler(String service) {
+    Provider<? extends DataRequestHandler> provider = handlers.get(service);
+    if (provider == null) {
+      return null;
+    }
+
+    return provider.get();
+  }
+
+  /**
+   * Adds a custom handler.
+   */
+  public void addHandler(String service, Provider<? extends 
DataRequestHandler> handler) {
+    handlers.put(service, handler);
+  }
+}

Added: 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcherTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcherTest.java?rev=702625&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcherTest.java
 (added)
+++ 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/StandardHandlerDispatcherTest.java
 Tue Oct  7 13:43:32 2008
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.shindig.social.opensocial.service;
+
+import com.google.inject.Provider;
+
+import junit.framework.TestCase;
+
+import org.easymock.classextension.IMocksControl;
+import org.easymock.classextension.EasyMock;
+
+/**
+ * Tests StandardHandlerDispatcher.
+ */
+public class StandardHandlerDispatcherTest extends TestCase {
+
+  private Provider<PersonHandler> personHandlerProvider;
+  private Provider<AppDataHandler> appDataHandlerProvider;
+  private Provider<ActivityHandler> activityHandlerProvider;
+
+  private IMocksControl mockControl;
+
+  private StandardHandlerDispatcher dispatcher;
+
+  @Override
+  @SuppressWarnings("unchecked")
+  protected void setUp() throws Exception {
+    super.setUp();
+
+    mockControl = EasyMock.createControl();
+    personHandlerProvider = (Provider<PersonHandler>)  
mockControl.createMock(Provider.class);
+    appDataHandlerProvider = (Provider<AppDataHandler>)  
mockControl.createMock(Provider.class);
+    activityHandlerProvider = (Provider<ActivityHandler>) 
mockControl.createMock(Provider.class);
+    dispatcher = new StandardHandlerDispatcher(personHandlerProvider,
+            activityHandlerProvider, appDataHandlerProvider);
+  }
+
+  public void testGetHandler() {
+    PersonHandler handler = mockControl.createMock(PersonHandler.class);
+    EasyMock.expect(personHandlerProvider.get()).andReturn(handler);
+
+    mockControl.replay();
+
+    assertSame(handler, 
dispatcher.getHandler(DataServiceServlet.PEOPLE_ROUTE));
+
+    mockControl.verify();
+  }
+
+  public void testGetHandler_serviceDoesntExist() {
+    mockControl.replay();
+
+    assertNull(dispatcher.getHandler("makebelieve"));
+
+    mockControl.verify();
+  }
+
+  public void testAddHandler() {
+    DataRequestHandler mockHandler = 
mockControl.createMock(DataRequestHandler.class);
+    @SuppressWarnings("unchecked")
+    Provider<DataRequestHandler> mockProvider = 
mockControl.createMock(Provider.class);
+    dispatcher.addHandler("mock", mockProvider);
+
+    EasyMock.expect(mockProvider.get()).andReturn(mockHandler);
+
+    mockControl.replay();
+
+    assertSame(mockHandler, dispatcher.getHandler("mock"));
+
+    mockControl.verify();
+  }
+}


Reply via email to