This project has retired. For details please refer to its Attic page.
JcrServiceFactory 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   */
20  package org.apache.chemistry.opencmis.jcr;
21  
22  import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
23  import org.apache.chemistry.opencmis.commons.impl.server.AbstractServiceFactory;
24  import org.apache.chemistry.opencmis.commons.server.CallContext;
25  import org.apache.chemistry.opencmis.commons.server.CmisService;
26  import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
27  import org.apache.chemistry.opencmis.jcr.impl.DefaultDocumentTypeHandler;
28  import org.apache.chemistry.opencmis.jcr.impl.DefaultFolderTypeHandler;
29  import org.apache.chemistry.opencmis.jcr.impl.DefaultUnversionedDocumentTypeHandler;
30  import org.apache.chemistry.opencmis.jcr.type.JcrTypeHandlerManager;
31  import org.apache.chemistry.opencmis.server.support.CmisServiceWrapper;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import javax.imageio.spi.ServiceRegistry;
36  import javax.jcr.Repository;
37  import javax.jcr.RepositoryException;
38  import javax.jcr.RepositoryFactory;
39  import java.math.BigInteger;
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.HashMap;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.Map;
46  
47  /**
48   * A {@link CmisServiceFactory} implementation which returns {@link JcrService} instances.  
49   */
50  public class JcrServiceFactory extends AbstractServiceFactory {
51      private static final Log log = LogFactory.getLog(JcrServiceFactory.class);
52  
53      public static final String MOUNT_PATH_CONFIG = "mount-path";
54      public static final String PREFIX_JCR_CONFIG = "jcr.";
55  
56      public static final BigInteger DEFAULT_MAX_ITEMS_TYPES = BigInteger.valueOf(50);
57      public static final BigInteger DEFAULT_DEPTH_TYPES = BigInteger.valueOf(-1);
58      public static final BigInteger DEFAULT_MAX_ITEMS_OBJECTS = BigInteger.valueOf(200);
59      public static final BigInteger DEFAULT_DEPTH_OBJECTS = BigInteger.valueOf(10);
60  
61      private JcrTypeManager typeManager;
62      private Map<String, String> jcrConfig;
63      private String mountPath;
64      private JcrRepository jcrRepository;
65  
66      @Override
67      public void init(Map<String, String> parameters) {
68          typeManager = createTypeManager();
69          readConfiguration(parameters);
70          PathManager pathManger = new PathManager(mountPath);
71          JcrTypeHandlerManager typeHandlerManager = createTypeHandlerManager(pathManger, typeManager);
72          jcrRepository = new JcrRepository(acquireJcrRepository(jcrConfig), pathManger, typeManager, typeHandlerManager);
73      }
74  
75      @Override
76      public void destroy() {
77          jcrRepository = null;
78          typeManager = null;
79      }
80  
81      @Override
82      public CmisService getService(CallContext context) {
83          CmisServiceWrapper<JcrService> serviceWrapper = new CmisServiceWrapper<JcrService>(
84                  createJcrService(jcrRepository, context), DEFAULT_MAX_ITEMS_TYPES, DEFAULT_DEPTH_TYPES,
85                  DEFAULT_MAX_ITEMS_OBJECTS, DEFAULT_DEPTH_OBJECTS);
86  
87          serviceWrapper.getWrappedService().setCallContext(context);
88          return serviceWrapper;
89      }
90  
91      //------------------------------------------< factories >---
92  
93      /**
94       * Acquire the JCR repository given a configuration. This implementation used
95       * {@link javax.imageio.spi.ServiceRegistry#lookupProviders(Class)} for
96       * locating <code>RepositoryFactory</code> instances. The first instance
97       * which can handle the <code>jcrConfig</code> parameters is used to
98       * acquire the repository. 
99       *
100      * @param jcrConfig  configuration determining the JCR repository to be returned
101      * @return
102      * @throws RepositoryException
103      */
104     protected Repository acquireJcrRepository(Map<String, String> jcrConfig) {
105         try {
106             Iterator<RepositoryFactory> factories = ServiceRegistry.lookupProviders(RepositoryFactory.class);
107             while (factories.hasNext()) {
108                 RepositoryFactory factory = factories.next();
109                 log.debug("Trying to acquire JCR repository from factory " + factory);
110                 Repository repository = factory.getRepository(jcrConfig);
111                 if (repository != null) {
112                     log.debug("Successfully acquired JCR repository from factory " + factory);
113                     return repository;
114                 }
115                 else {
116                     log.debug("Could not acquire JCR repository from factory " + factory);
117                 }
118             }
119             throw new CmisConnectionException("No JCR repository factory for configured parameters");
120         }
121         catch (RepositoryException e) {
122             log.debug(e.getMessage(), e);
123             throw new CmisConnectionException(e.getMessage(), e);
124         }
125     }
126 
127     /**
128      * Create a <code>JcrService</code> from a <code>JcrRepository</code>JcrRepository> and
129      * <code>CallContext</code>.
130      * 
131      * @param jcrRepository
132      * @param context
133      * @return
134      */
135     protected JcrService createJcrService(JcrRepository jcrRepository, CallContext context) {
136         return new JcrService(jcrRepository);
137     }
138 
139     protected JcrTypeManager createTypeManager() {
140         return new JcrTypeManager();
141     }
142 
143     protected JcrTypeHandlerManager createTypeHandlerManager(PathManager pathManager, JcrTypeManager typeManager) {
144         JcrTypeHandlerManager typeHandlerManager = new JcrTypeHandlerManager(pathManager, typeManager);
145         typeHandlerManager.addHandler(new DefaultFolderTypeHandler());
146         typeHandlerManager.addHandler(new DefaultDocumentTypeHandler());
147         typeHandlerManager.addHandler(new DefaultUnversionedDocumentTypeHandler());
148         return typeHandlerManager;
149     }
150 
151     //------------------------------------------< private >---
152 
153     private void readConfiguration(Map<String, String> parameters) {
154         Map<String, String> map = new HashMap<String, String>();
155         List<String> keys = new ArrayList<String>(parameters.keySet());
156         Collections.sort(keys);
157 
158         for (String key : keys) {
159             if (key.startsWith(PREFIX_JCR_CONFIG)) {
160                 String jcrKey = key.substring(PREFIX_JCR_CONFIG.length());
161                 String jcrValue = replaceSystemProperties(parameters.get(key));
162                 map.put(jcrKey, jcrValue);
163             }
164 
165             else if (MOUNT_PATH_CONFIG.equals(key)) {
166                 mountPath = parameters.get(key);
167                 log.debug("Configuration: " + MOUNT_PATH_CONFIG + '=' + mountPath);
168             }
169 
170             else {
171                 log.warn("Configuration: unrecognized key: " + key);
172             }
173         }
174 
175         jcrConfig = Collections.unmodifiableMap(map);
176         log.debug("Configuration: jcr=" + jcrConfig);
177     }
178 
179     private static String replaceSystemProperties(String s) {
180         if (s == null) {
181             return null;
182         }
183 
184         StringBuilder result = new StringBuilder();
185         StringBuilder property = null;
186         boolean inProperty = false;
187 
188         for (int i = 0; i < s.length(); i++) {
189             char c = s.charAt(i);
190 
191             if (inProperty) {
192                 if (c == '}') {
193                     String value = System.getProperty(property.toString());
194                     if (value != null) {
195                         result.append(value);
196                     }
197                     inProperty = false;
198                 } else {
199                     property.append(c);
200                 }
201             } else {
202                 if (c == '{') {
203                     property = new StringBuilder();
204                     inProperty = true;
205                 } else {
206                     result.append(c);
207                 }
208             }
209         }
210 
211         return result.toString();
212     }
213 
214 }