test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JavaAppDesc.java
branchJDK-8200758-branch
changeset 58648 3bf53ffa9ae7
child 58696 61c44899b4eb
equal deleted inserted replaced
58647:2c43b89b1679 58648:3bf53ffa9ae7
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package jdk.jpackage.test;
       
    24 
       
    25 import java.util.Objects;
       
    26 
       
    27 
       
    28 public final class JavaAppDesc {
       
    29     public JavaAppDesc() {
       
    30     }
       
    31 
       
    32     public JavaAppDesc setClassName(String v) {
       
    33         qualifiedClassName = v;
       
    34         return this;
       
    35     }
       
    36 
       
    37     public JavaAppDesc setModuleName(String v) {
       
    38         moduleName = v;
       
    39         return this;
       
    40     }
       
    41 
       
    42     public JavaAppDesc setJarFileName(String v) {
       
    43         jarFileName = v;
       
    44         return this;
       
    45     }
       
    46 
       
    47     public JavaAppDesc setModuleVersion(String v) {
       
    48         moduleVersion = v;
       
    49         return this;
       
    50     }
       
    51 
       
    52     public JavaAppDesc setJarWithMainClass(boolean v) {
       
    53         jarWithMainClass = v;
       
    54         return this;
       
    55     }
       
    56 
       
    57     public String className() {
       
    58         return qualifiedClassName;
       
    59     }
       
    60 
       
    61     public String moduleName() {
       
    62         return moduleName;
       
    63     }
       
    64 
       
    65     public String packageName() {
       
    66         int lastDotIdx = qualifiedClassName.lastIndexOf('.');
       
    67         if (lastDotIdx == -1) {
       
    68             return null;
       
    69         }
       
    70         return qualifiedClassName.substring(0, lastDotIdx);
       
    71     }
       
    72 
       
    73     public String jarFileName() {
       
    74         return jarFileName;
       
    75     }
       
    76 
       
    77     public String moduleVersion() {
       
    78         return moduleVersion;
       
    79     }
       
    80 
       
    81     public boolean jarWithMainClass() {
       
    82         return jarWithMainClass;
       
    83     }
       
    84 
       
    85     @Override
       
    86     public String toString() {
       
    87         StringBuilder sb = new StringBuilder();
       
    88         if (jarFileName != null) {
       
    89             sb.append(jarFileName).append(':');
       
    90         }
       
    91         if (moduleName != null) {
       
    92             sb.append(moduleName).append('/');
       
    93         }
       
    94         if (qualifiedClassName != null) {
       
    95             sb.append(qualifiedClassName);
       
    96         }
       
    97         if (jarWithMainClass) {
       
    98             sb.append('!');
       
    99         }
       
   100         if (moduleVersion != null) {
       
   101             sb.append('@').append(moduleVersion);
       
   102         }
       
   103         return sb.toString();
       
   104     }
       
   105 
       
   106     /**
       
   107      * Create Java application description form encoded string value.
       
   108      *
       
   109      * Syntax of encoded Java application description is
       
   110      * [jar_file:][module_name/]qualified_class_name[!][@module_version].
       
   111      *
       
   112      * E.g.: `duke.jar:com.other/com.other.foo.bar.Buz!@3.7` encodes modular
       
   113      * application. Module name is `com.other`. Main class is
       
   114      * `com.other.foo.bar.Buz`. Module version is `3.7`. Application will be
       
   115      * compiled and packed in `duke.jar` jar file. jar command will set module
       
   116      * version (3.7) and main class (Buz) attributes in the jar file.
       
   117      *
       
   118      * E.g.: `Ciao` encodes non-modular `Ciao` class in the default package.
       
   119      * jar command will not put main class attribute in the jar file.
       
   120      * Default name will be picked for jar file - `hello.jar`.
       
   121      *
       
   122      * @param cmd jpackage command to configure
       
   123      * @param javaAppDesc encoded Java application description
       
   124      */
       
   125     public static JavaAppDesc parse(String javaAppDesc) {
       
   126         JavaAppDesc desc = HelloApp.createDefaltAppDesc();
       
   127 
       
   128         if (javaAppDesc == null) {
       
   129             return desc;
       
   130         }
       
   131 
       
   132         String moduleNameAndOther = Functional.identity(() -> {
       
   133             String[] components = javaAppDesc.split(":", 2);
       
   134             if (components.length == 2) {
       
   135                 desc.setJarFileName(components[0]);
       
   136             }
       
   137             return components[components.length - 1];
       
   138         }).get();
       
   139 
       
   140         String classNameAndOther = Functional.identity(() -> {
       
   141             String[] components = moduleNameAndOther.split("/", 2);
       
   142             if (components.length == 2) {
       
   143                 desc.setModuleName(components[0]);
       
   144             }
       
   145             return components[components.length - 1];
       
   146         }).get();
       
   147 
       
   148         Functional.identity(() -> {
       
   149             String[] components = classNameAndOther.split("@", 2);
       
   150             if (components[0].endsWith("!")) {
       
   151                 components[0] = components[0].substring(0,
       
   152                         components[0].length() - 1);
       
   153                 desc.setJarWithMainClass(true);
       
   154             }
       
   155             desc.setClassName(components[0]);
       
   156             if (components.length == 2) {
       
   157                 desc.setModuleVersion(components[1]);
       
   158             }
       
   159         }).run();
       
   160 
       
   161         return desc;
       
   162     }
       
   163 
       
   164     private String qualifiedClassName;
       
   165     private String moduleName;
       
   166     private String jarFileName;
       
   167     private String moduleVersion;
       
   168     private boolean jarWithMainClass;
       
   169 }