This project has retired. For details please refer to its Attic page.
FileShareServiceFactory xref

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   *
20   */
21  package org.apache.chemistry.opencmis.fileshare;
22  
23  import java.io.File;
24  import java.math.BigInteger;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.xml.bind.JAXBElement;
31  import javax.xml.bind.Unmarshaller;
32  
33  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
34  import org.apache.chemistry.opencmis.commons.impl.Converter;
35  import org.apache.chemistry.opencmis.commons.impl.JaxBHelper;
36  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionType;
37  import org.apache.chemistry.opencmis.commons.impl.server.AbstractServiceFactory;
38  import org.apache.chemistry.opencmis.commons.server.CallContext;
39  import org.apache.chemistry.opencmis.commons.server.CmisService;
40  import org.apache.chemistry.opencmis.server.support.CmisServiceWrapper;
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  public class FileShareServiceFactory extends AbstractServiceFactory {
45  
46      private static final String PREFIX_LOGIN = "login.";
47      private static final String PREFIX_REPOSITORY = "repository.";
48      private static final String PREFIX_TYPE = "type.";
49      private static final String SUFFIX_READWRITE = ".readwrite";
50      private static final String SUFFIX_READONLY = ".readonly";
51  
52      private static final BigInteger DEFAULT_MAX_ITEMS_TYPES = BigInteger.valueOf(50);
53      private static final BigInteger DEFAULT_DEPTH_TYPES = BigInteger.valueOf(-1);
54      private static final BigInteger DEFAULT_MAX_ITEMS_OBJECTS = BigInteger.valueOf(200);
55      private static final BigInteger DEFAULT_DEPTH_OBJECTS = BigInteger.valueOf(10);
56  
57      private static final Log log = LogFactory.getLog(FileShareServiceFactory.class);
58  
59      private RepositoryMap repositoryMap;
60      private TypeManager typeManager;
61  
62      private ThreadLocal<CmisServiceWrapper<FileShareService>> threadLocalService = new ThreadLocal<CmisServiceWrapper<FileShareService>>();
63  
64      @Override
65      public void init(Map<String, String> parameters) {
66          repositoryMap = new RepositoryMap();
67          typeManager = new TypeManager();
68  
69          readConfiguration(parameters);
70      }
71  
72      @Override
73      public void destroy() {
74          threadLocalService = null;
75      }
76  
77      @Override
78      public CmisService getService(CallContext context) {
79          repositoryMap.authenticate(context);
80  
81          CmisServiceWrapper<FileShareService> wrapperService = threadLocalService.get();
82          if (wrapperService == null) {
83              wrapperService = new CmisServiceWrapper<FileShareService>(new FileShareService(repositoryMap),
84                      DEFAULT_MAX_ITEMS_TYPES, DEFAULT_DEPTH_TYPES, DEFAULT_MAX_ITEMS_OBJECTS, DEFAULT_DEPTH_OBJECTS);
85              threadLocalService.set(wrapperService);
86          }
87  
88          wrapperService.getWrappedService().setCallContext(context);
89  
90          return wrapperService;
91      }
92  
93      // ---- helpers ----
94  
95      private void readConfiguration(Map<String, String> parameters) {
96          List<String> keys = new ArrayList<String>(parameters.keySet());
97          Collections.sort(keys);
98  
99          for (String key : keys) {
100             if (key.startsWith(PREFIX_LOGIN)) {
101                 // get logins
102                 String usernameAndPassword = replaceSystemProperties(parameters.get(key));
103                 if (usernameAndPassword == null) {
104                     continue;
105                 }
106 
107                 String username = usernameAndPassword;
108                 String password = "";
109 
110                 int x = usernameAndPassword.indexOf(':');
111                 if (x > -1) {
112                     username = usernameAndPassword.substring(0, x);
113                     password = usernameAndPassword.substring(x + 1);
114                 }
115 
116                 repositoryMap.addLogin(username, password);
117 
118                 log.info("Added login '" + username + "'.");
119             } else if (key.startsWith(PREFIX_TYPE)) {
120                 // load type definition
121                 TypeDefinition type = loadType(replaceSystemProperties(parameters.get(key)));
122                 if (type != null) {
123                     typeManager.addType(type);
124                 }
125             } else if (key.startsWith(PREFIX_REPOSITORY)) {
126                 // configure repositories
127                 String repositoryId = key.substring(PREFIX_REPOSITORY.length()).trim();
128                 int x = repositoryId.lastIndexOf('.');
129                 if (x > 0) {
130                     repositoryId = repositoryId.substring(0, x);
131                 }
132 
133                 if (repositoryId.length() == 0) {
134                     throw new IllegalArgumentException("No repository id!");
135                 }
136 
137                 if (key.endsWith(SUFFIX_READWRITE)) {
138                     // read-write users
139                     FileShareRepository fsr = repositoryMap.getRepository(repositoryId);
140                     for (String user : split(parameters.get(key))) {
141                         fsr.addUser(replaceSystemProperties(user), false);
142                     }
143                 } else if (key.endsWith(SUFFIX_READONLY)) {
144                     // read-only users
145                     FileShareRepository fsr = repositoryMap.getRepository(repositoryId);
146                     for (String user : split(parameters.get(key))) {
147                         fsr.addUser(replaceSystemProperties(user), true);
148                     }
149                 } else {
150                     // new repository
151                     String root = replaceSystemProperties(parameters.get(key));
152                     FileShareRepository fsr = new FileShareRepository(repositoryId, root, typeManager);
153 
154                     repositoryMap.addRepository(fsr);
155 
156                     log.info("Added repository '" + fsr.getRepositoryId() + "': " + root);
157                 }
158             }
159         }
160     }
161 
162     private static List<String> split(String csl) {
163         if (csl == null) {
164             return Collections.emptyList();
165         }
166 
167         List<String> result = new ArrayList<String>();
168         for (String s : csl.split(",")) {
169             result.add(s.trim());
170         }
171 
172         return result;
173     }
174 
175     private static String replaceSystemProperties(String s) {
176         if (s == null) {
177             return null;
178         }
179 
180         StringBuilder result = new StringBuilder();
181         StringBuilder property = null;
182         boolean inProperty = false;
183 
184         for (int i = 0; i < s.length(); i++) {
185             char c = s.charAt(i);
186 
187             if (inProperty) {
188                 if (c == '}') {
189                     String value = System.getProperty(property.toString());
190                     if (value != null) {
191                         result.append(value);
192                     }
193                     inProperty = false;
194                 } else {
195                     property.append(c);
196                 }
197             } else {
198                 if (c == '{') {
199                     property = new StringBuilder();
200                     inProperty = true;
201                 } else {
202                     result.append(c);
203                 }
204             }
205         }
206 
207         return result.toString();
208     }
209 
210     @SuppressWarnings("unchecked")
211     private static TypeDefinition loadType(String filename) {
212         TypeDefinition result = null;
213 
214         try {
215             Unmarshaller u = JaxBHelper.createUnmarshaller();
216             JAXBElement<CmisTypeDefinitionType> type = (JAXBElement<CmisTypeDefinitionType>) u.unmarshal(new File(
217                     filename));
218             result = Converter.convert(type.getValue());
219         } catch (Exception e) {
220             log.info("Could not load type: '" + filename + "'", e);
221         }
222 
223         return result;
224     }
225 }