This project has retired. For details please refer to its Attic page.
JcrRepositorySessionFactory xref
View Javadoc
1   package org.apache.archiva.metadata.repository.jcr;
2   
3   /*
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
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.metadata.model.MetadataFacetFactory;
23  import org.apache.archiva.metadata.repository.MetadataRepository;
24  import org.apache.archiva.metadata.repository.MetadataResolver;
25  import org.apache.archiva.metadata.repository.RepositorySession;
26  import org.apache.archiva.metadata.repository.RepositorySessionFactory;
27  import org.apache.archiva.metadata.repository.RepositorySessionFactoryBean;
28  import org.apache.commons.lang.StringUtils;
29  import org.apache.commons.lang.time.StopWatch;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.context.ApplicationContext;
33  import org.springframework.stereotype.Service;
34  
35  import javax.annotation.PostConstruct;
36  import javax.inject.Inject;
37  import javax.jcr.Repository;
38  import javax.jcr.RepositoryException;
39  import java.util.HashMap;
40  import java.util.Map;
41  
42  /**
43   *
44   */
45  @Service("repositorySessionFactory#jcr")
46  public class JcrRepositorySessionFactory
47      implements RepositorySessionFactory
48  {
49  
50      private Logger logger = LoggerFactory.getLogger( getClass() );
51  
52      @Inject
53      private ApplicationContext applicationContext;
54  
55      private Map<String, MetadataFacetFactory> metadataFacetFactories;
56  
57      @Inject
58      private Repository repository;
59  
60      @Inject
61      private MetadataResolver metadataResolver;
62  
63      @Inject
64      private RepositorySessionFactoryBean repositorySessionFactoryBean;
65  
66      @Override
67      public RepositorySession createSession()
68      {
69          try
70          {
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 up
73              //  the ambiguities in the API where session & repository are the inverse of JCR; and the resolver is
74              //  retrieved from the session but must have it passed in. These should be reviewed before finalising the
75              //  API.
76              MetadataRepository metadataRepository = new JcrMetadataRepository( metadataFacetFactories, repository );
77  
78              return new RepositorySession( metadataRepository, metadataResolver );
79          }
80          catch ( RepositoryException e )
81          {
82              // FIXME: a custom exception requires refactoring for callers to handle it
83              throw new RuntimeException( e );
84          }
85      }
86  
87      @PostConstruct
88      public void initialize()
89          throws Exception
90      {
91  
92          // skip initialisation if not cassandra
93          if ( !StringUtils.equals( repositorySessionFactoryBean.getId(), "jcr" ) )
94          {
95              return;
96          }
97  
98          StopWatch stopWatch = new StopWatch();
99          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 }