1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
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 }