Author: aadamchik
Date: Sun Apr 30 17:33:58 2006
New Revision: 398476

URL: http://svn.apache.org/viewcvs?rev=398476&view=rev
Log:
adding instrumentation agent ... no actual enhancers yet

Added:
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/
      - copied from r398461, 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/CayenneAgent.java
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentationContext.java
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnit.java
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnitFactory.java
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/resources/META-INF/MANIFEST.MF
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/instrument/
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/instrument/CayenneAgentTest.java
Removed:
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/
    
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/EnhancerContext.java
Modified:
    incubator/cayenne/jpa/trunk/cayenne-jpa/pom.xml

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/pom.xml
URL: 
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/pom.xml?rev=398476&r1=398475&r2=398476&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/pom.xml (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/pom.xml Sun Apr 30 17:33:58 2006
@@ -140,6 +140,15 @@
                                <showPackage>false</showPackage>
                        </configuration>
       </plugin>
+      <plugin>
+               <groupId>org.apache.maven.plugins</groupId>
+               <artifactId>maven-jar-plugin</artifactId>
+                       <configuration>
+                               <archive>
+                                       
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
+                               </archive>
+                       </configuration>
+               </plugin> 
     </plugins>
   </build>
 

Added: 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/CayenneAgent.java
URL: 
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/CayenneAgent.java?rev=398476&view=auto
==============================================================================
--- 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/CayenneAgent.java
 (added)
+++ 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/CayenneAgent.java
 Sun Apr 30 17:33:58 2006
@@ -0,0 +1,48 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.instrument;
+
+import java.lang.instrument.Instrumentation;
+
+import org.apache.cayenne.jpa.spi.JpaPersistenceProvider;
+
+/**
+ * An instrumentation agent that configures a
+ * [EMAIL PROTECTED] org.apache.cayenne.jpa.spi.JpaUnitFactory} that will load 
JPA class enhancers in
+ * the main Instrumentation instance.
+ * <p>
+ * To enable CayenneAgent (and hence class enhancers in the Java SE 
environment), start
+ * the JVM with the "-javaagent:" option. E.g.:
+ * 
+ * <pre>
+ * java -javaagent:/path/to/cayenne-jpa-3.0.jar org.example.Main
+ * </pre>
+ * 
+ * @author Andrus Adamchik
+ */
+public class CayenneAgent {
+
+    // do not use the actual Java class to prevent dependencies from loading 
too early..;
+    // using a unit test to ensure that the factory name is valid.
+    static final String FACTORY_CLASS = 
"org.apache.cayenne.jpa.instrument.InstrumentingUnitFactory";
+
+    public static void premain(String agentArgs, Instrumentation 
instrumentation) {
+        System.out.println("*** CayenneAgent starting...");
+        InstrumentationContext.sharedInstance = new InstrumentationContext(
+                instrumentation);
+        System.setProperty(JpaPersistenceProvider.UNIT_FACTORY_PROPERTY, 
FACTORY_CLASS);
+    }
+}

Added: 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentationContext.java
URL: 
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentationContext.java?rev=398476&view=auto
==============================================================================
--- 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentationContext.java
 (added)
+++ 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentationContext.java
 Sun Apr 30 17:33:58 2006
@@ -0,0 +1,81 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.instrument;
+
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.IllegalClassFormatException;
+import java.lang.instrument.Instrumentation;
+import java.security.ProtectionDomain;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.spi.ClassTransformer;
+
+/**
+ * A static context that holds instrumentation-related information.
+ * 
+ * @author Andrus Adamchik
+ */
+public class InstrumentationContext {
+
+    static InstrumentationContext sharedInstance;
+
+    public static InstrumentationContext getInstance() {
+        return sharedInstance;
+    }
+
+    protected final Instrumentation instrumentation;
+    protected Set<String> loadedTransformerTypes;
+
+    public InstrumentationContext(Instrumentation instrumentation) {
+        this.instrumentation = instrumentation;
+        this.loadedTransformerTypes = new HashSet<String>();
+    }
+
+    /**
+     * Adds a new JPA ClassTransformer to the shared Instrumentation instance. 
Supports a
+     * single instance per transformer class to prevent the same transformer 
loaded over
+     * and over again. If a second instance addition is attempted, returns 
false.
+     */
+    public synchronized boolean addTransformer(final ClassTransformer 
transformer) {
+
+        // a simple check to ensure that no duplicate transformers are 
loaded...
+        if (loadedTransformerTypes.add(transformer.getClass().getName())) {
+            instrumentation.addTransformer(new ClassFileTransformer() {
+
+                public byte[] transform(
+                        ClassLoader loader,
+                        String className,
+                        Class<?> classBeingRedefined,
+                        ProtectionDomain protectionDomain,
+                        byte[] classfileBuffer) throws 
IllegalClassFormatException {
+
+                    return transformer.transform(
+                            loader,
+                            className,
+                            classBeingRedefined,
+                            protectionDomain,
+                            classfileBuffer);
+                }
+            });
+
+            return true;
+        }
+        else {
+            return false;
+        }
+    }
+}

Added: 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnit.java
URL: 
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnit.java?rev=398476&view=auto
==============================================================================
--- 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnit.java
 (added)
+++ 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnit.java
 Sun Apr 30 17:33:58 2006
@@ -0,0 +1,60 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.instrument;
+
+import javax.persistence.spi.ClassTransformer;
+
+import org.apache.cayenne.jpa.spi.JpaUnit;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * A unit that loads all transformers into a shared
+ * [EMAIL PROTECTED] org.apache.cayenne.jpa.instrument.InstrumentationContext} 
instance.
+ * 
+ * @author Andrus Adamchik
+ */
+public class InstrumentingUnit extends JpaUnit {
+
+    protected Log logger;
+
+    @Override
+    public void addTransformer(ClassTransformer transformer) {
+
+        // sanity check
+        if (InstrumentationContext.getInstance() == null) {
+            getLogger().warn(
+                    "*** No instrumentation context present. "
+                            + "Check the -javaagent: option");
+        }
+        else {
+            boolean added = 
InstrumentationContext.getInstance().addTransformer(
+                    transformer);
+
+            if (!added) {
+                getLogger().info("Duplicate transformer, ignored: " + 
transformer);
+            }
+        }
+    }
+
+    protected Log getLogger() {
+        if (logger == null) {
+            logger = LogFactory.getLog(getClass());
+        }
+
+        return logger;
+    }
+}

