View Javadoc
1   package org.codehaus.mojo.webstart.generator;
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.plugin.logging.Log;
24  import org.apache.velocity.VelocityContext;
25  import org.codehaus.mojo.webstart.JnlpExtension;
26  
27  import java.util.Collection;
28  import java.util.List;
29  
30  /**
31   * Generates a JNLP deployment descriptor
32   *
33   * @author ngc
34   * @author <a href="jerome@coffeebreaks.org">Jerome Lacoste</a>
35   */
36  public class Generator
37      extends AbstractGenerator<GeneratorConfig>
38  {
39  
40      public Generator( Log log, GeneratorTechnicalConfig technicalConfig, GeneratorConfig extraConfig )
41      {
42          super( log, technicalConfig, extraConfig );
43      }
44  
45      protected String getArgumentsText()
46      {
47          return "";
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      protected String getDependenciesText()
54      {
55          return indentText( 4, getDependenciesText( getExtraConfig() ) );
56      }
57  
58      protected VelocityContext createAndPopulateContext()
59      {
60          VelocityContext context = super.createAndPopulateContext();
61          if ( getExtraConfig().hasJnlpExtensions() )
62          {
63              // add extensions
64              context.put( "extensions", indentText( 4, getExtensionsText( getExtraConfig() ) ) );
65          } else {
66              context.put( "extensions", "" );
67          }
68          return context;
69      }
70  
71      static String getDependenciesText( GeneratorConfig config )
72      {
73          return getDependenciesText( config, config.getPackagedJnlpArtifacts() );
74      }
75  
76      static String getDependenciesText( GeneratorExtraConfigWithDeps config, Collection<Artifact> artifacts )
77      {
78          String dependenciesText = "";
79          if ( !artifacts.isEmpty() )
80          {
81              StringBuilder buffer = new StringBuilder( 100 * artifacts.size() );
82              buffer.append( EOL );
83              if ( config.isPack200() )
84              {
85                  /*
86                   * http://jira.codehaus.org/browse/MWEBSTART-174
87                   *
88                   * If we're going to use Pack200, we should specify jnlp.packEnabled
89                   *
90                   */
91                  buffer.append( "<property name=\"jnlp.packEnabled\" value=\"true\" />" ).append( EOL );
92              }
93              if ( config.isOutputJarVersions() )
94              {
95                  /*
96                   * http://jira.codehaus.org/browse/MWEBSTART-221
97                   *
98                   * If we're going to use version files, we should specify jnlp.versionEnabled
99                   *
100                  */
101                 buffer.append( "<property name=\"jnlp.versionEnabled\" value=\"true\" />" ).append( EOL );
102             }
103             String jarLibPath = null;
104             if ( config.getLibPath() != null )
105             {
106                 jarLibPath = config.getLibPath();
107                 jarLibPath = ( jarLibPath != null && jarLibPath.trim().length() != 0 ) ? jarLibPath.trim() : null;
108             }
109 
110             for ( Artifact artifact : artifacts )
111             {
112                 buffer.append( "<jar href=\"" );
113                 if ( jarLibPath != null )
114                 {
115                     buffer.append( jarLibPath ).append( "/" );
116                 }
117 
118 //                    String filename = artifact.getFile().getName();
119                 if ( config.isOutputJarVersions() )
120                 {
121                     String filename = config.getDependencyFilename( artifact, null, config.isUseUniqueVersions() );
122 //                      String extension = filename.substring( filename.lastIndexOf( "." ) );
123 //                    buffer.append( artifact.getArtifactId() ).append( extension ).append( "\"" );
124                     buffer.append( filename ).append( "\"" );
125                     if (config.isUseUniqueVersions()) 
126                     {
127                     	buffer.append( " version=\"" ).append( artifact.getBaseVersion() ).append( "\"" );
128                     }
129                     else 
130                     {
131                     	buffer.append( " version=\"" ).append( artifact.getVersion() ).append( "\"" );
132                     }
133                 }
134                 else
135                 {
136                     String filename = config.getDependencyFilename( artifact, false, config.isUseUniqueVersions() );
137                     buffer.append( filename ).append( "\"" );
138                 }
139 
140                 if ( config.isArtifactWithMainClass( artifact ) )
141                 {
142                     buffer.append( " main=\"true\"" );
143                 }
144                 buffer.append( "/>" ).append( EOL );
145             }
146             dependenciesText = buffer.toString();
147         }
148         return dependenciesText;
149     }
150 
151     static String getExtensionsText( GeneratorConfig config )
152     {
153         String text = "";
154         List<JnlpExtension> extensions = config.getJnlpExtensions();
155         if ( extensions != null && !extensions.isEmpty() )
156         {
157             StringBuilder buffer = new StringBuilder( 100 * extensions.size() );
158             buffer.append( "\n" );
159 
160             for ( JnlpExtension extension : extensions )
161             {
162                 buffer.append( "<extension name=\"" );
163                 buffer.append( extension.getName() );
164                 buffer.append( "\" href=\"" );
165                 buffer.append( extension.getOutputFile() );
166                 buffer.append( "\"/>" ).append( EOL );
167             }
168             text = buffer.toString();
169         }
170         return text;
171     }
172 }