This project has retired. For details please refer to its Attic page.
CmisBindingImpl xref

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.spi.BindingSession;
25  import org.apache.chemistry.opencmis.client.bindings.spi.CmisSpi;
26  import org.apache.chemistry.opencmis.client.bindings.spi.SessionAwareAuthenticationProvider;
27  import org.apache.chemistry.opencmis.commons.SessionParameter;
28  import org.apache.chemistry.opencmis.commons.impl.dataobjects.BindingsObjectFactoryImpl;
29  import org.apache.chemistry.opencmis.commons.spi.AclService;
30  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
31  import org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory;
32  import org.apache.chemistry.opencmis.commons.spi.CmisBinding;
33  import org.apache.chemistry.opencmis.commons.spi.DiscoveryService;
34  import org.apache.chemistry.opencmis.commons.spi.MultiFilingService;
35  import org.apache.chemistry.opencmis.commons.spi.NavigationService;
36  import org.apache.chemistry.opencmis.commons.spi.ObjectService;
37  import org.apache.chemistry.opencmis.commons.spi.PolicyService;
38  import org.apache.chemistry.opencmis.commons.spi.RelationshipService;
39  import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
40  import org.apache.chemistry.opencmis.commons.spi.VersioningService;
41  
42  /**
43   * CMIS binding implementation.
44   */
45  public class CmisBindingImpl implements CmisBinding, Serializable {
46  
47      private static final long serialVersionUID = 1L;
48  
49      private BindingSession session;
50      private final BindingsObjectFactory objectFactory;
51      private final RepositoryService repositoryServiceWrapper;
52  
53      /**
54       * Constructor.
55       * 
56       * @param sessionParameters
57       *            the session parameters
58       */
59      public CmisBindingImpl(Map<String, String> sessionParameters) {
60          this(sessionParameters, null);
61      }
62  
63      /**
64       * Constructor.
65       * 
66       * @param sessionParameters
67       *            the session parameters
68       * @param authenticationProvider
69       *            an authentication provider instance
70       */
71      public CmisBindingImpl(Map<String, String> sessionParameters, AuthenticationProvider authenticationProvider) {
72          // some checks first
73          if (sessionParameters == null) {
74              throw new IllegalArgumentException("Session parameters must be set!");
75          }
76          if (!sessionParameters.containsKey(SessionParameter.BINDING_SPI_CLASS)) {
77              throw new IllegalArgumentException("Session parameters do not contain a SPI class name!");
78          }
79  
80          // initialize session
81          session = new SessionImpl();
82          for (Map.Entry<String, String> entry : sessionParameters.entrySet()) {
83              session.put(entry.getKey(), entry.getValue());
84          }
85  
86          if (authenticationProvider == null) {
87              // create authentication provider and add it session
88              String authProviderClassName = sessionParameters.get(SessionParameter.AUTHENTICATION_PROVIDER_CLASS);
89              if (authProviderClassName != null) {
90                  Object authProviderObj = null;
91  
92                  try {
93                      authProviderObj = Class.forName(authProviderClassName).newInstance();
94                  } catch (Exception e) {
95                      throw new IllegalArgumentException("Could not load authentication provider: " + e, e);
96                  }
97  
98                  if (!(authProviderObj instanceof AuthenticationProvider)) {
99                      throw new IllegalArgumentException(
100                             "Authentication provider does not implement AuthenticationProvider!");
101                 }
102                 authenticationProvider = (AuthenticationProvider) authProviderObj;
103             }
104         }
105 
106         // locale
107         String language = sessionParameters.get(SessionParameter.LOCALE_ISO639_LANGUAGE);
108         if (language != null) {
109             language = language.trim();
110             if (language.length() > 0) {
111                 String country = sessionParameters.get(SessionParameter.LOCALE_ISO3166_COUNTRY);
112                 if (country != null) {
113                     country = country.trim();
114                     if (country.length() > 0) {
115                         country = "-" + country;
116                     }
117                 } else {
118                     country = "";
119                 }
120 
121                 String acceptLanguage = language + country;
122                 if ((acceptLanguage.indexOf('\n') == -1) && (acceptLanguage.indexOf('\r') == -1)) {
123                     session.put(CmisBindingsHelper.ACCEPT_LANGUAGE, acceptLanguage);
124                 }
125             }
126         }
127 
128         // set up caches
129         clearAllCaches();
130 
131         // initialize the SPI
132         CmisBindingsHelper.getSPI(session);
133 
134         // set up object factory
135         objectFactory = new BindingsObjectFactoryImpl();
136 
137         // set up repository service
138         repositoryServiceWrapper = new RepositoryServiceImpl(session);
139 
140         // add authentication provider to session
141         if (authenticationProvider != null) {
142             session.put(CmisBindingsHelper.AUTHENTICATION_PROVIDER_OBJECT, authenticationProvider);
143             if (authenticationProvider instanceof SessionAwareAuthenticationProvider) {
144                 ((SessionAwareAuthenticationProvider) authenticationProvider).setSession(session);
145             }
146         }
147     }
148 
149     public RepositoryService getRepositoryService() {
150         checkSession();
151         return repositoryServiceWrapper;
152     }
153 
154     public NavigationService getNavigationService() {
155         checkSession();
156         CmisSpi spi = CmisBindingsHelper.getSPI(session);
157         return spi.getNavigationService();
158     }
159 
160     public ObjectService getObjectService() {
161         checkSession();
162         CmisSpi spi = CmisBindingsHelper.getSPI(session);
163         return spi.getObjectService();
164     }
165 
166     public DiscoveryService getDiscoveryService() {
167         checkSession();
168         CmisSpi spi = CmisBindingsHelper.getSPI(session);
169         return spi.getDiscoveryService();
170     }
171 
172     public RelationshipService getRelationshipService() {
173         checkSession();
174         CmisSpi spi = CmisBindingsHelper.getSPI(session);
175         return spi.getRelationshipService();
176     }
177 
178     public VersioningService getVersioningService() {
179         checkSession();
180         CmisSpi spi = CmisBindingsHelper.getSPI(session);
181         return spi.getVersioningService();
182     }
183 
184     public AclService getAclService() {
185         checkSession();
186         CmisSpi spi = CmisBindingsHelper.getSPI(session);
187         return spi.getAclService();
188     }
189 
190     public MultiFilingService getMultiFilingService() {
191         checkSession();
192         CmisSpi spi = CmisBindingsHelper.getSPI(session);
193         return spi.getMultiFilingService();
194     }
195 
196     public PolicyService getPolicyService() {
197         checkSession();
198         CmisSpi spi = CmisBindingsHelper.getSPI(session);
199         return spi.getPolicyService();
200     }
201 
202     public BindingsObjectFactory getObjectFactory() {
203         return objectFactory;
204     }
205 
206     public AuthenticationProvider getAuthenticationProvider() {
207         return CmisBindingsHelper.getAuthenticationProvider(session);
208     }
209 
210     public void clearAllCaches() {
211         checkSession();
212 
213         session.writeLock();
214         try {
215             session.put(CmisBindingsHelper.REPOSITORY_INFO_CACHE, new RepositoryInfoCache(session));
216             session.put(CmisBindingsHelper.TYPE_DEFINTION_CACHE, new TypeDefinitionCache(session));
217 
218             CmisSpi spi = CmisBindingsHelper.getSPI(session);
219             spi.clearAllCaches();
220         } finally {
221             session.writeUnlock();
222         }
223     }
224 
225     public void clearRepositoryCache(String repositoryId) {
226         checkSession();
227 
228         if (repositoryId == null) {
229             return;
230         }
231 
232         session.writeLock();
233         try {
234             RepositoryInfoCache repInfoCache = (RepositoryInfoCache) session
235                     .get(CmisBindingsHelper.REPOSITORY_INFO_CACHE);
236             repInfoCache.remove(repositoryId);
237 
238             TypeDefinitionCache typeDefCache = (TypeDefinitionCache) session
239                     .get(CmisBindingsHelper.TYPE_DEFINTION_CACHE);
240             typeDefCache.remove(repositoryId);
241 
242             CmisSpi spi = CmisBindingsHelper.getSPI(session);
243             spi.clearRepositoryCache(repositoryId);
244         } finally {
245             session.writeUnlock();
246         }
247     }
248 
249     public void close() {
250         checkSession();
251 
252         session.writeLock();
253         try {
254             CmisSpi spi = CmisBindingsHelper.getSPI(session);
255             spi.close();
256         } finally {
257             session.writeUnlock();
258             session = null;
259         }
260 
261     }
262 
263     private void checkSession() {
264         if (session == null) {
265             throw new IllegalStateException("Already closed.");
266         }
267     }
268 }