public WrappingTransformer(ClassTransformer delegate, ServiceReference<?> persistenceProvider) { validate(delegate, persistenceProvider); this.delegate = delegate; Object packages = persistenceProvider.getProperty("org.apache.aries.jpa.container.weaving.packages"); if (packages instanceof String[]) { for (String s : (String[])packages) { packageImportsToAdd.add(s); } } else { Bundle provider = persistenceProvider.getBundle(); String suffix = ";" + Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE + "=" + provider.getSymbolicName() + ";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=" + provider.getVersion(); BundleRevision br = provider.adapt(BundleWiring.class).getRevision(); for (BundleCapability bc : br.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE)) { packageImportsToAdd.add(bc.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE) + suffix); } } }
@Override public void weave(WovenClass wovenClass) { BundleWiring wiring = wovenClass.getBundleWiring(); Bundle bundle = wiring.getBundle(); ClassLoader cl = wiring.getClassLoader(); Collection<ClassTransformer> transformersToTry = getTransformers(bundle); for (ClassTransformer transformer : transformersToTry) { if (transformClass(wovenClass, cl, transformer)) { LOGGER.info("Weaving " + wovenClass.getClassName() + " using " + transformer.getClass().getName()); break; } } Class<?> dClass = wovenClass.getDefinedClass(); if (transformersToTry.isEmpty() && dClass != null && dClass.getAnnotation(Entity.class) != null) { LOGGER.warn("Loading " + wovenClass.getClassName() + " before transformer is present"); } }
private static boolean transformClass(WovenClass wovenClass, ClassLoader cl, ClassTransformer transformer) throws ThreadDeath, OutOfMemoryError { try { byte[] result = transformer .transform(cl, wovenClass.getClassName(), wovenClass.getDefinedClass(), wovenClass.getProtectionDomain(), wovenClass.getBytes()); if (result != null) { wovenClass.setBytes(result); wovenClass.getDynamicImports().add("org.eclipse.persistence.*"); wovenClass.getDynamicImports().add("org.apache.openjpa.*"); return true; } } catch (Exception t) { Bundle b = wovenClass.getBundleWiring().getBundle(); String msg = String.format("Weaving failure on class %s in bundle %s/%s using transformer %s", wovenClass.getClassName(), b.getSymbolicName(), b.getVersion(), transformer); throw new WeavingException(msg, t); } return false; }
@Override @SuppressWarnings("unchecked") public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) { properties.put("datanucleus.jpa.addClassTransformer", "false"); properties.put("datanucleus.plugin.pluginRegistryClassName", "org.datanucleus.plugin.OSGiPluginRegistry"); info.addTransformer(new ClassTransformer() { @Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { // TODO Auto-generated method stub return null; } }); return pp.createContainerEntityManagerFactory(info, properties); }
private static void validate(ClassTransformer delegate, ServiceReference<?> persistenceProvider) { if (delegate == null) { throw new NullPointerException("Transformer delegate may not be null"); } if (persistenceProvider == null) { throw new NullPointerException("PersistenceProvider may not be null"); } }
@Override public synchronized void addTransformer(Bundle pBundle, ClassTransformer transformer) { LOGGER.info("Adding transformer " + transformer.getClass().getName()); LinkedHashSet<ClassTransformer> transformers = registeredTransformers.get(pBundle); if (transformers == null) { transformers = new LinkedHashSet<ClassTransformer>(); registeredTransformers.put(pBundle, transformers); } transformers.add(transformer); }
@Override public synchronized void removeTransformer(Bundle pBundle, ClassTransformer transformer) { LinkedHashSet<ClassTransformer> set = registeredTransformers.get(pBundle); if (set == null || !set.remove(transformer)) { throw new IllegalStateException("Transformer " + transformer + " not registered"); } if (set.isEmpty()) { registeredTransformers.remove(pBundle); } }
/** * This implementation delegates to the LoadTimeWeaver, if specified. */ @Override public void addTransformer(ClassTransformer classTransformer) { if (this.loadTimeWeaver == null) { throw new IllegalStateException("Cannot apply class transformer without LoadTimeWeaver specified"); } this.loadTimeWeaver.addTransformer(new ClassFileTransformerAdapter(classTransformer)); }
@Override public void weave(WovenClass clazz) { try { if (transformers.isEmpty()) { return; } BundleWiring wiring = clazz.getBundleWiring(); Bundle b = wiring.getBundle(); ClassTransformer trfs[]; synchronized (transformers) { Collection<ClassTransformer> list = transformers.get(b); if (list == null) { return; } trfs = list.toArray(new ClassTransformer[list.size()]); } LOGGER.info("Transforming {} with {}", clazz.getClassName(), Arrays.toString(trfs)); for (ClassTransformer ctf : trfs) { if (ctf != null) { ctf.transform(wiring.getClassLoader(), clazz.getClassName(), clazz.getDefinedClass(), clazz.getProtectionDomain(), clazz.getBytes()); } } if (!imports.isEmpty()) { clazz.getDynamicImports().addAll(imports); } } catch (Exception e) { LOGGER.error("Error while weaving class {}", clazz.getClassName(), e); } }
boolean register(Bundle b, ClassTransformer ctf) { LOGGER.info("register transformer {} on bundle {}", ctf, b); if (ctf == null) { ctf = DUMMY_TRANSFORMER; } synchronized (transformers) { List<ClassTransformer> list = transformers.get(b); if (list == null) { list = new ArrayList<>(); transformers.put(b, list); } list.add(ctf); return true; } }
@Override public void weave(WovenClass clazz) { try { if (transformers.isEmpty()) return; BundleWiring wiring = clazz.getBundleWiring(); Bundle b = wiring.getBundle(); ClassTransformer trfs[]; synchronized (transformers) { List<ClassTransformer> list = transformers.get(b); if (list == null) return; trfs = list.toArray(empty); } System.out.println("transforming " + Arrays.toString(trfs) + " " + clazz); for (ClassTransformer ctf : trfs) { if (ctf != null) { ctf.transform(wiring.getClassLoader(), clazz.getClassName(), clazz.getDefinedClass(), clazz.getProtectionDomain(), clazz.getBytes()); } } if (!imports.isEmpty()) clazz.getDynamicImports().addAll(imports); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
boolean register(Bundle b, ClassTransformer ctf) { System.out.println("register transformer " + ctf + " on bundle " + b); if (ctf == null) { ctf = DUMMY_TRANSFORMER; } synchronized (transformers) { return transformers.add(b, ctf); } }
/** * Add a new transformer. SPEC: can this be called multiple times? * * @see javax.persistence.spi.PersistenceUnitInfo#addTransformer(javax.persistence.spi.ClassTransformer) */ @Override public void addTransformer(ClassTransformer transformer) { try { sourceBundle.bridge.transformersHook.register(sourceBundle.bundle, transformer); if (transformer != null) transformers.add(transformer); } catch (RuntimeException e) { e.printStackTrace(); throw e; } }
@Override public void addTransformer(ClassTransformer transformer) { if (isNull(transformers)) { transformers = new ArrayList<>(); } transformers.add(transformer); }
@Override public void addTransformer(final ClassTransformer transformer) { // This is called by eclipselink but doing nothing with it seems to be no problem right now // TODO investigate }
@Override public void addTransformer(ClassTransformer transformer) { TransformerRegistry reg = TransformerRegistrySingleton.get(); reg.addTransformer(bundle, transformer); }
public WrappingTransformer(ClassTransformer transformer) { delegate = transformer; }
@SuppressWarnings("unchecked") private synchronized Collection<ClassTransformer> getTransformers(Bundle bundle) { LinkedHashSet<ClassTransformer> transformers = registeredTransformers.get(bundle); return (Collection<ClassTransformer>)(transformers != null ? new ArrayList<ClassTransformer>(transformers) : Collections.emptyList()); }
@Override public void addTransformer(ClassTransformer arg0) { delegate.addTransformer(arg0); }
/** * This implementation throws an UnsupportedOperationException. */ @Override public void addTransformer(ClassTransformer classTransformer) { throw new UnsupportedOperationException("addTransformer not supported"); }
public ClassFileTransformerAdapter(ClassTransformer classTransformer) { Assert.notNull(classTransformer, "ClassTransformer must not be null"); this.classTransformer = classTransformer; }
@Override public void addTransformer(ClassTransformer transformer) { }
public void addTransformer(ClassTransformer transformer) { classTransformers.add(transformer); }
public void addTransformer(ClassTransformer transformer) { delegate.addTransformer(transformer); }
@Override public void addTransformer(ClassTransformer transformer) { logger.warn("Dropwizard EntityManager does not support JPA class transformation. " + "The " + transformer.getClass().getName() + " class transformer will be ignored."); }
@Test public void testAddingClassTransformerGeneratesWarning() { ClassTransformer classTransformer = mock(ClassTransformer.class); persistenceUnitInfo.addTransformer(classTransformer); verify(logger).warn(contains("does not support")); }
@Override public void addTransformer(ClassTransformer transformer) { System.out.println("WARNING: bytecode weaving using " + transformer.getClass().getName() + " not performed for unit " + definition.name); }
/** * This implementation throws an UnsupportedOperationException. */ public void addTransformer(ClassTransformer classTransformer) { throw new UnsupportedOperationException("addTransformer not supported"); }
public void addTransformer(ClassTransformer transformer) { logger.info("Add transformer called on the persistent unit info. Ignoring"); // TODO Support adding transformers. // Ignore this for now }
boolean unregister(Bundle b, ClassTransformer ctf) { System.out.println("unregister transformer " + ctf + " on bundle " + b); synchronized (transformers) { return transformers.remove(b, ctf); } }