1package org.apache.archiva.metadata.repository.jcr;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.metadata.model.MetadataFacetFactory;
23import org.apache.archiva.metadata.repository.MetadataRepository;
24import org.apache.archiva.metadata.repository.MetadataResolver;
25import org.apache.archiva.metadata.repository.RepositorySession;
26import org.apache.archiva.metadata.repository.RepositorySessionFactory;
27import org.apache.archiva.metadata.repository.RepositorySessionFactoryBean;
28import org.apache.commons.lang.StringUtils;
29import org.apache.commons.lang.time.StopWatch;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32import org.springframework.context.ApplicationContext;
33import org.springframework.stereotype.Service;
3435import javax.annotation.PostConstruct;
36import javax.inject.Inject;
37import javax.jcr.Repository;
38import javax.jcr.RepositoryException;
39import java.util.HashMap;
40import java.util.Map;
4142/**43 *44 */45 @Service("repositorySessionFactory#jcr")
46publicclassJcrRepositorySessionFactory47implementsRepositorySessionFactory48 {
4950private Logger logger = LoggerFactory.getLogger( getClass() );
5152 @Inject
53private ApplicationContext applicationContext;
5455private Map<String, MetadataFacetFactory> metadataFacetFactories;
5657 @Inject
58private Repository repository;
5960 @Inject
61privateMetadataResolver metadataResolver;
6263 @Inject
64privateRepositorySessionFactoryBean repositorySessionFactoryBean;
6566 @Override
67publicRepositorySession createSession()
68 {
69try70 {
71// FIXME: is this the right separation? or should a JCR session object contain the JCR session information?72// such a change might allow us to avoid creating two objects for each request. It would also clear up73// the ambiguities in the API where session & repository are the inverse of JCR; and the resolver is74// retrieved from the session but must have it passed in. These should be reviewed before finalising the75// API.76MetadataRepository metadataRepository = newJcrMetadataRepository( metadataFacetFactories, repository );
7778returnnewRepositorySession( metadataRepository, metadataResolver );
79 }
80catch ( RepositoryException e )
81 {
82// FIXME: a custom exception requires refactoring for callers to handle it83thrownew RuntimeException( e );
84 }
85 }
8687 @PostConstruct
88publicvoid initialize()
89throws Exception
90 {
9192// skip initialisation if not cassandra93if ( !StringUtils.equals( repositorySessionFactoryBean.getId(), "jcr" ) )
94 {
95return;
96 }
9798 StopWatch stopWatch = new StopWatch();
99 stopWatch.start();
100101 metadataFacetFactories = applicationContext.getBeansOfType( MetadataFacetFactory.class );
102// olamy with spring the "id" is now "metadataFacetFactory#hint"103// whereas was only hint with plexus so let remove metadataFacetFactory#104 Map<String, MetadataFacetFactory> cleanedMetadataFacetFactories =
105new HashMap<>( metadataFacetFactories.size() );
106107for ( Map.Entry<String, MetadataFacetFactory> entry : metadataFacetFactories.entrySet() )
108 {
109 cleanedMetadataFacetFactories.put( StringUtils.substringAfterLast( entry.getKey(), "#" ),
110 entry.getValue() );
111 }
112113 metadataFacetFactories = cleanedMetadataFacetFactories;
114115JcrMetadataRepository metadataRepository = null;
116try117 {
118 metadataRepository = newJcrMetadataRepository( metadataFacetFactories, repository );
119 JcrMetadataRepository.initialize( metadataRepository.getJcrSession() );
120 }
121catch ( RepositoryException e )
122 {
123thrownew RuntimeException( e.getMessage(), e );
124 }
125finally126 {
127if ( metadataRepository != null )
128 {
129 metadataRepository.close();
130 }
131 }
132133 stopWatch.stop();
134 logger.info( "time to initialize JcrRepositorySessionFactory: {}", stopWatch.getTime() );
135 }
136 }