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