View Javadoc
1   /*
2    * Copyright  2001,2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.codehaus.plexus.archiver.manager;
18  
19  import java.io.File;
20  import java.util.Locale;
21  import javax.annotation.Nonnull;
22  import org.codehaus.plexus.PlexusConstants;
23  import org.codehaus.plexus.PlexusContainer;
24  import org.codehaus.plexus.archiver.Archiver;
25  import org.codehaus.plexus.archiver.UnArchiver;
26  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
27  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
28  import org.codehaus.plexus.context.Context;
29  import org.codehaus.plexus.context.ContextException;
30  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * @author dantran
36   */
37  public class DefaultArchiverManager
38      implements ArchiverManager, Contextualizable
39  {
40  
41      private PlexusContainer container;
42  
43      // ----------------------------------------------------------------------
44      // Component Lifecycle
45      // ----------------------------------------------------------------------
46  
47      @Override
48      public void contextualize( Context context )
49          throws ContextException
50      {
51          container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
52      }
53  
54      @Override
55      @Nonnull public Archiver getArchiver( @Nonnull String archiverName )
56          throws NoSuchArchiverException
57      {
58          try
59          {
60              return (Archiver) container.lookup( Archiver.ROLE, archiverName );
61          }
62          catch ( ComponentLookupException e )
63          {
64              throw new NoSuchArchiverException( archiverName );
65          }
66      }
67  
68      @Override
69      @Nonnull public UnArchiver getUnArchiver( @Nonnull String unArchiverName )
70          throws NoSuchArchiverException
71      {
72          try
73          {
74              return (UnArchiver) container.lookup( UnArchiver.ROLE, unArchiverName );
75          }
76          catch ( ComponentLookupException e )
77          {
78              throw new NoSuchArchiverException( unArchiverName );
79          }
80      }
81  
82      @Override
83      public @Nonnull
84      PlexusIoResourceCollection getResourceCollection( String resourceCollectionName )
85          throws NoSuchArchiverException
86      {
87          try
88          {
89              return (PlexusIoResourceCollection) container.lookup( PlexusIoResourceCollection.ROLE,
90                                                                    resourceCollectionName );
91          }
92          catch ( ComponentLookupException e )
93          {
94              throw new NoSuchArchiverException( resourceCollectionName );
95          }
96      }
97  
98      private static @Nonnull
99      String getFileExtention( @Nonnull File file )
100     {
101         String path = file.getAbsolutePath();
102 
103         String archiveExt = FileUtils.getExtension( path ).toLowerCase( Locale.ENGLISH );
104 
105         if ( "gz".equals( archiveExt )
106                  || "bz2".equals( archiveExt )
107                  || "xz".equals( archiveExt )
108                  || "snappy".equals( archiveExt ) )
109         {
110             String[] tokens = StringUtils.split( path, "." );
111 
112             if ( tokens.length > 2 && "tar".equals( tokens[tokens.length - 2].toLowerCase( Locale.ENGLISH ) ) )
113             {
114                 archiveExt = "tar." + archiveExt;
115             }
116         }
117 
118         return archiveExt;
119 
120     }
121 
122     @Override
123     @Nonnull public Archiver getArchiver( @Nonnull File file )
124         throws NoSuchArchiverException
125     {
126         return getArchiver( getFileExtention( file ) );
127     }
128 
129     @Override
130     @Nonnull public UnArchiver getUnArchiver( @Nonnull File file )
131         throws NoSuchArchiverException
132     {
133         return getUnArchiver( getFileExtention( file ) );
134     }
135 
136     @Override
137     @Nonnull public PlexusIoResourceCollection getResourceCollection( @Nonnull File file )
138         throws NoSuchArchiverException
139     {
140         return getResourceCollection( getFileExtention( file ) );
141     }
142 
143 }