001
014
015 package com.liferay.portal.jcr.jackrabbit;
016
017 import com.liferay.portal.jcr.JCRFactory;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.FileUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.PropsKeys;
023 import com.liferay.portal.kernel.util.SystemProperties;
024 import com.liferay.portal.kernel.util.Time;
025 import com.liferay.portal.util.PropsUtil;
026
027 import java.io.File;
028 import java.io.IOException;
029
030 import javax.jcr.Credentials;
031 import javax.jcr.RepositoryException;
032 import javax.jcr.Session;
033 import javax.jcr.SimpleCredentials;
034
035 import org.apache.jackrabbit.core.TransientRepository;
036
037
040 public class JCRFactoryImpl implements JCRFactory {
041
042 public static final String CONFIG_FILE_PATH = PropsUtil.get(
043 PropsKeys.JCR_JACKRABBIT_CONFIG_FILE_PATH);
044
045 public static final char[] CREDENTIALS_PASSWORD = GetterUtil.getString(
046 PropsUtil.get(PropsKeys.JCR_JACKRABBIT_CREDENTIALS_PASSWORD)).
047 toCharArray();
048
049 public static final String CREDENTIALS_USERNAME = PropsUtil.get(
050 PropsKeys.JCR_JACKRABBIT_CREDENTIALS_USERNAME);
051
052 public static final String REPOSITORY_HOME = PropsUtil.get(
053 PropsKeys.JCR_JACKRABBIT_REPOSITORY_HOME);
054
055 public static final String REPOSITORY_ROOT = PropsUtil.get(
056 PropsKeys.JCR_JACKRABBIT_REPOSITORY_ROOT);
057
058 @Override
059 public Session createSession(String workspaceName)
060 throws RepositoryException {
061
062 Credentials credentials = new SimpleCredentials(
063 CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD);
064
065 Session session = null;
066
067 try {
068 session = _transientRepository.login(credentials, workspaceName);
069 }
070 catch (RepositoryException re) {
071 _log.error("Could not login to the workspace " + workspaceName);
072
073 throw re;
074 }
075
076 return session;
077 }
078
079 @Override
080 public void initialize() throws RepositoryException {
081 Session session = null;
082
083 try {
084 session = createSession(null);
085 }
086 catch (RepositoryException re) {
087 _log.error("Could not initialize Jackrabbit");
088
089 throw re;
090 }
091 finally {
092 if (session != null) {
093 session.logout();
094 }
095 }
096
097 _initialized = true;
098 }
099
100 @Override
101 public void prepare() throws RepositoryException {
102 try {
103 FileUtil.mkdirs(JCRFactoryImpl.REPOSITORY_ROOT);
104
105 File tempFile = new File(
106 SystemProperties.get(SystemProperties.TMP_DIR) +
107 File.separator + Time.getTimestamp());
108
109 String repositoryXmlPath =
110 "com/liferay/portal/jcr/jackrabbit/dependencies/" +
111 "repository-ext.xml";
112
113 ClassLoader classLoader = getClass().getClassLoader();
114
115 if (classLoader.getResource(repositoryXmlPath) == null) {
116 repositoryXmlPath =
117 "com/liferay/portal/jcr/jackrabbit/dependencies/" +
118 "repository.xml";
119 }
120
121 FileUtil.write(
122 tempFile, classLoader.getResourceAsStream(repositoryXmlPath));
123
124 FileUtil.copyFile(
125 tempFile, new File(JCRFactoryImpl.CONFIG_FILE_PATH));
126
127 tempFile.delete();
128 }
129 catch (IOException ioe) {
130 _log.error("Could not prepare Jackrabbit directory");
131
132 throw new RepositoryException(ioe);
133 }
134 }
135
136 @Override
137 public void shutdown() {
138 if (_initialized) {
139 _transientRepository.shutdown();
140 }
141
142 _initialized = false;
143 }
144
145 protected JCRFactoryImpl() throws Exception {
146 try {
147 _transientRepository = new TransientRepository(
148 CONFIG_FILE_PATH, REPOSITORY_HOME);
149 }
150 catch (Exception e) {
151 _log.error("Problem initializing Jackrabbit JCR.", e);
152
153 throw e;
154 }
155
156 if (_log.isInfoEnabled()) {
157 _log.info(
158 "Jackrabbit JCR intialized with config file path " +
159 CONFIG_FILE_PATH + " and repository home " +
160 REPOSITORY_HOME);
161 }
162 }
163
164 private static Log _log = LogFactoryUtil.getLog(JCRFactoryImpl.class);
165
166 private boolean _initialized;
167 private TransientRepository _transientRepository;
168
169 }