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