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.runtime;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.client.api.ObjectFactory;
27  import org.apache.chemistry.opencmis.client.api.Repository;
28  import org.apache.chemistry.opencmis.client.api.Session;
29  import org.apache.chemistry.opencmis.client.api.SessionFactory;
30  import org.apache.chemistry.opencmis.client.bindings.cache.TypeDefinitionCache;
31  import org.apache.chemistry.opencmis.client.runtime.cache.Cache;
32  import org.apache.chemistry.opencmis.client.runtime.repository.RepositoryImpl;
33  import org.apache.chemistry.opencmis.commons.SessionParameter;
34  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
35  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
36  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
37  import org.apache.chemistry.opencmis.commons.spi.CmisBinding;
38  
39  /**
40   * Default implementation of a session factory. Used by unit tests or
41   * applications that depend directly on runtime implementation.
42   * 
43   * <pre>
44   * {@code
45   * SessionFactory sf = new SessionFactoryImpl();<br>
46   * Session s = sf.createSession(...);
47   * }
48   * </pre>
49   * <p>
50   * Alternative factory lookup methods:
51   * 
52   * <pre>
53   * {@code
54   * Context ctx = new DefaultContext();<
55   * SessionFactory sf = ctx.lookup(jndi_key);
56   * }
57   * </pre>
58   */
59  public class SessionFactoryImpl implements SessionFactory, Serializable {
60  
61      private static final long serialVersionUID = 1L;
62  
63      protected SessionFactoryImpl() {
64      }
65  
66      public static SessionFactoryImpl newInstance() {
67          return new SessionFactoryImpl();
68      }
69  
70      @Override
71      public Session createSession(Map<String, String> parameters) {
72          return createSession(parameters, null, null, null, null);
73      }
74  
75      /**
76       * Creates a new session. The provided object factory, authentication
77       * provider and cache instance override the values in the session parameters
78       * if they are not <code>null</code>.
79       * 
80       * @param parameters
81       *            a {@code Map} of name/value pairs with parameters for the
82       *            session
83       * @param objectFactory
84       *            an object factory instance
85       * @param authenticationProvider
86       *            an authentication provider instance
87       * @param cache
88       *            a cache instance
89       * @param typeDefCache
90       *            a type definition cache instance
91       * @return a {@link Session} connected to the CMIS repository
92       * @throws CmisBaseException
93       *             if the connection could not be established
94       * 
95       * @see SessionParameter
96       */
97      public Session createSession(Map<String, String> parameters, ObjectFactory objectFactory,
98              AuthenticationProvider authenticationProvider, Cache cache, TypeDefinitionCache typeDefCache) {
99          SessionImpl session = new SessionImpl(parameters, objectFactory, authenticationProvider, cache, typeDefCache);
100         session.connect();
101 
102         return session;
103     }
104 
105     @Override
106     public List<Repository> getRepositories(Map<String, String> parameters) {
107         return getRepositories(parameters, null, null, null, null);
108     }
109 
110     /**
111      * Returns all repositories that are available at the endpoint. See
112      * {@link #createSession(Map, ObjectFactory, AuthenticationProvider, Cache, TypeDefinitionCache)}
113      * for parameter details. The parameter
114      * {@code SessionParameter#REPOSITORY_ID} should not be set.
115      */
116     public List<Repository> getRepositories(Map<String, String> parameters, ObjectFactory objectFactory,
117             AuthenticationProvider authenticationProvider, Cache cache, TypeDefinitionCache typeDefCache) {
118         CmisBinding binding = CmisBindingHelper.createBinding(parameters, authenticationProvider, typeDefCache);
119 
120         List<RepositoryInfo> repositoryInfos = binding.getRepositoryService().getRepositoryInfos(null);
121 
122         List<Repository> result = new ArrayList<Repository>();
123         for (RepositoryInfo data : repositoryInfos) {
124             result.add(new RepositoryImpl(data, parameters, this, objectFactory, binding.getAuthenticationProvider(),
125                     cache, typeDefCache));
126         }
127 
128         return result;
129     }
130 }