1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.util.ArrayMap; 23 24 import java.util.Map; 25 import java.util.Objects; 26 27 /** 28 * The registry for in-process module interfaces. 29 * <p> 30 * In-process module interfaces should be named with the suffix {@code ManagerLocal} for 31 * consistency. 32 * 33 * @hide 34 */ 35 @SystemApi(client = SystemApi.Client.SYSTEM_SERVER) 36 public final class LocalManagerRegistry { LocalManagerRegistry()37 private LocalManagerRegistry() {} 38 39 @NonNull 40 private static final Map<Class<?>, Object> sManagers = new ArrayMap<>(); 41 42 /** 43 * Get a manager from the registry. 44 * 45 * @param managerClass the class that the manager implements 46 * @return the manager, or {@code null} if not found 47 */ 48 @Nullable 49 @SuppressWarnings("unchecked") getManager(@onNull Class<T> managerClass)50 public static <T> T getManager(@NonNull Class<T> managerClass) { 51 synchronized (sManagers) { 52 return (T) sManagers.get(managerClass); 53 } 54 } 55 56 /** 57 * Returns a manager from the registry, or throws {@link ManagerNotFoundException} if not found. 58 * 59 * @hide 60 */ 61 @NonNull getManagerOrThrow(@onNull Class<T> managerClass)62 public static <T> T getManagerOrThrow(@NonNull Class<T> managerClass) 63 throws ManagerNotFoundException { 64 T manager = getManager(managerClass); 65 if (manager == null) { 66 throw new ManagerNotFoundException(managerClass); 67 } 68 return manager; 69 } 70 71 /** 72 * Adds a manager to the registry. 73 * 74 * @param managerClass the class that the manager implements 75 * @param manager the manager 76 * @throws IllegalStateException if the manager class is already registered 77 */ addManager(@onNull Class<T> managerClass, @NonNull T manager)78 public static <T> void addManager(@NonNull Class<T> managerClass, @NonNull T manager) { 79 Objects.requireNonNull(managerClass, "managerClass"); 80 Objects.requireNonNull(manager, "manager"); 81 synchronized (sManagers) { 82 if (sManagers.containsKey(managerClass)) { 83 throw new IllegalStateException(managerClass.getName() + " is already registered"); 84 } 85 sManagers.put(managerClass, manager); 86 } 87 } 88 89 /** 90 * Exception thrown when no local manager published for given class. 91 * 92 * @hide 93 */ 94 public static class ManagerNotFoundException extends Exception { ManagerNotFoundException(@onNull Class<T> managerClass)95 public <T> ManagerNotFoundException(@NonNull Class<T> managerClass) { 96 super("Local manager " + managerClass.getName() + " does not exist or is not ready"); 97 } 98 } 99 } 100