View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.apache.jackrabbit.commons.cnd.CndImporter;
20  import org.apache.jackrabbit.commons.cnd.ParseException;
21  import org.kuali.ole.pojo.OleException;
22  
23  import javax.jcr.RepositoryException;
24  import javax.jcr.Session;
25  import javax.jcr.nodetype.NodeType;
26  import java.io.*;
27  
28  /**
29   * Created by IntelliJ IDEA.
30   * User: peris
31   * Date: 5/1/11
32   * Time: 12:57 PM
33   * To change this template use File | Settings | File Templates.
34   */
35  public class CustomNodeRegistrar {
36      private String CUSTOM_NODES_FILE_NAME = "customFileNode.cnd";
37  
38      public NodeType[] registerCustomNodeTypes(Session session) throws OleException {
39  
40          NodeType[] nodeTypes = new NodeType[0];
41          try {
42              File tempDir = new File(System.getProperty("java.io.tmpdir"));
43              File temporaryFile = new File(tempDir, CUSTOM_NODES_FILE_NAME);
44              InputStream templateStream = getClass().getResourceAsStream(CUSTOM_NODES_FILE_NAME);
45              IOUtils.copy(templateStream, new FileOutputStream(temporaryFile));
46              String absolutePath = temporaryFile.getAbsolutePath();
47              Session newSession = null;
48              if (session == null) {
49                  newSession = getSession();
50                  session = newSession;
51              }
52              nodeTypes = CndImporter.registerNodeTypes(new FileReader(new File(absolutePath)), session);
53              if (newSession != null) {
54                  RepositoryManager.getRepositoryManager().logout(newSession);
55              }
56          } catch (ParseException e) {
57              throw new OleException(e.getMessage());
58          } catch (RepositoryException e) {
59              throw new OleException(e.getMessage());
60          } catch (IOException e) {
61              throw new OleException(e.getMessage());
62          }
63          return nodeTypes;
64      }
65  
66      public Session getSession() throws OleException {
67          RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
68          Session session = repositoryManager.getSession("CustomNodeRegistrar", "getSession");
69          return session;
70      }
71  }