下面列出了java.util.jar.Manifest#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static List<Pattern> loadPublicPackagesPatterns(Project project) {
List<Pattern> toRet = new ArrayList<Pattern>();
String[] params = PluginBackwardPropertyUtils.getPluginPropertyList(project,
"publicPackages", "publicPackage", "manifest"); //NOI18N
if (params != null) {
toRet = prepareMavenPublicPackagesPatterns(params);
} else {
FileObject obj = project.getProjectDirectory().getFileObject(MANIFEST_PATH);
if (obj != null) {
InputStream in = null;
try {
in = obj.getInputStream();
Manifest man = new Manifest();
man.read(in);
String value = man.getMainAttributes().getValue(ATTR_PUBLIC_PACKAGE);
toRet = prepareManifestPublicPackagesPatterns(value);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
} finally {
IOUtil.close(in);
}
}
}
return toRet;
}
public Map<String, String> getManifest(final InputStream input) {
final Map<String, String> retMap = new HashMap<String, String>();
try {
Manifest manifest = new Manifest();
manifest.read(input);
final Attributes attributes = manifest.getMainAttributes();
for (final Map.Entry attribute : attributes.entrySet()) {
retMap.put(attribute.getKey().toString(), attribute.getValue().toString());
}
} catch (final Exception ex) {
logger.error( "Failed to load manifest ", ex);
}
return retMap;
}
public Map<String, String> getManifest(final InputStream input) {
final Map<String, String> retMap = new HashMap<String, String>();
try {
Manifest manifest = new Manifest();
manifest.read(input);
final Attributes attributes = manifest.getMainAttributes();
for (final Map.Entry attribute : attributes.entrySet()) {
retMap.put(attribute.getKey().toString(), attribute.getValue().toString());
}
} catch (final Exception ex) {
logger.error("Failed to load manifest ", ex);
}
return retMap;
}
ImplementationDetails getImplementationVersion() throws FileNotFoundException, IOException{
String appServerHome = context.getRealPath("/");
File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");
Manifest mf = new Manifest();
mf.read(new FileInputStream(manifestFile));
Attributes atts = mf.getMainAttributes();
ImplementationDetails response = new ImplementationDetails();
response.setImplementationTitle(atts.getValue("Implementation-Title"));
response.setImplementationVersion(atts.getValue("Implementation-Version"));
response.setImplementationVendorId(atts.getValue("Implementation-Vendor-Id"));
return response;
}
/**
* Creates a manifest and returns an input stream for its contents.
*/
private InputStream createManifest() throws IOException {
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attributes.put(new Attributes.Name("Created-By"), "blaze-singlejar");
if (mainClass != null) {
attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
}
if (extraManifestContent != null) {
ByteArrayInputStream in = new ByteArrayInputStream(extraManifestContent.getBytes("UTF8"));
manifest.read(in);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
return new ByteArrayInputStream(out.toByteArray());
}
private Optional<String> parseManifest(URL infoResource) {
try {
InputStream in = infoResource.openStream();
if (in == null) {
return Optional.empty();
}
Manifest m = new Manifest();
try {
m.read(in);
} finally {
in.close();
}
String bundleName = m.getMainAttributes().getValue(BUNDLE_NAME);
if (!BUNDLE_NAME_VALUE.equals(bundleName)) {
return Optional.empty();
}
return Optional.ofNullable(m.getMainAttributes().getValue(BUNDLE_VERSION));
} catch (IOException e) {
return Optional.empty();
}
}
private static Manifest loadManifest(File mani) throws IOException {
Manifest m = new Manifest();
if (mani != null) {
InputStream is = new FileInputStream(mani);
try {
m.read(is);
} finally {
is.close();
}
}
m.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug
return m;
}
private static Manifest loadManifest(File mani) throws IOException {
Manifest m = new Manifest();
if (mani != null) {
InputStream is = new FileInputStream(mani);
try {
m.read(is);
} finally {
is.close();
}
}
m.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug
return m;
}
/**
* Reads response from server and stores it into internal
* <code>Manifest</code> object. Value of <i>exit-code<i> attribute
* is verified to detect if command completed successfully. If not,
* <i>message</i> value is checked for "please wait" <code>String</code>
* to eventually set <code>retry</code> value to <code>true</code>.
* <p/>
* Override to read the response data sent by the server. Do not close
* the stream parameter when finished. Caller will take care of that.
* <p/>
* @param in Stream to read data from.
* @return true if response was read correctly.
* @throws CommandException in case of stream error.
*/
@Override
protected boolean readResponse(InputStream in, HttpURLConnection hconn) {
boolean readResult;
manifest = new Manifest();
try {
Logger.log(Level.FINEST, "Reading response from {0}:{1}",
new Object[] {server.getHost(),
Integer.toString(server.getAdminPort())});
manifest.read(in);
} catch (IOException ioe) {
throw new CommandException(CommandException.HTTP_RESP_IO_EXCEPTION,
ioe);
}
if (successExitCode(manifest)) {
readResult = true;
}
else {
readResult = false;
String message = getMessage(manifest);
if (message != null) {
if (message.contains("please wait")) {
retry = true;
} else if (message.contains(
"javax.security.auth.login.LoginException")) {
auth = false;
}
}
}
return readResult;
}
/**
* Reads response from server and stores it into internal
* <code>Manifest</code> object. Value of <i>exit-code<i> attribute
* is verified to detect if command completed successfully. If not,
* <i>message</i> value is checked for "please wait" <code>String</code>
* to eventually set <code>retry</code> value to <code>true</code>.
* <p/>
* Override to read the response data sent by the server. Do not close
* the stream parameter when finished. Caller will take care of that.
* <p/>
* @param in Stream to read data from.
* @return true if response was read correctly.
* @throws CommandException in case of stream error.
*/
@Override
protected boolean readResponse(InputStream in, HttpURLConnection hconn) {
boolean readResult;
manifest = new Manifest();
try {
Logger.log(Level.FINEST, "Reading response from {0}:{1}",
new Object[] {server.getHost(),
Integer.toString(server.getAdminPort())});
manifest.read(in);
} catch (IOException ioe) {
throw new CommandException(CommandException.HTTP_RESP_IO_EXCEPTION,
ioe);
}
if (successExitCode(manifest)) {
readResult = true;
}
else {
readResult = false;
String message = getMessage(manifest);
if (message != null) {
if (message.contains("please wait")) {
retry = true;
} else if (message.contains(
"javax.security.auth.login.LoginException")) {
auth = false;
}
}
}
return readResult;
}
private Manifest getManifest() throws IOException {
Manifest manifest = new Manifest();
try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
jar.getNextJarEntry();
JarEntry manifestEntry = jar.getNextJarEntry();
assertEquals(JarFile.MANIFEST_NAME, manifestEntry.getName());
manifest.read(jar);
}
return manifest;
}
private void readManifest()
throws IOException
{
_manifest = new Manifest();
try (InputStream is = _manifestJarItem.read()) {
_manifest.read(is);
}
}
private void appendManifest(URL url, ClassLoader cl)
throws URISyntaxException, IOException {
String jar = url.getPath();
if (jar.lastIndexOf('!') > 0) {
jar = jar.substring(0, jar.lastIndexOf('!'));
}
java.net.URI uri = new java.net.URI(jar);
Manifest manifest = new Manifest();
InputStream in = url.openStream();
try {
manifest.read(in);
} finally {
in.close();
}
Attributes attributes = manifest.getMainAttributes();
String dependencies = attributes.getValue("Class-Path");
if (dependencies == null) {
dependencies = attributes.getValue("Class-path");
}
if (dependencies != null) {
for (String entry : dependencies.split("\\s+")) {
if (entry.length() > 0) {
classpath.add(uri.resolve(entry));
}
}
}
}
/**
* Based on the JARs detected on the classpath, determine which version of selenium-api is available.
* @return the detected version of Selenium API, or DEFAULT_SELENIUM_VERSION if it could not be determined
*/
public static String determineClasspathSeleniumVersion() {
Set<String> seleniumVersions = new HashSet<>();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> manifests = classLoader.getResources("META-INF/MANIFEST.MF");
while (manifests.hasMoreElements()) {
URL manifestURL = manifests.nextElement();
try (InputStream is = manifestURL.openStream()) {
Manifest manifest = new Manifest();
manifest.read(is);
String seleniumVersion = getSeleniumVersionFromManifest(manifest);
if (seleniumVersion != null) {
seleniumVersions.add(seleniumVersion);
LOGGER.info("Selenium API version {} detected on classpath", seleniumVersion);
}
}
}
} catch (Exception e) {
LOGGER.debug("Failed to determine Selenium-Version from selenium-api JAR Manifest", e);
}
if (seleniumVersions.size() == 0) {
LOGGER.warn("Failed to determine Selenium version from classpath - will use default version of {}", DEFAULT_SELENIUM_VERSION);
return DEFAULT_SELENIUM_VERSION;
}
String foundVersion = seleniumVersions.iterator().next();
if (seleniumVersions.size() > 1) {
LOGGER.warn("Multiple versions of Selenium API found on classpath - will select {}, but this may not be reliable", foundVersion);
}
return foundVersion;
}
/**
* Check if Selenium Version detected is the correct one.
* @param urlManifest : manifest file
* @throws IOException
*/
private void checkSeleniumVersionDetected(String urlManifest, String expectedVersion) throws IOException {
Manifest manifest = new Manifest();
manifest.read(this.getClass().getClassLoader().getResourceAsStream(urlManifest));
String seleniumVersion = SeleniumUtils.getSeleniumVersionFromManifest(manifest);
assertEquals("Check if Selenium Version detected is the correct one.", expectedVersion, seleniumVersion);
}
private static Manifest read(Path file) {
JkUtilsAssert.argument(Files.exists(file), file.normalize() + " not found.");
JkUtilsAssert.argument(Files.isRegularFile(file), file.normalize() + " is directory : need file.");
final Manifest manifest = new Manifest();
try (InputStream is = Files.newInputStream(file)){
manifest.read(is);
return manifest;
} catch (final IOException e) {
throw JkUtilsThrowable.unchecked(e);
}
}
private static Manifest read(InputStream inputStream) {
final Manifest manifest = new Manifest();
try {
manifest.read(inputStream);
return manifest;
} catch (final IOException e) {
throw JkUtilsThrowable.unchecked(e);
}
}
private String doRoundTrip(Object versionName) throws Exception {
Manifest m1 = new Manifest();
m1.getMainAttributes().put(Attributes.Name.CONTENT_TYPE, "image/pr0n");
if (versionName != null) {
m1.getMainAttributes().putValue(versionName.toString(), "1.2.3");
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
m1.write(os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
Manifest m2 = new Manifest();
m2.read(is);
return (String) m2.getMainAttributes().get(Attributes.Name.CONTENT_TYPE);
}
Attributes getManifestAttributes() throws FileNotFoundException, IOException{
InputStream resourceAsStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");
Manifest mf = new Manifest();
mf.read(resourceAsStream);
Attributes atts = mf.getMainAttributes();
return atts;
}
protected Class findClass (final String name) throws ClassNotFoundException {
if (!fast && name.indexOf ('.') != -1) {
Logger.getLogger(NbClassLoader.class.getName()).log(Level.FINE, "NBFS used!");
String pkg = name.substring (0, name.lastIndexOf ('.'));
if (getPackage (pkg) == null) {
String resource = name.replace ('.', '/') + ".class"; // NOI18N
URL[] urls = getURLs ();
for (int i = 0; i < urls.length; i++) {
// System.err.println (urls[i].toString ());
FileObject root = URLMapper.findFileObject(urls[i]);
if (root == null) {
continue;
}
try {
FileObject fo = root.getFileObject(resource);
if (fo != null) {
// Got it. If there is an associated manifest, load it.
FileObject manifo = root.getFileObject("META-INF/MANIFEST.MF");
if (manifo == null)
manifo = root.getFileObject("meta-inf/manifest.mf");
if (manifo != null) {
// System.err.println (manifo.toString () + " " + manifo.getClass ().getName () + " " + manifo.isValid ());
Manifest mani = new Manifest();
InputStream is = manifo.getInputStream();
try {
mani.read(is);
}
finally {
is.close();
}
definePackage(pkg, mani, urls[i]);
}
break;
}
}
catch (IOException ioe) {
Exceptions.attachLocalizedMessage(ioe,
urls[i].toString());
Exceptions.printStackTrace(ioe);
continue;
}
}
}
}
return super.findClass (name);
}