1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.InvocationTargetException;
15 import java.net.URL;
16
17 import org.ietf.jgss.GSSManager;
18
19
20
21
22
23
24 public abstract class GSSManagerFactory {
25
26
27
28
29
30 public static GSSManagerFactory detect() {
31 return (SunGSSManagerFactory.isSupported()) ? new SunGSSManagerFactory()
32 : new DefaultGSSManagerFactory();
33 }
34
35
36
37
38
39
40
41
42 public abstract GSSManager newInstance(URL url);
43
44
45
46
47
48
49
50 private static class DefaultGSSManagerFactory extends GSSManagerFactory {
51 private static final GSSManager INSTANCE = GSSManager.getInstance();
52
53 @Override
54 public GSSManager newInstance(URL url) {
55 return INSTANCE;
56 }
57 }
58
59 private static class SunGSSManagerFactory extends GSSManagerFactory {
60 private static boolean IS_SUPPORTED;
61 private static Constructor<?> HTTP_CALLER_INFO_CONSTRUCTOR;
62 private static Constructor<?> HTTP_CALLER_CONSTRUCTOR;
63
64 private static Constructor<?> GSS_MANAGER_IMPL_CONSTRUCTOR;
65
66 static {
67 try {
68 init();
69 IS_SUPPORTED = true;
70 } catch (Exception e) {
71 IS_SUPPORTED = false;
72 }
73 }
74
75 private static void init() throws ClassNotFoundException,
76 NoSuchMethodException {
77 Class<?> httpCallerInfoClazz = Class
78 .forName("sun.net.www.protocol.http.HttpCallerInfo");
79 HTTP_CALLER_INFO_CONSTRUCTOR = httpCallerInfoClazz
80 .getConstructor(URL.class);
81
82 Class<?> httpCallerClazz = Class
83 .forName("sun.security.jgss.HttpCaller");
84 HTTP_CALLER_CONSTRUCTOR = httpCallerClazz
85 .getConstructor(httpCallerInfoClazz);
86
87 Class<?> gssCallerClazz = Class
88 .forName("sun.security.jgss.GSSCaller");
89 Class<?> gssManagerImplClazz = Class
90 .forName("sun.security.jgss.GSSManagerImpl");
91 GSS_MANAGER_IMPL_CONSTRUCTOR = gssManagerImplClazz
92 .getConstructor(gssCallerClazz);
93
94 }
95
96
97
98
99
100
101 public static boolean isSupported() {
102 return IS_SUPPORTED;
103 }
104
105 @Override
106 public GSSManager newInstance(URL url) {
107 try {
108 Object httpCallerInfo = HTTP_CALLER_INFO_CONSTRUCTOR
109 .newInstance(url);
110 Object httpCaller = HTTP_CALLER_CONSTRUCTOR
111 .newInstance(httpCallerInfo);
112
113 return (GSSManager) GSS_MANAGER_IMPL_CONSTRUCTOR
114 .newInstance(httpCaller);
115 } catch (InstantiationException | IllegalAccessException
116 | IllegalArgumentException | InvocationTargetException e) {
117 throw new Error(e);
118 }
119 }
120 }
121 }