001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.jcr;
018:
019: import org.apache.avalon.framework.configuration.Configuration;
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021: import org.apache.cocoon.components.source.SourceUtil;
022: import org.apache.commons.lang.SystemUtils;
023: import org.apache.excalibur.source.Source;
024: import org.apache.excalibur.source.SourceResolver;
025: import org.apache.excalibur.source.impl.FileSource;
026: import org.apache.jackrabbit.core.RepositoryImpl;
027: import org.apache.jackrabbit.core.config.RepositoryConfig;
028: import org.xml.sax.InputSource;
029:
030: /**
031: * JackrabbitRepository is a JCR repository component based on <a
032: * href="http://incubator.apache.org/jackrabbit">Jackrabbit</a>
033: *
034: * <p>
035: * The configuration is as follows:
036: *
037: * <pre>
038: * <jcr-repository>
039: * <credentials login="<i>expression</i>" password="<i>expression</i>"/>
040: * <home src="file://path/to/repository"/>
041: * <configuration src="resource://your/application/jcr/repository.xml"/>
042: * </jcr-repository>
043: * </pre>
044: *
045: * The <code>home</code> URI points to the base location of the repository,
046: * and <code>configuration</code> points to the Jackrabbit repository
047: * configuration file.
048: *
049: * @see AbstractRepository
050: * @version $Id: JackrabbitRepository.java 449153 2006-09-23 04:27:50Z crossley $
051: */
052: public class JackrabbitRepository extends AbstractRepository {
053:
054: public void configure(Configuration config)
055: throws ConfigurationException {
056: super .configure(config);
057: // Java VM must be at least 1.4
058: if (SystemUtils.isJavaVersionAtLeast(140) == false) {
059: String message = "The jcr block needs at least a java VM version 1.4 to run properly. Please update to a newer java or exclude the jcr block from your Cocoon block configuration.";
060: getLogger().error(message);
061: throw new ConfigurationException(message);
062: }
063:
064: String homeURI = config.getChild("home").getAttribute("src");
065: String homePath;
066: String configURI = config.getChild("configuration")
067: .getAttribute("src");
068:
069: // having to release sources is a major PITA...
070: SourceResolver resolver = null;
071: try {
072: resolver = (SourceResolver) this .manager
073: .lookup(SourceResolver.ROLE);
074:
075: // Ensure home uri is a file and absolutize it
076: Source homeSrc = resolver.resolveURI(homeURI);
077: try {
078: if (!(homeSrc instanceof FileSource)) {
079: throw new ConfigurationException("Home path '"
080: + homeURI + "' should map to a file, at "
081: + config.getChild("home").getLocation());
082: }
083: homePath = ((FileSource) homeSrc).getFile()
084: .getAbsolutePath();
085: } finally {
086: resolver.release(homeSrc);
087: }
088:
089: // Load repository configuration
090: Source configSrc = resolver.resolveURI(configURI);
091: RepositoryConfig repoConfig;
092: try {
093: InputSource is = SourceUtil.getInputSource(configSrc);
094: repoConfig = RepositoryConfig.create(is, homePath);
095: } finally {
096: resolver.release(configSrc);
097: }
098: // And create the repository
099: this .delegate = RepositoryImpl.create(repoConfig);
100:
101: } catch (ConfigurationException ce) {
102: throw ce;
103: } catch (Exception e) {
104: throw new ConfigurationException(
105: "Cannot access configuration information at "
106: + config.getLocation(), e);
107: } finally {
108: this .manager.release(resolver);
109: }
110: }
111:
112: public void dispose() {
113: // Shutdown the repository to release the concurrent access lock
114: RepositoryImpl repo = (RepositoryImpl) delegate;
115: super.dispose();
116: repo.shutdown();
117: }
118: }
|