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;
20  
21  import java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.lang.reflect.Method;
26  
27  import org.apache.chemistry.opencmis.client.api.SessionFactory;
28  
29  /**
30   * Finds a {@link SessionFactory} implementation and creates a factory object.
31   * <p>
32   * Sample code:
33   * </p>
34   * 
35   * <pre>
36   * SessionFactory factory = SessionFactoryFinder.find();
37   * 
38   * Map&lt;String, String&gt; parameter = new HashMap&lt;String, String&gt;();
39   * parameter.put(SessionParameter.USER, "Otto");
40   * parameter.put(SessionParameter.PASSWORD, "****");
41   * 
42   * parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost/cmis/atom");
43   * parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
44   * parameter.put(SessionParameter.REPOSITORY_ID, "myRepository");
45   * ...
46   * Session session = factory.createSession(parameter);
47   * </pre>
48   * 
49   * @see SessionFactory
50   */
51  public final class SessionFactoryFinder {
52  
53      /**
54       * Private constructor.
55       */
56      private SessionFactoryFinder() {
57      }
58  
59      /**
60       * Creates a default {@link SessionFactory} object.
61       * 
62       * @return the newly created {@link SessionFactory} object
63       * 
64       * @throws ClassNotFoundException
65       *             if the session factory class cannot be found
66       * @throws InstantiationException
67       *             if the session factory object cannot be instantiated
68       */
69      public static SessionFactory find() throws ClassNotFoundException, InstantiationException {
70          return find("org.apache.chemistry.opencmis.client.SessionFactory", null);
71      }
72  
73      /**
74       * Creates a {@link SessionFactory} object.
75       * 
76       * @param factoryId
77       *            the factory ID of the {@link SessionFactory}
78       * 
79       * @return the newly created {@link SessionFactory} object
80       * 
81       * @throws ClassNotFoundException
82       *             if the session factory class cannot be found
83       * @throws InstantiationException
84       *             if the session factory object cannot be instantiated
85       */
86      public static SessionFactory find(String factoryId) throws ClassNotFoundException, InstantiationException {
87          return find(factoryId, null);
88      }
89  
90      /**
91       * Creates a {@link SessionFactory} object.
92       * 
93       * @param factoryId
94       *            the factory ID of the {@link SessionFactory}
95       * @param classLoader
96       *            the class loader to use
97       * 
98       * @return the newly created {@link SessionFactory} object
99       * 
100      * @throws ClassNotFoundException
101      *             if the session factory class cannot be found
102      * @throws InstantiationException
103      *             if the session factory object cannot be instantiated
104      */
105     public static SessionFactory find(String factoryId, ClassLoader classLoader) throws ClassNotFoundException,
106             InstantiationException {
107         return find(factoryId, classLoader, "org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl");
108     }
109 
110     /**
111      * Creates a {@link SessionFactory} object.
112      * 
113      * @param factoryId
114      *            the factory ID of the {@link SessionFactory}
115      * @param classLoader
116      *            the class loader to use
117      * @param fallbackClassName
118      *            the name of the class to use if no other class name has been
119      *            provided
120      * 
121      * @return the newly created {@link SessionFactory} object
122      * 
123      * @throws ClassNotFoundException
124      *             if the session factory class cannot be found
125      * @throws InstantiationException
126      *             if the session factory object cannot be instantiated
127      */
128     private static SessionFactory find(String factoryId, ClassLoader classLoader, String fallbackClassName)
129             throws ClassNotFoundException, InstantiationException {
130         ClassLoader cl = classLoader;
131         if (cl == null) {
132             cl = Thread.currentThread().getContextClassLoader();
133             if (cl == null) {
134                 cl = SessionFactoryFinder.class.getClassLoader();
135             }
136         }
137 
138         String factoryClassName = null;
139 
140         if (factoryId != null) {
141             factoryClassName = System.getProperty(factoryId);
142 
143             if (factoryClassName == null) {
144                 String serviceId = "META-INF/services/" + factoryId;
145                 InputStream stream = cl.getResourceAsStream(serviceId);
146                 if (stream != null) {
147                     BufferedReader reader = null;
148                     try {
149                         reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
150                         factoryClassName = reader.readLine();
151                     } catch (IOException e) {
152                         factoryClassName = null;
153                     } finally {
154                         try {
155                             if (reader != null) {
156                                 reader.close();
157                             } else {
158                                 stream.close();
159                             }
160                         } catch (IOException e) {
161                             // ignore
162                         }
163                     }
164                 }
165             }
166         }
167 
168         if (factoryClassName == null) {
169             factoryClassName = fallbackClassName;
170         }
171 
172         Class<?> clazz = null;
173         if (cl != null) {
174             clazz = cl.loadClass(factoryClassName);
175         } else {
176             clazz = Class.forName(factoryClassName);
177         }
178 
179         SessionFactory result = null;
180         try {
181             Method newInstanceMethod = clazz.getMethod("newInstance", new Class[0]);
182 
183             if (!SessionFactory.class.isAssignableFrom(newInstanceMethod.getReturnType())) {
184                 throw new ClassNotFoundException("newInstance() method does not return a SessionFactory object!");
185             }
186 
187             try {
188                 result = (SessionFactory) newInstanceMethod.invoke(null, new Object[0]);
189             } catch (Exception e) {
190                 throw new InstantiationException("Could not create SessionFactory object!");
191             }
192         } catch (NoSuchMethodException nsme) {
193             if (!SessionFactory.class.isAssignableFrom(clazz)) {
194                 throw new ClassNotFoundException("The class does not implemnt the SessionFactory interface!", nsme);
195             }
196 
197             try {
198                 result = (SessionFactory) clazz.newInstance();
199             } catch (Exception e) {
200                 throw new InstantiationException("Could not create SessionFactory object!");
201             }
202         }
203 
204         return result;
205     }
206 }