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.jackrabbit.core.TransientRepository;
19 import org.kuali.ole.logger.MetricsLogger;
20 import org.kuali.ole.pojo.OleException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import javax.jcr.RepositoryException;
25 import javax.jcr.Session;
26 import javax.jcr.SimpleCredentials;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30
31
32
33
34
35
36
37 public class RepositoryManager {
38 private static final Logger LOG = LoggerFactory.getLogger(RepositoryManager.class);
39
40 private TransientRepository transientRepository;
41 private static RepositoryManager repositoryManager;
42 private MetricsLogger metricsLogger;
43 private List<Session> sessionPool = new ArrayList<Session>();
44 boolean customNodeTypesInitialllized = false;
45
46 private RepositoryManager() throws OleException {
47
48 }
49
50 public static RepositoryManager getRepositoryManager() throws OleException {
51 if (null == repositoryManager) {
52 repositoryManager = new RepositoryManager();
53 }
54 return repositoryManager;
55 }
56
57
58 public synchronized Session getSession() throws OleException {
59 return getSession(null, null);
60 }
61
62 public synchronized Session getSession(String userName, String actionSource) throws OleException {
63 if (!customNodeTypesInitialllized) {
64 init();
65 }
66 Session session = null;
67 getMetricsLogger().startRecording();
68
69 try {
70 LOG.debug("Environment: " + System.getProperty("app.environment"));
71
72 session = getTransientRepository()
73 .login(new SimpleCredentials(null == userName ? "username" : userName, "password".toCharArray()));
74 LOG.info("session successfully created with id: " + session.getUserID() + " for the following action " + (
75 (actionSource == null) ? "default" : actionSource));
76 } catch (RepositoryException e) {
77 return throwException(e);
78 } catch (IOException e) {
79 throwException(e);
80 } catch (Exception e) {
81 throwException(e);
82 }
83 metricsLogger.endRecording();
84 metricsLogger.printTimes("time taken to initiate a session was: ");
85 return session;
86 }
87
88 private MetricsLogger getMetricsLogger() {
89 if (null == metricsLogger) {
90 metricsLogger = new MetricsLogger(this.getClass().getName());
91 }
92 return metricsLogger;
93 }
94
95 private Session throwException(Exception e) throws OleException {
96 LOG.error("Unable to get the session from the repository", e);
97 throw new OleException("Unable to get the session from the repository", e);
98 }
99
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 }