1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.bull.javamelody.internal.web;
19
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24
25 import net.bull.javamelody.Parameter;
26 import net.bull.javamelody.internal.common.I18N;
27 import net.bull.javamelody.internal.model.Collector;
28 import net.bull.javamelody.internal.model.CollectorServer;
29 import net.bull.javamelody.internal.model.Range;
30
31 /**
32  * JNLP pour lancer l'ihm Swing avec JavaWebStart.
33  * @author Emeric Vernat
34  */

35 public class JnlpPage {
36     public static final String JNLP_PREFIX = "jnlp.";
37     private final Collector collector;
38     private final CollectorServer collectorServer;
39     private final String codebase;
40     private final String cookies;
41     private final Range range;
42     private final Writer writer;
43
44     JnlpPage(Collector collector, CollectorServer collectorServer, String codebase, String cookies,
45             Range range, Writer writer) {
46         super();
47         this.collector = collector;
48         this.collectorServer = collectorServer;
49         this.codebase = codebase;
50         this.cookies = cookies;
51         this.range = range;
52         this.writer = writer;
53     }
54
55     void toJnlp() throws IOException {
56         println("<jnlp spec='1.0+' codebase='" + codebase + "'>");
57         println("   <information>");
58         println("      <title>JavaMelody</title>");
59         println("      <vendor>JavaMelody</vendor>");
60         println("      <description>Monitoring</description>");
61         println("      <icon href='" + codebase + "?resource=systemmonitor.png'/>");
62         println("      <offline-allowed />");
63         println("   </information>");
64         println("   <security> <all-permissions/> </security>");
65         println("   <update check='always' policy='always'/>");
66         println("   <resources>");
67         println("      <j2se version='1.7+' max-heap-size='300m'/>");
68         final String jarFileUrl = getJarFileUrl();
69         println("      <jar href='" + jarFileUrl + "' />");
70         final Map<String, Object> properties = new LinkedHashMap<>();
71         properties.put("javamelody.application", collector.getApplication());
72         properties.put("javamelody.collectorServer", collectorServer != null);
73         final String url;
74         if (collectorServer == null) {
75             url = codebase + "?format=serialized";
76         } else {
77             url = codebase + "?format=serialized&application=" + collector.getApplication();
78         }
79         properties.put("javamelody.url", url);
80         properties.put("javamelody.range", range.getValue());
81         properties.put("javamelody.locale", I18N.getCurrentLocale());
82         // les valeurs des paramètres sont importantes notamment pour :
83         // WARNING_THRESHOLD_MILLIS, SEVERE_THRESHOLD_MILLIS, SYSTEM_ACTIONS_ENABLED et NO_DATABASE
84         for (final Parameter parameter : Parameter.values()) {
85             if (parameter.getValue() != null && parameter != Parameter.ADMIN_EMAILS) {
86                 properties.put("javamelody." + parameter.getCode(), parameter.getValue());
87             }
88         }
89         if (cookies != null) {
90             properties.put("cookies", cookies);
91         }
92         // JNLP_PREFIX to fix:
93         // http://stackoverflow.com/questions/19400725/with-java-update-7-45-the-system-properties-no-more-set-from-jnlp-tag-property
94         // https://bugs.openjdk.java.net/browse/JDK-8023821
95         for (final Map.Entry<String, Object> entry : properties.entrySet()) {
96             println("      <property name='" + JNLP_PREFIX + entry.getKey() + "' value='"
97                     + entry.getValue() + "'/>");
98         }
99         println("   </resources>");
100         println("   <application-desc main-class='net.bull.javamelody.Main' />");
101         println("</jnlp>");
102     }
103
104     private void println(String string) throws IOException {
105         writer.write(string);
106         writer.write('\n');
107     }
108
109     private static String getJarFileUrl() {
110         final String jarFileUrl;
111         if (Parameter.JAVAMELODY_SWING_URL.getValue() != null) {
112             jarFileUrl = Parameter.JAVAMELODY_SWING_URL.getValue();
113             //        } else if (Parameters.JAVAMELODY_VERSION != null) {
114             //            jarFileUrl = "https://github.com/javamelody/javamelody/releases/download/javamelody-core-1.49.0/javamelody-swing-"
115             //                    + Parameters.JAVAMELODY_VERSION + ".jar";
116         } else {
117             // TODO publish newer javamelody-swing files on github and review code above
118             jarFileUrl = "https://github.com/javamelody/javamelody/releases/download/javamelody-core-1.49.0/javamelody-swing-1.88.0.jar";
119         }
120         return jarFileUrl;
121     }
122 }
123