use of io.fabric8.kubernetes.api.builder.Visitor in project fabric8 by jboss-fuse.
the class TraceStrategy method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
byte[] buffer = null;
ClassInfo classInfo = context.getClassInfo(className);
classInfo.setOriginalClass(classBeingRedefined);
if (classInfo.getTransformed() == null) {
// we haven't been transformed before
classInfo.setOriginal(classfileBuffer);
}
if (!cleanUp.get()) {
byte[] classBufferToRedefine = classInfo.getOriginal();
if (configuration.isAudit(className)) {
if (classInfo.isTransformed()) {
// remove metrics from methods no longer defined
context.resetMethods(classInfo);
}
ClassReader cr = new ClassReader(classBufferToRedefine);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
ApmClassVisitor visitor = new ApmClassVisitor(this, cw, classInfo);
cr.accept(visitor, ClassReader.SKIP_FRAMES);
buffer = cw.toByteArray();
if (!verifyClass(className, buffer)) {
classInfo.setCanTransform(false);
buffer = null;
}
classInfo.setTransformed(buffer);
}
} else {
if (classInfo.getOriginal() != null) {
buffer = classInfo.getOriginal();
context.resetAll(classInfo);
}
}
return buffer;
}
use of io.fabric8.kubernetes.api.builder.Visitor in project fabric8 by jboss-fuse.
the class SubsystemResolveContext method findProviders.
@Override
public List<Capability> findProviders(Requirement requirement) {
List<Capability> caps = new ArrayList<Capability>();
Region requirerRegion = getRegion(requirement.getResource());
if (requirerRegion != null) {
Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
if (res != null && !res.isEmpty()) {
caps.addAll(res);
} else if (globalRepository != null) {
// Only bring in external resources for non optional requirements
if (!RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(RESOLUTION_DIRECTIVE))) {
resMap = globalRepository.findProviders(Collections.singleton(requirement));
res = resMap != null ? resMap.get(requirement) : null;
if (res != null && !res.isEmpty()) {
caps.addAll(res);
}
}
}
// Use the digraph to prune non visible capabilities
Visitor visitor = new Visitor(caps);
requirerRegion.visitSubgraph(visitor);
Collection<Capability> allowed = visitor.getAllowed();
caps.retainAll(allowed);
// the parent one
if (caps.size() > 1) {
Map<String, Resource> providers = new HashMap<String, Resource>();
for (Capability cap : caps) {
Resource resource = cap.getResource();
String id = getSymbolicName(resource) + "|" + getVersion(resource);
Resource prev = providers.get(id);
if (prev != null && prev != resource) {
Region r1 = getRegion(prev);
Region r2 = getRegion(resource);
boolean r2canSeeR1 = isResourceVisibleFromRegion(prev, r2);
boolean r1canSeeR2 = isResourceVisibleFromRegion(resource, r1);
if (r1canSeeR2 && r2canSeeR1) {
// r1 and r2 can see each other
int reqDiff = prev.getRequirements(null).size() - resource.getRequirements(null).size();
if (reqDiff == 0) {
String r1Name = getRegion(prev).getName();
String r2Name = getRegion(resource).getName();
int c = r1Name.compareTo(r2Name);
if (c == 0) {
// One of the resource has to be a bundle, use that one
c = (prev instanceof BundleRevision) ? -1 : +1;
}
resource = c < 0 ? prev : resource;
} else {
// one of the resource has less requirements, so use this one
// This can be the case when one resource has conditionals, which adds further
// requirements to the condition feature.
resource = reqDiff < 0 ? prev : resource;
}
} else {
// only one region can see the other, grab the correct
resource = r1canSeeR2 ? prev : resource;
}
}
providers.put(id, resource);
}
for (Iterator<Capability> it = caps.iterator(); it.hasNext(); ) {
Capability cap = it.next();
if (!providers.values().contains(cap.getResource())) {
it.remove();
}
}
}
// Sort caps
Collections.sort(caps, candidateComparator);
}
return caps;
}
use of io.fabric8.kubernetes.api.builder.Visitor in project fabric8 by fabric8io.
the class KubernetesModelProcessorProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
CompilationTaskFactory compilationTaskFactory = new CompilationTaskFactory(processingEnv);
Set<TypeElement> processors = new HashSet<>();
// 1st pass collect classes to compile.
for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
processors.add(getClassElement(element));
}
if (processors.isEmpty()) {
return true;
}
StringWriter writer = new StringWriter();
try {
Callable<Boolean> compileTask = compilationTaskFactory.create(processors, writer);
if (!compileTask.call()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to compile provider classes. See output below.");
printCompileErrors(compilationTaskFactory);
return false;
}
} catch (Exception e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error to compile provider classes, due to: " + e.getMessage() + ". See output below.");
return false;
} finally {
String output = writer.toString();
if (Strings.isNullOrBlank(output)) {
output = "success";
}
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Fabric8 model generator compiler output:" + output);
}
// 2nd pass generate json.
for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
KubernetesModelProcessor annotation = element.getAnnotation(KubernetesModelProcessor.class);
String kubernetesJsonFileName = annotation.value();
KubernetesResource json = readJson(kubernetesJsonFileName);
Builder<? extends KubernetesResource> builder;
if (json instanceof KubernetesList) {
builder = new KubernetesListBuilder((KubernetesList) json);
} else if (json instanceof Template) {
builder = new TemplateBuilder((Template) json);
} else if (json != null) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Unknown Kubernetes json type:" + json.getClass());
return false;
} else {
return false;
}
try {
if (element instanceof TypeElement) {
for (ExecutableElement methodElement : ElementFilter.methodsIn(element.getEnclosedElements())) {
TypeElement classElement = getClassElement(element);
Class<?> cls = Class.forName(classElement.getQualifiedName().toString());
final Object instance = cls.newInstance();
final String methodName = methodElement.getSimpleName().toString();
if (builder instanceof Visitable) {
((Visitable) builder).accept(new Visitor() {
@Override
public void visit(Object o) {
for (Method m : findMethods(instance, methodName, o.getClass())) {
Named named = m.getAnnotation(Named.class);
if (named != null && !Strings.isNullOrBlank(named.value())) {
String objectName = getName(o);
// If a name has been explicitly specified check if there is a match
if (!named.value().equals(objectName)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Named method:" + m.getName() + " with name:" + named.value() + " doesn't match: " + objectName + ", ignoring");
return;
}
}
try {
m.invoke(instance, o);
} catch (IllegalAccessException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
} catch (InvocationTargetException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
}
}
}
});
} else {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Json type is not visitable.");
}
}
}
json = builder.build();
generateJson(kubernetesJsonFileName, json);
} catch (Exception ex) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error creating Kubernetes configuration:" + ex.getMessage());
}
}
return true;
}
use of io.fabric8.kubernetes.api.builder.Visitor in project fabric8 by fabric8io.
the class TraceStrategy method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
byte[] buffer = null;
ClassInfo classInfo = context.getClassInfo(className);
classInfo.setOriginalClass(classBeingRedefined);
if (classInfo.getTransformed() == null) {
// we haven't been transformed before
classInfo.setOriginal(classfileBuffer);
}
if (!cleanUp.get()) {
byte[] classBufferToRedefine = classInfo.getOriginal();
if (configuration.isAudit(className)) {
if (classInfo.isTransformed()) {
// remove metrics from methods no longer defined
context.resetMethods(classInfo);
}
ClassReader cr = new ClassReader(classBufferToRedefine);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
ApmClassVisitor visitor = new ApmClassVisitor(this, cw, classInfo);
cr.accept(visitor, ClassReader.SKIP_FRAMES);
buffer = cw.toByteArray();
if (!verifyClass(className, buffer)) {
classInfo.setCanTransform(false);
buffer = null;
}
classInfo.setTransformed(buffer);
}
} else {
if (classInfo.getOriginal() != null) {
buffer = classInfo.getOriginal();
context.resetAll(classInfo);
}
}
return buffer;
}
use of io.fabric8.kubernetes.api.builder.Visitor in project fabric8 by fabric8io.
the class SessionListener method enhance.
private KubernetesList enhance(final Session session, Configuration configuration, KubernetesList kubernetesList) {
if (configuration == null || configuration.getProperties() == null || !configuration.getProperties().containsKey(Constants.KUBERNETES_MODEL_PROCESSOR_CLASS)) {
return kubernetesList;
}
String processorClassName = configuration.getProperties().get(Constants.KUBERNETES_MODEL_PROCESSOR_CLASS);
try {
final Object instance = SessionListener.class.getClassLoader().loadClass(processorClassName).newInstance();
KubernetesListBuilder builder = new KubernetesListBuilder(kubernetesList);
((Visitable) builder).accept(new Visitor() {
@Override
public void visit(Object o) {
for (Method m : findMethods(instance, o.getClass())) {
Named named = m.getAnnotation(Named.class);
if (named != null && !Strings.isNullOrBlank(named.value())) {
String objectName = o instanceof ObjectMeta ? getName((ObjectMeta) o) : getName((HasMetadata) o);
// If a name has been explicitly specified check if there is a match
if (!named.value().equals(objectName)) {
session.getLogger().warn("Named method:" + m.getName() + " with name:" + named.value() + " doesn't match: " + objectName + ", ignoring");
return;
}
}
try {
m.invoke(instance, o);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
session.getLogger().error("Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
}
}
}
});
return builder.build();
} catch (Exception e) {
session.getLogger().warn("Failed to load processor class:" + processorClassName + ". Ignoring");
return kubernetesList;
}
}
Aggregations