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.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   * A collection of static methods that are used in multiple places within the
37   * bindings implementation.
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       * Private constructor.
51       */
52      private CmisBindingsHelper() {
53      }
54  
55      /**
56       * Gets the SPI object for the given session. If there is already a SPI
57       * object in the session it will be returned. If there is no SPI object it
58       * will be created and put into the session.
59       * 
60       * @param session
61       *            the session object
62       * 
63       * @return the SPI object
64       */
65      public static CmisSpi getSPI(BindingSession session) {
66          assert session != null;
67  
68          // fetch from session
69          CmisSpi spi = (CmisSpi) session.get(SPI_OBJECT);
70          if (spi != null) {
71              return spi;
72          }
73  
74          session.writeLock();
75          try {
76              // try again
77              spi = (CmisSpi) session.get(SPI_OBJECT);
78              if (spi != null) {
79                  return spi;
80              }
81  
82              // ok, we have to create it...
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              // we have a SPI object -> put it into the session
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      * Gets the HTTP Invoker object from the session.
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             // try again
121             invoker = (HttpInvoker) session.get(HTTP_INVOKER_OBJECT);
122             if (invoker != null) {
123                 return invoker;
124             }
125 
126             // ok, we have to create it...
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             // we have an Invoker object -> put it into the session
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      * Returns a CMIS version if the user set one, <code>null</code> otherwise.
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      * Returns the authentication provider from the session or <code>null</code>
158      * if no authentication provider is set.
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      * Returns the repository info cache from the session.
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      * Returns the type definition cache from the session.
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             // try again
190             cache = (TypeDefinitionCache) session.get(TYPE_DEFINTION_CACHE);
191             if (cache != null) {
192                 return cache;
193             }
194 
195             // ok, we have to create it...
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             // we have a cache object -> put it into the session
207             session.put(TYPE_DEFINTION_CACHE, cache);
208         } finally {
209             session.writeUnlock();
210         }
211 
212         assert cache != null;
213 
214         return cache;
215     }
216 }