This project has retired. For details please refer to its Attic page.
JcrTypeHandlerManager 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.jcr.type;
20  
21  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
22  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
23  import org.apache.chemistry.opencmis.jcr.JcrNode;
24  import org.apache.chemistry.opencmis.jcr.JcrTypeManager;
25  import org.apache.chemistry.opencmis.jcr.PathManager;
26  import org.apache.chemistry.opencmis.jcr.query.IdentifierMap;
27  import org.apache.chemistry.opencmis.jcr.util.Predicate;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  import javax.jcr.Node;
32  import javax.jcr.RepositoryException;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * Manages a set of registered type handlers.
38   */
39  public class JcrTypeHandlerManager {
40  
41      private static final Log log = LogFactory.getLog(JcrTypeHandlerManager.class);
42  
43      private final PathManager pathManager;
44      private final JcrTypeManager typeManager;
45      private final Map<String, JcrTypeHandler> typeHandlers = new HashMap<String, JcrTypeHandler>();
46  
47      public JcrTypeHandlerManager(PathManager pathManager, JcrTypeManager typeManager) {
48          this.pathManager = pathManager;
49          this.typeManager = typeManager;
50      }
51  
52      public void addHandler(JcrTypeHandler typeHandler) {
53          if (typeManager.addType(typeHandler.getTypeDefinition())) {
54              typeHandlers.put(typeHandler.getTypeId(), typeHandler);
55              typeHandler.initialize(pathManager, typeManager, this);
56          }
57      }
58  
59      public JcrTypeHandler getTypeHandler(String typeId) {
60          JcrTypeHandler typeHandler = typeHandlers.get(typeId);
61          if (typeHandler == null) {
62              throw new CmisObjectNotFoundException("Type '" + typeId + "' is unknown!");
63          }
64          return typeHandler;
65      }
66  
67      public JcrFolderTypeHandler getFolderTypeHandler(String typeId) {
68          JcrTypeHandler typeHandler = getTypeHandler(typeId);
69          if (!(typeHandler instanceof JcrFolderTypeHandler)) {
70              throw new CmisObjectNotFoundException("Type '" + typeId + "' is not a folder!");
71          }
72          return (JcrFolderTypeHandler) typeHandler;
73      }
74  
75      public JcrDocumentTypeHandler getDocumentTypeHandler(String typeId) {
76          JcrTypeHandler typeHandler = getTypeHandler(typeId);
77          if (!(typeHandler instanceof JcrDocumentTypeHandler)) {
78              throw new CmisObjectNotFoundException("Type '" + typeId + "' is not a document!");
79          }
80          return (JcrDocumentTypeHandler) typeHandler;
81      }
82  
83      public Predicate<Node> getNodePredicate() {
84          return new Predicate<Node>() {
85              public boolean evaluate(Node node) {
86                  try {
87                      for (JcrTypeHandler typeHandler : typeHandlers.values()) {
88                          if (typeHandler.canHandle(node)) {
89                              return true;
90                          }
91                      }
92                      return false;
93                  }
94                  catch (RepositoryException e) {
95                      log.debug(e.getMessage(), e);
96                      throw new CmisRuntimeException(e.getMessage(), e);
97                  }
98              }
99          };
100     }
101 
102     public IdentifierMap getIdentifierMap(String typeId) {
103         JcrTypeHandler typeHandler = getTypeHandler(typeId);
104         IdentifierMap identifierMap = typeHandler.getIdentifierMap();
105         if (identifierMap == null) {
106             throw new CmisRuntimeException("Not supported: query for type " + typeId);
107         }
108         return identifierMap;
109     }
110 
111     public JcrNode create(Node node) {
112         try {
113             for (JcrTypeHandler typeHandler : typeHandlers.values()) {
114                 if (typeHandler.canHandle(node)) {
115                     return typeHandler.getJcrNode(node);
116                 }
117             }
118             throw new CmisObjectNotFoundException("No object type for object '" + node.getIdentifier() + "'");
119         }
120         catch (RepositoryException e) {
121             log.debug(e.getMessage(), e);
122             throw new CmisObjectNotFoundException(e.getMessage(), e);
123         }
124     }
125 }