This project has retired. For details please refer to its
Attic page.
JcrRepositorySessionFactory xref
1 package org.apache.archiva.metadata.repository.jcr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.metadata.model.MetadataFacet;
23 import org.apache.archiva.metadata.model.MetadataFacetFactory;
24 import org.apache.archiva.metadata.repository.*;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.commons.lang3.time.StopWatch;
27 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.stereotype.Service;
32
33 import javax.annotation.PreDestroy;
34 import javax.inject.Inject;
35 import javax.jcr.Repository;
36 import javax.jcr.RepositoryException;
37 import java.io.IOException;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.HashMap;
41 import java.util.Map;
42
43
44
45
46 @Service( "repositorySessionFactory#jcr" )
47 public class JcrRepositorySessionFactory extends AbstractRepositorySessionFactory
48 implements RepositorySessionFactory
49 {
50
51 private Logger logger = LoggerFactory.getLogger( getClass() );
52
53 private Repository repository;
54
55
56 private MetadataResolver metadataResolver;
57
58 @Inject
59 private RepositorySessionFactoryBean repositorySessionFactoryBean;
60
61 @Inject
62 private MetadataService metadataService;
63
64 private OakRepositoryFactory repositoryFactory;
65
66 private JcrMetadataRepository jcrMetadataRepository;
67
68 @Override
69 public RepositorySession createSession() throws MetadataRepositoryException
70 {
71 try
72 {
73 return new JcrRepositorySession( jcrMetadataRepository, getMetadataResolver() );
74 }
75 catch ( RepositoryException e )
76 {
77
78 throw new RuntimeException( e );
79 }
80 }
81
82 private MetadataResolver getMetadataResolver() {
83 return metadataService.getMetadataResolver( );
84 }
85
86 protected void initialize()
87 {
88
89
90 if ( repositorySessionFactoryBean!=null && !StringUtils.equals( repositorySessionFactoryBean.getId(), "jcr" ) )
91 {
92 return;
93 }
94
95 StopWatch stopWatch = new StopWatch();
96 stopWatch.start();
97
98 try
99 {
100
101 repositoryFactory = new OakRepositoryFactory();
102
103 Path directoryPath = Paths.get( System.getProperty( "appserver.base" ), "data/jcr" );
104 repositoryFactory.setRepositoryPath( directoryPath );
105 try {
106 repository = repositoryFactory.createRepository();
107 } catch (InvalidFileStoreVersionException | IOException e) {
108 logger.error("Repository creation failed {}", e.getMessage());
109 throw new RuntimeException("Fatal error. Could not create metadata repository.");
110 }
111 jcrMetadataRepository = new JcrMetadataRepository( metadataService, repository );
112 try ( JcrRepositorySessiony/jcr/JcrRepositorySession.html#JcrRepositorySession">JcrRepositorySession session = new JcrRepositorySession( jcrMetadataRepository, metadataResolver )) {
113 JcrMetadataRepository.initializeNodeTypes( session.getJcrSession() );
114
115 }
116 }
117 catch ( RepositoryException e )
118 {
119 throw new RuntimeException( e.getMessage(), e );
120 }
121
122 stopWatch.stop();
123 logger.info( "time to initialize JcrRepositorySessionFactory: {}", stopWatch.getTime() );
124 }
125
126 @Override
127 protected void shutdown() {
128 logger.info( "Shutting down JcrRepositorySessionFactory" );
129 repositoryFactory.close();
130 }
131
132 @PreDestroy
133 public void close()
134 {
135 super.close();
136 }
137
138 public void setMetadataResolver(MetadataResolver metadataResolver) {
139 this.metadataResolver = metadataResolver;
140 }
141
142 public JcrMetadataRepository getMetadataRepository() {
143 return jcrMetadataRepository;
144 }
145
146
147 public MetadataService getMetadataService( )
148 {
149 return metadataService;
150 }
151
152 public void setMetadataService( MetadataService metadataService )
153 {
154 this.metadataService = metadataService;
155 }
156 }