From d490f545c48f2d63c88b176e47d30ad3e95df677 Mon Sep 17 00:00:00 2001 From: "A. Abram White" Date: Sun, 8 Oct 2006 18:17:28 +0000 Subject: [PATCH] Make the inability to instantiate an auxiliary enhancer non-fatal. Also cache auxiliary enhancers statically to speed up enhancement. git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@454184 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/enhance/PCEnhancer.java | 46 +++++++++---------- .../openjpa/enhance/localizer.properties | 6 +-- .../org/apache/openjpa/lib/util/Services.java | 3 +- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java index c42ff82a5..8671bb9a9 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java @@ -109,6 +109,22 @@ public class PCEnhancer { private static final Localizer _loc = Localizer.forPackage (PCEnhancer.class); + private static final AuxiliaryEnhancer[] _auxEnhancers; + static { + Class[] classes = Services.getImplementorClasses( + AuxiliaryEnhancer.class, + AuxiliaryEnhancer.class.getClassLoader()); + List auxEnhancers = new ArrayList(classes.length); + for (int i = 0; i < classes.length; i++) { + try { + auxEnhancers.add(classes[i].newInstance()); + } catch (Throwable t) { + // aux enhancer may rely on non-existant spec classes, etc + } + } + _auxEnhancers = (AuxiliaryEnhancer[]) auxEnhancers.toArray + (new AuxiliaryEnhancer[auxEnhancers.size()]); + } private final BCClass _pc; private final MetaDataRepository _repos; @@ -118,7 +134,6 @@ public class PCEnhancer { private boolean _defCons = true; private boolean _fail = false; - private AuxiliaryEnhancer[] _auxEnhancers = null; private File _dir = null; private BytecodeWriter _writer = null; private Map _backingFields = null; @@ -2639,35 +2654,17 @@ public class PCEnhancer { /** * Gets the auxiliary enhancers registered as {@link Services services}. - * Multi-call safe -- the first call locates the auxiliary enhancers, - * subsequent calls merely returns the existing set. - * - * @return array of auxiliary enhancers. empty array if none is registered. */ public AuxiliaryEnhancer[] getAuxiliaryEnhancers() { - if (_auxEnhancers == null) { - try { - Class[] classes = Services.getImplementorClasses( - AuxiliaryEnhancer.class, - AuxiliaryEnhancer.class.getClassLoader()); - _auxEnhancers = new AuxiliaryEnhancer[classes.length]; - for (int i = 0; i < _auxEnhancers.length; i++) - _auxEnhancers[i] = (AuxiliaryEnhancer) classes[i]. - newInstance(); - } catch (Throwable t) { - throw new GeneralException(t); - } - } - return _auxEnhancers; + return _auxEnhancers; } /** * Allow any registered auxiliary code generators to run. */ private void runAuxiliaryEnhancers() { - AuxiliaryEnhancer[] auxEnhancers = getAuxiliaryEnhancers(); - for (int i = 0; i < auxEnhancers.length; i++) - auxEnhancers[i].run(_pc, _meta); + for (int i = 0; i < _auxEnhancers.length; i++) + _auxEnhancers[i].run(_pc, _meta); } /** @@ -2677,9 +2674,8 @@ public class PCEnhancer { * @return true if any of the auxiliary enhancers skips the given method */ private boolean skipEnhance(BCMethod method) { - AuxiliaryEnhancer[] auxEnhancers = getAuxiliaryEnhancers(); - for (int i = 0; i < auxEnhancers.length; i++) - if (auxEnhancers[i].skipEnhance(method)) + for (int i = 0; i < _auxEnhancers.length; i++) + if (_auxEnhancers[i].skipEnhance(method)) return true; return false; } diff --git a/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties b/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties index 32552403d..0ff9a385f 100644 --- a/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties +++ b/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties @@ -2,9 +2,9 @@ needs-runtime-enhance: "{0}" requires runtime enhancement: {1} runtime-enhance-pcclasses: You have enabled runtime enhancement, but have not \ specified the set of persistent classes. OpenJPA must look for metadata for \ every loaded class, which might increase class load times significantly. -running-all-classes: No targets were given. Running on all classes listed in \ - org.apache.openjpa.PersistentClasses, or all metadata files in classpath directories if \ - the property is not specified. +running-all-classes: No targets were given. Running on all classes in your \ + persistent classes list, or all metadata files in classpath directories if \ + you have not listed your persistent classes. detach-custom-ser: Type "{0}" is set to detach on serialize, but implements \ a custom readObject and/or writeObject method. You cannot use custom \ serialization with detachment. diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Services.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Services.java index ca94d5f48..095e12032 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Services.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Services.java @@ -124,8 +124,7 @@ public class Services { } } } - } - finally { + } finally { try { in.close(); } catch (IOException ioe) {