View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.impl;
20  
21  import java.io.Serializable;
22  import java.util.Map;
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.SessionAwareAuthenticationProvider;
28  import org.apache.chemistry.opencmis.commons.SessionParameter;
29  import org.apache.chemistry.opencmis.commons.enums.BindingType;
30  import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
31  import org.apache.chemistry.opencmis.commons.impl.ClassLoaderUtil;
32  import org.apache.chemistry.opencmis.commons.impl.dataobjects.BindingsObjectFactoryImpl;
33  import org.apache.chemistry.opencmis.commons.spi.AclService;
34  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
35  import org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory;
36  import org.apache.chemistry.opencmis.commons.spi.CmisBinding;
37  import org.apache.chemistry.opencmis.commons.spi.DiscoveryService;
38  import org.apache.chemistry.opencmis.commons.spi.MultiFilingService;
39  import org.apache.chemistry.opencmis.commons.spi.NavigationService;
40  import org.apache.chemistry.opencmis.commons.spi.ObjectService;
41  import org.apache.chemistry.opencmis.commons.spi.PolicyService;
42  import org.apache.chemistry.opencmis.commons.spi.RelationshipService;
43  import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
44  import org.apache.chemistry.opencmis.commons.spi.VersioningService;
45  
46  /**
47   * CMIS binding implementation.
48   */
49  public final class CmisBindingImpl implements CmisBinding, Serializable {
50  
51      private static final long serialVersionUID = 1L;
52  
53      private BindingSession session;
54      private final BindingsObjectFactory objectFactory;
55      private final RepositoryService repositoryServiceWrapper;
56  
57      /**
58       * Constructor.
59       * 
60       * @param sessionParameters
61       *            the session parameters
62       */
63      public CmisBindingImpl(Map<String, String> sessionParameters) {
64          this(sessionParameters, null, null);
65      }
66  
67      /**
68       * Constructor.
69       * 
70       * @param sessionParameters
71       *            the session parameters
72       * @param authenticationProvider
73       *            an authentication provider instance
74       */
75      public CmisBindingImpl(final Map<String, String> sessionParameters, AuthenticationProvider authenticationProvider,
76              TypeDefinitionCache typeDefCache) {
77          // some checks first
78          if (sessionParameters == null) {
79              throw new IllegalArgumentException("Session parameters must be set!");
80          }
81          if (!sessionParameters.containsKey(SessionParameter.BINDING_SPI_CLASS)) {
82              throw new IllegalArgumentException("Session parameters do not contain a SPI class name!");
83          }
84  
85          // initialize session
86          session = new SessionImpl();
87          for (Map.Entry<String, String> entry : sessionParameters.entrySet()) {
88              session.put(entry.getKey(), entry.getValue());
89          }
90  
91          if (authenticationProvider == null) {
92              // create authentication provider and add it session
93              String authProviderClassName = sessionParameters.get(SessionParameter.AUTHENTICATION_PROVIDER_CLASS);
94              if (authProviderClassName != null) {
95                  Object authProviderObj = null;
96  
97                  try {
98                      authProviderObj = ClassLoaderUtil.loadClass(authProviderClassName).newInstance();
99                  } catch (Exception e) {
100                     throw new IllegalArgumentException("Could not load authentication provider: " + e, e);
101                 }
102 
103                 if (!(authProviderObj instanceof AuthenticationProvider)) {
104                     throw new IllegalArgumentException(
105                             "Authentication provider does not implement AuthenticationProvider!");
106                 }
107                 authenticationProvider = (AuthenticationProvider) authProviderObj;
108             }
109         }
110 
111         // locale
112         String language = sessionParameters.get(SessionParameter.LOCALE_ISO639_LANGUAGE);
113         if (language != null) {
114             language = language.trim();
115             if (language.length() > 0) {
116                 String country = sessionParameters.get(SessionParameter.LOCALE_ISO3166_COUNTRY);
117                 if (country != null) {
118                     country = country.trim();
119                     if (country.length() > 0) {
120                         country = "-" + country;
121                     }
122                 } else {
123                     country = "";
124                 }
125 
126                 String acceptLanguage = language + country;
127                 if ((acceptLanguage.indexOf('\n') == -1) && (acceptLanguage.indexOf('\r') == -1)) {
128                     session.put(CmisBindingsHelper.ACCEPT_LANGUAGE, acceptLanguage);
129                 }
130             }
131         }
132 
133         // force CMIS version
134         String forceCmisVersion = sessionParameters.get(SessionParameter.FORCE_CMIS_VERSION);
135         if (forceCmisVersion != null) {
136             try {
137                 session.put(CmisBindingsHelper.FORCE_CMIS_VERSION, CmisVersion.fromValue(forceCmisVersion));
138             } catch (IllegalArgumentException e) {
139                 throw new IllegalArgumentException("Invalid CMIS version value: " + forceCmisVersion, e);
140             }
141         }
142 
143         // add type definition cache to session
144         if (typeDefCache != null) {
145             session.put(CmisBindingsHelper.TYPE_DEFINTION_CACHE, typeDefCache);
146             typeDefCache.initialize(session);
147         }
148 
149         // set up caches
150         clearAllCaches();
151 
152         // initialize the SPI
153         CmisBindingsHelper.getSPI(session);
154 
155         // set up object factory
156         objectFactory = new BindingsObjectFactoryImpl();
157 
158         // set up repository service
159         repositoryServiceWrapper = new RepositoryServiceImpl(session);
160 
161         // add authentication provider to session
162         if (authenticationProvider != null) {
163             session.put(CmisBindingsHelper.AUTHENTICATION_PROVIDER_OBJECT, authenticationProvider);
164             if (authenticationProvider instanceof SessionAwareAuthenticationProvider) {
165                 ((SessionAwareAuthenticationProvider) authenticationProvider).setSession(session);
166             }
167         }
168     }
169 
170     @Override
171     public String getSessionId() {
172         return session.getSessionId();
173     }
174 
175     @Override
176     public BindingType getBindingType() {
177         Object bindingType = session.get(SessionParameter.BINDING_TYPE);
178         if (!(bindingType instanceof String)) {
179             return BindingType.CUSTOM;
180         }
181 
182         try {
183             return BindingType.fromValue((String) bindingType);
184         } catch (IllegalArgumentException e) {
185             return BindingType.CUSTOM;
186         }
187     }
188 
189     @Override
190     public RepositoryService getRepositoryService() {
191         checkSession();
192         return repositoryServiceWrapper;
193     }
194 
195     @Override
196     public NavigationService getNavigationService() {
197         checkSession();
198         CmisSpi spi = CmisBindingsHelper.getSPI(session);
199         return spi.getNavigationService();
200     }
201 
202     @Override
203     public ObjectService getObjectService() {
204         checkSession();
205         CmisSpi spi = CmisBindingsHelper.getSPI(session);
206         return spi.getObjectService();
207     }
208 
209     @Override
210     public DiscoveryService getDiscoveryService() {
211         checkSession();
212         CmisSpi spi = CmisBindingsHelper.getSPI(session);
213         return spi.getDiscoveryService();
214     }
215 
216     @Override
217     public RelationshipService getRelationshipService() {
218         checkSession();
219         CmisSpi spi = CmisBindingsHelper.getSPI(session);
220         return spi.getRelationshipService();
221     }
222 
223     @Override
224     public VersioningService getVersioningService() {
225         checkSession();
226         CmisSpi spi = CmisBindingsHelper.getSPI(session);
227         return spi.getVersioningService();
228     }
229 
230     @Override
231     public AclService getAclService() {
232         checkSession();
233         CmisSpi spi = CmisBindingsHelper.getSPI(session);
234         return spi.getAclService();
235     }
236 
237     @Override
238     public MultiFilingService getMultiFilingService() {
239         checkSession();
240         CmisSpi spi = CmisBindingsHelper.getSPI(session);
241         return spi.getMultiFilingService();
242     }
243 
244     @Override
245     public PolicyService getPolicyService() {
246         checkSession();
247         CmisSpi spi = CmisBindingsHelper.getSPI(session);
248         return spi.getPolicyService();
249     }
250 
251     @Override
252     public BindingsObjectFactory getObjectFactory() {
253         return objectFactory;
254     }
255 
256     @Override
257     public AuthenticationProvider getAuthenticationProvider() {
258         return CmisBindingsHelper.getAuthenticationProvider(session);
259     }
260 
261     @Override
262     public void clearAllCaches() {
263         checkSession();
264 
265         session.writeLock();
266         try {
267             session.put(CmisBindingsHelper.REPOSITORY_INFO_CACHE, new RepositoryInfoCache(session));
268             TypeDefinitionCache typeDefCache = CmisBindingsHelper.getTypeDefinitionCache(session);
269             typeDefCache.removeAll();
270 
271             CmisSpi spi = CmisBindingsHelper.getSPI(session);
272             spi.clearAllCaches();
273         } finally {
274             session.writeUnlock();
275         }
276     }
277 
278     @Override
279     public void clearRepositoryCache(String repositoryId) {
280         checkSession();
281 
282         if (repositoryId == null) {
283             return;
284         }
285 
286         session.writeLock();
287         try {
288             RepositoryInfoCache repInfoCache = (RepositoryInfoCache) session
289                     .get(CmisBindingsHelper.REPOSITORY_INFO_CACHE);
290             repInfoCache.remove(repositoryId);
291 
292             TypeDefinitionCache typeDefCache = CmisBindingsHelper.getTypeDefinitionCache(session);
293             typeDefCache.remove(repositoryId);
294 
295             CmisSpi spi = CmisBindingsHelper.getSPI(session);
296             spi.clearRepositoryCache(repositoryId);
297         } finally {
298             session.writeUnlock();
299         }
300     }
301 
302     @Override
303     public void close() {
304         checkSession();
305 
306         session.writeLock();
307         try {
308             CmisSpi spi = CmisBindingsHelper.getSPI(session);
309             spi.close();
310         } finally {
311             session.writeUnlock();
312             session = null;
313         }
314 
315     }
316 
317     private void checkSession() {
318         if (session == null) {
319             throw new IllegalStateException("Already closed.");
320         }
321     }
322 }