use of java.util.jar.JarEntry in project camel by apache.
the class DefaultPackageScanClassResolver method doLoadJarClassEntries.
/**
* Loads all the class entries from the JAR.
*
* @param stream the inputstream of the jar file to be examined for classes
* @param urlPath the url of the jar file to be examined for classes
* @return all the .class entries from the JAR
*/
protected List<String> doLoadJarClassEntries(InputStream stream, String urlPath) {
List<String> entries = new ArrayList<String>();
JarInputStream jarStream = null;
try {
jarStream = new JarInputStream(stream);
JarEntry entry;
while ((entry = jarStream.getNextJarEntry()) != null) {
String name = entry.getName();
if (name != null) {
name = name.trim();
if (!entry.isDirectory() && name.endsWith(".class")) {
entries.add(name);
}
}
}
} catch (IOException ioe) {
log.warn("Cannot search jar file '" + urlPath + " due to an IOException: " + ioe.getMessage(), ioe);
} finally {
IOHelper.close(jarStream, urlPath, log);
}
return entries;
}
use of java.util.jar.JarEntry in project flink by apache.
the class JarFileCreator method createJarFile.
/**
* Creates a jar file which contains the previously added class. The content of the jar file is written to
* <code>outputFile</code> which has been provided to the constructor. If <code>outputFile</code> already exists, it
* is overwritten by this operation.
*
* @throws IOException
* thrown if an error occurs while writing to the output file
*/
public synchronized void createJarFile() throws IOException {
//Retrieve dependencies automatically
addDependencies();
// Temporary buffer for the stream copy
final byte[] buf = new byte[128];
// Check if output file is valid
if (this.outputFile == null) {
throw new IOException("Output file is null");
}
// If output file already exists, delete it
if (this.outputFile.exists()) {
this.outputFile.delete();
}
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(this.outputFile), new Manifest())) {
final Iterator<Class<?>> it = this.classSet.iterator();
while (it.hasNext()) {
final Class<?> clazz = it.next();
final String entry = clazz.getName().replace('.', '/') + CLASS_EXTENSION;
jos.putNextEntry(new JarEntry(entry));
String name = clazz.getName();
int n = name.lastIndexOf('.');
String className = null;
if (n > -1) {
className = name.substring(n + 1, name.length());
}
//Using the part after last dot instead of class.getSimpleName() could resolve the problem of inner class.
final InputStream classInputStream = clazz.getResourceAsStream(className + CLASS_EXTENSION);
int num = classInputStream.read(buf);
while (num != -1) {
jos.write(buf, 0, num);
num = classInputStream.read(buf);
}
classInputStream.close();
jos.closeEntry();
}
}
}
use of java.util.jar.JarEntry in project hadoop by apache.
the class TestFSDownload method createJarFile.
static LocalResource createJarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException, URISyntaxException {
byte[] bytes = new byte[len];
r.nextBytes(bytes);
File archiveFile = new File(p.toUri().getPath() + ".jar");
archiveFile.createNewFile();
JarOutputStream out = new JarOutputStream(new FileOutputStream(archiveFile));
out.putNextEntry(new JarEntry(p.getName()));
out.write(bytes);
out.closeEntry();
out.close();
LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
ret.setResource(URL.fromPath(new Path(p.toString() + ".jar")));
ret.setSize(len);
ret.setType(LocalResourceType.ARCHIVE);
ret.setVisibility(vis);
ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".jar")).getModificationTime());
return ret;
}
use of java.util.jar.JarEntry in project hbase by apache.
the class ClassLoaderTestHelper method createJarArchive.
/**
* Jar a list of files into a jar archive.
*
* @param archiveFile the target jar archive
* @param tobejared a list of files to be jared
*/
private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
try {
byte[] buffer = new byte[BUFFER_SIZE];
// Open archive file
FileOutputStream stream = new FileOutputStream(archiveFile);
JarOutputStream out = new JarOutputStream(stream, new Manifest());
for (int i = 0; i < tobeJared.length; i++) {
if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) {
continue;
}
// Add archive entry
JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
jarAdd.setTime(tobeJared[i].lastModified());
out.putNextEntry(jarAdd);
// Write file to archive
FileInputStream in = new FileInputStream(tobeJared[i]);
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
out.write(buffer, 0, nRead);
}
in.close();
}
out.close();
stream.close();
LOG.info("Adding classes to jar file completed");
return true;
} catch (Exception ex) {
LOG.error("Error: " + ex.getMessage());
return false;
}
}
use of java.util.jar.JarEntry in project hbase by apache.
the class ClassFinder method findClassesFromJar.
private Set<Class<?>> findClassesFromJar(String jarFileName, String packageName, boolean proceedOnExceptions) throws IOException, ClassNotFoundException, LinkageError {
JarInputStream jarFile = null;
try {
jarFile = new JarInputStream(new FileInputStream(jarFileName));
} catch (IOException ioEx) {
LOG.warn("Failed to look for classes in " + jarFileName + ": " + ioEx);
throw ioEx;
}
Set<Class<?>> classes = new HashSet<>();
JarEntry entry = null;
try {
while (true) {
try {
entry = jarFile.getNextJarEntry();
} catch (IOException ioEx) {
if (!proceedOnExceptions) {
throw ioEx;
}
LOG.warn("Failed to get next entry from " + jarFileName + ": " + ioEx);
break;
}
if (entry == null) {
// loop termination condition
break;
}
String className = entry.getName();
if (!className.endsWith(CLASS_EXT)) {
continue;
}
int ix = className.lastIndexOf('/');
String fileName = (ix >= 0) ? className.substring(ix + 1) : className;
if (null != this.fileNameFilter && !this.fileNameFilter.isCandidateFile(fileName, className)) {
continue;
}
className = className.substring(0, className.length() - CLASS_EXT.length()).replace('/', '.');
if (!className.startsWith(packageName)) {
continue;
}
Class<?> c = makeClass(className, proceedOnExceptions);
if (c != null) {
if (!classes.add(c)) {
LOG.warn("Ignoring duplicate class " + className);
}
}
}
return classes;
} finally {
jarFile.close();
}
}
Aggregations