1 package org.apache.archiva.metadata.repository.jcr;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.MetadataFacetFactory;
23 import org.apache.archiva.metadata.repository.*;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.time.StopWatch;
26 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.stereotype.Service;
32 import javax.annotation.PreDestroy;
33 import javax.inject.Inject;
34 import javax.jcr.Repository;
35 import javax.jcr.RepositoryException;
36 import java.io.IOException;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.HashMap;
45 @Service( "repositorySessionFactory#jcr" )
46 public class JcrRepositorySessionFactory extends AbstractRepositorySessionFactory
47 implements RepositorySessionFactory
50 private Logger logger = LoggerFactory.getLogger( getClass() );
53 private ApplicationContext applicationContext;
55 private Map<String, MetadataFacetFactory> metadataFacetFactories;
57 private Repository repository;
59 // Lazy evaluation to avoid problems with circular dependencies during initialization
60 private MetadataResolver metadataResolver;
63 private RepositorySessionFactoryBean repositorySessionFactoryBean;
65 private OakRepositoryFactory repositoryFactory;
67 private JcrMetadataRepository jcrMetadataRepository;
70 public RepositorySession createSession() throws MetadataRepositoryException
74 return new JcrRepositorySession( jcrMetadataRepository, getMetadataResolver() );
76 catch ( RepositoryException e )
78 // FIXME: a custom exception requires refactoring for callers to handle it
79 throw new RuntimeException( e );
83 // Lazy evaluation to avoid problems with circular dependencies during initialization
84 private MetadataResolver getMetadataResolver()
86 if ( this.metadataResolver == null && applicationContext!=null)
88 this.metadataResolver = applicationContext.getBean( MetadataResolver.class );
90 return this.metadataResolver;
93 protected void initialize()
96 // skip initialisation if not jcr
97 if ( repositorySessionFactoryBean!=null && !StringUtils.equals( repositorySessionFactoryBean.getId(), "jcr" ) )
102 StopWatch stopWatch = new StopWatch();
105 if (applicationContext!=null) {
106 metadataFacetFactories = applicationContext.getBeansOfType(MetadataFacetFactory.class);
108 // olamy with spring the "id" is now "metadataFacetFactory#hint"
109 // whereas was only hint with plexus so let remove metadataFacetFactory#
110 Map<String, MetadataFacetFactory> cleanedMetadataFacetFactories =
111 new HashMap<>( metadataFacetFactories.size() );
113 for ( Map.Entry<String, MetadataFacetFactory> entry : metadataFacetFactories.entrySet() )
115 if (entry.getKey().contains("#")) {
116 cleanedMetadataFacetFactories.put( StringUtils.substringAfterLast( entry.getKey(), "#" ),
120 cleanedMetadataFacetFactories.put(entry.getKey(), entry.getValue());
124 metadataFacetFactories = cleanedMetadataFacetFactories;
129 repositoryFactory = new OakRepositoryFactory();
130 // FIXME this need to be configurable
131 Path directoryPath = Paths.get( System.getProperty( "appserver.base" ), "data/jcr" );
132 repositoryFactory.setRepositoryPath( directoryPath );
134 repository = repositoryFactory.createRepository();
135 } catch (InvalidFileStoreVersionException | IOException e) {
136 logger.error("Repository creation failed {}", e.getMessage());
137 throw new RuntimeException("Fatal error. Could not create metadata repository.");
139 jcrMetadataRepository = new JcrMetadataRepository( metadataFacetFactories, repository );
140 try ( JcrRepositorySession session = new JcrRepositorySession( jcrMetadataRepository, metadataResolver )) {
141 JcrMetadataRepository.initializeNodeTypes( session.getJcrSession() );
142 // Saves automatically with close
145 catch ( RepositoryException e )
147 throw new RuntimeException( e.getMessage(), e );
151 logger.info( "time to initialize JcrRepositorySessionFactory: {}", stopWatch.getTime() );
155 protected void shutdown() {
156 logger.info( "Shutting down JcrRepositorySessionFactory" );
157 repositoryFactory.close();
166 public void setMetadataResolver(MetadataResolver metadataResolver) {
167 this.metadataResolver = metadataResolver;
170 public JcrMetadataRepository getMetadataRepository() {
171 return jcrMetadataRepository;
174 public void setMetadataFacetFactories(Map<String, MetadataFacetFactory> metadataFacetFactories) {
175 this.metadataFacetFactories = metadataFacetFactories;