Added: 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnitFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnitFactory.java?rev=398476&view=auto
==============================================================================
--- 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnitFactory.java
 (added)
+++ 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/instrument/InstrumentingUnitFactory.java
 Sun Apr 30 17:33:58 2006
@@ -0,0 +1,26 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.instrument;
+
+import org.apache.cayenne.jpa.spi.JpaUnit;
+import org.apache.cayenne.jpa.spi.JpaUnitFactory;
+
+public class InstrumentingUnitFactory implements JpaUnitFactory {
+
+    public JpaUnit newUnitInfo() {
+        return new InstrumentingUnit();
+    }
+}

Added: 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/resources/META-INF/MANIFEST.MF
URL: 
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/resources/META-INF/MANIFEST.MF?rev=398476&view=auto
==============================================================================
--- 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/resources/META-INF/MANIFEST.MF 
(added)
+++ 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/resources/META-INF/MANIFEST.MF 
Sun Apr 30 17:33:58 2006
@@ -0,0 +1 @@
+Premain-Class: org.apache.cayenne.jpa.instrument.CayenneAgent
\ No newline at end of file

Added: 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/instrument/CayenneAgentTest.java
URL: 
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/instrument/CayenneAgentTest.java?rev=398476&view=auto
==============================================================================
--- 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/instrument/CayenneAgentTest.java
 (added)
+++ 
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/instrument/CayenneAgentTest.java
 Sun Apr 30 17:33:58 2006
@@ -0,0 +1,28 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.instrument;
+
+import org.apache.cayenne.jpa.instrument.CayenneAgent;
+import org.apache.cayenne.jpa.instrument.InstrumentingUnitFactory;
+
+import junit.framework.TestCase;
+
+public class CayenneAgentTest extends TestCase {
+
+    public void testFactoryClass() {
+        assertEquals(InstrumentingUnitFactory.class.getName(), 
CayenneAgent.FACTORY_CLASS);
+    }
+}


Reply via email to