1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.lang.reflect.Method;
26
27 import org.apache.chemistry.opencmis.client.api.SessionFactory;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public final class SessionFactoryFinder {
52
53
54
55
56 private SessionFactoryFinder() {
57 }
58
59
60
61
62
63
64
65
66
67
68
69 public static SessionFactory find() throws ClassNotFoundException, InstantiationException {
70 return find("org.apache.chemistry.opencmis.client.SessionFactory", null);
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public static SessionFactory find(String factoryId) throws ClassNotFoundException, InstantiationException {
87 return find(factoryId, null);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public static SessionFactory find(String factoryId, ClassLoader classLoader) throws ClassNotFoundException,
106 InstantiationException {
107 return find(factoryId, classLoader, "org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl");
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 private static SessionFactory find(String factoryId, ClassLoader classLoader, String fallbackClassName)
129 throws ClassNotFoundException, InstantiationException {
130 ClassLoader cl = classLoader;
131 if (cl == null) {
132 cl = Thread.currentThread().getContextClassLoader();
133 if (cl == null) {
134 cl = SessionFactoryFinder.class.getClassLoader();
135 }
136 }
137
138 String factoryClassName = null;
139
140 if (factoryId != null) {
141 factoryClassName = System.getProperty(factoryId);
142
143 if (factoryClassName == null) {
144 String serviceId = "META-INF/services/" + factoryId;
145 InputStream stream = cl.getResourceAsStream(serviceId);
146 if (stream != null) {
147 BufferedReader reader = null;
148 try {
149 reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
150 factoryClassName = reader.readLine();
151 } catch (IOException e) {
152 factoryClassName = null;
153 } finally {
154 try {
155 if (reader != null) {
156 reader.close();
157 } else {
158 stream.close();
159 }
160 } catch (IOException e) {
161
162 }
163 }
164 }
165 }
166 }
167
168 if (factoryClassName == null) {
169 factoryClassName = fallbackClassName;
170 }
171
172 Class<?> clazz = null;
173 if (cl != null) {
174 clazz = cl.loadClass(factoryClassName);
175 } else {
176 clazz = Class.forName(factoryClassName);
177 }
178
179 SessionFactory result = null;
180 try {
181 Method newInstanceMethod = clazz.getMethod("newInstance", new Class[0]);
182
183 if (!SessionFactory.class.isAssignableFrom(newInstanceMethod.getReturnType())) {
184 throw new ClassNotFoundException("newInstance() method does not return a SessionFactory object!");
185 }
186
187 try {
188 result = (SessionFactory) newInstanceMethod.invoke(null, new Object[0]);
189 } catch (Exception e) {
190 throw new InstantiationException("Could not create SessionFactory object!");
191 }
192 } catch (NoSuchMethodException nsme) {
193 if (!SessionFactory.class.isAssignableFrom(clazz)) {
194 throw new ClassNotFoundException("The class does not implemnt the SessionFactory interface!", nsme);
195 }
196
197 try {
198 result = (SessionFactory) clazz.newInstance();
199 } catch (Exception e) {
200 throw new InstantiationException("Could not create SessionFactory object!");
201 }
202 }
203
204 return result;
205 }
206 }