1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.GetterUtil;
27  import com.liferay.portal.util.PropsUtil;
28  
29  import javax.jcr.Credentials;
30  import javax.jcr.Repository;
31  import javax.jcr.RepositoryException;
32  import javax.jcr.Session;
33  import javax.jcr.SimpleCredentials;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.jackrabbit.api.JackrabbitRepository;
38  import org.apache.jackrabbit.core.TransientRepository;
39  
40  /**
41   * <a href="JCRFactoryImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Michael Young
44   *
45   */
46  public class JCRFactoryImpl implements JCRFactory {
47  
48      public static final String REPOSITORY_ROOT = PropsUtil.get(
49          PropsUtil.JCR_JACKRABBIT_REPOSITORY_ROOT);
50  
51      public static final String CONFIG_FILE_PATH = PropsUtil.get(
52          PropsUtil.JCR_JACKRABBIT_CONFIG_FILE_PATH);
53  
54      public static final String REPOSITORY_HOME = PropsUtil.get(
55          PropsUtil.JCR_JACKRABBIT_REPOSITORY_HOME);
56  
57      public static final String CREDENTIALS_USERNAME = PropsUtil.get(
58          PropsUtil.JCR_JACKRABBIT_CREDENTIALS_USERNAME);
59  
60      public static final char[] CREDENTIALS_PASSWORD = GetterUtil.getString(
61          PropsUtil.get(PropsUtil.JCR_JACKRABBIT_CREDENTIALS_PASSWORD)).
62              toCharArray();
63  
64      public Session createSession(String workspaceName)
65          throws RepositoryException {
66  
67          Credentials credentials = new SimpleCredentials(
68              CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD);
69  
70          Session session = null;
71  
72          try {
73              session = _repository.login(credentials, workspaceName);
74          }
75          catch (RepositoryException e) {
76              _log.error("Could not login to the workspace " + workspaceName);
77  
78              throw e;
79          }
80  
81          return session;
82      }
83  
84      public void initialize() throws RepositoryException {
85          Session session = null;
86  
87          try {
88              session = createSession(null);
89          }
90          catch (RepositoryException e) {
91              _log.error("Could not initialize Jackrabbit");
92  
93              throw e;
94          }
95          finally {
96              if (session != null) {
97                  session.logout();
98              }
99          }
100 
101         _initialized = true;
102     }
103 
104     public void shutdown() throws RepositoryException {
105         if (_initialized) {
106             Session session = null;
107 
108             try {
109                 session = createSession(null);
110 
111                 JackrabbitRepository repository =
112                     (JackrabbitRepository)session.getRepository();
113 
114                 repository.shutdown();
115             }
116             catch (RepositoryException e) {
117                 _log.error("Could not shutdown Jackrabbit");
118 
119                 throw e;
120             }
121         }
122 
123         _initialized = false;
124     }
125 
126     protected JCRFactoryImpl() throws Exception {
127         try {
128             _repository = new TransientRepository(
129                 CONFIG_FILE_PATH, REPOSITORY_HOME);
130         }
131         catch (Exception e) {
132             _log.error("Problem initializing Jackrabbit JCR.", e);
133 
134             throw e;
135         }
136 
137         if (_log.isInfoEnabled()) {
138             _log.info(
139                 "Jackrabbit JCR intialized with config file path " +
140                     CONFIG_FILE_PATH + " and repository home " +
141                         REPOSITORY_HOME);
142         }
143     }
144 
145     private static Log _log = LogFactory.getLog(JCRFactoryImpl.class);
146 
147     private Repository _repository;
148     private boolean _initialized;
149 
150 }