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 */ 016package org.kuali.ole; 017 018import org.apache.jackrabbit.core.TransientRepository; 019import org.kuali.ole.logger.MetricsLogger; 020import org.kuali.ole.pojo.OleException; 021import org.slf4j.Logger; 022import org.slf4j.LoggerFactory; 023 024import javax.jcr.RepositoryException; 025import javax.jcr.Session; 026import javax.jcr.SimpleCredentials; 027import java.io.IOException; 028import java.util.ArrayList; 029import java.util.List; 030 031/** 032 * User: Peri Subrahmanya 033 * Date: 3/30/11 034 * Time: 10:07 AM 035 * To change this template use File | Settings | File Templates. 036 */ 037public class RepositoryManager { 038 private static final Logger LOG = LoggerFactory.getLogger(RepositoryManager.class); 039 040 private TransientRepository transientRepository; 041 private static RepositoryManager repositoryManager; 042 private MetricsLogger metricsLogger; 043 private List<Session> sessionPool = new ArrayList<Session>(); 044 boolean customNodeTypesInitialllized = false; 045 046 private RepositoryManager() throws OleException { 047 048 } 049 050 public static RepositoryManager getRepositoryManager() throws OleException { 051 if (null == repositoryManager) { 052 repositoryManager = new RepositoryManager(); 053 } 054 return repositoryManager; 055 } 056 057 058 public synchronized Session getSession() throws OleException { 059 return getSession(null, null); 060 } 061 062 public synchronized Session getSession(String userName, String actionSource) throws OleException { 063 if (!customNodeTypesInitialllized) { 064 init(); 065 } 066 Session session = null; 067 getMetricsLogger().startRecording(); 068 069 try { 070 LOG.debug("Environment: " + System.getProperty("app.environment")); 071 072 session = getTransientRepository() 073 .login(new SimpleCredentials(null == userName ? "username" : userName, "password".toCharArray())); 074 LOG.info("session successfully created with id: " + session.getUserID() + " for the following action " + ( 075 (actionSource == null) ? "default" : actionSource)); 076 } catch (RepositoryException e) { 077 return throwException(e); 078 } catch (IOException e) { 079 throwException(e); 080 } catch (Exception e) { 081 throwException(e); 082 } 083 metricsLogger.endRecording(); 084 metricsLogger.printTimes("time taken to initiate a session was: "); 085 return session; 086 } 087 088 private MetricsLogger getMetricsLogger() { 089 if (null == metricsLogger) { 090 metricsLogger = new MetricsLogger(this.getClass().getName()); 091 } 092 return metricsLogger; 093 } 094 095 private Session throwException(Exception e) throws OleException { 096 LOG.error("Unable to get the session from the repository", e); 097 throw new OleException("Unable to get the session from the repository", e); 098 } 099 100 private void registerNodeTypes() throws OleException { 101 try { 102 Session session = getSession("repositoryManager", "registerNodeTypes"); 103 new CustomNodeRegistrar().registerCustomNodeTypes(session); 104 sessionPool.add(session); 105 } catch (OleException e) { 106 LOG.error("Error registering node types", e); 107 } 108 } 109 110 public synchronized void logout(Session session) { 111 if (null != session && session.isLive()) { 112 if (!sessionPool.contains(session)) { 113 session.logout(); 114 LOG.info("session successfully closed for user: " + session.getUserID()); 115 } 116 } 117 LOG.info("Num sessions in the pool: " + sessionPool.size()); 118 } 119 120 public TransientRepository getTransientRepository() throws IOException { 121 if (null == transientRepository) { 122 transientRepository = new TransientRepository(); 123 } 124 return transientRepository; 125 } 126 127 public void shutdown() { 128 if (null != transientRepository) { 129 transientRepository.shutdown(); 130 } 131 } 132 133 134 public void init() throws OleException { 135 customNodeTypesInitialllized = true; 136 registerNodeTypes(); 137 setupInternalModel(); 138 } 139 140 private void setupInternalModel() throws OleException { 141 try { 142 new DocumentStoreModelInitiallizer().init(); 143 } catch (Exception e) { 144 throw new OleException(e.getMessage()); 145 } 146 } 147 148 public void shutdownRepository() { 149 transientRepository.shutdown(); 150 LOG.info("Repository has been down"); 151 } 152 153 public static void setRepositoryManager(RepositoryManager repositoryManager) { 154 RepositoryManager.repositoryManager = repositoryManager; 155 } 156}