1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
* Copyright (C) 2016 Google Inc.
*
* Licensed 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 com.google.android.mobly.snippet.rpc;
import android.content.Context;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import com.google.android.mobly.snippet.util.Log;
public abstract class SnippetManager {
private final Map<Class<? extends Snippet>, Snippet> mReceivers;
/**
* A map of strings to known RPCs.
*/
private final Map<String, MethodDescriptor> mKnownRpcs = new HashMap<String, MethodDescriptor>();
public SnippetManager(Collection<Class<? extends Snippet>> classList) {
mReceivers = new HashMap<>();
for (Class<? extends Snippet> receiverClass : classList) {
mReceivers.put(receiverClass, null);
Collection<MethodDescriptor> methodList = MethodDescriptor.collectFrom(receiverClass);
for (MethodDescriptor m : methodList) {
if (mKnownRpcs.containsKey(m.getName())) {
// We already know an RPC of the same name. We don't catch this anywhere because
// this is a programming error.
throw new RuntimeException("An RPC with the name " + m.getName()
+ " is already known.");
}
mKnownRpcs.put(m.getName(), m);
}
}
}
private Snippet get(Class<? extends Snippet> clazz) {
Snippet object = mReceivers.get(clazz);
if (object != null) {
return object;
}
Constructor<? extends Snippet> constructor;
try {
constructor = clazz.getConstructor();
object = constructor.newInstance();
mReceivers.put(clazz, object);
} catch (Exception e) {
Log.e(e);
}
return object;
}
public MethodDescriptor getMethodDescriptor(String methodName) {
return mKnownRpcs.get(methodName);
}
public SortedSet<String> getMethodNames() {
return new TreeSet<>(mKnownRpcs.keySet());
}
public Object invoke(Class<? extends Snippet> clazz, Method method, Object[] args)
throws Exception {
Snippet object = get(clazz);
return method.invoke(object, args);
}
public void shutdown() {
for (Snippet receiver : mReceivers.values()) {
try {
if (receiver != null) {
receiver.shutdown();
}
} catch (Exception e) {
Log.e("Failed to shut down an Snippet", e);
}
}
}
}
|