1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.jcr.jackrabbit;
24  
25  import com.liferay.portal.jcr.JCRFactory;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Time;
32  import com.liferay.portal.util.PropsKeys;
33  import com.liferay.portal.util.PropsUtil;
34  import com.liferay.util.SystemProperties;
35  
36  import java.io.File;
37  import java.io.IOException;
38  
39  import javax.jcr.Credentials;
40  import javax.jcr.Repository;
41  import javax.jcr.RepositoryException;
42  import javax.jcr.Session;
43  import javax.jcr.SimpleCredentials;
44  
45  import org.apache.jackrabbit.api.JackrabbitRepository;
46  import org.apache.jackrabbit.core.TransientRepository;
47  
48  /**
49   * <a href="JCRFactoryImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Michael Young
52   */
53  public class JCRFactoryImpl implements JCRFactory {
54  
55      public static final String REPOSITORY_ROOT = PropsUtil.get(
56          PropsKeys.JCR_JACKRABBIT_REPOSITORY_ROOT);
57  
58      public static final String CONFIG_FILE_PATH = PropsUtil.get(
59          PropsKeys.JCR_JACKRABBIT_CONFIG_FILE_PATH);
60  
61      public static final String REPOSITORY_HOME = PropsUtil.get(
62          PropsKeys.JCR_JACKRABBIT_REPOSITORY_HOME);
63  
64      public static final String CREDENTIALS_USERNAME = PropsUtil.get(
65          PropsKeys.JCR_JACKRABBIT_CREDENTIALS_USERNAME);
66  
67      public static final char[] CREDENTIALS_PASSWORD = GetterUtil.getString(
68          PropsUtil.get(PropsKeys.JCR_JACKRABBIT_CREDENTIALS_PASSWORD)).
69              toCharArray();
70  
71      public Session createSession(String workspaceName)
72          throws RepositoryException {
73  
74          Credentials credentials = new SimpleCredentials(
75              CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD);
76  
77          Session session = null;
78  
79          try {
80              session = _repository.login(credentials, workspaceName);
81          }
82          catch (RepositoryException re) {
83              _log.error("Could not login to the workspace " + workspaceName);
84  
85              throw re;
86          }
87  
88          return session;
89      }
90  
91      public void initialize() throws RepositoryException {
92          Session session = null;
93  
94          try {
95              session = createSession(null);
96          }
97          catch (RepositoryException re) {
98              _log.error("Could not initialize Jackrabbit");
99  
100             throw re;
101         }
102         finally {
103             if (session != null) {
104                 session.logout();
105             }
106         }
107 
108         _initialized = true;
109     }
110 
111     public void prepare() throws RepositoryException {
112         try {
113             File repositoryRoot = new File(JCRFactoryImpl.REPOSITORY_ROOT);
114 
115             if (repositoryRoot.exists()) {
116                 return;
117             }
118 
119             repositoryRoot.mkdirs();
120 
121             File tempFile = new File(
122                 SystemProperties.get(SystemProperties.TMP_DIR) +
123                     File.separator + Time.getTimestamp());
124 
125             String repositoryXmlPath =
126                 "com/liferay/portal/jcr/jackrabbit/dependencies/" +
127                     "repository-ext.xml";
128 
129             ClassLoader classLoader = getClass().getClassLoader();
130 
131             if (classLoader.getResource(repositoryXmlPath) == null) {
132                 repositoryXmlPath =
133                     "com/liferay/portal/jcr/jackrabbit/dependencies/" +
134                         "repository.xml";
135             }
136 
137             String content = StringUtil.read(classLoader, repositoryXmlPath);
138 
139             FileUtil.write(tempFile, content);
140 
141             FileUtil.copyFile(
142                 tempFile, new File(JCRFactoryImpl.CONFIG_FILE_PATH));
143 
144             tempFile.delete();
145         }
146         catch (IOException ioe) {
147             _log.error("Could not prepare Jackrabbit directory");
148 
149             throw new RepositoryException(ioe);
150         }
151     }
152 
153     public void shutdown() throws RepositoryException {
154         if (_initialized) {
155             Session session = null;
156 
157             try {
158                 session = createSession(null);
159 
160                 JackrabbitRepository repository =
161                     (JackrabbitRepository)session.getRepository();
162 
163                 repository.shutdown();
164             }
165             catch (RepositoryException re) {
166                 _log.error("Could not shutdown Jackrabbit");
167 
168                 throw re;
169             }
170         }
171 
172         _initialized = false;
173     }
174 
175     protected JCRFactoryImpl() throws Exception {
176         try {
177             _repository = new TransientRepository(
178                 CONFIG_FILE_PATH, REPOSITORY_HOME);
179         }
180         catch (Exception e) {
181             _log.error("Problem initializing Jackrabbit JCR.", e);
182 
183             throw e;
184         }
185 
186         if (_log.isInfoEnabled()) {
187             _log.info(
188                 "Jackrabbit JCR intialized with config file path " +
189                     CONFIG_FILE_PATH + " and repository home " +
190                         REPOSITORY_HOME);
191         }
192     }
193 
194     private static Log _log = LogFactoryUtil.getLog(JCRFactoryImpl.class);
195 
196     private Repository _repository;
197     private boolean _initialized;
198 
199 }