Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
ceylon.modules.jboss.repository.SourceResourceLoader Maven / Gradle / Ivy
package ceylon.modules.jboss.repository;
import ceylon.modules.api.compiler.CompilerAdapterFactory;
import org.jboss.modules.ClassSpec;
import org.jboss.modules.PackageSpec;
import org.jboss.modules.Resource;
import org.jboss.modules.ResourceLoader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
class SourceResourceLoader implements ResourceLoader {
private File sourcesRoot;
private File classesRoot;
private String rootName;
private Manifest manifest;
SourceResourceLoader(File sourcesRoot, File classesRoot, String rootName) {
if (sourcesRoot == null )
throw new IllegalArgumentException("Null sources root" );
if (classesRoot == null )
throw new IllegalArgumentException("Null classes root" );
if (rootName == null )
throw new IllegalArgumentException("Null root name" );
this .sourcesRoot = sourcesRoot;
this .classesRoot = classesRoot;
this .rootName = rootName;
final File manifestFile = new File(sourcesRoot, "META-INF" + File.separator + "MANIFEST.MF" );
manifest = readManifestFile(manifestFile);
}
private static Manifest readManifestFile (final File manifestFile) {
try {
InputStream is = new FileInputStream(manifestFile);
try {
return new Manifest(is);
} finally {
safeClose(is);
}
} catch (IOException e) {
return null ;
}
}
public String getRootName () {
return rootName;
}
public ClassSpec getClassSpec (String name) throws IOException {
File file = new File(classesRoot, name);
if (file.exists()) {
return toClassSpec(file);
} else {
CompilerAdapterFactory factory = CompilerAdapterFactory.getInstance();
file = factory.findAndCompile(sourcesRoot, name, classesRoot);
return file != null ? toClassSpec(file) : null ;
}
}
protected ClassSpec toClassSpec (File file) throws IOException {
final long size = file.length();
final ClassSpec spec = new ClassSpec();
final InputStream is = new FileInputStream(file);
try {
if (size <= (long ) Integer.MAX_VALUE) {
final int castSize = (int ) size;
byte [] bytes = new byte [castSize];
int a = 0 , res;
while ((res = is.read(bytes, a, castSize - a)) > 0 ) {
a += res;
}
is.close();
spec.setBytes(bytes);
return spec;
} else {
throw new IOException("Resource is too large to be a valid class file" );
}
} finally {
safeClose(is);
}
}
private static void safeClose (final Closeable closeable) {
if (closeable != null ) try {
closeable.close();
} catch (IOException e) {
}
}
static String fileNameOfClass (final String className, String typeSuffix) {
return className.replace('.' , '/' ) + "." + typeSuffix;
}
public PackageSpec getPackageSpec (String name) throws IOException {
final PackageSpec spec = new PackageSpec();
final Manifest manifest = this .manifest;
if (manifest == null ) {
return spec;
}
final Attributes mainAttribute = manifest.getAttributes(name);
final Attributes entryAttribute = manifest.getAttributes(name);
spec.setSpecTitle(getDefinedAttribute(Attributes.Name.SPECIFICATION_TITLE, entryAttribute, mainAttribute));
spec.setSpecVersion(getDefinedAttribute(Attributes.Name.SPECIFICATION_VERSION, entryAttribute, mainAttribute));
spec.setSpecVendor(getDefinedAttribute(Attributes.Name.SPECIFICATION_VENDOR, entryAttribute, mainAttribute));
spec.setImplTitle(getDefinedAttribute(Attributes.Name.IMPLEMENTATION_TITLE, entryAttribute, mainAttribute));
spec.setImplVersion(getDefinedAttribute(Attributes.Name.IMPLEMENTATION_VERSION, entryAttribute, mainAttribute));
spec.setImplVendor(getDefinedAttribute(Attributes.Name.IMPLEMENTATION_VENDOR, entryAttribute, mainAttribute));
if (Boolean.parseBoolean(getDefinedAttribute(Attributes.Name.SEALED, entryAttribute, mainAttribute))) {
spec.setSealBase(classesRoot.toURI().toURL());
}
return spec;
}
private static String getDefinedAttribute (Attributes.Name name, Attributes entryAttribute, Attributes mainAttribute) {
final String value = entryAttribute == null ? null : entryAttribute.getValue(name);
return value == null ? mainAttribute == null ? null : mainAttribute.getValue(name) : value;
}
public Resource getResource (String name) {
try {
File file = new File(classesRoot, name);
if (file.exists()) {
return new FileEntryResource(name, file, file.toURI().toURL());
} else {
file = new File(sourcesRoot, name);
if (file.exists() == false )
return null ;
return new SourceEntryResource(name, file, file.toURI().toURL(), classesRoot);
}
} catch (MalformedURLException e) {
return null ;
}
}
public String getLibrary (String name) {
return null ;
}
public Collection getPaths () {
List paths = new ArrayList();
buildIndex(paths, sourcesRoot, "" );
return paths;
}
private void buildIndex (final List index, final File root, final String pathBase) {
for (File file : root.listFiles()) {
if (file.isDirectory()) {
index.add(pathBase + file.getName());
buildIndex(index, file, pathBase + file.getName() + File.separator);
}
}
}
}