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