001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.ole; 017 018 import org.apache.commons.io.IOUtils; 019 import org.apache.jackrabbit.commons.cnd.CndImporter; 020 import org.apache.jackrabbit.commons.cnd.ParseException; 021 import org.kuali.ole.pojo.OleException; 022 023 import javax.jcr.RepositoryException; 024 import javax.jcr.Session; 025 import javax.jcr.nodetype.NodeType; 026 import java.io.*; 027 028 /** 029 * Created by IntelliJ IDEA. 030 * User: peris 031 * Date: 5/1/11 032 * Time: 12:57 PM 033 * To change this template use File | Settings | File Templates. 034 */ 035 public class CustomNodeRegistrar { 036 private String CUSTOM_NODES_FILE_NAME = "customFileNode.cnd"; 037 038 public NodeType[] registerCustomNodeTypes(Session session) throws OleException { 039 040 NodeType[] nodeTypes = new NodeType[0]; 041 try { 042 File tempDir = new File(System.getProperty("java.io.tmpdir")); 043 File temporaryFile = new File(tempDir, CUSTOM_NODES_FILE_NAME); 044 InputStream templateStream = getClass().getResourceAsStream(CUSTOM_NODES_FILE_NAME); 045 IOUtils.copy(templateStream, new FileOutputStream(temporaryFile)); 046 String absolutePath = temporaryFile.getAbsolutePath(); 047 Session newSession = null; 048 if (session == null) { 049 newSession = getSession(); 050 session = newSession; 051 } 052 nodeTypes = CndImporter.registerNodeTypes(new FileReader(new File(absolutePath)), session); 053 if (newSession != null) { 054 RepositoryManager.getRepositoryManager().logout(newSession); 055 } 056 } 057 catch (ParseException e) { 058 throw new OleException(e.getMessage()); 059 } 060 catch (RepositoryException e) { 061 throw new OleException(e.getMessage()); 062 } 063 catch (IOException e) { 064 throw new OleException(e.getMessage()); 065 } 066 return nodeTypes; 067 } 068 069 public Session getSession() throws OleException { 070 RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager(); 071 Session session = repositoryManager.getSession("CustomNodeRegistrar", "getSession"); 072 return session; 073 } 074 }