KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > jcr > JackrabbitRepository


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.jcr;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.cocoon.components.source.SourceUtil;
21 import org.apache.commons.lang.SystemUtils;
22 import org.apache.excalibur.source.Source;
23 import org.apache.excalibur.source.SourceResolver;
24 import org.apache.excalibur.source.impl.FileSource;
25 import org.apache.jackrabbit.core.RepositoryImpl;
26 import org.apache.jackrabbit.core.config.RepositoryConfig;
27 import org.xml.sax.InputSource JavaDoc;
28
29 /**
30  * JackrabbitRepository is a JCR repository component based on <a
31  * HREF="http://incubator.apache.org/jackrabbit">Jackrabbit</a>
32  *
33  * <p>
34  * The configuration is as follows:
35  *
36  * <pre>
37  * &lt;jcr-repository&gt;
38  * &lt;credentials login="<i>expression</i>" password="<i>expression</i>"/&gt;
39  * &lt;home SRC="file://path/to/repository"/&gt;
40  * &lt;configuration SRC="resource://your/application/jcr/repository.xml"/&gt;
41  * &lt;/jcr-repository&gt;
42  * </pre>
43  *
44  * The <code>home</code> URI points to the base location of the repository,
45  * and <code>configuration</code> points to the Jackrabbit repository
46  * configuration file.
47  *
48  * @see AbstractRepository
49  * @version $Id: JackrabbitRepository.java 293188 2005-10-03 00:35:04Z antonio $
50  */

51 public class JackrabbitRepository extends AbstractRepository {
52
53     public void configure(Configuration config) throws ConfigurationException {
54         super.configure(config);
55         // Java VM must be at least 1.4
56
if (SystemUtils.isJavaVersionAtLeast(140) == false) {
57             String JavaDoc 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.";
58             getLogger().error(message);
59             throw new ConfigurationException(message);
60         }
61
62         String JavaDoc homeURI = config.getChild("home").getAttribute("src");
63         String JavaDoc homePath;
64         String JavaDoc configURI = config.getChild("configuration").getAttribute("src");
65
66         // having to release sources is a major PITA...
67
SourceResolver resolver = null;
68         try {
69             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
70
71             // Ensure home uri is a file and absolutize it
72
Source homeSrc = resolver.resolveURI(homeURI);
73             try {
74                 if (!(homeSrc instanceof FileSource)) {
75                     throw new ConfigurationException("Home path '" + homeURI + "' should map to a file, at " +
76                                                      config.getChild("home").getLocation());
77                 }
78                 homePath = ((FileSource) homeSrc).getFile().getAbsolutePath();
79             } finally {
80                 resolver.release(homeSrc);
81             }
82
83             // Load repository configuration
84
Source configSrc = resolver.resolveURI(configURI);
85             RepositoryConfig repoConfig;
86             try {
87                 InputSource JavaDoc is = SourceUtil.getInputSource(configSrc);
88                 repoConfig = RepositoryConfig.create(is, homePath);
89             } finally {
90                 resolver.release(configSrc);
91             }
92             // And create the repository
93
this.delegate = RepositoryImpl.create(repoConfig);
94
95         } catch (ConfigurationException ce) {
96             throw ce;
97         } catch (Exception JavaDoc e) {
98             throw new ConfigurationException("Cannot access configuration information at " + config.getLocation(), e);
99         } finally {
100             this.manager.release(resolver);
101         }
102     }
103     
104     public void dispose() {
105         // Shutdown the repository to release the concurrent access lock
106
RepositoryImpl repo = (RepositoryImpl)delegate;
107         super.dispose();
108         repo.shutdown();
109     }
110 }
111
Popular Tags