001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
038     * @author Michael Young
039     */
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                            File jackrabbitConfigFile = new File(
104                                    JCRFactoryImpl.CONFIG_FILE_PATH);
105    
106                            if (jackrabbitConfigFile.exists() &&
107                                    jackrabbitConfigFile.isFile()) {
108    
109                                    return;
110                            }
111    
112                            FileUtil.mkdirs(JCRFactoryImpl.REPOSITORY_ROOT);
113    
114                            File tempFile = new File(
115                                    SystemProperties.get(SystemProperties.TMP_DIR) +
116                                            File.separator + Time.getTimestamp());
117    
118                            String repositoryXmlPath =
119                                    "com/liferay/portal/jcr/jackrabbit/dependencies/" +
120                                            "repository-ext.xml";
121    
122                            ClassLoader classLoader = getClass().getClassLoader();
123    
124                            if (classLoader.getResource(repositoryXmlPath) == null) {
125                                    repositoryXmlPath =
126                                            "com/liferay/portal/jcr/jackrabbit/dependencies/" +
127                                                    "repository.xml";
128                            }
129    
130                            FileUtil.write(
131                                    tempFile, classLoader.getResourceAsStream(repositoryXmlPath));
132    
133                            FileUtil.copyFile(tempFile, jackrabbitConfigFile);
134    
135                            tempFile.delete();
136                    }
137                    catch (IOException ioe) {
138                            _log.error("Could not prepare Jackrabbit directory");
139    
140                            throw new RepositoryException(ioe);
141                    }
142            }
143    
144            @Override
145            public void shutdown() {
146                    if (_initialized) {
147                            _transientRepository.shutdown();
148                    }
149    
150                    _initialized = false;
151            }
152    
153            protected JCRFactoryImpl() throws Exception {
154                    try {
155                            _transientRepository = new TransientRepository(
156                                    CONFIG_FILE_PATH, REPOSITORY_HOME);
157                    }
158                    catch (Exception e) {
159                            _log.error("Problem initializing Jackrabbit JCR.", e);
160    
161                            throw e;
162                    }
163    
164                    if (_log.isInfoEnabled()) {
165                            _log.info(
166                                    "Jackrabbit JCR intialized with config file path " +
167                                            CONFIG_FILE_PATH + " and repository home " +
168                                                    REPOSITORY_HOME);
169                    }
170            }
171    
172            private static Log _log = LogFactoryUtil.getLog(JCRFactoryImpl.class);
173    
174            private boolean _initialized;
175            private TransientRepository _transientRepository;
176    
177    }