Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/tools/AddStructureBlob.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9ddr.tools;
23
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.IOException;
27
import java.util.HashMap;
28
import java.util.Properties;
29
30
import com.ibm.j9ddr.tools.store.J9DDRStructureStore;
31
import com.ibm.j9ddr.tools.store.StructureKey;
32
import com.ibm.j9ddr.tools.store.StructureMismatchError;
33
34
public class AddStructureBlob {
35
private static final String PROP_DDR_ORDER = "ddr.order"; //property to control the order in which ddr processes blobs/supersets
36
37
HashMap<String, String> opts = new HashMap<String, String>();
38
39
public AddStructureBlob() {
40
super();
41
initArgs();
42
}
43
44
private void initArgs() {
45
opts.put("-d", null); // database directory
46
opts.put("-f", null); // structure file or directory of structure files to add
47
opts.put("-k", null); // key
48
opts.put("-s", null); // optional superset filename
49
opts.put("-c", ""); //optional value to provide a cache properties file
50
}
51
52
public static void main(String[] args) {
53
AddStructureBlob app = new AddStructureBlob();
54
app.parseArgs(args);
55
app.run();
56
}
57
58
//loads the order (if specified) in which blobs/supersets are to be processed
59
private String[] loadSpecNames() {
60
String path = opts.get("-c");
61
if((null == path) || (path.length() == 0)) {
62
//path for properties file not specified so return an empty array
63
return new String[0];
64
}
65
Properties props = new Properties();
66
File file = new File(path);
67
if(file.exists()) {
68
try {
69
FileInputStream in = new FileInputStream(file);
70
props.load(in);
71
String specs = props.getProperty(PROP_DDR_ORDER);
72
if(null == specs) {
73
String msg = String.format("The cache properties file [%s] specified by the -c option does not specify a %s key", file.getAbsolutePath(), PROP_DDR_ORDER);
74
throw new IllegalArgumentException(msg);
75
}
76
return specs.split(",");
77
} catch (Exception e) {
78
String msg = String.format("The cache properties file [%s] specified by the -c option could not be read", file.getAbsolutePath());
79
throw new IllegalArgumentException(msg, e);
80
}
81
} else {
82
String msg = String.format("The cache properties file [%s] specified by the -c option does not exist", file.getAbsolutePath());
83
throw new IllegalArgumentException(msg);
84
}
85
}
86
87
private void run() {
88
String directoryName = opts.get("-d");
89
String supersetName = opts.get("-s");
90
91
if( supersetName == null ) {
92
supersetName = J9DDRStructureStore.DEFAULT_SUPERSET_FILE_NAME;
93
}
94
95
try {
96
J9DDRStructureStore store = new J9DDRStructureStore(directoryName, supersetName);
97
String path = opts.get("-c");
98
if((null == path) || (path.length() == 0)) {
99
//path for properties file not specified so default to adding from a single file or directory
100
addFromFile(store);
101
} else {
102
//a property file controls the order in which blobs are processed
103
addFromPropertiesFile(store);
104
}
105
} catch (IOException e) {
106
e.printStackTrace();
107
} catch (StructureMismatchError e) {
108
e.printStackTrace();
109
}
110
}
111
112
//add a structure from a specified path
113
private void addFromFile(J9DDRStructureStore store) throws IOException, StructureMismatchError {
114
String directoryName = opts.get("-d");
115
String structureName = opts.get("-f");
116
String keyString = opts.get("-k");
117
String supersetName = opts.get("-s");
118
119
if (keyString == null) {
120
File structurePath = new File(structureName);
121
File[] structureFiles;
122
if( structurePath.isDirectory() ) {
123
structureFiles = structurePath.listFiles();
124
} else {
125
structureFiles = new File[] {structurePath};
126
structurePath = structurePath.getParentFile();
127
}
128
for( File file: structureFiles ) {
129
if(file.exists()) {
130
store.add(null, file.getPath(), false);
131
store.updateSuperset();
132
System.out.println("Added " + file.getName() + " to superset " + supersetName + " in " + directoryName);
133
} else {
134
System.out.println("WARNING : The specified structure file " + file.getName() + " does not exist and was ignored");
135
}
136
}
137
} else {
138
StructureKey key = new StructureKey(keyString);
139
store.add(key, structureName, true);
140
store.updateSuperset();
141
System.out.println(String.format("Added %s to %s/%s as %s", structureName, directoryName, store.getSuperSetFileName(), keyString));
142
}
143
}
144
145
//the order in which files are added is controlled by a properties file, it expects the -f parameter to point to a directory containing
146
//either blobs or supersets
147
private void addFromPropertiesFile(J9DDRStructureStore store) throws IOException, StructureMismatchError {
148
String directoryName = opts.get("-d");
149
String keyString = opts.get("-k");
150
File structurePath = new File(opts.get("-f"));
151
String[] specs = loadSpecNames(); //load the spec order (if specified on the command line)
152
File[] structureFiles = new File[specs.length];
153
for(int i = 0; i < specs.length; i++) {
154
structureFiles[i] = new File(structurePath, specs[i]);
155
}
156
for( File file: structureFiles ) {
157
if(file.exists()) {
158
StructureKey key = new StructureKey(file.getName() + "." + keyString);
159
store.add(key, file.getPath(), false);
160
store.updateSuperset();
161
System.out.println(String.format("Added %s to %s/%s as %s", file.getAbsolutePath(), directoryName, store.getSuperSetFileName(), key));
162
} else {
163
System.out.println("WARNING : The specified structure file " + file.getName() + " does not exist and was ignored");
164
}
165
}
166
}
167
168
private void parseArgs(String[] args) {
169
170
if((args.length == 0) || ((args.length % 2) != 0)) {
171
System.out.println("Not enough options specified as program arguments");
172
System.out.println(" args [" + args.length + "]");
173
printHelp();
174
System.exit(1);
175
}
176
177
for(int i = 0; i < args.length; i+=2) {
178
if(opts.containsKey(args[i])) {
179
opts.put(args[i], args[i+1]);
180
} else {
181
System.out.println("Invalid option : " + args[i]);
182
printHelp();
183
System.exit(1);
184
}
185
}
186
187
for(String key:opts.keySet()) {
188
String value = opts.get(key);
189
if(value == null) {
190
if (!(key.equals("-k") || key.equals("-s"))) {
191
System.err.println("The option " + key + " has not been set.\n");
192
printHelp();
193
System.exit(1);
194
}
195
}
196
}
197
}
198
199
/**
200
* Print usage help to stdout
201
*/
202
private static void printHelp() {
203
System.out.println("Usage :\n\njava AddStructureBlob -d <directory name> -f <structure file name> [-s <superset file name>] [-k <structure key>]\n");
204
}
205
}
206
207