View Javadoc
1   package org.codehaus.mojo.webstart.dependency;
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.maven.artifact.Artifact;
23  import org.apache.maven.shared.jarsigner.JarSignerUtil;
24  import org.codehaus.mojo.webstart.dependency.task.JnlpDependencyTask;
25  import org.codehaus.mojo.webstart.dependency.task.Pack200Task;
26  import org.codehaus.mojo.webstart.dependency.task.SignTask;
27  import org.codehaus.mojo.webstart.dependency.task.UnPack200Task;
28  import org.codehaus.mojo.webstart.dependency.task.UnsignTask;
29  import org.codehaus.mojo.webstart.dependency.task.UpdateManifestTask;
30  import org.codehaus.plexus.PlexusConstants;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
34  import org.codehaus.plexus.context.Context;
35  import org.codehaus.plexus.context.ContextException;
36  import org.codehaus.plexus.logging.AbstractLogEnabled;
37  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
38  
39  import java.io.IOException;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * To create some {@link JnlpDependencyRequest}.
45   * Created on 1/4/14.
46   *
47   * @author Tony Chemit <chemit@codelutin.com>
48   * @since 1.0-beta-5
49   */
50  @Component( role = JnlpDependencyRequestBuilder.class, instantiationStrategy = "per-lookup" )
51  public class DefaultJnlpDependencyRequestBuilder
52      extends AbstractLogEnabled
53      implements JnlpDependencyRequestBuilder, Contextualizable
54  {
55  
56      private PlexusContainer container;
57  
58      private JnlpDependencyGlobalConfig globalConfig;
59  
60      /**
61       * {@inheritDoc}
62       */
63      public void init( JnlpDependencyGlobalConfig globalConfig )
64      {
65          if ( globalConfig == null )
66          {
67              throw new NullPointerException( "Can't use a null *globalConfig*" );
68          }
69          this.globalConfig = globalConfig;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public JnlpDependencyRequests createRequests()
76      {
77          return new JnlpDependencyRequests( globalConfig );
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      public JnlpDependencyRequest createRequest( Artifact artifact, boolean outputJarVersion, boolean useUniqueVersions )
84      {
85  
86          if ( globalConfig == null )
87          {
88              throw new IllegalStateException( "No config found, use init method before creating a request" );
89          }
90  
91          JnlpDependencyConfig config = createConfig( artifact, outputJarVersion, useUniqueVersions );
92  
93          JnlpDependencyTask[] tasks = createTasks( config );
94  
95          JnlpDependencyRequest request = new JnlpDependencyRequest( config, tasks );
96          return request;
97      }
98  
99      private JnlpDependencyConfig createConfig( Artifact artifact, boolean outputJarVersion, boolean useUniqueVersions )
100     {
101 
102         String finalName = globalConfig.getDependencyFilenameStrategy().getDependencyFileBasename( artifact, false, false );
103 
104         return new JnlpDependencyConfig( globalConfig, artifact, finalName, outputJarVersion, useUniqueVersions );
105     }
106 
107     private JnlpDependencyTask[] createTasks( JnlpDependencyConfig config )
108     {
109         List<JnlpDependencyTask> tasks = new ArrayList<JnlpDependencyTask>();
110 
111         boolean doPack200 = config.isPack200();
112 
113         if ( config.isSign() )
114         {
115 
116             if ( config.isUnsignAlreadySignedJars() )
117             {
118 
119                 boolean signed;
120                 try
121                 {
122                     signed = JarSignerUtil.isArchiveSigned( config.getArtifact().getFile() );
123                 }
124                 catch ( IOException e )
125                 {
126                     throw new RuntimeException( "Could not check if jar is signed", e );
127                 }
128 
129                 if ( signed && config.isCanUnsign() )
130                 {
131 
132                     // unsign
133                     registerTask( tasks, UnsignTask.ROLE_HINT, config );
134                 }
135             }
136 
137             if ( doPack200 )
138             {
139 
140                 // http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/pack200.html
141                 // we need to pack then unpack the files before signing them
142 
143                 // pack200
144                 registerTask( tasks, Pack200Task.ROLE_HINT, config );
145 
146                 // unpack200
147                 registerTask( tasks, UnPack200Task.ROLE_HINT, config );
148             }
149 
150             if ( config.isUpdateManifest() )
151             {
152                 // update manifest
153                 registerTask( tasks, UpdateManifestTask.ROLE_HINT, config );
154             }
155 
156             // sign jar
157             registerTask( tasks, SignTask.ROLE_HINT, config );
158         }
159 
160         if ( doPack200 )
161         {
162 
163             // pack signed jar
164             registerTask( tasks, Pack200Task.ROLE_HINT, config );
165         }
166 
167         return tasks.toArray( new JnlpDependencyTask[tasks.size()] );
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     public void contextualize( final Context context )
174         throws ContextException
175     {
176         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
177     }
178 
179     protected void setContainer( final PlexusContainer container )
180     {
181         this.container = container;
182     }
183 
184     protected <Task extends JnlpDependencyTask> Task registerTask( List<JnlpDependencyTask> tasks, String roleHint,
185                                                                    JnlpDependencyConfig config )
186     {
187         try
188         {
189             // create task
190             Task result = (Task) container.lookup( JnlpDependencyTask.ROLE, roleHint );
191 
192             // check configution
193             result.check( config );
194 
195             // register task
196             tasks.add( result );
197 
198             return result;
199         }
200         catch ( ComponentLookupException e )
201         {
202             throw new RuntimeException( "Could not find task with roleHint: " + roleHint, e );
203         }
204     }
205 }