1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.jcr.jackrabbit;
21  
22  import com.liferay.portal.jcr.JCRFactory;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Time;
29  import com.liferay.portal.util.PropsKeys;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.util.SystemProperties;
32  
33  import java.io.File;
34  import java.io.IOException;
35  
36  import javax.jcr.Credentials;
37  import javax.jcr.Repository;
38  import javax.jcr.RepositoryException;
39  import javax.jcr.Session;
40  import javax.jcr.SimpleCredentials;
41  
42  import org.apache.jackrabbit.api.JackrabbitRepository;
43  import org.apache.jackrabbit.core.TransientRepository;
44  
45  /**
46   * <a href="JCRFactoryImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Michael Young
49   *
50   */
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 = _repository.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() throws RepositoryException {
152         if (_initialized) {
153             Session session = null;
154 
155             try {
156                 session = createSession(null);
157 
158                 JackrabbitRepository repository =
159                     (JackrabbitRepository)session.getRepository();
160 
161                 repository.shutdown();
162             }
163             catch (RepositoryException re) {
164                 _log.error("Could not shutdown Jackrabbit");
165 
166                 throw re;
167             }
168         }
169 
170         _initialized = false;
171     }
172 
173     protected JCRFactoryImpl() throws Exception {
174         try {
175             _repository = new TransientRepository(
176                 CONFIG_FILE_PATH, REPOSITORY_HOME);
177         }
178         catch (Exception e) {
179             _log.error("Problem initializing Jackrabbit JCR.", e);
180 
181             throw e;
182         }
183 
184         if (_log.isInfoEnabled()) {
185             _log.info(
186                 "Jackrabbit JCR intialized with config file path " +
187                     CONFIG_FILE_PATH + " and repository home " +
188                         REPOSITORY_HOME);
189         }
190     }
191 
192     private static Log _log = LogFactoryUtil.getLog(JCRFactoryImpl.class);
193 
194     private Repository _repository;
195     private boolean _initialized;
196 
197 }