01: /*
02: * Copyright 2007 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.outerj.daisy.launcher.repository;
17:
18: import java.lang.reflect.Method;
19: import java.lang.reflect.InvocationTargetException;
20:
21: public class RuntimeHandle {
22: private Object runtime;
23: private Method getServiceMethod;
24: private Method shutdownMethod;
25:
26: public RuntimeHandle(Object runtime) throws NoSuchMethodException {
27: this .runtime = runtime;
28: this .getServiceMethod = runtime.getClass().getMethod(
29: "getService", Class.class);
30: this .shutdownMethod = runtime.getClass().getMethod("shutdown");
31: }
32:
33: public Object getService(Class type) {
34: try {
35: return getServiceMethod.invoke(runtime, type);
36: } catch (IllegalAccessException e) {
37: throw new RuntimeException(
38: "Error getting service from the Daisy runtime.", e);
39: } catch (InvocationTargetException e) {
40: throw new RuntimeException(
41: "Error getting service from the Daisy runtime.", e
42: .getTargetException());
43: }
44: }
45:
46: public void shutdown() {
47: try {
48: shutdownMethod.invoke(runtime);
49: } catch (InvocationTargetException e) {
50: throw new RuntimeException(
51: "Error shutting down the Daisy runtime.", e
52: .getTargetException());
53: } catch (Exception e) {
54: throw new RuntimeException(
55: "Error shutting down the Daisy runtime.", e);
56: }
57: }
58: }
|