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.bindings.impl;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23
24 import org.apache.chemistry.opencmis.client.bindings.cache.TypeDefinitionCache;
25 import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
26 import org.apache.chemistry.opencmis.client.bindings.spi.CmisSpi;
27 import org.apache.chemistry.opencmis.client.bindings.spi.http.HttpInvoker;
28 import org.apache.chemistry.opencmis.commons.SessionParameter;
29 import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
30 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
31 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
32 import org.apache.chemistry.opencmis.commons.impl.ClassLoaderUtil;
33 import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
34
35
36
37
38
39 public final class CmisBindingsHelper {
40
41 public static final String REPOSITORY_INFO_CACHE = "org.apache.chemistry.opencmis.binding.repositoryInfoCache";
42 public static final String TYPE_DEFINTION_CACHE = "org.apache.chemistry.opencmis.binding.typeDefintionCache";
43 public static final String SPI_OBJECT = "org.apache.chemistry.opencmis.binding.spi.object";
44 public static final String HTTP_INVOKER_OBJECT = "org.apache.chemistry.opencmis.binding.httpinvoker.object";
45 public static final String AUTHENTICATION_PROVIDER_OBJECT = "org.apache.chemistry.opencmis.binding.auth.object";
46 public static final String ACCEPT_LANGUAGE = "org.apache.chemistry.opencmis.binding.acceptLanguage";
47 public static final String FORCE_CMIS_VERSION = "org.apache.chemistry.opencmis.cmisversion";
48
49
50
51
52 private CmisBindingsHelper() {
53 }
54
55
56
57
58
59
60
61
62
63
64
65 public static CmisSpi getSPI(BindingSession session) {
66 assert session != null;
67
68
69 CmisSpi spi = (CmisSpi) session.get(SPI_OBJECT);
70 if (spi != null) {
71 return spi;
72 }
73
74 session.writeLock();
75 try {
76
77 spi = (CmisSpi) session.get(SPI_OBJECT);
78 if (spi != null) {
79 return spi;
80 }
81
82
83 try {
84 String spiName = (String) session.get(SessionParameter.BINDING_SPI_CLASS);
85 Constructor<?> c = ClassLoaderUtil.loadClass(spiName).getConstructor(BindingSession.class);
86 spi = (CmisSpi) c.newInstance(session);
87 } catch (CmisBaseException e) {
88 throw e;
89 } catch (InvocationTargetException ite) {
90 throw new CmisRuntimeException("SPI cannot be initialized: " + ite.getCause().getMessage(), ite);
91 } catch (Exception e) {
92 throw new CmisRuntimeException("SPI cannot be initialized: " + e.getMessage(), e);
93 }
94
95
96 session.put(SPI_OBJECT, spi, true);
97 } finally {
98 session.writeUnlock();
99 }
100
101 assert spi != null;
102
103 return spi;
104 }
105
106
107
108
109 public static HttpInvoker getHttpInvoker(BindingSession session) {
110 assert session != null;
111
112 HttpInvoker invoker = (HttpInvoker) session.get(HTTP_INVOKER_OBJECT);
113
114 if (invoker != null) {
115 return invoker;
116 }
117
118 session.writeLock();
119 try {
120
121 invoker = (HttpInvoker) session.get(HTTP_INVOKER_OBJECT);
122 if (invoker != null) {
123 return invoker;
124 }
125
126
127 try {
128 String invokerName = (String) session.get(SessionParameter.HTTP_INVOKER_CLASS);
129 invoker = (HttpInvoker) ClassLoaderUtil.loadClass(invokerName).newInstance();
130 } catch (CmisBaseException e) {
131 throw e;
132 } catch (Exception e) {
133 throw new CmisRuntimeException("HTTP invoker cannot be initialized: " + e.getMessage(), e);
134 }
135
136
137 session.put(HTTP_INVOKER_OBJECT, invoker, true);
138 } finally {
139 session.writeUnlock();
140 }
141
142 assert invoker != null;
143
144 return invoker;
145 }
146
147
148
149
150 public static CmisVersion getForcedCmisVersion(BindingSession session) {
151 assert session != null;
152
153 return (CmisVersion) session.get(FORCE_CMIS_VERSION);
154 }
155
156
157
158
159
160 public static AuthenticationProvider getAuthenticationProvider(BindingSession session) {
161 assert session != null;
162
163 return (AuthenticationProvider) session.get(AUTHENTICATION_PROVIDER_OBJECT);
164 }
165
166
167
168
169 public static RepositoryInfoCache getRepositoryInfoCache(BindingSession session) {
170 assert session != null;
171
172 return (RepositoryInfoCache) session.get(REPOSITORY_INFO_CACHE);
173 }
174
175
176
177
178 public static TypeDefinitionCache getTypeDefinitionCache(BindingSession session) {
179 assert session != null;
180
181 TypeDefinitionCache cache = (TypeDefinitionCache) session.get(TYPE_DEFINTION_CACHE);
182
183 if (cache != null) {
184 return cache;
185 }
186
187 session.writeLock();
188 try {
189
190 cache = (TypeDefinitionCache) session.get(TYPE_DEFINTION_CACHE);
191 if (cache != null) {
192 return cache;
193 }
194
195
196 try {
197 String cacheName = (String) session.get(SessionParameter.TYPE_DEFINITION_CACHE_CLASS);
198 cache = (TypeDefinitionCache) ClassLoaderUtil.loadClass(cacheName).newInstance();
199 cache.initialize(session);
200 } catch (CmisBaseException e) {
201 throw e;
202 } catch (Exception e) {
203 throw new CmisRuntimeException("Type definition cache cannot be initialized: " + e.getMessage(), e);
204 }
205
206
207 session.put(TYPE_DEFINTION_CACHE, cache);
208 } finally {
209 session.writeUnlock();
210 }
211
212 assert cache != null;
213
214 return cache;
215 }
216 }