This project has retired. For details please refer to its Attic page.
DefaultPluginsServices xref
View Javadoc
1   package org.apache.archiva.rest.services;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
22  import org.apache.archiva.rest.api.services.PluginsService;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.core.io.Resource;
27  import org.springframework.stereotype.Service;
28  
29  import javax.inject.Inject;
30  import java.io.IOException;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  
35  /**
36   * @author Eric Barboni
37   * @since 1.4.0
38   */
39  @Service( "pluginsService#rest" )
40  public class DefaultPluginsServices
41      implements PluginsService
42  {
43  
44      private List<String> repositoryType = new ArrayList<>();
45  
46      private List<String> adminFeatures = new ArrayList<>();
47  
48      private ApplicationContext applicationContext;
49  
50      private Logger log = LoggerFactory.getLogger( getClass() );
51  
52      private String adminPlugins;
53  
54      @Inject
55      public DefaultPluginsServices( ApplicationContext applicationContext )
56          throws IOException
57      {
58          log.debug( "init DefaultPluginsServices" );
59          this.applicationContext = applicationContext;
60  
61          // rebuild
62          repositoryType = feed( "repository" );
63          log.debug( "feed {}:{}", "repository" , repositoryType);
64          adminFeatures = feed( "features" );
65          log.debug( "feed {}:{}", "features", adminFeatures );
66          StringBuilder sb = new StringBuilder();
67          for ( String repoType : repositoryType )
68          {
69              sb.append( repoType ).append( "|" );
70          }
71          for ( String repoType : adminFeatures )
72          {
73              sb.append( repoType ).append( "|" );
74          }
75          log.debug( "getAdminPlugins: {}", sb );
76          if ( sb.length() > 1 )
77          {
78              adminPlugins = sb.substring( 0, sb.length() - 1 );
79          }
80          else
81          {
82              adminPlugins = sb.toString();
83          }
84      }
85  
86      private List<String> feed( String key )
87          throws IOException
88      {
89          log.info( "Feeding: {}", key );
90          Resource[] xmlResources = applicationContext.getResources( "/**/" + key + "/**/main.js" );
91          if (xmlResources == null)
92          {
93              return Collections.emptyList();
94          }
95          List<String> repository = new ArrayList<>( xmlResources.length );
96          for ( Resource rc : xmlResources )
97          {
98              String tmp = rc.getURL().toString();
99              tmp = tmp.substring( tmp.lastIndexOf( key ) + key.length() + 1, tmp.length() - 8 );
100             repository.add( "archiva/admin/" + key + "/" + tmp + "/main" );
101         }
102         return repository;
103     }
104 
105     @Override
106     public String getAdminPlugins()
107         throws ArchivaRestServiceException
108     {
109         return adminPlugins;
110     }
111 }