This project has retired. For details please refer to its Attic page.
CmisBindingsHelper 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.lang.reflect.Constructor;
22  
23  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
24  import org.apache.chemistry.opencmis.client.bindings.spi.CmisSpi;
25  import org.apache.chemistry.opencmis.commons.SessionParameter;
26  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
27  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
28  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
29  
30  /**
31   * A collection of static methods that are used in multiple places within the
32   * bindings implementation.
33   */
34  public final class CmisBindingsHelper {
35  
36      public static final String REPOSITORY_INFO_CACHE = "org.apache.chemistry.opencmis.binding.repositoryInfoCache";
37      public static final String TYPE_DEFINTION_CACHE = "org.apache.chemistry.opencmis.binding.typeDefintionCache";
38      public static final String SPI_OBJECT = "org.apache.chemistry.opencmis.binding.spi.object";
39      public static final String AUTHENTICATION_PROVIDER_OBJECT = "org.apache.chemistry.opencmis.binding.auth.object";
40      public static final String ACCEPT_LANGUAGE = "org.apache.chemistry.opencmis.binding.acceptLanguage";
41  
42      /**
43       * Private constructor.
44       */
45      private CmisBindingsHelper() {
46      }
47  
48      /**
49       * Gets the SPI object for the given session. If there is already a SPI
50       * object in the session it will be returned. If there is no SPI object it
51       * will be created and put into the session.
52       * 
53       * @param session
54       *            the session object
55       * 
56       * @return the SPI object
57       */
58      public static CmisSpi getSPI(BindingSession session) {
59          // fetch from session
60          CmisSpi spi = (CmisSpi) session.get(SPI_OBJECT);
61          if (spi != null) {
62              return spi;
63          }
64  
65          session.writeLock();
66          try {
67              // try again
68              spi = (CmisSpi) session.get(SPI_OBJECT);
69              if (spi != null) {
70                  return spi;
71              }
72  
73              // ok, we have to create it...
74              try {
75                  String spiName = (String) session.get(SessionParameter.BINDING_SPI_CLASS);
76                  Constructor<?> c = Class.forName(spiName).getConstructor(BindingSession.class);
77                  spi = (CmisSpi) c.newInstance(session);
78              } catch (CmisBaseException e) {
79                  throw e;
80              } catch (Exception e) {
81                  throw new CmisRuntimeException("SPI cannot be initialized: " + e.getMessage(), e);
82              }
83  
84              // we have a SPI object -> put it into the session
85              session.put(SPI_OBJECT, spi, true);
86          } finally {
87              session.writeUnlock();
88          }
89  
90          return spi;
91      }
92  
93      /**
94       * Returns the authentication provider from the session or <code>null</code>
95       * if no authentication provider is set.
96       */
97      public static AuthenticationProvider getAuthenticationProvider(BindingSession session) {
98          return (AuthenticationProvider) session.get(AUTHENTICATION_PROVIDER_OBJECT);
99      }
100 
101     /**
102      * Returns the repository info cache from the session.
103      */
104     public static RepositoryInfoCache getRepositoryInfoCache(BindingSession session) {
105         return (RepositoryInfoCache) session.get(REPOSITORY_INFO_CACHE);
106     }
107 
108     /**
109      * Returns the type definition cache from the session.
110      */
111     public static TypeDefinitionCache getTypeDefinitionCache(BindingSession session) {
112         return (TypeDefinitionCache) session.get(TYPE_DEFINTION_CACHE);
113     }
114 }