001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.jcr.jackrabbit;
022:
023: import com.liferay.portal.jcr.JCRFactory;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.util.PropsUtil;
026:
027: import javax.jcr.Credentials;
028: import javax.jcr.Repository;
029: import javax.jcr.RepositoryException;
030: import javax.jcr.Session;
031: import javax.jcr.SimpleCredentials;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.jackrabbit.api.JackrabbitRepository;
036: import org.apache.jackrabbit.core.TransientRepository;
037:
038: /**
039: * <a href="JCRFactoryImpl.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Michael Young
042: *
043: */
044: public class JCRFactoryImpl implements JCRFactory {
045:
046: public static final String REPOSITORY_ROOT = PropsUtil
047: .get(PropsUtil.JCR_JACKRABBIT_REPOSITORY_ROOT);
048:
049: public static final String CONFIG_FILE_PATH = PropsUtil
050: .get(PropsUtil.JCR_JACKRABBIT_CONFIG_FILE_PATH);
051:
052: public static final String REPOSITORY_HOME = PropsUtil
053: .get(PropsUtil.JCR_JACKRABBIT_REPOSITORY_HOME);
054:
055: public static final String CREDENTIALS_USERNAME = PropsUtil
056: .get(PropsUtil.JCR_JACKRABBIT_CREDENTIALS_USERNAME);
057:
058: public static final char[] CREDENTIALS_PASSWORD = GetterUtil
059: .getString(
060: PropsUtil
061: .get(PropsUtil.JCR_JACKRABBIT_CREDENTIALS_PASSWORD))
062: .toCharArray();
063:
064: public Session createSession(String workspaceName)
065: throws RepositoryException {
066:
067: Credentials credentials = new SimpleCredentials(
068: CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD);
069:
070: Session session = null;
071:
072: try {
073: session = _repository.login(credentials, workspaceName);
074: } catch (RepositoryException e) {
075: _log.error("Could not login to the workspace "
076: + workspaceName);
077:
078: throw e;
079: }
080:
081: return session;
082: }
083:
084: public void initialize() throws RepositoryException {
085: Session session = null;
086:
087: try {
088: session = createSession(null);
089: } catch (RepositoryException e) {
090: _log.error("Could not initialize Jackrabbit");
091:
092: throw e;
093: } finally {
094: if (session != null) {
095: session.logout();
096: }
097: }
098:
099: _initialized = true;
100: }
101:
102: public void shutdown() throws RepositoryException {
103: if (_initialized) {
104: Session session = null;
105:
106: try {
107: session = createSession(null);
108:
109: JackrabbitRepository repository = (JackrabbitRepository) session
110: .getRepository();
111:
112: repository.shutdown();
113: } catch (RepositoryException e) {
114: _log.error("Could not shutdown Jackrabbit");
115:
116: throw e;
117: }
118: }
119:
120: _initialized = false;
121: }
122:
123: protected JCRFactoryImpl() throws Exception {
124: try {
125: _repository = new TransientRepository(CONFIG_FILE_PATH,
126: REPOSITORY_HOME);
127: } catch (Exception e) {
128: _log.error("Problem initializing Jackrabbit JCR.", e);
129:
130: throw e;
131: }
132:
133: if (_log.isInfoEnabled()) {
134: _log
135: .info("Jackrabbit JCR intialized with config file path "
136: + CONFIG_FILE_PATH
137: + " and repository home " + REPOSITORY_HOME);
138: }
139: }
140:
141: private static Log _log = LogFactory.getLog(JCRFactoryImpl.class);
142:
143: private Repository _repository;
144: private boolean _initialized;
145:
146: }
|