This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.rest.services;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
022import org.apache.archiva.rest.api.services.PluginsService;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.springframework.context.ApplicationContext;
026import org.springframework.core.io.Resource;
027import org.springframework.stereotype.Service;
028
029import javax.inject.Inject;
030import java.io.IOException;
031import java.util.ArrayList;
032import java.util.Collections;
033import java.util.List;
034
035/**
036 * @author Eric Barboni
037 * @since 1.4.0
038 */
039@Service( "pluginsService#rest" )
040public class DefaultPluginsServices
041    implements PluginsService
042{
043
044    private List<String> repositoryType = new ArrayList<>();
045
046    private List<String> adminFeatures = new ArrayList<>();
047
048    private ApplicationContext applicationContext;
049
050    private Logger log = LoggerFactory.getLogger( getClass() );
051
052    private String adminPlugins;
053
054    @Inject
055    public DefaultPluginsServices( ApplicationContext applicationContext )
056        throws IOException
057    {
058        log.debug( "init DefaultPluginsServices" );
059        this.applicationContext = applicationContext;
060
061        // rebuild
062        repositoryType = feed( "repository" );
063        log.debug( "feed {}:{}", "repository" , repositoryType);
064        adminFeatures = feed( "features" );
065        log.debug( "feed {}:{}", "features", adminFeatures );
066        StringBuilder sb = new StringBuilder();
067        for ( String repoType : repositoryType )
068        {
069            sb.append( repoType ).append( "|" );
070        }
071        for ( String repoType : adminFeatures )
072        {
073            sb.append( repoType ).append( "|" );
074        }
075        log.debug( "getAdminPlugins: {}", sb );
076        if ( sb.length() > 1 )
077        {
078            adminPlugins = sb.substring( 0, sb.length() - 1 );
079        }
080        else
081        {
082            adminPlugins = sb.toString();
083        }
084    }
085
086    private List<String> feed( String key )
087        throws IOException
088    {
089        log.info( "Feeding: {}", key );
090        Resource[] xmlResources = applicationContext.getResources( "/**/" + key + "/**/main.js" );
091        if (xmlResources == null)
092        {
093            return Collections.emptyList();
094        }
095        List<String> repository = new ArrayList<>( xmlResources.length );
096        for ( Resource rc : xmlResources )
097        {
098            String tmp = rc.getURL().toString();
099            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}