View Javadoc
1   package org.codehaus.plexus.archiver.manager;
2   
3   /*
4    * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés.
5    * Author(s) : Franck Bonin, Neticoa SAS France
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * 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, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   *
19   */
20  
21  
22  import java.io.File;
23  import java.util.Locale;
24  
25  //import javax.annotation.Nonnull;
26  
27  import org.codehaus.plexus.PlexusConstants;
28  import org.codehaus.plexus.PlexusContainer;
29  import org.codehaus.plexus.archiver.ArchiveContentLister;
30  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
31  //import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
32  import org.codehaus.plexus.context.Context;
33  import org.codehaus.plexus.context.ContextException;
34  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  /**
39   * @author Franck Bonin
40   * @version $Revision$ $Date$
41   */
42  
43  public class DefaultArchiveContentListerManager
44      implements ArchiveContentListerManager, Contextualizable
45  {
46      private PlexusContainer container;
47  
48      // ----------------------------------------------------------------------
49      // Component Lifecycle
50      // ----------------------------------------------------------------------
51  
52      public void contextualize( Context context )
53          throws ContextException
54      {
55          container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
56      }
57      
58      public ArchiveContentLister getArchiveContentLister( String archiveContentListerName )
59          throws org.codehaus.plexus.archiver.manager.NoSuchArchiverException
60      {
61          try
62          {
63              return (ArchiveContentLister) container.lookup( ArchiveContentLister.ROLE, archiveContentListerName );
64          }
65          catch ( ComponentLookupException e )
66          {
67              throw new org.codehaus.plexus.archiver.manager.NoSuchArchiverException( archiveContentListerName );
68          }
69      }
70          
71      public ArchiveContentLister getArchiveContentLister( File file )
72          throws org.codehaus.plexus.archiver.manager.NoSuchArchiverException
73      {
74          return getArchiveContentLister( getFileExtention( file ) );
75      }
76      
77  /*
78      @Nonnull public Archiver getArchiver( @Nonnull String archiverName )
79          throws NoSuchArchiverException
80      {
81          try
82          {
83              return (Archiver) container.lookup( Archiver.ROLE, archiverName );
84          }
85          catch ( ComponentLookupException e )
86          {
87              throw new NoSuchArchiverException( archiverName );
88          }
89      }
90  
91      @Nonnull public UnArchiver getUnArchiver( @Nonnull String unArchiverName )
92          throws NoSuchArchiverException
93      {
94          try
95          {
96              return (UnArchiver) container.lookup( UnArchiver.ROLE, unArchiverName );
97          }
98          catch ( ComponentLookupException e )
99          {
100             throw new NoSuchArchiverException( unArchiverName );
101         }
102     }
103 
104 
105     public @Nonnull PlexusIoResourceCollection getResourceCollection( String resourceCollectionName )
106         throws NoSuchArchiverException
107     {
108         try
109         {
110             return (PlexusIoResourceCollection) container.lookup( PlexusIoResourceCollection.ROLE,
111                 resourceCollectionName );
112         }
113         catch ( ComponentLookupException e )
114         {
115             throw new NoSuchArchiverException( resourceCollectionName );
116         }
117     }    
118     */
119     private static String getFileExtention ( File file )
120     {
121         String path = file.getAbsolutePath();
122         
123         String archiveExt = FileUtils.getExtension( path ).toLowerCase( Locale.ENGLISH );
124         
125         if ( "gz".equals( archiveExt ) || "bz2".equals( archiveExt ) )
126         {
127             String [] tokens = StringUtils.split( path, "." );
128             
129             if ( tokens.length > 2  && "tar".equals( tokens[tokens.length - 2].toLowerCase( Locale.ENGLISH ) ) )
130             {
131                 archiveExt = "tar." + archiveExt;
132             }
133         }
134         return archiveExt;
135     }
136     /*
137     @Nonnull public Archiver getArchiver( @Nonnull File file )
138         throws NoSuchArchiverException
139     {
140         return getArchiver( getFileExtention( file ) );
141     }
142     
143     @Nonnull public UnArchiver getUnArchiver( @Nonnull File file )
144         throws NoSuchArchiverException
145     {        
146         return getUnArchiver( getFileExtention( file ) );
147     }
148 
149     @Nonnull public PlexusIoResourceCollection getResourceCollection( @Nonnull File file )
150         throws NoSuchArchiverException
151     {
152         return getResourceCollection( getFileExtention( file ) );
153     }*/
154 }