| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StandardHandlerDispatcher |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one | |
| 3 | * or more contributor license agreements. See the NOTICE file | |
| 4 | * distributed with this work for additional information | |
| 5 | * regarding copyright ownership. The ASF licenses this file | |
| 6 | * to you under the Apache License, Version 2.0 (the | |
| 7 | * "License"); you may not use this file except in compliance | |
| 8 | * with the License. You may obtain a copy of the License at | |
| 9 | * | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | * | |
| 12 | * Unless required by applicable law or agreed to in writing, | |
| 13 | * software distributed under the License is distributed on an | |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| 15 | * KIND, either express or implied. See the License for the | |
| 16 | * specific language governing permissions and limitations | |
| 17 | * under the License. | |
| 18 | */ | |
| 19 | package org.apache.shindig.social.opensocial.service; | |
| 20 | ||
| 21 | import com.google.inject.Inject; | |
| 22 | import com.google.inject.Provider; | |
| 23 | ||
| 24 | import com.google.common.collect.ImmutableMap; | |
| 25 | import com.google.common.collect.Maps; | |
| 26 | ||
| 27 | import java.util.Map; | |
| 28 | ||
| 29 | /** | |
| 30 | * Default implementation of HandlerDispatcher. Provides default | |
| 31 | * bindings for the person, activity, and app data handlers. | |
| 32 | */ | |
| 33 | public class StandardHandlerDispatcher implements HandlerDispatcher { | |
| 34 | private final Map<String, Provider<? extends DataRequestHandler>> handlers; | |
| 35 | ||
| 36 | /** | |
| 37 | * Creates a dispatcher with the standard handlers. | |
| 38 | * @param personHandlerProvider provider for the person handler | |
| 39 | * @param activityHandlerProvider provider for the activity handler | |
| 40 | * @param appDataHandlerProvider provider for the app data handler | |
| 41 | */ | |
| 42 | @Inject | |
| 43 | public StandardHandlerDispatcher(Provider<PersonHandler> personHandlerProvider, | |
| 44 | Provider<ActivityHandler> activityHandlerProvider, | |
| 45 | Provider<AppDataHandler> appDataHandlerProvider) { | |
| 46 | 44 | this(ImmutableMap.of( |
| 47 | DataServiceServlet.PEOPLE_ROUTE, personHandlerProvider, | |
| 48 | DataServiceServlet.ACTIVITY_ROUTE, activityHandlerProvider, | |
| 49 | DataServiceServlet.APPDATA_ROUTE, appDataHandlerProvider)); | |
| 50 | 44 | } |
| 51 | ||
| 52 | /** | |
| 53 | * Creates a dispatcher with a custom list of handlers. | |
| 54 | * @param handlers a map of handlers by service name | |
| 55 | */ | |
| 56 | 44 | public StandardHandlerDispatcher(Map<String,Provider<? extends DataRequestHandler>> handlers) { |
| 57 | 44 | this.handlers = Maps.newHashMap(handlers); |
| 58 | 44 | } |
| 59 | ||
| 60 | /** | |
| 61 | * Gets a handler by service name. | |
| 62 | */ | |
| 63 | public DataRequestHandler getHandler(String service) { | |
| 64 | 65 | Provider<? extends DataRequestHandler> provider = handlers.get(service); |
| 65 | 65 | if (provider == null) { |
| 66 | 3 | return null; |
| 67 | } | |
| 68 | ||
| 69 | 62 | return provider.get(); |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Adds a custom handler. | |
| 74 | */ | |
| 75 | public void addHandler(String service, Provider<? extends DataRequestHandler> handler) { | |
| 76 | 1 | handlers.put(service, handler); |
| 77 | 1 | } |
| 78 | } |