package org.eclipse.osgi.internal.hooks;
import java.io.File;
import java.util.ArrayList;
import org.eclipse.osgi.framework.util.KeyedElement;
import org.eclipse.osgi.internal.framework.EquinoxConfiguration;
import org.eclipse.osgi.internal.hookregistry.ClassLoaderHook;
import org.eclipse.osgi.internal.loader.classpath.*;
import org.eclipse.osgi.storage.BundleInfo.Generation;
import org.eclipse.osgi.storage.bundlefile.BundleFile;
public class DevClassLoadingHook extends ClassLoaderHook implements KeyedElement {
public static final String KEY = DevClassLoadingHook.class.getName();
public static final int HASHCODE = KEY.hashCode();
private static final String FRAGMENT = "@fragment@";
private final EquinoxConfiguration configuration;
public DevClassLoadingHook(EquinoxConfiguration configuration) {
this.configuration = configuration;
}
@Override
public boolean addClassPathEntry(ArrayList<ClasspathEntry> cpEntries, String cp, ClasspathManager hostmanager, Generation sourceGeneration) {
String[] devClassPaths = !configuration.inDevelopmentMode() ? null : configuration.getDevClassPath(sourceGeneration.getRevision().getSymbolicName());
if (devClassPaths == null || devClassPaths.length == 0)
return false;
if (cpEntries.size() > 0 && cpEntries.get(0).getUserObject(KEY) != null)
return false;
boolean result = false;
for (String devClassPath : devClassPaths) {
if (hostmanager.addClassPathEntry(cpEntries, devClassPath, hostmanager, sourceGeneration)) {
result = true;
} else {
String devCP = devClassPath;
boolean fromFragment = devCP.endsWith(FRAGMENT);
if (!fromFragment && devCP.indexOf("..") >= 0) {
File base = sourceGeneration.getBundleFile().getBaseFile();
if (base.isDirectory()) {
ClasspathEntry entry = hostmanager.getExternalClassPath(new File(base, devCP).getAbsolutePath(), sourceGeneration);
if (entry != null) {
cpEntries.add(entry);
result = true;
}
}
} else {
if (fromFragment)
devCP = devCP.substring(0, devCP.length() - FRAGMENT.length());
Generation fragSource = findFragmentSource(sourceGeneration, devCP, hostmanager, fromFragment);
if (fragSource != null) {
ClasspathEntry entry = hostmanager.getExternalClassPath(devCP, fragSource);
if (entry != null) {
cpEntries.add(entry);
result = true;
}
}
}
}
}
if (result && cpEntries.size() > 0)
cpEntries.get(0).addUserObject(this);
return result;
}
private Generation findFragmentSource(Generation hostGeneration, String cp, ClasspathManager manager, boolean fromFragment) {
if (hostGeneration != manager.getGeneration())
return hostGeneration;
File file = new File(cp);
if (!file.isAbsolute())
return hostGeneration;
FragmentClasspath[] fragCPs = manager.getFragmentClasspaths();
for (FragmentClasspath fragCP : fragCPs) {
BundleFile fragBase = fragCP.getGeneration().getBundleFile();
File fragFile = fragBase.getBaseFile();
if (fragFile != null && file.getPath().startsWith(fragFile.getPath())) {
return fragCP.getGeneration();
}
}
return fromFragment ? null : hostGeneration;
}
@Override
public boolean compare(KeyedElement other) {
return other.getKey() == KEY;
}
@Override
public Object getKey() {
return KEY;
}
@Override
public int getKeyHashCode() {
return HASHCODE;
}
@Override
public boolean isProcessClassRecursionSupported() {
return true;
}
}