1
14
15 package com.liferay.portal.jcr.jackrabbit;
16
17 import com.liferay.portal.jcr.JCRFactory;
18 import com.liferay.portal.kernel.log.Log;
19 import com.liferay.portal.kernel.log.LogFactoryUtil;
20 import com.liferay.portal.kernel.util.FileUtil;
21 import com.liferay.portal.kernel.util.GetterUtil;
22 import com.liferay.portal.kernel.util.PropsKeys;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.kernel.util.Time;
25 import com.liferay.portal.util.PropsUtil;
26 import com.liferay.util.SystemProperties;
27
28 import java.io.File;
29 import java.io.IOException;
30
31 import javax.jcr.Credentials;
32 import javax.jcr.RepositoryException;
33 import javax.jcr.Session;
34 import javax.jcr.SimpleCredentials;
35
36 import org.apache.jackrabbit.core.TransientRepository;
37
38
43 public class JCRFactoryImpl implements JCRFactory {
44
45 public static final String REPOSITORY_ROOT = PropsUtil.get(
46 PropsKeys.JCR_JACKRABBIT_REPOSITORY_ROOT);
47
48 public static final String CONFIG_FILE_PATH = PropsUtil.get(
49 PropsKeys.JCR_JACKRABBIT_CONFIG_FILE_PATH);
50
51 public static final String REPOSITORY_HOME = PropsUtil.get(
52 PropsKeys.JCR_JACKRABBIT_REPOSITORY_HOME);
53
54 public static final String CREDENTIALS_USERNAME = PropsUtil.get(
55 PropsKeys.JCR_JACKRABBIT_CREDENTIALS_USERNAME);
56
57 public static final char[] CREDENTIALS_PASSWORD = GetterUtil.getString(
58 PropsUtil.get(PropsKeys.JCR_JACKRABBIT_CREDENTIALS_PASSWORD)).
59 toCharArray();
60
61 public Session createSession(String workspaceName)
62 throws RepositoryException {
63
64 Credentials credentials = new SimpleCredentials(
65 CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD);
66
67 Session session = null;
68
69 try {
70 session = _transientRepository.login(credentials, workspaceName);
71 }
72 catch (RepositoryException re) {
73 _log.error("Could not login to the workspace " + workspaceName);
74
75 throw re;
76 }
77
78 return session;
79 }
80
81 public void initialize() throws RepositoryException {
82 Session session = null;
83
84 try {
85 session = createSession(null);
86 }
87 catch (RepositoryException re) {
88 _log.error("Could not initialize Jackrabbit");
89
90 throw re;
91 }
92 finally {
93 if (session != null) {
94 session.logout();
95 }
96 }
97
98 _initialized = true;
99 }
100
101 public void prepare() throws RepositoryException {
102 try {
103 File repositoryRoot = new File(JCRFactoryImpl.REPOSITORY_ROOT);
104
105 if (repositoryRoot.exists()) {
106 return;
107 }
108
109 repositoryRoot.mkdirs();
110
111 File tempFile = new File(
112 SystemProperties.get(SystemProperties.TMP_DIR) +
113 File.separator + Time.getTimestamp());
114
115 String repositoryXmlPath =
116 "com/liferay/portal/jcr/jackrabbit/dependencies/" +
117 "repository-ext.xml";
118
119 ClassLoader classLoader = getClass().getClassLoader();
120
121 if (classLoader.getResource(repositoryXmlPath) == null) {
122 repositoryXmlPath =
123 "com/liferay/portal/jcr/jackrabbit/dependencies/" +
124 "repository.xml";
125 }
126
127 String content = StringUtil.read(classLoader, repositoryXmlPath);
128
129 FileUtil.write(tempFile, content);
130
131 FileUtil.copyFile(
132 tempFile, new File(JCRFactoryImpl.CONFIG_FILE_PATH));
133
134 tempFile.delete();
135 }
136 catch (IOException ioe) {
137 _log.error("Could not prepare Jackrabbit directory");
138
139 throw new RepositoryException(ioe);
140 }
141 }
142
143 public void shutdown() {
144 if (_initialized) {
145 _transientRepository.shutdown();
146 }
147
148 _initialized = false;
149 }
150
151 protected JCRFactoryImpl() throws Exception {
152 try {
153 _transientRepository = new TransientRepository(
154 CONFIG_FILE_PATH, REPOSITORY_HOME);
155 }
156 catch (Exception e) {
157 _log.error("Problem initializing Jackrabbit JCR.", e);
158
159 throw e;
160 }
161
162 if (_log.isInfoEnabled()) {
163 _log.info(
164 "Jackrabbit JCR intialized with config file path " +
165 CONFIG_FILE_PATH + " and repository home " +
166 REPOSITORY_HOME);
167 }
168 }
169
170 private static Log _log = LogFactoryUtil.getLog(JCRFactoryImpl.class);
171
172 private boolean _initialized;
173 private TransientRepository _transientRepository;
174
175 }