1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.jcr.jackrabbit;
16  
17  import com.liferay.portal.jcr.JCRFactory;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.Time;
24  import com.liferay.portal.util.PropsUtil;
25  import com.liferay.util.SystemProperties;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import javax.jcr.Credentials;
31  import javax.jcr.RepositoryException;
32  import javax.jcr.Session;
33  import javax.jcr.SimpleCredentials;
34  
35  import org.apache.jackrabbit.core.TransientRepository;
36  
37  /**
38   * <a href="JCRFactoryImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Michael Young
41   */
42  public class JCRFactoryImpl implements JCRFactory {
43  
44      public static final String REPOSITORY_ROOT = PropsUtil.get(
45          PropsKeys.JCR_JACKRABBIT_REPOSITORY_ROOT);
46  
47      public static final String CONFIG_FILE_PATH = PropsUtil.get(
48          PropsKeys.JCR_JACKRABBIT_CONFIG_FILE_PATH);
49  
50      public static final String REPOSITORY_HOME = PropsUtil.get(
51          PropsKeys.JCR_JACKRABBIT_REPOSITORY_HOME);
52  
53      public static final String CREDENTIALS_USERNAME = PropsUtil.get(
54          PropsKeys.JCR_JACKRABBIT_CREDENTIALS_USERNAME);
55  
56      public static final char[] CREDENTIALS_PASSWORD = GetterUtil.getString(
57          PropsUtil.get(PropsKeys.JCR_JACKRABBIT_CREDENTIALS_PASSWORD)).
58              toCharArray();
59  
60      public Session createSession(String workspaceName)
61          throws RepositoryException {
62  
63          Credentials credentials = new SimpleCredentials(
64              CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD);
65  
66          Session session = null;
67  
68          try {
69              session = _transientRepository.login(credentials, workspaceName);
70          }
71          catch (RepositoryException re) {
72              _log.error("Could not login to the workspace " + workspaceName);
73  
74              throw re;
75          }
76  
77          return session;
78      }
79  
80      public void initialize() throws RepositoryException {
81          Session session = null;
82  
83          try {
84              session = createSession(null);
85          }
86          catch (RepositoryException re) {
87              _log.error("Could not initialize Jackrabbit");
88  
89              throw re;
90          }
91          finally {
92              if (session != null) {
93                  session.logout();
94              }
95          }
96  
97          _initialized = true;
98      }
99  
100     public void prepare() throws RepositoryException {
101         try {
102             File repositoryRoot = new File(JCRFactoryImpl.REPOSITORY_ROOT);
103 
104             if (repositoryRoot.exists()) {
105                 return;
106             }
107 
108             repositoryRoot.mkdirs();
109 
110             File tempFile = new File(
111                 SystemProperties.get(SystemProperties.TMP_DIR) +
112                     File.separator + Time.getTimestamp());
113 
114             String repositoryXmlPath =
115                 "com/liferay/portal/jcr/jackrabbit/dependencies/" +
116                     "repository-ext.xml";
117 
118             ClassLoader classLoader = getClass().getClassLoader();
119 
120             if (classLoader.getResource(repositoryXmlPath) == null) {
121                 repositoryXmlPath =
122                     "com/liferay/portal/jcr/jackrabbit/dependencies/" +
123                         "repository.xml";
124             }
125 
126             FileUtil.write(
127                 tempFile, classLoader.getResourceAsStream(repositoryXmlPath));
128 
129             FileUtil.copyFile(
130                 tempFile, new File(JCRFactoryImpl.CONFIG_FILE_PATH));
131 
132             tempFile.delete();
133         }
134         catch (IOException ioe) {
135             _log.error("Could not prepare Jackrabbit directory");
136 
137             throw new RepositoryException(ioe);
138         }
139     }
140 
141     public void shutdown() {
142         if (_initialized) {
143             _transientRepository.shutdown();
144         }
145 
146         _initialized = false;
147     }
148 
149     protected JCRFactoryImpl() throws Exception {
150         try {
151             _transientRepository = new TransientRepository(
152                 CONFIG_FILE_PATH, REPOSITORY_HOME);
153         }
154         catch (Exception e) {
155             _log.error("Problem initializing Jackrabbit JCR.", e);
156 
157             throw e;
158         }
159 
160         if (_log.isInfoEnabled()) {
161             _log.info(
162                 "Jackrabbit JCR intialized with config file path " +
163                     CONFIG_FILE_PATH + " and repository home " +
164                         REPOSITORY_HOME);
165         }
166     }
167 
168     private static Log _log = LogFactoryUtil.getLog(JCRFactoryImpl.class);
169 
170     private boolean _initialized;
171     private TransientRepository _transientRepository;
172 
173 }