forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDescriptor.java
More file actions
289 lines (267 loc) · 8.66 KB
/
AppDescriptor.java
File metadata and controls
289 lines (267 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package act.internal.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import org.osgl.$;
import org.osgl.logging.Logger;
import org.osgl.util.E;
import org.osgl.util.S;
import osgl.version.Version;
import java.io.*;
/**
* Describe an application's name, package and version
*/
public class AppDescriptor implements Serializable {
private static final Logger LOGGER = Act.LOGGER;
private String appName;
private String packageName;
private Version version;
/**
* Construct an `AppVersion` with name and version
*
* @param appName
* the app name
* @param version
* the app version
*/
private AppDescriptor(String appName, String packageName, Version version) {
if (appName.startsWith("${")) {
LOGGER.warn("app name is substitute variable - fallback to default app name");
appName = S.afterLast(packageName, ".");
}
this.appName = appName;
this.packageName = packageName;
this.version = version;
}
/**
* Returns the app name
* @return
* the app name
*/
public String getAppName() {
return appName;
}
/**
* Returns the package name
* @return
* the package name
*/
public String getPackageName() {
return packageName;
}
/**
* Returns the app version
* @return
* the app version
*/
public Version getVersion() {
return version;
}
/**
* Check if this `AppVersion` data is valid.
*
* An `AppVersion` is considered to be valid if and only if
* the {@link #getVersion()} is **not** {@link Version#isUnknown() unknown}
*
* @return
* `true` if this `AppVersion` is valid or `false` otherwise
*/
public boolean isValid() {
return !version.isUnknown();
}
/**
* Serialize this `AppDescriptor` and output byte array
*
* @return
* serialize this `AppDescriptor` into byte array
*/
public byte[] toByteArray() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
return baos.toByteArray();
} catch (IOException e) {
throw E.ioException(e);
}
}
// - factory methods
/**
* Create an `AppDescriptor` with appName, package name and app version.
*
* If `appName` is `null` or blank, it will try the following
* approach to get app name:
*
* 1. check the {@link Version#getArtifactId() artifact id} and use it unless
* 2. if artifact id is null or empty, then infer app name using {@link AppNameInferer}
*
* @param appName
* the app name, optional
* @param packageName
* the package name
* @param appVersion
* the app version
* @return
* an `AppDescriptor`
*/
public static AppDescriptor of(String appName, String packageName, Version appVersion) {
String[] packages = packageName.split(S.COMMON_SEP);
String effectPackageName = packageName;
if (packages.length > 0) {
effectPackageName = packages[0];
}
E.illegalArgumentIf(!JavaNames.isPackageOrClassName(effectPackageName),
"valid package name expected. found: " + effectPackageName);
return new AppDescriptor(ensureAppName(appName, effectPackageName, $.requireNotNull(appVersion)),
packageName,
appVersion);
}
/**
* Create an `AppDescriptor` with appName, entry class and app version.
*
* If `appName` is `null` or blank, it will try the following
* approach to get app name:
*
* 1. check the {@link Version#getArtifactId() artifact id} and use it unless
* 2. if artifact id is null or empty, then infer app name using {@link AppNameInferer}
*
* @param appName
* the app name, optional
* @param entryClass
* the entry class
* @param appVersion
* the app version
* @return
* an `AppDescriptor`
*/
public static AppDescriptor of(String appName, Class<?> entryClass, Version appVersion) {
return new AppDescriptor(ensureAppName(appName, entryClass, $.requireNotNull(appVersion)),
JavaNames.packageNameOf(entryClass),
appVersion);
}
/**
* Create an `AppDescriptor` with appName and entry class specified.
*
* If `appName` is `null` or blank, it will try the following
* approach to get app name:
*
* 1. check the {@link Version#getArtifactId() artifact id} and use it unless
* 2. if artifact id is null or empty, then infer app name using {@link AppNameInferer}
*
* @param appName
* the app name
* @param entryClass
* the entry class
* @return
* an `AppDescriptor` instance
*/
public static AppDescriptor of(String appName, Class<?> entryClass) {
return of(appName, entryClass, Version.of(entryClass));
}
/**
* Create an `AppDescriptor` with appName and package name specified
*
* If `appName` is `null` or blank, it will try the following
* approach to get app name:
*
* 1. check the {@link Version#getArtifactId() artifact id} and use it unless
* 2. if artifact id is null or empty, then infer app name using {@link AppNameInferer}
*
* @param appName
* the app name
* @param packageName
* the package name of the app
* @return
* an `AppDescriptor` instance
*/
public static AppDescriptor of(String appName, String packageName) {
String[] packages = packageName.split(S.COMMON_SEP);
return of(appName, packageName, Version.ofPackage(packages[0]));
}
/**
* Create an `AppDescriptor` with package name.
*
* This method relies on {@link Version#ofPackage(String)} to get
* the corresponding {@link Version} instance to the package name.
*
* @param packageName
* the package name
* @return
* an `AppDescriptor` instance
*/
public static AppDescriptor of(String packageName) {
return of(null, packageName);
}
/**
* Create an `AppDescriptor` with entry class (the class with main method)
*
* This method relies on {@link Version#of(Class)} to get the
* corresponding {@link Version} instance to the entry class
*
* @param entryClass
* the entry class
* @return
* An `AppDescriptor` instance
*/
public static AppDescriptor of(Class<?> entryClass) {
return of(null, entryClass);
}
/**
* Deserialize an `AppDescriptor` from byte array
*
* @param bytes
* the byte array
* @return
* an `AppDescriptor` instance
*/
public static AppDescriptor deserializeFrom(byte[] bytes) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return (AppDescriptor) ois.readObject();
} catch (IOException e) {
throw E.ioException(e);
} catch (ClassNotFoundException e) {
throw E.unexpected(e);
}
}
private static String ensureAppName(String appName, Class<?> entryClass, Version version) {
if (S.blank(appName)) {
if (!version.isUnknown()) {
appName = version.getArtifactId();
}
if (S.blank(appName)) {
appName = AppNameInferer.from(entryClass);
}
}
return appName;
}
private static String ensureAppName(String appName, String packageName, Version version) {
if (S.blank(appName)) {
if (!version.isUnknown()) {
appName = version.getArtifactId();
}
if (S.blank(appName)) {
appName = AppNameInferer.fromPackageName(packageName);
}
}
return appName;
}
}