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