001package org.apache.archiva.metadata.repository.jcr; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.metadata.model.MetadataFacetFactory; 023import org.apache.archiva.metadata.repository.MetadataRepository; 024import org.apache.archiva.metadata.repository.MetadataResolver; 025import org.apache.archiva.metadata.repository.RepositorySession; 026import org.apache.archiva.metadata.repository.RepositorySessionFactory; 027import org.apache.archiva.metadata.repository.RepositorySessionFactoryBean; 028import org.apache.commons.lang.StringUtils; 029import org.apache.commons.lang.time.StopWatch; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import org.springframework.context.ApplicationContext; 033import org.springframework.stereotype.Service; 034 035import javax.annotation.PostConstruct; 036import javax.inject.Inject; 037import javax.jcr.Repository; 038import javax.jcr.RepositoryException; 039import java.util.HashMap; 040import java.util.Map; 041 042/** 043 * 044 */ 045@Service("repositorySessionFactory#jcr") 046public class JcrRepositorySessionFactory 047 implements RepositorySessionFactory 048{ 049 050 private Logger logger = LoggerFactory.getLogger( getClass() ); 051 052 @Inject 053 private ApplicationContext applicationContext; 054 055 private Map<String, MetadataFacetFactory> metadataFacetFactories; 056 057 @Inject 058 private Repository repository; 059 060 @Inject 061 private MetadataResolver metadataResolver; 062 063 @Inject 064 private RepositorySessionFactoryBean repositorySessionFactoryBean; 065 066 @Override 067 public RepositorySession createSession() 068 { 069 try 070 { 071 // FIXME: is this the right separation? or should a JCR session object contain the JCR session information? 072 // such a change might allow us to avoid creating two objects for each request. It would also clear up 073 // the ambiguities in the API where session & repository are the inverse of JCR; and the resolver is 074 // retrieved from the session but must have it passed in. These should be reviewed before finalising the 075 // API. 076 MetadataRepository metadataRepository = new JcrMetadataRepository( metadataFacetFactories, repository ); 077 078 return new RepositorySession( metadataRepository, metadataResolver ); 079 } 080 catch ( RepositoryException e ) 081 { 082 // FIXME: a custom exception requires refactoring for callers to handle it 083 throw new RuntimeException( e ); 084 } 085 } 086 087 @PostConstruct 088 public void initialize() 089 throws Exception 090 { 091 092 // skip initialisation if not cassandra 093 if ( !StringUtils.equals( repositorySessionFactoryBean.getId(), "jcr" ) ) 094 { 095 return; 096 } 097 098 StopWatch stopWatch = new StopWatch(); 099 stopWatch.start(); 100 101 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 = 105 new HashMap<>( metadataFacetFactories.size() ); 106 107 for ( Map.Entry<String, MetadataFacetFactory> entry : metadataFacetFactories.entrySet() ) 108 { 109 cleanedMetadataFacetFactories.put( StringUtils.substringAfterLast( entry.getKey(), "#" ), 110 entry.getValue() ); 111 } 112 113 metadataFacetFactories = cleanedMetadataFacetFactories; 114 115 JcrMetadataRepository metadataRepository = null; 116 try 117 { 118 metadataRepository = new JcrMetadataRepository( metadataFacetFactories, repository ); 119 JcrMetadataRepository.initialize( metadataRepository.getJcrSession() ); 120 } 121 catch ( RepositoryException e ) 122 { 123 throw new RuntimeException( e.getMessage(), e ); 124 } 125 finally 126 { 127 if ( metadataRepository != null ) 128 { 129 metadataRepository.close(); 130 } 131 } 132 133 stopWatch.stop(); 134 logger.info( "time to initialize JcrRepositorySessionFactory: {}", stopWatch.getTime() ); 135 } 136}